-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.h
72 lines (54 loc) · 1.48 KB
/
parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include "lexer.h"
class Variable {
public:
Variable();
std::string name;
bool isTypeSafe;
Token::Primitive prim;
Token* tok;
int i_val;
bool b_val;
std::string s_value;
};
class AST { // Abstract Syntax Tree
public:
enum Type {
Function,
ControlFlow,
Operation,
Tok,
};
AST();
AST(AST* parent) : parent(parent) { };
inline void add(AST o) { tree.push_back(o); };
inline AST* getParent() {return parent; };
inline Type getType() { return type; }
Type type;
std::string repr();
std::vector<std::tuple<Variable*, std::string>> scope;
private:
std::vector<AST> tree;
AST *parent;
Token *tok;
};
class Parser {
public:
Parser(Lexer lex);
inline AST get_ast() { return main; };
private:
enum Build {
variable,
};
void build(Build build);
bool is_legal(std::string name);
std::vector<Token> tokens;
int pos;
Token* current_token;
AST main;
AST *workingtree;
std::vector<std::string> types{"num", "string", "bool", "func", "arr", "none"};
std::vector<std::string> keywords{"print", "input", "define", "while", "for", "if", "else", "elif", "class", "not", "or", "and", "var"};
std::vector<std::string> savedwords { "print", "input", "define", "while", "for", "if", "else", "elif", "class", "not", "or", "and", "num", "string", "bool", "func", "arr", "none", "true", "false" };
std::vector<Error> errors;
};