Wrap Expression in Node

This commit is contained in:
greg 2019-01-05 15:47:44 -08:00
parent 4c2e0b8a21
commit 879a7de83d
4 changed files with 13 additions and 7 deletions

View File

@ -24,12 +24,18 @@ impl<T> Node<T> {
struct SourceMap { struct SourceMap {
} }
impl From<Expression> for Node<Expression> {
fn from(expr: Expression) -> Node<Expression> {
Node { n: expr, source_map: SourceMap::default() }
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct AST(pub Vec<Node<Statement>>); pub struct AST(pub Vec<Node<Statement>>);
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum Statement { pub enum Statement {
ExpressionStatement(Expression), ExpressionStatement(Node<Expression>),
Declaration(Declaration), Declaration(Declaration),
} }

View File

@ -310,7 +310,7 @@ impl Parser {
Keyword(Let) => self.binding_declaration().map(|decl| Statement::Declaration(decl)), Keyword(Let) => self.binding_declaration().map(|decl| Statement::Declaration(decl)),
Keyword(Interface) => self.interface_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)), 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(), LCurlyBrace => self.block(),
_ => { _ => {
let expr = self.expression()?; 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))) } ($op:expr, $lhs:expr) => { PrefixExp(PrefixOp::from_sigil($op), bx!(Expression($lhs, None))) }
} }
macro_rules! exst { macro_rules! exst {
($expr_type:expr) => { Node::new(Statement::ExpressionStatement(Expression($expr_type, None))) }; ($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)))) }; ($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)))) }; ($op:expr, $lhs:expr, $rhs:expr) => { Node::new(Statement::ExpressionStatement(ex!(binexp!($op, $lhs, $rhs)))) };
(s $statement_text:expr) => { (s $statement_text:expr) => {
{ {

View File

@ -108,7 +108,7 @@ impl Statement {
fn reduce(&self, symbol_table: &SymbolTable) -> Stmt { fn reduce(&self, symbol_table: &SymbolTable) -> Stmt {
use ast::Statement::*; use ast::Statement::*;
match self { 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), Declaration(decl) => decl.reduce(symbol_table),
} }
} }

View File

@ -128,7 +128,7 @@ impl<'a> TypeContext<'a> {
fn infer_statement(&mut self, stmt: &Statement) -> InferResult<Type<UVar>> { fn infer_statement(&mut self, stmt: &Statement) -> InferResult<Type<UVar>> {
match stmt { 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), Statement::Declaration(ref decl) => self.infer_decl(decl),
} }
} }