|
| 1 | +#[derive(Debug, Clone)] |
| 2 | +pub enum AstNode { |
| 3 | + Program { statements: Vec<Statement> }, |
| 4 | + Statement(Statement), |
| 5 | + Expression(Expression), |
| 6 | +} |
| 7 | + |
| 8 | + |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub enum Statement { |
| 11 | + FunctionDeclaration(FunctionDeclaration), |
| 12 | + VariableDeclaration(VariableDeclaration), |
| 13 | + IfStatement(IfStatement), |
| 14 | + WhileStatement(WhileStatement), |
| 15 | + PrintStatement(PrintStatement), |
| 16 | + ReturnStatement(ReturnStatement), |
| 17 | + Assignment(Assignment), |
| 18 | + ExpressionStatement(Expression), |
| 19 | + Block(Vec<Statement>), |
| 20 | + ForInStatement(String, Expression, Vec<Statement>), |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Clone)] |
| 24 | +pub enum Expression { |
| 25 | + Literal(Literal), |
| 26 | + Variable(String), |
| 27 | + BinaryOperation(Box<Expression>, Operator, Box<Expression>), |
| 28 | + FunctionCall(String, Vec<Expression>), |
| 29 | + List(Vec<Expression>), |
| 30 | + Dictionary(Vec<(Expression, Expression)>), |
| 31 | + Tuple(Vec<Expression>), |
| 32 | + Null, |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Debug, Clone)] |
| 37 | +pub struct FunctionDeclaration { |
| 38 | + pub name: String, |
| 39 | + pub parameters: Vec<VariableDeclaration>, |
| 40 | + pub return_type: String, |
| 41 | + pub body: Vec<Statement>, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Debug, Clone)] |
| 45 | +pub struct VariableDeclaration { |
| 46 | + pub name: String, |
| 47 | + pub data_type: String, |
| 48 | + pub value: Option<Expression>, |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Debug, Clone)] |
| 52 | +pub struct IfStatement { |
| 53 | + pub condition: Expression, |
| 54 | + pub body: Vec<Statement>, |
| 55 | + pub elif: Option<Box<IfStatement>>, |
| 56 | + pub else_body: Option<Vec<Statement>>, |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Debug, Clone)] |
| 60 | +pub struct WhileStatement { |
| 61 | + pub condition: Expression, |
| 62 | + pub body: Vec<Statement>, |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Debug, Clone)] |
| 66 | +pub struct PrintStatement { |
| 67 | + pub value: Expression, |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Debug, Clone)] |
| 71 | +pub struct ReturnStatement { |
| 72 | + pub value: Option<Expression>, |
| 73 | +} |
| 74 | + |
| 75 | +#[derive(Debug, Clone)] |
| 76 | +pub struct Assignment { |
| 77 | + pub name: String, |
| 78 | + pub value: Expression, |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Debug, Clone)] |
| 82 | +pub enum Operator { |
| 83 | + Add, |
| 84 | + Subtract, |
| 85 | + Multiply, |
| 86 | + Divide, |
| 87 | + GreaterThan, |
| 88 | + LessThan, |
| 89 | + Equal, |
| 90 | + NotEqual, |
| 91 | + And, |
| 92 | + Or, |
| 93 | +} |
| 94 | + |
| 95 | +#[derive(Debug, Clone)] |
| 96 | +pub enum Literal { |
| 97 | + Integer(i64), |
| 98 | + Float(f64), |
| 99 | + Boolean(bool), |
| 100 | + String(String), |
| 101 | + Null, |
| 102 | +} |
| 103 | + |
| 104 | +#[derive(Debug, Clone)] |
| 105 | +pub enum TokenType { |
| 106 | + COMMENT_MULTILINE, |
| 107 | + COMMENT_SINGLELINE, |
| 108 | + KEYWORD, |
| 109 | + IDENTIFIER, |
| 110 | + INTEGER, |
| 111 | + FLOAT, |
| 112 | + STRING, |
| 113 | + BOOLEAN, |
| 114 | + LIST, |
| 115 | + DICT, |
| 116 | + SET, |
| 117 | + TUPLE, |
| 118 | + NULL, |
| 119 | + OPTION, |
| 120 | + ERROR, |
| 121 | + FUNCTION, |
| 122 | + OPERATOR, |
| 123 | + SYMBOL, |
| 124 | + BRACKET, |
| 125 | + PUNCTUATION, |
| 126 | + PRINT, |
| 127 | + EOF, |
| 128 | +} |
| 129 | + |
| 130 | +#[derive(Debug)] |
| 131 | +pub struct Token { |
| 132 | + pub token_type: TokenType, |
| 133 | + pub value: String, |
| 134 | +} |
| 135 | + |
| 136 | +impl AstNode { |
| 137 | + pub fn build_program(statements: Vec<Statement>) -> Self { |
| 138 | + AstNode::Program { statements } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl Statement { |
| 143 | + pub fn function_declaration(name: String, parameters: Vec<VariableDeclaration>, return_type: String, body: Vec<Statement>) -> Self { |
| 144 | + Statement::FunctionDeclaration(FunctionDeclaration { name, parameters, return_type, body }) |
| 145 | + } |
| 146 | + |
| 147 | + pub fn variable_declaration(name: String, data_type: String, value: Option<Expression>) -> Self { |
| 148 | + Statement::VariableDeclaration(VariableDeclaration { name, data_type, value }) |
| 149 | + } |
| 150 | + |
| 151 | + pub fn if_statement(condition: Expression, body: Vec<Statement>, elif: Option<IfStatement>, else_body: Option<Vec<Statement>>) -> Self { |
| 152 | + Statement::IfStatement(IfStatement { |
| 153 | + condition, |
| 154 | + body, |
| 155 | + elif: elif.map(Box::new), |
| 156 | + else_body |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + pub fn while_statement(condition: Expression, body: Vec<Statement>) -> Self { |
| 161 | + Statement::WhileStatement(WhileStatement { condition, body }) |
| 162 | + } |
| 163 | + |
| 164 | + pub fn print_statement(value: Expression) -> Self { |
| 165 | + Statement::PrintStatement(PrintStatement { value }) |
| 166 | + } |
| 167 | + |
| 168 | + pub fn return_statement(value: Option<Expression>) -> Self { |
| 169 | + Statement::ReturnStatement(ReturnStatement { value }) |
| 170 | + } |
| 171 | + |
| 172 | + pub fn assignment(name: String, value: Expression) -> Self { |
| 173 | + Statement::Assignment(Assignment { name, value }) |
| 174 | + } |
| 175 | + |
| 176 | + pub fn for_in_statement(loop_var: String, collection: Expression, body: Vec<Statement>) -> Self { |
| 177 | + Statement::ForInStatement(loop_var, collection, body) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +impl Expression { |
| 182 | + pub fn literal(literal: Literal) -> Self { |
| 183 | + Expression::Literal(literal) |
| 184 | + } |
| 185 | + |
| 186 | + pub fn variable(name: String) -> Self { |
| 187 | + Expression::Variable(name) |
| 188 | + } |
| 189 | + |
| 190 | + pub fn binary_operation(left: Expression, op: Operator, right: Expression) -> Self { |
| 191 | + Expression::BinaryOperation(Box::new(left), op, Box::new(right)) |
| 192 | + } |
| 193 | + |
| 194 | + pub fn function_call(name: String, args: Vec<Expression>) -> Self { |
| 195 | + Expression::FunctionCall(name, args) |
| 196 | + } |
| 197 | + |
| 198 | + pub fn list(elements: Vec<Expression>) -> Self { |
| 199 | + Expression::List(elements) |
| 200 | + } |
| 201 | + |
| 202 | + pub fn dictionary(pairs: Vec<(Expression, Expression)>) -> Self { |
| 203 | + Expression::Dictionary(pairs) |
| 204 | + } |
| 205 | + |
| 206 | + pub fn null() -> Self { |
| 207 | + Expression::Null |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +impl Operator { |
| 212 | + pub fn add() -> Self { |
| 213 | + Operator::Add |
| 214 | + } |
| 215 | + |
| 216 | + pub fn subtract() -> Self { |
| 217 | + Operator::Subtract |
| 218 | + } |
| 219 | + |
| 220 | + pub fn multiply() -> Self { |
| 221 | + Operator::Multiply |
| 222 | + } |
| 223 | + |
| 224 | + pub fn divide() -> Self { |
| 225 | + Operator::Divide |
| 226 | + } |
| 227 | + |
| 228 | + pub fn greater_than() -> Self { |
| 229 | + Operator::GreaterThan |
| 230 | + } |
| 231 | + |
| 232 | + pub fn less_than() -> Self { |
| 233 | + Operator::LessThan |
| 234 | + } |
| 235 | + |
| 236 | + pub fn equal() -> Self { |
| 237 | + Operator::Equal |
| 238 | + } |
| 239 | + |
| 240 | + pub fn not_equal() -> Self { |
| 241 | + Operator::NotEqual |
| 242 | + } |
| 243 | + |
| 244 | + pub fn and() -> Self { |
| 245 | + Operator::And |
| 246 | + } |
| 247 | + |
| 248 | + pub fn or() -> Self { |
| 249 | + Operator::Or |
| 250 | + } |
| 251 | +} |
0 commit comments