From 879a7de83de2bc83030cc9557a2cac67bc03d5f1 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 5 Jan 2019 15:47:44 -0800 Subject: [PATCH] Wrap Expression in Node --- schala-lang/language/src/ast.rs | 8 +++++++- schala-lang/language/src/parsing.rs | 8 ++++---- schala-lang/language/src/reduced_ast.rs | 2 +- schala-lang/language/src/typechecking.rs | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index 3c01436..9c65033 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -24,12 +24,18 @@ impl Node { struct SourceMap { } +impl From for Node { + fn from(expr: Expression) -> Node { + Node { n: expr, source_map: SourceMap::default() } + } +} + #[derive(Debug, PartialEq)] pub struct AST(pub Vec>); #[derive(Debug, PartialEq, Clone)] pub enum Statement { - ExpressionStatement(Expression), + ExpressionStatement(Node), Declaration(Declaration), } diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index cf3b220..c8fbfc1 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -310,7 +310,7 @@ impl Parser { Keyword(Let) => self.binding_declaration().map(|decl| Statement::Declaration(decl)), Keyword(Interface) => self.interface_declaration().map(|decl| Statement::Declaration(decl)), Keyword(Impl) => self.impl_declaration().map(|decl| Statement::Declaration(decl)), - _ => self.expression().map(|expr| { Statement::ExpressionStatement(expr) } ), + _ => self.expression().map(|expr| { Statement::ExpressionStatement(expr.into()) } ), } } @@ -910,7 +910,7 @@ impl Parser { LCurlyBrace => self.block(), _ => { let expr = self.expression()?; - Ok(vec![Node::new(Statement::ExpressionStatement(expr))]) + Ok(vec![Node::new(Statement::ExpressionStatement(expr.into()))]) } } } @@ -1164,8 +1164,8 @@ mod parse_tests { ($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None))) } } macro_rules! exst { - ($expr_type:expr) => { Node::new(Statement::ExpressionStatement(Expression($expr_type, None))) }; - ($expr_type:expr, $type_anno:expr) => { Node::new(Statement::ExpressionStatement(Expression($expr_type, Some($type_anno)))) }; + ($expr_type:expr) => { Node::new(Statement::ExpressionStatement(Expression($expr_type, None).into())) }; + ($expr_type:expr, $type_anno:expr) => { Node::new(Statement::ExpressionStatement(Expression($expr_type, Some($type_anno)).into())) }; ($op:expr, $lhs:expr, $rhs:expr) => { Node::new(Statement::ExpressionStatement(ex!(binexp!($op, $lhs, $rhs)))) }; (s $statement_text:expr) => { { diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index 44324ac..9e032f8 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -108,7 +108,7 @@ impl Statement { fn reduce(&self, symbol_table: &SymbolTable) -> Stmt { use ast::Statement::*; match self { - ExpressionStatement(expr) => Stmt::Expr(expr.reduce(symbol_table)), + ExpressionStatement(expr) => Stmt::Expr(expr.node().reduce(symbol_table)), Declaration(decl) => decl.reduce(symbol_table), } } diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index 3e48c23..a6925c9 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -128,7 +128,7 @@ impl<'a> TypeContext<'a> { fn infer_statement(&mut self, stmt: &Statement) -> InferResult> { match stmt { - Statement::ExpressionStatement(ref expr) => self.infer_expr(expr), + Statement::ExpressionStatement(ref expr) => self.infer_expr(expr.node()), Statement::Declaration(ref decl) => self.infer_decl(decl), } }