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
|
#define DEF_PARSER_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <variant>
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -54,28 +55,15 @@ enum class NodeType {
|
|||||||
Assignment // -> Identifier = Expr
|
Assignment // -> Identifier = Expr
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Node;
|
struct InnerNode;
|
||||||
|
|
||||||
|
using Node = variant<InnerNode, Token>;
|
||||||
|
|
||||||
struct InnerNode {
|
struct InnerNode {
|
||||||
NodeType type;
|
NodeType type;
|
||||||
vector<Node> children;
|
vector<Node> children;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// A Leaf is always corresponding to a Token
|
// 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
|
#endif
|
@ -2,22 +2,14 @@
|
|||||||
#define TOKENIZE_H
|
#define TOKENIZE_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <variant>
|
||||||
#include <string>
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
enum class TokenType { Type, Identifier, Number, Plus, Minus, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese };
|
enum class TokenType { Type, Identifier, Number, Plus, Minus, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese };
|
||||||
enum class Type { Int };
|
enum class Type { Int };
|
||||||
|
|
||||||
union TokenData {
|
using TokenData = variant<double, string, Type>;
|
||||||
double number;
|
|
||||||
Type type;
|
|
||||||
string identifier;
|
|
||||||
|
|
||||||
TokenData() : number(0.0) {}
|
|
||||||
|
|
||||||
// Explicitly define the destructor
|
|
||||||
~TokenData() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Token {
|
struct Token {
|
||||||
TokenType type;
|
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)";
|
cout << "Type(INT)";
|
||||||
break;
|
break;
|
||||||
case TokenType::Number:
|
case TokenType::Number:
|
||||||
cout << "Number(" << token.data.number << ")";
|
cout << "Number(" << get<double>(token.data) << ")";
|
||||||
break;
|
break;
|
||||||
case TokenType::Identifier:
|
case TokenType::Identifier:
|
||||||
cout << "Identifier(" << token.data.identifier << ")";
|
cout << "Identifier(" << get<string>(token.data) << ")";
|
||||||
break;
|
break;
|
||||||
case TokenType::Plus:
|
case TokenType::Plus:
|
||||||
cout << "+";
|
cout << "+";
|
||||||
@ -62,7 +62,7 @@ vector<Token> tokenize(string str) {
|
|||||||
if (regex_search(str, m, NUMBER_REGEX, regex_constants::match_continuous)) {
|
if (regex_search(str, m, NUMBER_REGEX, regex_constants::match_continuous)) {
|
||||||
Token token = {
|
Token token = {
|
||||||
.type = TokenType::Number,
|
.type = TokenType::Number,
|
||||||
.data = { .number = stof(m.str()) }
|
.data = stod(m.str())
|
||||||
};
|
};
|
||||||
tokens.emplace_back(token);
|
tokens.emplace_back(token);
|
||||||
str.erase(0, m.str().length());
|
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)) {
|
else if (regex_search(str, m, TYPE_INT_REGEX, regex_constants::match_continuous)) {
|
||||||
Token token = {
|
Token token = {
|
||||||
.type = TokenType::Type,
|
.type = TokenType::Type,
|
||||||
.data = { .type = Type::Int }
|
.data = Type::Int
|
||||||
};
|
};
|
||||||
tokens.emplace_back(token);
|
tokens.emplace_back(token);
|
||||||
str.erase(0, m.str().length());
|
str.erase(0, m.str().length());
|
||||||
}
|
}
|
||||||
else if (regex_search(str, m, IDENTIFIER_REGEX, regex_constants::match_continuous)) {
|
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 = {
|
Token token = {
|
||||||
.type = TokenType::Identifier,
|
.type = TokenType::Identifier,
|
||||||
.data = { .identifier = identifier }
|
.data = m.str()
|
||||||
};
|
};
|
||||||
tokens.emplace_back(token);
|
tokens.emplace_back(token);
|
||||||
str.erase(0, m.str().length());
|
str.erase(0, m.str().length());
|
||||||
|
Loading…
Reference in New Issue
Block a user