-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.h
114 lines (100 loc) · 3.39 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
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
108
109
110
111
112
113
114
#pragma once
#include "color_text.h"
#include "common.h"
#include <iostream>
#include <memory>
#include <vector>
#include <map>
class Parser;
class Lexer;
class FunctionAST;
class VariableDefStatementAST;
class TypeAST;
class DataDefAST;
class GroupDefAST;
class Flame;
class VM;
enum{Left,Right};
enum{Unary,Binary};
class ConstantPool{
private:
vector< VMValue > refpool;
public:
int SetValue(VMValue item){
refpool.push_back(item);
return refpool.size()-1;
}
VMValue GetValue(int index){
return refpool[index];
}
unsigned int Size(){
return refpool.size();
}
void Clear(){
refpool.clear();
};
};
class OperatorInfo{
public:
OperatorInfo(){}
OperatorInfo(int uorb, int assoc, int prec) :UnaryOrBinary(uorb), Associativity(assoc), Precedence(prec){ UserDef = false; }
OperatorInfo(int uorb, int assoc, int prec,bool is_userdef) :UnaryOrBinary(uorb), Associativity(assoc), Precedence(prec),UserDef(is_userdef){ }
int UnaryOrBinary;
int Associativity;
int Precedence;
bool UserDef;
};
//パースした時に得た情報を詰め込んでおく
class CodegenInfo{
public:
multimap<string,OperatorInfo> OperatorList;
vector<shared_ptr<FunctionAST> > TopLevelFunction;
vector<shared_ptr<VariableDefStatementAST> > TopLevelVariableDef;
vector<shared_ptr<DataDefAST> > TopLevelDataDef;
vector<shared_ptr<GroupDefAST> > TopLevelGroupDef;
shared_ptr<vector<int> > Bootstrap;
shared_ptr<vector<label_or_immediate> > Bootstrap_labels;
int MainFuncPoolIndex; //main関数のコンスタントプール・インデックス
shared_ptr<vector< pair<string,shared_ptr<TypeAST> > > > LocalVariables;
CodegenInfo(){
LocalVariables=make_shared<vector< pair<string,shared_ptr<TypeAST> > > >();
Bootstrap=make_shared<vector<int> >();
Bootstrap_labels=make_shared<vector<label_or_immediate> >();
}
};
class Executable{
public:
shared_ptr<vector<label_or_immediate> > Bootstrap_labels;
int MainFuncPoolIndex; //main関数のコンスタントプール・インデックス
shared_ptr<vector< pair<string,shared_ptr<TypeAST> > > > LocalVariables;
};
class Compiler{
private:
void ASTgen();
void TypeCheck();
void CTFE(int);
void Codegen();
void RegisterBuiltinFunction(string name,void (*funcptr)(shared_ptr<Flame>),shared_ptr<vector< pair<string,shared_ptr<TypeAST> > > > arg,shared_ptr<TypeAST> rettype,bool ctfeable); //トップレベル関数リスト、ビルトイン関数リストへ登録します
public:
shared_ptr<Lexer> lexer;
shared_ptr<Parser> parser;
shared_ptr<CodegenInfo> genInfo;
Compiler(shared_ptr<Lexer> l,shared_ptr<Parser> p):lexer(l),parser(p){
genInfo=make_shared<CodegenInfo>();
}
shared_ptr<Executable> Compile(){
//cout<<endl<<BG_GREEN<<"構文解析を行っています..."<<RESET<<endl;
ASTgen();
//cout<<BG_GREEN<<"型検査を行っています..."<<RESET<<endl;
TypeCheck();
//cout<<BG_GREEN<<"コード生成を行っています..."<<RESET<<endl;
Codegen();
//cout<<BG_GREEN<<"コンパイル時関数実行を行っています..."<<RESET<<endl;
//CTFE(3);
shared_ptr<Executable> ret_executable=make_shared<Executable>();
ret_executable->Bootstrap_labels=move(genInfo->Bootstrap_labels);
ret_executable->MainFuncPoolIndex=genInfo->MainFuncPoolIndex;
ret_executable->LocalVariables=move(genInfo->LocalVariables);
return ret_executable;
}
};