-
Notifications
You must be signed in to change notification settings - Fork 2
/
compiler.cpp
129 lines (96 loc) · 2.36 KB
/
compiler.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
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
//#include <iterator>
#include "settings.hpp"
#include "compiler.hpp"
SpinObject::SpinObject(std::string code)
{
blocktypes["con"] = registry.getBlock("con");
blocktypes["obj"] = registry.getBlock("obj");
blocktypes["var"] = registry.getBlock("var");
blocktypes["pub"] = registry.getBlock("pub");
blocktypes["pri"] = registry.getBlock("pri");
blocktypes["dat"] = registry.getBlock("dat");
blocktypes["pub"] = registry.getBlock("pub");
BlockFactory* currBlock = blocktypes["con"];
std::string::iterator start = code.begin();
std::string::iterator end = start;
int commentdepth = 0;
int maxCommentLevel = settings.maxCommentLevel;
while (end != code.end())
{
//std::cout << *end;
switch (*end)
{
case '{':
{
if (++commentdepth > maxCommentLevel)
{
commentdepth = maxCommentLevel;
}
break;
}
case '}':
{
if (--commentdepth < 0)
{
commentdepth = 0;
}
break;
}
case '\n':
{
if (commentdepth == 0 && *(end+1) != '\n')
{
std::string::iterator end2 = end;
end2++;
while ((*end2 != ' ' && *end2 != '\n' && *end2 != '{') && end2 != code.end())
{
//std::cout << "block char: " << *end2 << '\n';
end2++;
}
std::string blockname = std::string(end+1, end2);
std::transform(blockname.begin(), blockname.end(), blockname.begin(), ::tolower);
//std::cout << "potential block: " << blockname << '\n';
BlockFactory* nextBlock = blocktypes[blockname];
//std::cout << "length: " << std::distance(start, end+1) << '\n';
if (nextBlock != NULL)
{
if (currBlock != NULL) // in case CON isn't defined
{
currBlock->newBlock(std::string(start, end+1));
}
else
{
std::cout << "Block: " << std::string(start, end+1) << '\n';
}
end = end2;
start = end+1;
currBlock = nextBlock;
//std::cout << "Next block type: " << blockname << '\n';
}
}
}
}
end++;
}
currBlock->newBlock(std::string(start+1, end+1));
}
Compiler::Compiler()
{
}
SpinObject* Compiler::compileFile(Path path)
{
if (SpinObject* obj = objects[path])
{
return obj;
}
std::ifstream f(path);
std::stringstream s;
s << f.rdbuf();
SpinObject* obj = new SpinObject(s.str());
objects[path] = obj;
return obj;
}