Move ast into separate file
This commit is contained in:
parent
7e2ef49481
commit
dd9ca660cc
111
src/ast.rs
Normal file
111
src/ast.rs
Normal file
@ -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<Expression>, Box<Expression>),
|
||||
/// Unary operation. Consists of type and operand
|
||||
UnOp(UnOpType, Box<Expression>),
|
||||
}
|
||||
|
||||
#[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<Expression>,
|
||||
/// 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<Statement>,
|
||||
}
|
||||
@ -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() {
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
pub mod lexer;
|
||||
pub mod token;
|
||||
pub mod parser;
|
||||
pub mod ast;
|
||||
pub mod interpreter;
|
||||
|
||||
113
src/parser.rs
113
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<Expression>, Box<Expression>),
|
||||
/// Unary operation. Consists of type and operand
|
||||
UnOp(UnOpType, Box<Expression>),
|
||||
}
|
||||
|
||||
#[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<Expression>,
|
||||
/// 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<Statement>
|
||||
}
|
||||
use crate::ast::*;
|
||||
|
||||
struct Parser<T: Iterator<Item = Token>> {
|
||||
tokens: Peekable<T>,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::parser::BinOpType;
|
||||
use crate::ast::BinOpType;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Token {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user