-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compiler.hpp
107 lines (84 loc) · 1.77 KB
/
Compiler.hpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef COMPILER_HPP
#define COMPILER_HPP
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include "Expression.hpp"
#include "Loc.hpp"
#include "Scopes.hpp"
#include "Env.hpp"
enum class Tok {
Print,
Var,
Exit,
If,
Else,
While,
True,
False,
Equal,
NotEqual,
LessOrEqual,
Less,
GreaterOrEqual,
Greater,
And,
Or,
Xor,
Not,
};
const std::map<Tok, const std::string> TokStr = {
{Tok::Print, "print"},
{Tok::Var, "var"},
{Tok::Exit, "exit"},
{Tok::If, "if"},
{Tok::Else, "else"},
{Tok::While, "while"},
{Tok::True, "true"},
{Tok::False, "false"},
{Tok::Equal, "=="},
{Tok::NotEqual, "!="},
{Tok::LessOrEqual, "<="},
{Tok::Less, "<"},
{Tok::GreaterOrEqual, ">="},
{Tok::Greater, ">"},
{Tok::And, "and"},
{Tok::Or, "or"},
{Tok::Xor, "xor"},
{Tok::Not, "not"},
};
enum class RTLabel {
PrintI,
PrintlnI,
PrintS,
PrintlnS
};
const std::map<RTLabel, const std::string> RTLabelStr = {
{RTLabel::PrintI, "_print_int"},
{RTLabel::PrintlnI, "_println_int"},
{RTLabel::PrintS, "_print_string"},
{RTLabel::PrintlnS, "_println_string"}
};
bool skipComment(Loc& loc);
void skipSpaces(Loc& loc);
bool read(Loc& loc, const std::string& what);
bool read(Loc& loc, Tok tok);
bool read(Loc& loc, char ch);
bool peek(Loc& loc, char what);
bool readNumber(Loc& loc, int* n);
bool readIdentifier(Loc& loc, std::string* identifier);
bool parsePrint(Env& env);
bool parseVar(Env& env);
bool parseVarAssign(Env& env, const std::string& name);
bool parseExit(Env& env);
bool parseLiteral(Env &env);
bool parseFactor(Env& env);
bool parseTerm(Env& env);
bool parseNumericExpression(Env& env);
bool parseCompareExpression(Env& env);
bool parseBooleanExpression(Env& env);
bool parseBlock(Env& env);
bool parseIf(Env& env);
bool parseCommand(Env& env);
#endif