This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cpp
162 lines (137 loc) · 3.2 KB
/
engine.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "engine.h"
#include "words.h"
#include "jit.h"
#include <sstream>
#include "words.inc"
Engine::Engine()
{
verbose = false;
latest = NULL;
lexer = new Lexer(std::cin);
#define WORD(name) words.push_back(new name())
#define BWORD(name) CreateWord(); { std::string _name = name
#define BUILDER JIT::GetSingleton().GetBuilder()
#define ARG(number) llvm::Value *arg##number = JIT::GetSingleton().CreateInputArgument()
#define OUT(number, val) BUILDER->CreateStore(val, JIT::GetSingleton().CreateOutputArgument())
#define EWORD() BUILDER->CreateRetVoid(); FinishWord(_name); }
#define IWORD(name, func, inputs, outputs) \
JIT::GetSingleton().AddInternalSymbol(name, (void *)&func); \
CreateExternWord(name, inputs, outputs);
#define IMMEDIATE() latest->SetImmediate(true)
#include "words_declare.inc"
#undef WORD
#undef BWORD
#undef JIT
#undef BUILDER
#undef ARG
#undef OUT
#undef EWORD
#undef IWORD
#undef INLINE
}
Engine::~Engine()
{
delete lexer;
}
void Engine::SetInputStream(std::istream &in)
{
delete lexer;
lexer = new Lexer(in);
}
Engine &Engine::GetSingleton()
{
static Engine engine;
return engine;
}
void Engine::MainLoop()
{
try
{
while(true)
{
std::string word = lexer->NextWord();
ExecuteWord(word);
}
}
catch(EndOfStream &eof)
{
}
}
Word *Engine::FindWord(const std::string &word)
{
for(Words::reverse_iterator it = words.rbegin(); it != words.rend(); it++)
if(!(*it)->IsHidden() && (*it)->GetName() == word)
return *it;
return NULL;
}
void Engine::ExecuteWord(const std::string &word)
{
Word *w = FindWord(word);
if(w != NULL)
{
w->Execute(false);
return;
}
// integer?
std::istringstream is(word);
int number;
if(is >> number)
{
LiteralWord lit(number);
lit.Execute(false);
return;
}
throw std::string("unknown word");
}
void Engine::CreateExternWord(const std::string &word, size_t inputs, size_t outputs)
{
JIT::GetSingleton().CreateExternWord(word, inputs, outputs);
latest = new FunctionWord();
latest->SetFunction(JIT::GetSingleton().GetLatest());
latest->SetInputSize(inputs);
latest->SetOutputSize(outputs);
words.push_back(latest);
}
void Engine::CreateWord()
{
JIT::GetSingleton().CreateWord();
compiler_stack.clear();
compiler_args.clear();
latest = new FunctionWord();
latest->SetHidden(true);
words.push_back(latest);
}
void Engine::FinishWord(const std::string& word)
{
latest->SetInputSize(JIT::GetSingleton().GetInputSize());
latest->SetOutputSize(JIT::GetSingleton().GetOutputSize());
JIT::GetSingleton().FinishWord(word);
latest->SetFunction(JIT::GetSingleton().GetLatest());
latest->SetHidden(false);
}
void Engine::Push(WordInstance *instance)
{
// setup outputs
for(size_t i = 0; i < instance->GetOutputSize(); i++)
compiler_stack.push_front(new WordIndex(instance, i));
}
WordIndex *Engine::Pop()
{
WordIndex *value;
if(compiler_stack.size() == 0)
{
// drop value from function argument
ArgumentWord *arg = new ArgumentWord(compiler_args.size());
WordInstance *arg_instance = new WordInstance(arg);
compiler_args.push_back(arg);
arg_instance->Compile();
value = new WordIndex(arg_instance, 0);
}
else
{
// drop value from stack
value = compiler_stack.back();
compiler_stack.pop_back();
}
return value;
}