Replace unions with variants
This commit is contained in:
parent
094eabfd76
commit
87f5ba4100
6
src/include/interpreter.h
Normal file
6
src/include/interpreter.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef INTERPRETER_H
|
||||
#define INTERPRETER_H
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -2,6 +2,7 @@
|
||||
#define DEF_PARSER_H
|
||||
|
||||
#include <vector>
|
||||
#include <variant>
|
||||
#include "tokenize.h"
|
||||
using namespace std;
|
||||
|
||||
@ -54,28 +55,15 @@ enum class NodeType {
|
||||
Assignment // -> Identifier = Expr
|
||||
};
|
||||
|
||||
struct Node;
|
||||
struct InnerNode;
|
||||
|
||||
using Node = variant<InnerNode, Token>;
|
||||
|
||||
struct InnerNode {
|
||||
NodeType type;
|
||||
vector<Node> 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
|
@ -2,22 +2,14 @@
|
||||
#define TOKENIZE_H
|
||||
|
||||
#include <vector>
|
||||
#include <variant>
|
||||
#include <string>
|
||||
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<double, string, Type>;
|
||||
|
||||
struct Token {
|
||||
TokenType type;
|
||||
|
16
src/interpreter.cpp
Normal file
16
src/interpreter.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include <vector>
|
||||
#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;
|
||||
}
|
@ -16,10 +16,10 @@ void print_tokens(vector<Token> tokens) {
|
||||
cout << "Type(INT)";
|
||||
break;
|
||||
case TokenType::Number:
|
||||
cout << "Number(" << token.data.number << ")";
|
||||
cout << "Number(" << get<double>(token.data) << ")";
|
||||
break;
|
||||
case TokenType::Identifier:
|
||||
cout << "Identifier(" << token.data.identifier << ")";
|
||||
cout << "Identifier(" << get<string>(token.data) << ")";
|
||||
break;
|
||||
case TokenType::Plus:
|
||||
cout << "+";
|
||||
@ -62,7 +62,7 @@ vector<Token> 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<Token> 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());
|
||||
|
Loading…
Reference in New Issue
Block a user