From 4b33b1f31862cd505fd9c90cf8d2e696c6f6ec97 Mon Sep 17 00:00:00 2001 From: Cori Barker Date: Mon, 23 Feb 2026 15:45:03 +0000 Subject: partial refactor of ast_node.hpp --- #test.bl# | 3 ++ bnf | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/ast_node.hpp | 46 +++++++++++++++++++-- 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 #test.bl# create mode 100644 bnf diff --git a/#test.bl# b/#test.bl# new file mode 100644 index 0000000..0548497 --- /dev/null +++ b/#test.bl# @@ -0,0 +1,3 @@ +int a = 6; +int b = 7; +int c = a * b; diff --git a/bnf b/bnf new file mode 100644 index 0000000..cc49d87 --- /dev/null +++ b/bnf @@ -0,0 +1,115 @@ + ::= { } + + ::= + "function" "(" [ ] ")" + "->" + + + ::= { "," } + + ::= + + ::= "int" + | "bool" + + + ::= "{" + + "}" + + ::= { } + + ::= + | + | + | + | + | + | + + + ::= + [ "=" ] ";" + + ::= + "=" ";" + + ::= + "return" [ ] ";" + + ::= + ";" + + + ::= + "if" "(" ")" + + [ "else" ] + + ::= + "while" "(" ")" + + + ::= + "for" "(" + [ ] + ";" + [ ] + ";" + [ ] + ")" + + + ::= [ "=" ] + | + + + ::= + + ::= + { "||" } + + ::= + { "&&" } + + ::= + { ( "==" | "!=" ) } + + ::= + { ( "<" | ">" | "<=" | ">=" ) } + + ::= + { ( "+" | "-" ) } + + ::= + { ( "*" | "/" | "%" ) } + + ::= ( "!" | "-" ) + | + + ::= + | + | + | + | "(" ")" + + + ::= + "(" [ ] ")" + + ::= + { "," } + + + ::= { | } + + ::= { } + + ::= "true" + | "false" + + ::= "a" | ... | "z" + | "A" | ... | "Z" + | "_" + + ::= "0" | ... | "9" \ No newline at end of file diff --git a/include/ast_node.hpp b/include/ast_node.hpp index caffb35..f1fcaf2 100644 --- a/include/ast_node.hpp +++ b/include/ast_node.hpp @@ -10,7 +10,7 @@ class ASTNode { public: int line; - int column; + int col; virtual ~ASTNode() = default; }; @@ -19,7 +19,7 @@ class ProgramNode : ASTNode { public: std::vector function_declarations; - explicit ProgramNode(std::vector f) : function_declarations(f) { } + ProgramNode(std::vector function_declarations, int line, int col) : function_declarations(function_declarations), line(line), col(col) { } }; class FunctionDeclNode : ASTNode { @@ -27,10 +27,48 @@ class FunctionDeclNode : ASTNode { std::vector parameters; std::vector body; - FunctionDeclNode(std::string t,std::vector p, std::vector b) : type(t), parameters(p), body(b) { } + FunctionDeclNode(Type type, std::vector parameters, std::vector body, int line, int col) : type(type), parameters(parameters), body(body), line(line), col(col) { } }; class ParameterNode : ASTNode { std::string name; - std::string type; + Type type; + + ParameterNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line) col(col) { } +}; + +class VarDeclNode : ASTNode { + std::string name; + Type type; + + VarDeclNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line), col(col) { } +}; + +class AssignmentNode : ASTNode { + std::string var_name; + Type var_type; + ASTNode* value; + + AssignmentNode(std::string var_name, Type var_type, ASTNode* value) : var_name(var_name), var_type(var_type), value(value), line(line), col(col) { } +}; + +class IfStatementNode : ASTNode { + ASTNode* expression; + ASTNode* body; + ASTNode* else_statement = nullptr; + ASTNode* elif_statement = nullptr; + + IfStatementNode(ASTNode* expression, ASTNode* body, ASTNode* else_statement, ASTNode* elif_statement, int line, int col) : expression(expression), body(body), else_statement(else_statement), elif_statement(elif_statement), line(line), col(col) { } +}; + +class WhileStatementNode : ASTNode { + ASTNode* expression; + ASTNode* body; + + WhileStatementNode(ASTNode* expression, ASTNode* body, int line, int col) : expression(expression), body(body), line(line), col(col) { } +}; + +class ForStatementNode : ASTNode { + StatementNode* init; + ASTNode* }; -- cgit v1.2.3