diff options
| author | Cori Barker <coribarker2@gmail.com> | 2026-01-19 09:36:05 +0000 |
|---|---|---|
| committer | Cori Barker <coribarker2@gmail.com> | 2026-01-19 09:36:05 +0000 |
| commit | 74eafbd39c4c622ce2ebdf699102ec7541dfe807 (patch) | |
| tree | 456cee50e5f33fe06fbff16a139df8df71f26ad9 /include | |
| parent | e34338219036acf103d161dfbd36fa67597d23e7 (diff) | |
parser
Diffstat (limited to 'include')
| -rw-r--r-- | include/parser/ast_node.h | 70 | ||||
| -rw-r--r-- | include/parser/node_type.h | 10 | ||||
| -rw-r--r-- | include/parser/parser.h | 31 |
3 files changed, 111 insertions, 0 deletions
diff --git a/include/parser/ast_node.h b/include/parser/ast_node.h new file mode 100644 index 0000000..710054d --- /dev/null +++ b/include/parser/ast_node.h @@ -0,0 +1,70 @@ +#ifndef AST_NODE_H +#define AST_NODE_H + +#include "parser/node_type.h" + +#include <string> +#include <vector> +#include <memory> + +class ASTNode { +public: + int line; + int column; + + virtual ~ASTNode() = default; +}; + +class Program : public ASTNode { +public: + std::vector<std::unique_ptr<ASTNode>> declarations; +}; + +class Declaration : public ASTNode { +public: + std::string type; + std::string var_name; + std::unique_ptr<ASTNode> value; + + Declaration(std::string type, std::string var_name, std::unique_ptr<ASTNode> value = nullptr) : type(type), var_name(var_name), value(std::move(value)) {} +}; + +class Assignment : public ASTNode { +public: + std::string variable_name; + std::unique_ptr<ASTNode> value; + + Assignment(std::string var, std::unique_ptr<ASTNode> val) : variable_name(var), value(std::move(val)) {} +}; + +class NumberLiteral : public ASTNode { +public: + double value; + + NumberLiteral(double val) : value(val) {} +}; + +class StringLiteral : public ASTNode { +public: + std::string value; + + StringLiteral(std::string val) : value(val) {} +}; + +class Identifier : public ASTNode { +public: + std::string name; + + Identifier(std::string name) : name(name) {} +}; + +class BinaryOp : public ASTNode { +public: + std::unique_ptr<ASTNode> left; + std::string value; + std::unique_ptr<ASTNode> right; + + BinaryOp(std::unique_ptr<ASTNode> left, std::string value, std::unique_ptr<ASTNode> right) : left(std::move(left)), value(std::move(value)), right(std::move(right)) {} +}; + +#endif diff --git a/include/parser/node_type.h b/include/parser/node_type.h new file mode 100644 index 0000000..1766b19 --- /dev/null +++ b/include/parser/node_type.h @@ -0,0 +1,10 @@ +#ifndef NODE_TYPE_H +#define NODE_TYPE_H + +enum class NodeType { + PROGRAM, + VARIABLE_DECLARATION, + ASSIGNMENT, +}; + +#endif diff --git a/include/parser/parser.h b/include/parser/parser.h new file mode 100644 index 0000000..3955e2e --- /dev/null +++ b/include/parser/parser.h @@ -0,0 +1,31 @@ +#ifndef PARSER_H +#define PARSER_H + +#include "lexer/token.h" +#include "parser/ast_node.h" + +#include <vector> + +class Parser { +public: + explicit Parser(const std::vector<Token>& tokens); + std::unique_ptr<Program> parse(); + +private: + std::vector<Token> tokens_; + int position_; + + std::unique_ptr<ASTNode> parseStatement(); + std::unique_ptr<ASTNode> parseExpression(); + std::unique_ptr<ASTNode> parseAddSub(); + std::unique_ptr<ASTNode> parseMulDiv(); + std::unique_ptr<ASTNode> parsePrimary(); + std::unique_ptr<ASTNode> parseDeclaration(); + std::unique_ptr<ASTNode> parseAssignment(); + Token advance(); + Token peek(); + bool isAtEnd(); + void error(std::string s); +}; + +#endif |
