diff --git a/src/include/interpreter.h b/src/include/interpreter.h new file mode 100644 index 0000000..86f6c75 --- /dev/null +++ b/src/include/interpreter.h @@ -0,0 +1,6 @@ +#ifndef INTERPRETER_H +#define INTERPRETER_H + + + +#endif \ No newline at end of file diff --git a/src/include/parser.h b/src/include/parser.h index 83a1c10..8172730 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -2,6 +2,7 @@ #define DEF_PARSER_H #include +#include #include "tokenize.h" using namespace std; @@ -54,28 +55,15 @@ enum class NodeType { Assignment // -> Identifier = Expr }; -struct Node; +struct InnerNode; + +using Node = variant; + struct InnerNode { NodeType type; vector children; }; - // A Leaf is always corresponding to a Token -struct Node { - bool is_leaf; - union { - InnerNode node; - Token leaf; - }; - - Node() : is_leaf(true), leaf{} {} - - // Explicitly define the destructor - ~Node() { - if (!is_leaf) node.~InnerNode(); - } -}; - #endif \ No newline at end of file diff --git a/src/include/tokenize.h b/src/include/tokenize.h index f8e1acd..a4a40f5 100644 --- a/src/include/tokenize.h +++ b/src/include/tokenize.h @@ -2,22 +2,14 @@ #define TOKENIZE_H #include +#include #include using namespace std; enum class TokenType { Type, Identifier, Number, Plus, Minus, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese }; enum class Type { Int }; -union TokenData { - double number; - Type type; - string identifier; - - TokenData() : number(0.0) {} - - // Explicitly define the destructor - ~TokenData() {} -}; +using TokenData = variant; struct Token { TokenType type; diff --git a/src/interpreter.cpp b/src/interpreter.cpp new file mode 100644 index 0000000..3cada2b --- /dev/null +++ b/src/interpreter.cpp @@ -0,0 +1,16 @@ +#include +#include "include/parser.h" +#include "include/interpreter.h" +using namespace std; + +void eval(Node &ast) { + +} + +int main() { + Token oneTok = { + .type = TokenType::Number, + .data = 1.0 + }; + Node one = oneTok; +} \ No newline at end of file diff --git a/src/tokenize.cpp b/src/tokenize.cpp index ae8127d..3dffa99 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -16,10 +16,10 @@ void print_tokens(vector tokens) { cout << "Type(INT)"; break; case TokenType::Number: - cout << "Number(" << token.data.number << ")"; + cout << "Number(" << get(token.data) << ")"; break; case TokenType::Identifier: - cout << "Identifier(" << token.data.identifier << ")"; + cout << "Identifier(" << get(token.data) << ")"; break; case TokenType::Plus: cout << "+"; @@ -62,7 +62,7 @@ vector tokenize(string str) { if (regex_search(str, m, NUMBER_REGEX, regex_constants::match_continuous)) { Token token = { .type = TokenType::Number, - .data = { .number = stof(m.str()) } + .data = stod(m.str()) }; tokens.emplace_back(token); str.erase(0, m.str().length()); @@ -70,17 +70,15 @@ vector tokenize(string str) { else if (regex_search(str, m, TYPE_INT_REGEX, regex_constants::match_continuous)) { Token token = { .type = TokenType::Type, - .data = { .type = Type::Int } + .data = Type::Int }; tokens.emplace_back(token); str.erase(0, m.str().length()); } else if (regex_search(str, m, IDENTIFIER_REGEX, regex_constants::match_continuous)) { - char* identifier = new char[m.str().length() + 1]; - strcpy(identifier, m.str().c_str()); Token token = { .type = TokenType::Identifier, - .data = { .identifier = identifier } + .data = m.str() }; tokens.emplace_back(token); str.erase(0, m.str().length());