From 905431b33cc808ecf009dcc89d1f14086bbd135a Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 3 Jan 2017 02:45:36 -0800 Subject: [PATCH] Change name: ASTNode -> Statement --- src/compilation.rs | 6 +++--- src/eval.rs | 16 ++++++++-------- src/parser.rs | 38 ++++++++++++++++---------------------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/compilation.rs b/src/compilation.rs index 063eae8..5700e38 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -3,7 +3,7 @@ extern crate llvm_sys; use std::collections::HashMap; use self::llvm_sys::prelude::*; -use parser::{AST, ASTNode, Function, Expression}; +use parser::{AST, Statement, Function, Expression}; use llvm_wrap as LLVMWrap; @@ -95,9 +95,9 @@ impl CodeGen for AST { } } -impl CodeGen for ASTNode { +impl CodeGen for Statement { fn codegen(&self, data: &mut CompilationData) -> LLVMValueRef { - use self::ASTNode::*; + use self::Statement::*; match self { &ExprNode(ref expr) => expr.codegen(data), &FuncDefNode(ref func) => func.codegen(data), diff --git a/src/eval.rs b/src/eval.rs index 70839f9..309282f 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -1,7 +1,7 @@ extern crate take_mut; use std::collections::HashMap; -use parser::{AST, ASTNode, Expression, Function}; +use parser::{AST, Statement, Expression, Function}; type Reduction = (T, Option); @@ -66,9 +66,9 @@ trait Evaluable { fn is_reducible(&self) -> bool; } -impl Evaluable for ASTNode { +impl Evaluable for Statement { fn is_reducible(&self) -> bool { - use parser::ASTNode::*; + use parser::Statement::*; match self { &ExprNode(ref expr) => expr.is_reducible(), &FuncDefNode(_) => true, @@ -102,7 +102,7 @@ impl Expression { } impl<'a> Evaluator<'a> { - fn reduction_loop(&mut self, mut node: ASTNode) -> ASTNode { + fn reduction_loop(&mut self, mut node: Statement) -> Statement { loop { node = self.step(node); if !node.is_reducible() { @@ -112,7 +112,7 @@ impl<'a> Evaluator<'a> { node } - fn step(&mut self, node: ASTNode) -> ASTNode { + fn step(&mut self, node: Statement) -> Statement { let (new_node, side_effect) = self.reduce_astnode(node); if let Some(s) = side_effect { self.perform_side_effect(s); @@ -133,8 +133,8 @@ impl<'a> Evaluator<'a> { } } - fn reduce_astnode(&mut self, node: ASTNode) -> Reduction { - use parser::ASTNode::*; + fn reduce_astnode(&mut self, node: Statement) -> Reduction { + use parser::Statement::*; match node { ExprNode(expr) => { if expr.is_reducible() { @@ -265,7 +265,7 @@ impl<'a> Evaluator<'a> { fn reduce_call(&mut self, name: String, arguments: Vec) -> Reduction { use parser::Expression::*; - use parser::ASTNode::*; + use parser::Statement::*; // ugly hack for now if name == "print" { diff --git a/src/parser.rs b/src/parser.rs index 440b6d8..9535f56 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -21,7 +21,7 @@ use std::collections::VecDeque; // #[derive(Debug, Clone)] -pub enum ASTNode { +pub enum Statement { ExprNode(Expression), FuncDefNode(Function), } @@ -29,7 +29,7 @@ pub enum ASTNode { #[derive(Debug, Clone)] pub struct Function { pub prototype: Prototype, - pub body: Vec, + pub body: Vec, } #[derive(Debug, Clone, PartialEq)] @@ -51,9 +51,9 @@ pub enum Expression { Block(VecDeque), } -impl fmt::Display for ASTNode { +impl fmt::Display for Statement { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::ASTNode::*; + use self::Statement::*; match *self { ExprNode(ref expr) => write!(f, "{}", expr), FuncDefNode(_) => write!(f, "UNIMPLEMENTED"), @@ -76,7 +76,7 @@ impl fmt::Display for Expression { } } -pub type AST = Vec; +pub type AST = Vec; type Precedence = u8; @@ -168,7 +168,7 @@ impl Parser { fn program(&mut self) -> ParseResult { let mut ast = Vec::new(); //TODO have this come from previously-parsed tree loop { - let result: ParseResult = match self.peek() { + let result: ParseResult = match self.peek() { Some(ref t) if is_delimiter(&t) => { self.next(); continue; @@ -190,24 +190,24 @@ impl Parser { Ok(ast) } - fn statement(&mut self) -> ParseResult { + fn statement(&mut self) -> ParseResult { use tokenizer::Token::*; - let node: ASTNode = match self.peek() { + let node: Statement = match self.peek() { Some(Keyword(Kw::Fn)) => try!(self.declaration()), - Some(_) => ASTNode::ExprNode(try!(self.expression())), + Some(_) => Statement::ExprNode(try!(self.expression())), None => panic!("unexpected end of tokens"), }; Ok(node) } - fn declaration(&mut self) -> ParseResult { + fn declaration(&mut self) -> ParseResult { use tokenizer::Token::*; expect!(self, Keyword(Kw::Fn)); let prototype = try!(self.prototype()); - let body: Vec = try!(self.body()); + let body: Vec = try!(self.body()); expect!(self, Keyword(Kw::End)); - Ok(ASTNode::FuncDefNode(Function { + Ok(Statement::FuncDefNode(Function { prototype: prototype, body: body, })) @@ -258,7 +258,7 @@ impl Parser { Ok(args) } - fn body(&mut self) -> ParseResult> { + fn body(&mut self) -> ParseResult> { use tokenizer::Token::*; let mut statements = Vec::new(); loop { @@ -428,7 +428,7 @@ impl Parser { } } -pub fn parse(tokens: &[Token], _parsed_tree: &[ASTNode]) -> ParseResult { +pub fn parse(tokens: &[Token], _parsed_tree: &[Statement]) -> ParseResult { let mut parser = Parser::initialize(tokens); parser.program() } @@ -437,6 +437,8 @@ pub fn parse(tokens: &[Token], _parsed_tree: &[ASTNode]) -> ParseResult { mod tests { use tokenizer; use super::*; + use super::Statement::*; + use super::Expression::*; macro_rules! parsetest { ($input:expr, $output:pat, $ifexpr:expr) => { @@ -453,10 +455,7 @@ mod tests { #[test] fn call_parse_test() { - use super::ASTNode::*; - use super::Expression::*; use super::Function; - parsetest!( "fn a() 1 + 2 end", &[FuncDefNode(Function {prototype: Prototype { ref name, ref parameters }, ref body})], @@ -474,8 +473,6 @@ mod tests { #[test] fn expression_parse_test() { - use super::ASTNode::*; - use super::Expression::*; parsetest!("a", &[ExprNode(Variable(ref s))], s == "a"); parsetest!("a + b", &[ExprNode(BinExp(ref plus, box Variable(ref a), box Variable(ref b)))], @@ -494,9 +491,6 @@ mod tests { #[test] fn conditional_parse_test() { use tokenizer; - use super::ASTNode::*; - use super::Expression::*; - let t1 = "if null then 20 else 40 end"; let tokens = tokenizer::tokenize(t1).unwrap(); match parse(&tokens, &[]).unwrap()[..] {