diff --git a/src/ast.rs b/src/ast.rs new file mode 100644 index 0000000..6390135 --- /dev/null +++ b/src/ast.rs @@ -0,0 +1,111 @@ +/// Types for binary operators +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum BinOpType { + /// Addition + Add, + + /// Subtraction + Sub, + + /// Multiplication + Mul, + + /// Divide + Div, + + /// Modulo + Mod, + + /// Compare Equal + EquEqu, + + /// Compare Not Equal + NotEqu, + + /// Less than + Less, + + /// Less than or Equal + LessEqu, + + /// Greater than + Greater, + + /// Greater than or Equal + GreaterEqu, + + /// Bitwise OR (inclusive or) + BOr, + + /// Bitwise And + BAnd, + + /// Bitwise Xor (exclusive or) + BXor, + + /// Shift Left + Shl, + + /// Shift Right + Shr, + + /// Assign value to variable + Assign, + + /// Declare new variable with value + Declare, +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum UnOpType { + /// Unary Negate + Negate, + + /// Bitwise Not + BNot, +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum Expression { + /// Integer literal (64-bit) + I64(i64), + /// Variable + Var(String), + /// Binary operation. Consists of type, left hand side and right hand side + BinOp(BinOpType, Box, Box), + /// Unary operation. Consists of type and operand + UnOp(UnOpType, Box), +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct Loop { + /// The condition that determines if the loop should continue + pub condition: Expression, + /// This is executed after each loop to advance the condition variables + pub advancement: Option, + /// The loop body that is executed each loop + pub body: Ast, +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct If { + /// The condition + pub condition: Expression, + /// The body that is executed when condition is true + pub body_true: Ast, + /// The if body that is executed when the condition is false + pub body_false: Ast, +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum Statement { + Expr(Expression), + Loop(Loop), + If(If), + Print(Expression), +} + +#[derive(Debug, PartialEq, Eq, Clone, Default)] +pub struct Ast { + pub prog: Vec, +} diff --git a/src/interpreter.rs b/src/interpreter.rs index 33fe357..1c31f98 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, fmt::Display}; -use crate::{parser::{Expression, BinOpType, UnOpType, Ast, Statement, parse, If}, lexer::lex}; +use crate::{ast::{Expression, BinOpType, UnOpType, Ast, Statement, If}, parser::parse, lexer::lex}; #[derive(Debug, PartialEq, Eq, Clone)] pub enum Value { @@ -156,7 +156,7 @@ impl Display for Value { #[cfg(test)] mod test { use super::{Interpreter, Value}; - use crate::parser::{Expression, BinOpType}; + use crate::ast::{Expression, BinOpType}; #[test] fn test_interpreter_expr() { diff --git a/src/lib.rs b/src/lib.rs index c188eff..d0fca0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod lexer; pub mod token; pub mod parser; +pub mod ast; pub mod interpreter; diff --git a/src/parser.rs b/src/parser.rs index e9c459f..7f99bc2 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,118 +1,7 @@ use std::iter::Peekable; use crate::token::Token; - -/// Types for binary operators -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum BinOpType { - /// Addition - Add, - - /// Subtraction - Sub, - - /// Multiplication - Mul, - - /// Divide - Div, - - /// Modulo - Mod, - - /// Compare Equal - EquEqu, - - /// Compare Not Equal - NotEqu, - - /// Less than - Less, - - /// Less than or Equal - LessEqu, - - /// Greater than - Greater, - - /// Greater than or Equal - GreaterEqu, - - /// Bitwise OR (inclusive or) - BOr, - - /// Bitwise And - BAnd, - - /// Bitwise Xor (exclusive or) - BXor, - - /// Shift Left - Shl, - - /// Shift Right - Shr, - - /// Assign value to variable - Assign, - - /// Declare new variable with value - Declare, -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum UnOpType { - /// Unary Negate - Negate, - - /// Bitwise Not - BNot, -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum Expression { - /// Integer literal (64-bit) - I64(i64), - /// Variable - Var(String), - /// Binary operation. Consists of type, left hand side and right hand side - BinOp(BinOpType, Box, Box), - /// Unary operation. Consists of type and operand - UnOp(UnOpType, Box), -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct Loop { - /// The condition that determines if the loop should continue - pub condition: Expression, - /// This is executed after each loop to advance the condition variables - pub advancement: Option, - /// The loop body that is executed each loop - pub body: Ast, -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub struct If { - /// The condition - pub condition: Expression, - /// The body that is executed when condition is true - pub body_true: Ast, - /// The if body that is executed when the condition is false - pub body_false: Ast, -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub enum Statement { - Expr(Expression), - Loop(Loop), - If(If), - Print(Expression), -} - -#[derive(Debug, PartialEq, Eq, Clone, Default)] -pub struct Ast { - pub prog: Vec -} +use crate::ast::*; struct Parser> { tokens: Peekable, diff --git a/src/token.rs b/src/token.rs index 7ed275d..5142ce3 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1,4 +1,4 @@ -use crate::parser::BinOpType; +use crate::ast::BinOpType; #[derive(Debug, PartialEq, Eq)] pub enum Token {