nabaztag_hack_kit 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +7 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +4 -0
- data/README.md +119 -0
- data/Rakefile +21 -0
- data/bin/mtl_comp +41 -0
- data/bin/mtl_merge +22 -0
- data/bin/mtl_simu +40 -0
- data/bytecode/lib/buffer.mtl +109 -0
- data/bytecode/lib/button.mtl +27 -0
- data/bytecode/lib/data_helper.mtl +88 -0
- data/bytecode/lib/ear.mtl +90 -0
- data/bytecode/lib/led.mtl +33 -0
- data/bytecode/lib/rfid.mtl +50 -0
- data/bytecode/main.mtl +129 -0
- data/config.ru +6 -0
- data/ext/bytecode/Commands.md +172 -0
- data/ext/bytecode/Docs.md +488 -0
- data/ext/bytecode/README.md +5 -0
- data/ext/bytecode/lib/arp.mtl +159 -0
- data/ext/bytecode/lib/cfg.mtl +74 -0
- data/ext/bytecode/lib/choreos.mtl +1487 -0
- data/ext/bytecode/lib/dhcp.mtl +152 -0
- data/ext/bytecode/lib/dns.mtl +89 -0
- data/ext/bytecode/lib/http.mtl +84 -0
- data/ext/bytecode/lib/tcp.mtl +340 -0
- data/ext/bytecode/lib/udp.mtl +49 -0
- data/ext/bytecode/lib/util.mtl +74 -0
- data/ext/bytecode/lib/var.mtl +15 -0
- data/ext/bytecode/lib/wifi.mtl +243 -0
- data/ext/bytecode/nominal-ping.mtl +5828 -0
- data/ext/mtl/Makefile +42 -0
- data/ext/mtl/README.md +13 -0
- data/ext/mtl/bc.cpp +1891 -0
- data/ext/mtl/conf.bin.sans_password +0 -0
- data/ext/mtl/config.txt +5 -0
- data/ext/mtl/dumpbc.c +2566 -0
- data/ext/mtl/extconf.rb +1 -0
- data/ext/mtl/linux_simu.c +271 -0
- data/ext/mtl/linux_simuaudio.c +16 -0
- data/ext/mtl/linux_simuaudio.h +18 -0
- data/ext/mtl/linux_simunet.c +620 -0
- data/ext/mtl/linux_simunet.h +7 -0
- data/ext/mtl/log.c +297 -0
- data/ext/mtl/log.h +20 -0
- data/ext/mtl/main_compiler.cpp +104 -0
- data/ext/mtl/main_simu.cpp +221 -0
- data/ext/mtl/mp3/GTKANAL.H +97 -0
- data/ext/mtl/mp3/LAYER3.C +2090 -0
- data/ext/mtl/mp3/TABINIT.C +82 -0
- data/ext/mtl/mp3/common.c +265 -0
- data/ext/mtl/mp3/dct64_i386.c +316 -0
- data/ext/mtl/mp3/decode_i386.c +155 -0
- data/ext/mtl/mp3/huffman.h +332 -0
- data/ext/mtl/mp3/interface.c +258 -0
- data/ext/mtl/mp3/mpg123.h +182 -0
- data/ext/mtl/mp3/mpglib.h +44 -0
- data/ext/mtl/properties.c +293 -0
- data/ext/mtl/properties.h +10 -0
- data/ext/mtl/simu.c +750 -0
- data/ext/mtl/simuaudio.c +662 -0
- data/ext/mtl/simuaudio.h +74 -0
- data/ext/mtl/simunet.c +400 -0
- data/ext/mtl/simunet.h +30 -0
- data/ext/mtl/utils/correct_const.sh +34 -0
- data/ext/mtl/vaudio.c +677 -0
- data/ext/mtl/vaudio.h +46 -0
- data/ext/mtl/vbc.h +160 -0
- data/ext/mtl/vbc_str.h +166 -0
- data/ext/mtl/vcomp/Makefile +29 -0
- data/ext/mtl/vcomp/bootstrap.cpp +89 -0
- data/ext/mtl/vcomp/compiler.cpp +470 -0
- data/ext/mtl/vcomp/compiler.h +200 -0
- data/ext/mtl/vcomp/compiler_file.cpp +929 -0
- data/ext/mtl/vcomp/compiler_prog.cpp +250 -0
- data/ext/mtl/vcomp/compiler_term.cpp +1053 -0
- data/ext/mtl/vcomp/compiler_type.cpp +872 -0
- data/ext/mtl/vcomp/compiler_var.cpp +289 -0
- data/ext/mtl/vcomp/file.cpp +79 -0
- data/ext/mtl/vcomp/file.h +39 -0
- data/ext/mtl/vcomp/filesystem.h +14 -0
- data/ext/mtl/vcomp/interpreter.cpp +85 -0
- data/ext/mtl/vcomp/interpreter.h +121 -0
- data/ext/mtl/vcomp/memory.cpp +241 -0
- data/ext/mtl/vcomp/memory.h +326 -0
- data/ext/mtl/vcomp/param.h +95 -0
- data/ext/mtl/vcomp/parser.cpp +427 -0
- data/ext/mtl/vcomp/parser.h +97 -0
- data/ext/mtl/vcomp/parser_xml.cpp +124 -0
- data/ext/mtl/vcomp/prodbuffer.cpp +125 -0
- data/ext/mtl/vcomp/prodbuffer.h +42 -0
- data/ext/mtl/vcomp/resource.h +17 -0
- data/ext/mtl/vcomp/stdlib_core.cpp +122 -0
- data/ext/mtl/vcomp/terminal.cpp +73 -0
- data/ext/mtl/vcomp/terminal.h +30 -0
- data/ext/mtl/vcomp/util.cpp +48 -0
- data/ext/mtl/vcomp/util.h +31 -0
- data/ext/mtl/vinterp.c +1349 -0
- data/ext/mtl/vinterp.h +11 -0
- data/ext/mtl/vloader.c +127 -0
- data/ext/mtl/vloader.h +31 -0
- data/ext/mtl/vlog.c +589 -0
- data/ext/mtl/vlog.h +69 -0
- data/ext/mtl/vmem.c +424 -0
- data/ext/mtl/vmem.h +107 -0
- data/ext/mtl/vnet.c +255 -0
- data/ext/mtl/vnet.h +19 -0
- data/lib/nabaztag_hack_kit/message/api.rb +39 -0
- data/lib/nabaztag_hack_kit/message/helper.rb +39 -0
- data/lib/nabaztag_hack_kit/message.rb +36 -0
- data/lib/nabaztag_hack_kit/server.rb +50 -0
- data/lib/nabaztag_hack_kit/version.rb +3 -0
- data/lib/nabaztag_hack_kit.rb +4 -0
- data/nabaztag_hack_kit.gemspec +29 -0
- data/public/bytecode.bin +0 -0
- data/test/bytecode/helper.mtl +60 -0
- data/test/bytecode/native.mtl +28 -0
- data/test/bytecode/test.mtl +221 -0
- data/test/spec_helper.rb +5 -0
- data/test/unit/message_spec.rb +56 -0
- metadata +209 -0
@@ -0,0 +1,470 @@
|
|
1
|
+
//-------------------
|
2
|
+
// MV
|
3
|
+
// version WIN32 et POCKETPC
|
4
|
+
// Sylvain Huet
|
5
|
+
// Derniere mise a jour : 07/01/2003
|
6
|
+
//
|
7
|
+
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <string.h>
|
10
|
+
|
11
|
+
#include "param.h"
|
12
|
+
#include "terminal.h"
|
13
|
+
#include "memory.h"
|
14
|
+
#include "prodbuffer.h"
|
15
|
+
#include "interpreter.h"
|
16
|
+
#include "compiler.h"
|
17
|
+
|
18
|
+
|
19
|
+
Compiler::Compiler(Memory* mem)
|
20
|
+
{
|
21
|
+
m=mem;
|
22
|
+
bc=new Prodbuffer();
|
23
|
+
outputbuf=new Prodbuffer();
|
24
|
+
brelease=new Prodbuffer();
|
25
|
+
};
|
26
|
+
|
27
|
+
Compiler::~Compiler()
|
28
|
+
{
|
29
|
+
delete bc;
|
30
|
+
delete outputbuf;
|
31
|
+
delete brelease;
|
32
|
+
};
|
33
|
+
|
34
|
+
|
35
|
+
int Compiler::start()
|
36
|
+
{
|
37
|
+
int k;
|
38
|
+
|
39
|
+
// création du package systeme
|
40
|
+
if (k=createpackage("system",8)) return k;
|
41
|
+
systempackage=STACKGET(m,0);
|
42
|
+
m->addroot(VALTOPNT(systempackage)); // le package systeme devient une racine
|
43
|
+
if (k=addstdlibcore()) return k;
|
44
|
+
int pck=STACKREF(m);
|
45
|
+
|
46
|
+
// création du tableau des types utilisés par le compilateur
|
47
|
+
if (k=creategraph("I",systempackage,0)) return k;
|
48
|
+
if (k=creategraph("F",systempackage,0)) return k;
|
49
|
+
if (k=creategraph("S",systempackage,0)) return k;
|
50
|
+
if (k=creategraph("Env",systempackage,0)) return k;
|
51
|
+
if (k=creategraph("Xml",systempackage,0)) return k;
|
52
|
+
|
53
|
+
if (k=creategraph("fun[u0 list u0]list u0",systempackage,0)) return k;
|
54
|
+
if (k=creategraph("fun[tab u0 I]u0",systempackage,0)) return k;
|
55
|
+
if (k=creategraph("fun[fun u0 u1 u0]u1",systempackage,0)) return k;
|
56
|
+
if (k=DEFTAB(m,STDTYPE_LENGTH)) return k;
|
57
|
+
stdtypes=VALTOPNT(STACKPULL(m));
|
58
|
+
m->addroot(stdtypes);
|
59
|
+
|
60
|
+
// dumppackage(systempackage);
|
61
|
+
return 0;
|
62
|
+
}
|
63
|
+
|
64
|
+
void Compiler::stop()
|
65
|
+
{
|
66
|
+
}
|
67
|
+
|
68
|
+
int Compiler::getsystempackage()
|
69
|
+
{
|
70
|
+
return systempackage;
|
71
|
+
}
|
72
|
+
|
73
|
+
// crée et empile un package
|
74
|
+
int Compiler::createpackage(const char* name,int loghach)
|
75
|
+
{
|
76
|
+
int k;
|
77
|
+
if (k=PUSHMALLOCCLEAR(m,PACK_LENGTH)) return k;
|
78
|
+
int* p=VALTOPNT(STACKGET(m,0));
|
79
|
+
|
80
|
+
if (k=STRPUSH(m,name)) return k;
|
81
|
+
TABSET(m,p,PACK_NAME,STACKPULL(m));
|
82
|
+
|
83
|
+
if (k=PUSHMALLOCCLEAR(m,1+(1<<loghach))) return k;
|
84
|
+
TABSET(m,p,PACK_HACH,STACKPULL(m));
|
85
|
+
|
86
|
+
return 0;
|
87
|
+
}
|
88
|
+
|
89
|
+
// calcule la fonction de hachage d'une chaine (entre 0 et 255)
|
90
|
+
int Compiler::hash(char* name)
|
91
|
+
{
|
92
|
+
int v=0;
|
93
|
+
int i=0;
|
94
|
+
while(*name)
|
95
|
+
{
|
96
|
+
int c=(*name++)&255;
|
97
|
+
c=c<<i;
|
98
|
+
v+=c+(c>>8);
|
99
|
+
i=(i+1)&7;
|
100
|
+
}
|
101
|
+
return v&255;
|
102
|
+
}
|
103
|
+
|
104
|
+
// ajoute une référence à un package
|
105
|
+
// [ref package] -> [package]
|
106
|
+
void Compiler::addreftopackage(int* ref,int* package)
|
107
|
+
{
|
108
|
+
int v;
|
109
|
+
if (TABGET(ref,REF_NAME)!=NIL) v=hash(STRSTART(VALTOPNT(TABGET(ref,REF_NAME))));
|
110
|
+
else v=hash(STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))));
|
111
|
+
|
112
|
+
int* p=VALTOPNT(TABGET(package,PACK_HACH));
|
113
|
+
|
114
|
+
// TABSET(m,ref,REF_PACKAGE,PNTTOVAL(package));
|
115
|
+
v&=TABLEN(p)-2;
|
116
|
+
TABSET(m,ref,REF_NEXT,TABGET(p,v));
|
117
|
+
TABSET(m,p,v,PNTTOVAL(ref));
|
118
|
+
v=TABLEN(p)-1;
|
119
|
+
TABSET(m,ref,REF_NEXTALL,TABGET(p,v));
|
120
|
+
TABSET(m,p,v,PNTTOVAL(ref));
|
121
|
+
}
|
122
|
+
|
123
|
+
// recherche d'un type dans un environnement
|
124
|
+
int* Compiler::searchtype(int env,char* name)
|
125
|
+
{
|
126
|
+
int v=hash(name);
|
127
|
+
while(env!=NIL)
|
128
|
+
{
|
129
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
130
|
+
int vref=TABGET(p,v&(TABLEN(p)-2));
|
131
|
+
while(vref!=NIL)
|
132
|
+
{
|
133
|
+
int* ref=VALTOPNT(vref);
|
134
|
+
if ((TABGET(ref,REF_NAME)==NIL)
|
135
|
+
&&(!strcmp(name,STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))))) )
|
136
|
+
return ref;
|
137
|
+
vref=TABGET(ref,REF_NEXT);
|
138
|
+
}
|
139
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
140
|
+
}
|
141
|
+
return NULL;
|
142
|
+
}
|
143
|
+
|
144
|
+
// recherche d'un type non défini dans un environnement
|
145
|
+
int* Compiler::searchemptytype(int env,char* name)
|
146
|
+
{
|
147
|
+
int v=hash(name);
|
148
|
+
while(env!=NIL)
|
149
|
+
{
|
150
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
151
|
+
int vref=TABGET(p,v&(TABLEN(p)-2));
|
152
|
+
while(vref!=NIL)
|
153
|
+
{
|
154
|
+
int* ref=VALTOPNT(vref);
|
155
|
+
if ((TABGET(ref,REF_CODE)==INTTOVAL(CODE_EMPTYTYPE))
|
156
|
+
&&(!strcmp(name,STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1))))) )
|
157
|
+
return ref;
|
158
|
+
vref=TABGET(ref,REF_NEXT);
|
159
|
+
}
|
160
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
161
|
+
}
|
162
|
+
return NULL;
|
163
|
+
}
|
164
|
+
|
165
|
+
void Compiler::dumppackage(int env)
|
166
|
+
{
|
167
|
+
while(env!=NIL)
|
168
|
+
{
|
169
|
+
PRINTF(m)(LOG_DEVCORE,"package %s",STRSTART(VALTOPNT(TABGET(VALTOPNT(env),PACK_NAME))));
|
170
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
171
|
+
int vref=TABGET(p,TABLEN(p)-1);
|
172
|
+
while(vref!=NIL)
|
173
|
+
{
|
174
|
+
int* ref=VALTOPNT(vref);
|
175
|
+
char* name=NULL;
|
176
|
+
if (TABGET(ref,REF_NAME)!=NIL) name=STRSTART(VALTOPNT(TABGET(ref,REF_NAME)));
|
177
|
+
if (!name) name=STRSTART(VALTOPNT(TABGET(VALTOPNT(TABGET(ref,REF_TYPE)),TYPEHEADER_LENGTH+1)));
|
178
|
+
if (name) PRINTF(m)(LOG_DEVCORE," %s",name);
|
179
|
+
vref=TABGET(ref,REF_NEXTALL);
|
180
|
+
}
|
181
|
+
PRINTF(m)(LOG_DEVCORE,"\n");
|
182
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
int Compiler::searchbytype(int env,int type)
|
187
|
+
{
|
188
|
+
while(env!=NIL)
|
189
|
+
{
|
190
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
191
|
+
int vref=TABGET(p,TABLEN(p)-1);
|
192
|
+
while(vref!=NIL)
|
193
|
+
{
|
194
|
+
if (TABGET(VALTOPNT(vref),REF_TYPE)==type) return vref;
|
195
|
+
vref=TABGET(VALTOPNT(vref),REF_NEXTALL);
|
196
|
+
}
|
197
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
198
|
+
}
|
199
|
+
return NIL;
|
200
|
+
}
|
201
|
+
|
202
|
+
int Compiler::fillproto(int env,int* fun)
|
203
|
+
{
|
204
|
+
int k;
|
205
|
+
int v=hash(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))));
|
206
|
+
while(env!=NIL)
|
207
|
+
{
|
208
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
209
|
+
int vref=TABGET(p,v&(TABLEN(p)-2));
|
210
|
+
while(vref!=NIL)
|
211
|
+
{
|
212
|
+
int* ref=VALTOPNT(vref);
|
213
|
+
if ((ref!=fun)&&(VALTOINT(TABGET(ref,REF_CODE))>=0)) // on recherche des fonctions
|
214
|
+
{
|
215
|
+
if ( (!strcmp(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))),STRSTART(VALTOPNT(TABGET(ref,REF_NAME)))))
|
216
|
+
&&(TABGET(ref,REF_VAL)==NIL) )
|
217
|
+
{
|
218
|
+
if (k=unifbigger(VALTOPNT(TABGET(fun,REF_TYPE)),VALTOPNT(TABGET(ref,REF_TYPE))))
|
219
|
+
{
|
220
|
+
PRINTF(m)(LOG_COMPILER,"Compiler : prototype does not match\n");
|
221
|
+
return k;
|
222
|
+
}
|
223
|
+
TABSET(m,ref,REF_VAL,TABGET(fun,REF_VAL));
|
224
|
+
// TABSET(m,ref,REF_PACKAGE,TABGET(fun,REF_PACKAGE));
|
225
|
+
}
|
226
|
+
}
|
227
|
+
vref=TABGET(ref,REF_NEXT);
|
228
|
+
}
|
229
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
230
|
+
}
|
231
|
+
return 0;
|
232
|
+
}
|
233
|
+
|
234
|
+
int Compiler::findproto(int env,int* fun)
|
235
|
+
{
|
236
|
+
int v=hash(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))));
|
237
|
+
while(env!=NIL)
|
238
|
+
{
|
239
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
240
|
+
int vref=TABGET(p,v&(TABLEN(p)-2));
|
241
|
+
while(vref!=NIL)
|
242
|
+
{
|
243
|
+
int* ref=VALTOPNT(vref);
|
244
|
+
if ((ref!=fun)&&(VALTOINT(TABGET(ref,REF_CODE))>=0)) // on recherche des fonctions
|
245
|
+
{
|
246
|
+
if ( (!strcmp(STRSTART(VALTOPNT(TABGET(fun,REF_NAME))),STRSTART(VALTOPNT(TABGET(ref,REF_NAME)))))
|
247
|
+
&&(TABGET(ref,REF_VAL)==NIL) )
|
248
|
+
{
|
249
|
+
return TABGET(ref,REF_PACKAGE);
|
250
|
+
}
|
251
|
+
}
|
252
|
+
vref=TABGET(ref,REF_NEXT);
|
253
|
+
}
|
254
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
255
|
+
}
|
256
|
+
return NIL;
|
257
|
+
}
|
258
|
+
|
259
|
+
// recherche d'une référence dans un environnement
|
260
|
+
int* Compiler::searchref(int env,char* name)
|
261
|
+
{
|
262
|
+
int v=hash(name);
|
263
|
+
while(env!=NIL)
|
264
|
+
{
|
265
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
266
|
+
int vref=TABGET(p,v&(TABLEN(p)-2));
|
267
|
+
while(vref!=NIL)
|
268
|
+
{
|
269
|
+
int* ref=VALTOPNT(vref);
|
270
|
+
if ((TABGET(ref,REF_NAME)!=NIL)
|
271
|
+
&&(!strcmp(name,STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) )
|
272
|
+
{
|
273
|
+
// à partir du moment où on recherche cette référence, c'est
|
274
|
+
// qu'elle est utlisé quelque part, donc...
|
275
|
+
TABSET(m,ref,REF_USED,INTTOVAL(1));
|
276
|
+
|
277
|
+
return ref;
|
278
|
+
}
|
279
|
+
vref=TABGET(ref,REF_NEXT);
|
280
|
+
}
|
281
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
282
|
+
}
|
283
|
+
return NULL;
|
284
|
+
}
|
285
|
+
|
286
|
+
// recherche d'une référence dans un environnement, ne marque pas la référence comme utilisée
|
287
|
+
int* Compiler::searchref_nosetused(int env,char* name)
|
288
|
+
{
|
289
|
+
int v=hash(name);
|
290
|
+
while(env!=NIL)
|
291
|
+
{
|
292
|
+
int* p=VALTOPNT(TABGET(VALTOPNT(env),PACK_HACH));
|
293
|
+
int vref=TABGET(p,v&(TABLEN(p)-2));
|
294
|
+
while(vref!=NIL)
|
295
|
+
{
|
296
|
+
int* ref=VALTOPNT(vref);
|
297
|
+
if ((TABGET(ref,REF_NAME)!=NIL)
|
298
|
+
&&(!strcmp(name,STRSTART(VALTOPNT(TABGET(ref,REF_NAME))))) )
|
299
|
+
return ref;
|
300
|
+
vref=TABGET(ref,REF_NEXT);
|
301
|
+
}
|
302
|
+
env=TABGET(VALTOPNT(env),PACK_NEXT);
|
303
|
+
}
|
304
|
+
return NULL;
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
// ajout d'un label dans une liste de labels
|
310
|
+
int Compiler::addlabel(int base,const char* name,int val,int ref)
|
311
|
+
{
|
312
|
+
int k;
|
313
|
+
int* p=MALLOCCLEAR(m,LABELLIST_LENGTH);
|
314
|
+
if (!p) return MTLERR_OM;
|
315
|
+
TABSET(m,p,LABELLIST_NEXT,STACKGETFROMREF(m,base,0));
|
316
|
+
STACKSETFROMREF(m,base,0,PNTTOVAL(p));
|
317
|
+
|
318
|
+
if (k=STRPUSH(m,name)) return k;
|
319
|
+
TABSET(m,p,LABELLIST_NAME,STACKPULL(m));
|
320
|
+
TABSET(m,p,LABELLIST_VAL,val);
|
321
|
+
TABSET(m,p,LABELLIST_REF,ref);
|
322
|
+
return 0;
|
323
|
+
}
|
324
|
+
|
325
|
+
// compte le nombre de labels
|
326
|
+
int Compiler::nblabels(int base)
|
327
|
+
{
|
328
|
+
int n=0;
|
329
|
+
int vlab=STACKGETFROMREF(m,base,0);
|
330
|
+
while(vlab!=NIL)
|
331
|
+
{
|
332
|
+
n++;
|
333
|
+
vlab=TABGET(VALTOPNT(vlab),LABELLIST_NEXT);
|
334
|
+
}
|
335
|
+
return n;
|
336
|
+
}
|
337
|
+
|
338
|
+
void Compiler::removenlabels(int base,int n)
|
339
|
+
{
|
340
|
+
while(n>0)
|
341
|
+
{
|
342
|
+
STACKSETFROMREF(m,base,0,TABGET(VALTOPNT(STACKGETFROMREF(m,base,0)),LABELLIST_NEXT));
|
343
|
+
n--;
|
344
|
+
}
|
345
|
+
}
|
346
|
+
|
347
|
+
// recherche d'un label dans une liste de labels
|
348
|
+
int Compiler::searchlabel_byname(int base,char* name,int* val,int* ref)
|
349
|
+
{
|
350
|
+
int vlab=STACKGETFROMREF(m,base,0);
|
351
|
+
while(vlab!=NIL)
|
352
|
+
{
|
353
|
+
int* lab=VALTOPNT(vlab);
|
354
|
+
if (!strcmp(name,STRSTART(VALTOPNT(TABGET(lab,LABELLIST_NAME)))) )
|
355
|
+
{
|
356
|
+
if (val) *val=TABGET(lab,LABELLIST_VAL);
|
357
|
+
if (ref) *ref=TABGET(lab,LABELLIST_REF);
|
358
|
+
return 0;
|
359
|
+
}
|
360
|
+
vlab=TABGET(lab,LABELLIST_NEXT);
|
361
|
+
}
|
362
|
+
return -1;
|
363
|
+
}
|
364
|
+
|
365
|
+
// recherche d'un label dans une liste de labels
|
366
|
+
int Compiler::searchlabel_byval(int base,int val,char** name)
|
367
|
+
{
|
368
|
+
int vlab=STACKGETFROMREF(m,base,0);
|
369
|
+
while(vlab!=NIL)
|
370
|
+
{
|
371
|
+
int* lab=VALTOPNT(vlab);
|
372
|
+
if (val==TABGET(lab,LABELLIST_VAL))
|
373
|
+
{
|
374
|
+
*name=STRSTART(VALTOPNT(TABGET(lab,LABELLIST_NAME)));
|
375
|
+
return 0;
|
376
|
+
}
|
377
|
+
vlab=TABGET(lab,LABELLIST_NEXT);
|
378
|
+
}
|
379
|
+
return -1;
|
380
|
+
}
|
381
|
+
|
382
|
+
// création d'un tuple à partir d'une liste de labels
|
383
|
+
int* Compiler::tuplefromlabels(int base)
|
384
|
+
{
|
385
|
+
int n=nblabels(base);
|
386
|
+
int* t=MALLOC(m,n,TYPE_TAB);
|
387
|
+
if (!t) return t;
|
388
|
+
|
389
|
+
int vlab=STACKGETFROMREF(m,base,0);
|
390
|
+
int i; for(i=n-1;i>=0;i--)
|
391
|
+
{
|
392
|
+
int* lab=VALTOPNT(vlab);
|
393
|
+
TABSET(m,t,i,TABGET(lab,LABELLIST_REF));
|
394
|
+
vlab=TABGET(lab,LABELLIST_NEXT);
|
395
|
+
}
|
396
|
+
return t;
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
// ajoute des fonctions à un package
|
401
|
+
int Compiler::addnative(int nref, const char** nameref, int* valref
|
402
|
+
, int* coderef, const char** typeref,void* arg)
|
403
|
+
{
|
404
|
+
int i,k;
|
405
|
+
|
406
|
+
for(i=0;i<nref;i++)
|
407
|
+
{
|
408
|
+
int* p=MALLOCCLEAR(m,REF_LENGTH);
|
409
|
+
if (!p) return MTLERR_OM;
|
410
|
+
if (k=STACKPUSH(m,PNTTOVAL(p))) return MTLERR_OM;
|
411
|
+
|
412
|
+
if (nameref[i])
|
413
|
+
{
|
414
|
+
if (k=STRPUSH(m,nameref[i])) return k;
|
415
|
+
TABSET(m,p,REF_NAME,STACKPULL(m));
|
416
|
+
}
|
417
|
+
|
418
|
+
TABSET(m,p,REF_CODE,INTTOVAL(coderef[i]));
|
419
|
+
|
420
|
+
if (coderef[i]>=0)
|
421
|
+
{
|
422
|
+
if (k=creategraph(typeref[i],systempackage,0)) return k;
|
423
|
+
TABSET(m,p,REF_TYPE,STACKPULL(m));
|
424
|
+
|
425
|
+
// if (k=PUSHPNT(m,(int*)valref[i])) return k;
|
426
|
+
if (k=STACKPUSH(m,INTTOVAL(valref[i]))) return k;
|
427
|
+
|
428
|
+
int* fun=MALLOCCLEAR(m,FUN_LENGTH);
|
429
|
+
if (!fun) return MTLERR_OM;
|
430
|
+
TABSET(m,p,REF_VAL,PNTTOVAL(fun));
|
431
|
+
|
432
|
+
TABSET(m,fun,FUN_NBARGS,INTTOVAL(coderef[i]));
|
433
|
+
TABSET(m,fun,FUN_NBLOCALS,NIL);
|
434
|
+
// PRINTF(m)(1,"valref %s = %x\n",nameref[i],valref[i]);
|
435
|
+
// TABSET(m,fun,FUN_BC,((int)valref[i])<<1);
|
436
|
+
TABSET(m,fun,FUN_BC,STACKPULL(m));
|
437
|
+
TABSET(m,fun,FUN_REF,(int)arg);
|
438
|
+
TABSET(m,fun,FUN_REFERENCE,PNTTOVAL(p));
|
439
|
+
}
|
440
|
+
else if ((coderef[i]==CODE_VAR)
|
441
|
+
||(coderef[i]==CODE_CONS)||(coderef[i]==CODE_CONS0))
|
442
|
+
{
|
443
|
+
if (k=creategraph(typeref[i],systempackage,0)) return k;
|
444
|
+
TABSET(m,p,REF_TYPE,STACKPULL(m));
|
445
|
+
TABSET(m,p,REF_VAL,(int)valref[i]);
|
446
|
+
}
|
447
|
+
else if (coderef[i]==CODE_FIELD)
|
448
|
+
{
|
449
|
+
if (k=creategraph(typeref[i],systempackage,0)) return k;
|
450
|
+
if (k=STACKPUSH(m,(int)valref[i])) return k;
|
451
|
+
int vtype=searchbytype(systempackage,
|
452
|
+
TABGET(argsfromfun(VALTOPNT(STACKGET(m,1))),TYPEHEADER_LENGTH));
|
453
|
+
if (k=STACKPUSH(m,vtype)) return k;
|
454
|
+
if (k=DEFTAB(m,2)) return k;
|
455
|
+
TABSET(m,p,REF_VAL,STACKPULL(m));
|
456
|
+
TABSET(m,p,REF_TYPE,STACKPULL(m));
|
457
|
+
}
|
458
|
+
else if ((coderef[i]==CODE_TYPE)||(coderef[i]==CODE_SUM)||(coderef[i]==CODE_STRUCT))
|
459
|
+
{
|
460
|
+
if (k=createnodetypecore(typeref[i])) return k;
|
461
|
+
TABSET(m,p,REF_TYPE,STACKPULL(m));
|
462
|
+
TABSET(m,p,REF_VAL,(int)valref[i]);
|
463
|
+
}
|
464
|
+
addreftopackage(p,VALTOPNT(systempackage));
|
465
|
+
STACKDROP(m);
|
466
|
+
}
|
467
|
+
return 0;
|
468
|
+
}
|
469
|
+
|
470
|
+
|
@@ -0,0 +1,200 @@
|
|
1
|
+
//-------------------
|
2
|
+
// MV
|
3
|
+
// version WIN32 et POCKETPC
|
4
|
+
// Sylvain Huet
|
5
|
+
// Premiere version : 07/01/2003
|
6
|
+
// Derniere mise a jour : 07/01/2003
|
7
|
+
//
|
8
|
+
|
9
|
+
#ifndef _COMPILER_
|
10
|
+
#define _COMPILER_
|
11
|
+
|
12
|
+
#include"interpreter.h"
|
13
|
+
|
14
|
+
class Memory;
|
15
|
+
class Parser;
|
16
|
+
class Prodbuffer;
|
17
|
+
|
18
|
+
class Compiler
|
19
|
+
{
|
20
|
+
private:
|
21
|
+
Memory* m;
|
22
|
+
int systempackage;
|
23
|
+
int* stdtypes;
|
24
|
+
|
25
|
+
Prodbuffer* bc; // structure de production de bytecode
|
26
|
+
|
27
|
+
Prodbuffer* outputbuf; // structure de production des messages de type
|
28
|
+
|
29
|
+
|
30
|
+
Parser* parser;
|
31
|
+
int* newpackage;
|
32
|
+
int* newref;
|
33
|
+
int globals;
|
34
|
+
int locals;
|
35
|
+
int nblocals;
|
36
|
+
|
37
|
+
int ifuns;
|
38
|
+
|
39
|
+
// typage
|
40
|
+
int createnodetypecore(const char* name);
|
41
|
+
int createnodetypecore(int name);
|
42
|
+
int createnodetype(int type);
|
43
|
+
int createnodetuple(int size);
|
44
|
+
int createnodetupleval(int size);
|
45
|
+
|
46
|
+
int* actualtype(int* p);
|
47
|
+
|
48
|
+
int parsegraph(Parser* p,int env,int mono,int rec,int labels,int newvars,int* rnode);
|
49
|
+
int parse_rnode(int* p);
|
50
|
+
|
51
|
+
int creategraph(Parser* p,int env,int mono);
|
52
|
+
int creategraph(Parser* p,int env,int mono,int labels);
|
53
|
+
int creategraph(const char* src,int env,int mono);
|
54
|
+
|
55
|
+
int recechograph(Prodbuffer *output,int* p,int rec,int labels);
|
56
|
+
|
57
|
+
int reccopytype(int* p);
|
58
|
+
int recresetcopy(int* p);
|
59
|
+
|
60
|
+
int recgoweak(int* p);
|
61
|
+
|
62
|
+
int restoreactual(int* t,int* s,int vt,int vs,int k);
|
63
|
+
int recunif(int* x,int* y);
|
64
|
+
int unif(int* x,int* y);
|
65
|
+
int unif_argfun();
|
66
|
+
|
67
|
+
int unifbigger(int* x,int* y);
|
68
|
+
|
69
|
+
int* argsfromfun(int *f);
|
70
|
+
|
71
|
+
void echonode(int code,int* p);
|
72
|
+
// packages
|
73
|
+
int hash(char* name);
|
74
|
+
int createpackage(const char* name,int loghach);
|
75
|
+
void addreftopackage(int* ref,int* package);
|
76
|
+
int* searchtype(int env,char* name);
|
77
|
+
int* searchemptytype(int env,char* name);
|
78
|
+
|
79
|
+
void dumppackage(int env);
|
80
|
+
|
81
|
+
int searchbytype(int env,int type);
|
82
|
+
|
83
|
+
// liste de labels
|
84
|
+
int addlabel(int base,const char* name,int val,int ref);
|
85
|
+
int nblabels(int base);
|
86
|
+
void removenlabels(int base,int n);
|
87
|
+
int searchlabel_byname(int base,char* name,int* val,int* ref);
|
88
|
+
int searchlabel_byval(int base,int val,char** name);
|
89
|
+
int* tuplefromlabels(int base);
|
90
|
+
|
91
|
+
// compilation
|
92
|
+
int parsefile(int ifdef);
|
93
|
+
int parsevar();
|
94
|
+
int parseconst();
|
95
|
+
int parseproto();
|
96
|
+
int parseifdef(int ifndef);
|
97
|
+
int skipifdef();
|
98
|
+
int fillproto(int env,int* fun);
|
99
|
+
int findproto(int env,int* fun);
|
100
|
+
int parsetype();
|
101
|
+
int parsestruct();
|
102
|
+
int parsesum();
|
103
|
+
|
104
|
+
int parsefun();
|
105
|
+
int parseprogram();
|
106
|
+
int parseexpression();
|
107
|
+
int parsearithm();
|
108
|
+
int parsea1();
|
109
|
+
int parsea2();
|
110
|
+
int parsea3();
|
111
|
+
int parsea4();
|
112
|
+
int parsea5();
|
113
|
+
int parsea6();
|
114
|
+
int parseterm();
|
115
|
+
int parseref();
|
116
|
+
int parseif();
|
117
|
+
int parselet();
|
118
|
+
int parseset();
|
119
|
+
int parsewhile();
|
120
|
+
int parsefor();
|
121
|
+
int parsepntfun();
|
122
|
+
int parselink();
|
123
|
+
int parsecall();
|
124
|
+
int parselocals();
|
125
|
+
int parsestring();
|
126
|
+
int parsefields(int* p);
|
127
|
+
int parsematch();
|
128
|
+
int parsematchcons(int* end);
|
129
|
+
|
130
|
+
int parsegetpoint();
|
131
|
+
int parsesetpoint(int local,int ind,int* opstore);
|
132
|
+
|
133
|
+
int parseupdate();
|
134
|
+
int parseupdatevals();
|
135
|
+
|
136
|
+
// parsing variables
|
137
|
+
int parseval();
|
138
|
+
int parseval3();
|
139
|
+
int parseval4();
|
140
|
+
int parseval5();
|
141
|
+
int parseval6();
|
142
|
+
int parseval7();
|
143
|
+
|
144
|
+
// production bytecode
|
145
|
+
void bc_byte_or_int(int val,int opbyte,int opint);
|
146
|
+
void bcint_byte_or_int(int val);
|
147
|
+
// autres
|
148
|
+
int addstdlibcore();
|
149
|
+
int addstdlibstr();
|
150
|
+
int addstdlibbuf();
|
151
|
+
int addstdlibfiles();
|
152
|
+
|
153
|
+
int recglobal(int val,Prodbuffer *b);
|
154
|
+
int recglobals(int vlab,Prodbuffer *b);
|
155
|
+
int recbc(int vref,Prodbuffer *b,Prodbuffer *btab,int offset);
|
156
|
+
|
157
|
+
public:
|
158
|
+
Prodbuffer* brelease;
|
159
|
+
Compiler(Memory* mem);
|
160
|
+
~Compiler();
|
161
|
+
int start();
|
162
|
+
void stop();
|
163
|
+
int addnative(int nref, const char** nameref, int* valref
|
164
|
+
, int* coderef, const char** typeref,void* arg);
|
165
|
+
|
166
|
+
int gocompile(int type); // [filename/src packages] -> [packages]
|
167
|
+
int getsystempackage();
|
168
|
+
|
169
|
+
int* searchref(int env,char* name);
|
170
|
+
int* searchref_nosetused(int env,char* name);
|
171
|
+
|
172
|
+
|
173
|
+
int echograph(Prodbuffer *output,int* p);
|
174
|
+
int copytype(int* p);
|
175
|
+
int recunifbigger(int* x,int* y);
|
176
|
+
|
177
|
+
};
|
178
|
+
|
179
|
+
#define LABELLIST_LENGTH 4
|
180
|
+
#define LABELLIST_NAME 0
|
181
|
+
#define LABELLIST_VAL 1
|
182
|
+
#define LABELLIST_REF 2
|
183
|
+
#define LABELLIST_NEXT 3
|
184
|
+
|
185
|
+
#define STDTYPE_LENGTH 8
|
186
|
+
#define STDTYPE_I 0
|
187
|
+
#define STDTYPE_F 1
|
188
|
+
#define STDTYPE_S 2
|
189
|
+
#define STDTYPE_Env 3
|
190
|
+
#define STDTYPE_Xml 4
|
191
|
+
#define STDTYPE_fun__u0_list_u0__list_u0 5
|
192
|
+
#define STDTYPE_fun__tab_u0_I__u0 6
|
193
|
+
#define STDTYPE_fun__fun_u0_u1_u0__u1 7
|
194
|
+
|
195
|
+
|
196
|
+
#define COMPILE_FROMFILE 0
|
197
|
+
#define COMPILE_FROMSTRING 1
|
198
|
+
|
199
|
+
|
200
|
+
#endif
|