-
Notifications
You must be signed in to change notification settings - Fork 5
/
24module.c
181 lines (147 loc) · 4.92 KB
/
24module.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "common.h"
class sModuleNode extends sNodeBase {
new(string name, sClassModule*% module, sInfo* info) {
string self.name = string(name);
sClassModule*% self.module = clone module;
}
string kind()
{
return string("sModuleNode");
}
bool compile(sInfo* info)
{
string name = string(self.name);
sClassModule* module = self.module;
return true;
}
};
class sHeaderNode extends sNodeBase {
new(string contents, sInfo* info) {
string self.contents = clone contents;
}
string kind()
{
return string("sHeaderNode");
}
bool compile(sInfo* info)
{
string contents = self.contents;
//add_come_code_at_come_header(info, "%s\n", contents);
return true;
}
};
sNode*% top_level(char* buf, char* head, int head_sline, sInfo* info) version 91
{
if(buf === "module") {
var type_name = parse_word();
string sname = info.sname;
int sline = info.sline;
list<string>*% params = new list<string>();
if(*info->p == '<') {
info->p++;
skip_spaces_and_lf();
while(true) {
string word = parse_word();
params.add(word);
if(*info->p == ',') {
info->p++;
skip_spaces_and_lf();
}
else if(*info->p == '\0') {
err_msg(info, "invalid source end");
exit(2);
}
else if(*info->p == '>') {
info->p++;
skip_spaces_and_lf();
break;
}
else {
err_msg(info, "invalid charactor(%c)", *info->p);
exit(2);
}
}
}
expected_next_character('{');
char* source_head = info.p;
char* source_tail = null;
var buf = new buffer();
bool squort = false;
bool dquort = false;
int nest = 1;
while(1) {
if(*info->p == '\0') {
err_msg(info, "unexpected source end in module");
exit(2);
}
else if(*info->p == '\\') {
buf.append_char(*info->p);
info->p++;
if(*info->p == '\n') {
info->sline++;
}
buf.append_char(*info->p);
info->p++;
}
else if(!squort && *info->p == '"') {
buf.append_char(*info->p);
info->p++;
dquort = !dquort;
}
else if(!dquort && *info->p == '\'') {
buf.append_char(*info->p);
info->p++;
squort = !squort;
}
else if(squort || dquort) {
buf.append_char(*info->p);
if(*info->p == '\n') {
info->sline++;
}
info->p++;
}
else if(*info->p == '{') {
nest++;
buf.append_char(*info->p);
info->p++;
}
else if(*info->p == '}') {
nest--;
if(nest == 0) {
source_tail = info->p - 1;
info->p++;
skip_spaces_and_lf();
break;
}
buf.append_char(*info->p);
info->p++;
}
else if(*info->p == '\n') {
info->sline++;
buf.append_char(*info->p);
info->p++;
}
else {
buf.append_char(*info->p);
info->p++;
}
}
buffer*% header = new buffer();
header.append_str(s"module \{type_name}\n");
header.append_str("{\n");
header.append(source_head, source_tail - source_head);
header.append_str("\n}\n");
add_come_code_at_come_header(info, "%s\n", header.to_string());
sClassModule*% module = new sClassModule(type_name, buf.to_string(), sname, sline, info);
module.mParams = clone params;
info.modules[string(type_name)] = module;
return new sModuleNode(string(type_name), module, info) implements sNode;
}
else if(buf === "header" && *info->p == '{') {
string block_text = skip_block();
string contents = block_text.substring(1, -3);
add_come_code_at_come_header(info, "%s\n", contents);
return new sHeaderNode(contents, info) implements sNode;
}
return inherit(buf, head, head_sline, info);
}