From 5b1a49ccc676d9a3564050e203a7a68bf0ece578 Mon Sep 17 00:00:00 2001 From: Cori Barker Date: Wed, 4 Mar 2026 15:26:45 +0000 Subject: removed bnf since not needed, and added some more nodes to the AST --- bnf | 115 --------------------------------------------------- include/ast_node.hpp | 94 +++++++++++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 145 deletions(-) delete mode 100644 bnf diff --git a/bnf b/bnf deleted file mode 100644 index cc49d87..0000000 --- a/bnf +++ /dev/null @@ -1,115 +0,0 @@ - ::= { } - - ::= - "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 f1fcaf2..5338034 100644 --- a/include/ast_node.hpp +++ b/include/ast_node.hpp @@ -13,62 +13,96 @@ public: int col; virtual ~ASTNode() = default; -}; +} class ProgramNode : ASTNode { public: - std::vector function_declarations; + std::vector function_declarations; - ProgramNode(std::vector function_declarations, int line, int col) : function_declarations(function_declarations), line(line), col(col) { } + ProgramNode(std::vector function_declarations, int line, int col) : function_declarations(function_declarations), line(line), col(col) { } }; -class FunctionDeclNode : ASTNode { - Type type; +class FunctionDeclarationNode : public ASTNode { +public: + std::string identifier; std::vector parameters; - std::vector body; + Type return_type; + std::vector body; - FunctionDeclNode(Type type, std::vector parameters, std::vector body, int line, int col) : type(type), parameters(parameters), body(body), line(line), col(col) { } + FunctionDeclarationNode(std::string identifier, std::vector parameters, Type return_type, std::vector body, int line, int col) : identifier(identifier), parameters(parameters), return_type(return_type), body(body), line(line), col(col) { } }; -class ParameterNode : ASTNode { - std::string name; +class ParameterNode : public ASTNode { +public: Type type; + std::string identifier; - ParameterNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line) col(col) { } + ParameterNode(Type type, std::string identifier, int line, int column) : type(type), identifier(identifier), line(line), column(column) { } }; -class VarDeclNode : ASTNode { - std::string name; +class ArgumentNode : public ASTNode { + ASTNode* expression; + ASTNode* variable; + + ArgumentNode(ASTNode* expression, ASTNode* variable, int line int column) : expression(expression), variable(variable), line(line), column(column) { } +}; + +class VariableDeclarationNode : public ASTNode { +public: Type type; + std::string identifier; + ASTNode* value; - VarDeclNode(std::string name, std::string type, int line, int col) : name(name), type(type), line(line), col(col) { } + VariableDeclarationNode(Type type, std::string identifier, ASTNode* value, int line, int column) : type(type), identifier(identifier), value(value), line(line), column(column) { } }; -class AssignmentNode : ASTNode { - std::string var_name; - Type var_type; +class VariableAssignmentNode : public ASTNode { +public: + std::string identifier; 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) { } + + VariableAssignmentNode(std::string identifier, ASTNode* value, int line, int column) : identifier(identifier), value(value), line(line), column(column) { } }; -class IfStatementNode : ASTNode { +class ReturnNode : public ASTNode { +public: 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) { } + ReturnNode(ASTNode* expression, int line, int column) : expression(expression), line(line), column(column) { } }; -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 BinaryOperationNode : public ASTNode { + char operation; + ASTNode* left; + ASTNode* right; + + BinaryOperationNode(std::string operation, ASTNode* left, ASTNode* right, int line, int column) : operation(operation), left(left), right(right), line(line), column(column) { } }; -class ForStatementNode : ASTNode { - StatementNode* init; - ASTNode* +class UnaryOperationNode : public ASTNode { + char operation; + ASTNode* operand; + + UnaryOperationNode(char operation, ASTNode* operand, int line, int column) : operation(operation), operand(operand), line(line), column(column) { } +}; + +class StringLiteralNode : public ASTNode { + std::string value; + + StringLiteralNode(std::string value, int line, int column) : value(value), line(line), column(column) { } +}; + +class NumberLiteralNode : public ASTNode { + int value; + + NumberLiteralNode(int value, int line, int column) : value(value), line(line), column(column) { } +}; + +class ComparisonNode : public ASTNode { + std::string operation; + ASTNode* left; + ASTNode* right; + + ComparisonNode(std::string operation, ASTNode* left, ASTNode* right, int line, int col) : operation(operation), left(left), right(right), line(line), column(column) { } }; -- cgit v1.2.3