-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.h
79 lines (69 loc) · 1.5 KB
/
compiler.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
73
74
75
76
77
78
79
#pragma once
#include <stack>
#include "parser.h"
enum opcode : uint32_t
{
pushNum,
pushStr,
getVar,
setVar,
pop,
add,
sub,
mult,
_div,
mod,
_and,
_or,
eq,
call,
_return,
jf,
jt,
jmp,
};
extern std::array<std::string, 18> OpcodeStrings;
extern std::array<int, 18> OpcodeParams;
struct Instruction
{
Instruction() {};
Instruction(opcode op) : op(op) {};
Instruction(opcode op, std::vector<int64_t> p) : op(op), p(p) {};
opcode op;
std::vector<int64_t> p;
};
struct Variable
{
Variable() : name(), offset(0) {}
Variable(std::string name, int64_t offset) : name(name), offset(offset) {}
std::string name;
int64_t offset;
};
struct sv;
struct Function
{
uint64_t addr;
std::vector<std::string> params;
bool isNative;
std::function<sv(std::vector<sv>&)> nativeFunc;
};
class Compiler
{
public:
Compiler(Node program);
std::vector<uint64_t> Compile();
std::string ToString();
std::tuple<std::map<std::string,Function>,std::vector<std::string>,std::vector<uint64_t>> Serialize();
private:
void Compile(Node n);
void Compile(std::vector<Node> n);
Variable& FindVar(const std::string& name);
void FindVarDecls(const Node& n);
Node program;
std::vector<uint64_t> iv;
std::map<std::string, Function> functions;
std::vector<std::vector<Variable>> varStack;
std::map<std::string, int> varOffsets;
int currVarOffset;
std::vector<std::string> strTable;
};