~ubuntu-branches/ubuntu/oneiric/yacas/oneiric

« back to all changes in this revision

Viewing changes to compile/compile3

  • Committer: Bazaar Package Importer
  • Author(s): Gopal Narayanan
  • Date: 2002-04-23 13:50:51 UTC
  • Revision ID: james.westby@ubuntu.com-20020423135051-bbd6ov4orr8eufmw
Tags: upstream-1.0.51
ImportĀ upstreamĀ versionĀ 1.0.51

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
   Compiling a line: expression -> LispPtr.
 
4
   Compile(...) is responsible for leaving one result on
 
5
    top of the stack.
 
6
 */
 
7
 
 
8
predefSymbols:={};
 
9
predefFuncs:={};
 
10
 
 
11
predefBody:={};
 
12
curBody:={};
 
13
 
 
14
initSection:={};
 
15
 
 
16
 
 
17
PopSection() :=
 
18
[
 
19
  curBody:=Pop(predefBody,1);
 
20
];
 
21
PushSection(section):=
 
22
[
 
23
  Push(predefBody,curBody);
 
24
  curBody:=section;
 
25
];
 
26
AddAsm(code):=
 
27
[
 
28
//Echo({"pushing ",code});
 
29
//Echo({curBody});
 
30
DestructiveAppend(curBody,code);
 
31
];
 
32
 
 
33
 
 
34
 
 
35
//  funcs["func"]["arity"]{"args","props",{"precedence","predicate","body"}}
 
36
 
 
37
MakeFunction(_name)_(predefFuncs[String(name)] = Empty)  <--
 
38
[
 
39
  predefFuncs[String(name)] := {};
 
40
];
 
41
 
 
42
DeclareSimpleFunction(name,args,body):=
 
43
[
 
44
//Echo({"ENTER ",name,args,body});
 
45
  MakeFunction(name);
 
46
  predefFuncs[String(name)][String(Length(args))] :=
 
47
  {
 
48
    args,
 
49
    {/*props*/},
 
50
    {{0,CompileExpression(True),body}}
 
51
  };
 
52
//  Echo({predefFuncs});
 
53
];
 
54
 
 
55
CompileFile(_f)<--
 
56
[
 
57
  Echo({"Converting file [",f,"] to ",DriverCodeType()});
 
58
  PushSection(initSection);
 
59
  AddAsm(PROLOG());
 
60
  AddAsm(ENTERFILE(f));
 
61
  PopSection();
 
62
 
 
63
  FromFile(f)
 
64
  [
 
65
    Local(line);
 
66
    While(line != EndOfFile)
 
67
    [
 
68
      Set(line,Read());
 
69
      If(line != EndOfFile,CompileBaseLine(line));
 
70
    ];
 
71
  ];
 
72
 
 
73
  PushSection(initSection);
 
74
  AddAsm(EPILOG());
 
75
  PopSection();
 
76
//Echo({initSection});
 
77
  //GarbageCollect();
 
78
];
 
79
 
 
80
 
 
81
 
 
82
10 # SymbolClob(sym_IsString) <--  String(UniqueConstant());
 
83
10 # SymbolClob(sym_IsNumber)_(Not IsInteger(sym)) <--  String(UniqueConstant());
 
84
20 # SymbolClob(sym_IsAtom) <-- "symbol_":String(sym);
 
85
30 # SymbolClob(_sym) <--
 
86
[
 
87
 Check(False,"Error: trying to make \"":(ToString()Write(sym)):"\" into a symbol");
 
88
];
 
89
 
 
90
10 # GetSymbol(sym_IsAtom)_(predefSymbols[String(sym)] != Empty) <--
 
91
[
 
92
 (predefSymbols[String(sym)][1]);
 
93
];
 
94
20 # GetSymbol(sym_IsAtom)_(predefSymbols[String(sym)] = Empty) <--
 
95
[
 
96
 predefSymbols[String(sym)] := {SymbolClob(sym),CompileExpression(sym)};
 
97
 GetSymbol(sym);
 
98
];
 
99
 
 
100
30 # GetSymbol(_sym) <--
 
101
[
 
102
 Check(False,"Error: trying to make \"":(ToString()Write(sym)):"\" into a symbol");
 
103
];
 
104
 
 
105
 
 
106
 
 
107
10 # Compile(Set(_a,_b)) <--
 
108
[
 
109
CompileFixedAtom(a);
 
110
Compile(b);
 
111
AddAsm(SETVAR(TOP(-1),TOP(0)));
 
112
AddAsm(POP());
 
113
AddAsm(SETTRUE(TOP(0)));
 
114
];
 
115
 
 
116
10 # Compile(a_IsAtom := _b) <--
 
117
[
 
118
CompileFixedAtom(a);
 
119
Compile(b);
 
120
AddAsm(SETVAR(TOP(-1),TOP(0)));
 
121
AddAsm(POP());
 
122
AddAsm(SETTRUE(TOP(0)));
 
123
];
 
124
 
 
125
 
 
126
19 # Compile(a_IsNumber) <-- CompileFixedAtom(a);
 
127
19 # Compile(True)       <-- CompileFixedAtom(True);
 
128
19 # Compile(False)      <-- CompileFixedAtom(False);
 
129
 
 
130
20 # CompileFixedAtom(a_IsAtom) <--
 
131
[
 
132
AddAsm(PUSH());
 
133
AddAsm(SET(TOP(0),GetSymbol(a)));
 
134
];
 
135
 
 
136
20 # Compile(a_IsAtom) <--
 
137
[
 
138
AddAsm(PUSH());
 
139
AddAsm(SET(TOP(0),CompileExpression(a)));
 
140
AddAsm(EVALUATE(TOP(0),TOP(0)));
 
141
];
 
142
 
 
143
 
 
144
10 # Compile(_f)_(Type(f) = "Local") <--
 
145
[
 
146
  Local(s);
 
147
  Set(s,Tail(Listify(f)));
 
148
  AddAsm(PUSH());
 
149
  ForEach(item,s)
 
150
    AddAsm(LOCAL(String(item)));
 
151
  AddAsm(SETTRUE(TOP(0)));
 
152
];
 
153
/*
 
154
*/
 
155
 
 
156
//native.GetPrecision(aEnvironment,TOPPTR());
 
157
 
 
158
 
 
159
 
 
160
0 # TypeIsDefined(_other)         <-- (predefFuncs[other]!=Empty);
 
161
10 # HasNativeDefined(f_IsFunction) <-- TypeIsDefined(Type(f));
 
162
20 # HasNativeDefined(_other) <-- False;
 
163
10 # Compile(f_HasNativeDefined) <--
 
164
[
 
165
  // place holder for result
 
166
  AddAsm(PUSH()); 
 
167
 
 
168
  // Push arguments
 
169
  Local(args);
 
170
  Set(args,Tail(Listify(f)));
 
171
  ForEach(item,args)Compile(item);
 
172
 
 
173
  // Call function
 
174
  AddAsm(NATIVEDEFINED(Type(f):"_":String(Length(args))));
 
175
];
 
176
 
 
177
 
 
178
// TypeIsNative can be expanded upon in the driver
 
179
20 # TypeIsNative(_other)         <-- False;
 
180
10 # HasNative(f_IsFunction) <-- TypeIsNative(Type(f));
 
181
20 # HasNative(_other) <-- False;
 
182
10 # Compile(f_HasNative) <--
 
183
[
 
184
  // place holder for result
 
185
  AddAsm(PUSH()); 
 
186
 
 
187
  // Push arguments
 
188
  Local(args);
 
189
  Set(args,Tail(Listify(f)));
 
190
  ForEach(item,args)Compile(item);
 
191
 
 
192
  // Call function
 
193
  AddAsm(NATIVE(Type(f):"_":String(Length(args))));
 
194
];
 
195
 
 
196
 
 
197
10 # Compile(_f)_(Type(f) = "Prog") <--
 
198
[
 
199
  AddAsm(PUSH());
 
200
  AddAsm(PROLOG());
 
201
//  Write(Subst(v,f)Hold(v));
 
202
  Set(f,Tail(Listify(Subst(v,f)Hold(v))));
 
203
//  Write(Subst(v,f)Hold(v));
 
204
 
 
205
  While(f != {})
 
206
  [
 
207
    Compile(Head(f));
 
208
    Set(f,Tail(f));
 
209
    If(f!={},AddAsm(POP()));
 
210
  ];
 
211
  AddAsm(COPY(TOP(-1),TOP(0)));
 
212
  AddAsm(POP());
 
213
  AddAsm(EPILOG());
 
214
//  AddAsm(SETTRUE(TOP(0)));
 
215
];
 
216
 
 
217
 
 
218
100 # Compile(_line) <--
 
219
[
 
220
/*
 
221
  AddAsm(PUSH());
 
222
  AddAsm(PUSH());
 
223
  AddAsm(SET(TOP(0),CompileExpression(line)));
 
224
  AddAsm(EVALUATE(TOP(-1),TOP(0)));
 
225
  AddAsm(POP());
 
226
*/
 
227
  AddAsm(PUSH());
 
228
  AddAsm(SET(TOP(0),CompileExpression(line)));
 
229
  AddAsm(EVALUATE(TOP(0),TOP(0)));
 
230
];
 
231
 
 
232
 
 
233
10 # CompileBaseLine(f_IsFunction := _body)_(Type(f)!="Nth") <--
 
234
[
 
235
  AddAsm(PUSH());
 
236
 
 
237
//  Echo({"Function definition!"});
 
238
  Local(name,args,bd,fn);
 
239
  fn:=Listify(f);
 
240
  name:=fn[1];
 
241
  args:=Tail(fn);
 
242
  bd:={};
 
243
  PushSection(bd);
 
244
//  AddAsm(PROLOG());
 
245
 
 
246
 
 
247
  // Add the arguments
 
248
  Local(i);
 
249
  Set(i,0);
 
250
  ForEach(var,args)
 
251
  [
 
252
    AddAsm(EVALUATE(TOP(i),TOP(i)));
 
253
    i--;
 
254
  ];
 
255
  ForEach(var,args)
 
256
  [
 
257
    AddAsm(LOCAL(String(var)));
 
258
  ];
 
259
  Set(i,-(Length(args)-1));
 
260
  ForEach(var,args)
 
261
  [
 
262
    AddAsm(SETVARSTR(String(var),TOP(i)));
 
263
    i++;
 
264
  ];
 
265
  ForEach(var,args)
 
266
  [
 
267
    AddAsm(POP());
 
268
  ];
 
269
 
 
270
 
 
271
/*
 
272
  // Add the arguments
 
273
  Local(i);
 
274
  Set(i,0);
 
275
  ForEach(var,args)
 
276
  [
 
277
    i++;
 
278
    AddAsm(PUSH());
 
279
 
 
280
    AddAsm(SET(TOP(0),"Argument(aArguments,":String(i):")"));
 
281
    AddAsm(EVALUATE(TOP(0),TOP(0)));
 
282
    //Compile(var);
 
283
 
 
284
    AddAsm(LOCAL(String(var)));
 
285
 
 
286
    AddAsm(SETVARSTR(String(var),TOP(0)));
 
287
    AddAsm(POP());
 
288
  ];
 
289
*/
 
290
  // Add the body
 
291
  Compile(body);
 
292
  AddAsm(COPY(TOP(-1),TOP(0)));
 
293
//  AddAsm(COPY(aResult,TOP(0)));
 
294
  AddAsm(POP());
 
295
 
 
296
//  AddAsm(EPILOG());
 
297
  PopSection();
 
298
  DeclareSimpleFunction(name,args,bd);
 
299
];
 
300
 
 
301
100 # CompileBaseLine(_line) <--
 
302
[
 
303
  PushSection(initSection);
 
304
  Compile(line);
 
305
  AddAsm(POP());
 
306
  PopSection();
 
307
];
 
308
 
 
309
/*
 
310
10 # Compile(If(_a,_b)) <--
 
311
[
 
312
  Echo({"if (IsTrue()) Evaluate();"});
 
313
 
 
314
];
 
315
*/
 
316
 
 
317
 
 
318
 
 
319
 
 
320
//CompileExpression(_expr) <-- (CompileLine(expr)[1]);
 
321
CompileExpression(_expr) <-- (CompileLine(expr));
 
322
 
 
323
RuleBase("CompileLineHeld",{arg});
 
324
HoldArg("CompileLineHeld",arg);
 
325
CompileLineHeld(_arg) <--
 
326
[
 
327
  CompileLine(arg);
 
328
];
 
329
 
 
330
10 # CompileLine(expr_IsFunction) <--
 
331
[
 
332
  Local(rs);
 
333
//Echo({"1...",rs});
 
334
  Set(rs,Listify(expr));
 
335
//Echo({"2...",rs});
 
336
  Set(rs,MapSingle("CompileLineHeld",rs));
 
337
//Echo({"2..."});
 
338
//  Set(rs,Flatten(rs,"List"));
 
339
 
 
340
  [
 
341
//    Echo({rs});
 
342
    Local(n,res);
 
343
    Set(res,"");
 
344
    Set(n,Length(rs)-1);
 
345
    While(n>0)
 
346
    [
 
347
      rs[n] := "AtomAdd(":rs[n]:",":rs[n+1]:")";
 
348
      n--;
 
349
    ];
 
350
    Set(rs,rs[1]);
 
351
//    Echo({rs});
 
352
  ];
 
353
//  Set(rs,Apply("ConcatStrings",rs));
 
354
//  {"MAKELIST(":rs:"LA(NULL))","+"};
 
355
  "MAKELIST(":rs:")";
 
356
];
 
357
 
 
358
20 # CompileLine(expr_IsAtom) <--
 
359
[
 
360
//  {"MAKEATOM(\"":CEscape(String(expr)):"\")","+"};
 
361
  "MAKEATOM(\"":CEscape(String(expr)):"\")";
 
362
];
 
363
 
 
364
30 # CompileLine(_expr) <-- Check(False,"Expression not handled");
 
365
 
 
366
 
 
367
 
 
368
 
 
369
Optimize(list_IsList) <-- ClosePushPops(list);
 
370
 
 
371
ClosePeep(POP(), PUSH()) <--
 
372
[
 
373
  DestructiveDelete(list,i);
 
374
  DestructiveDelete(list,i);
 
375
  Set(n,n-2);
 
376
  i--;
 
377
  i--;
 
378
  If(i<1,i:=1);
 
379
];
 
380
ClosePeep(SETTRUE(TOP(0)),POP()) <--
 
381
[
 
382
  DestructiveDelete(list,i);
 
383
  Set(n,n-1);
 
384
  i--;
 
385
 
 
386
];
 
387
ClosePeep(SETTRUE(TOP(0)),SET(TOP(0),_a)) <--
 
388
[
 
389
  DestructiveDelete(list,i);
 
390
  Set(n,n-1);
 
391
  i--;
 
392
];
 
393
 
 
394
ClosePeep(EVALUATE(TOP(0),TOP(0)),COPY(_a,TOP(0))) <--
 
395
[
 
396
  list[i] := EVALUATE(a,TOP(0));
 
397
  DestructiveDelete(list,i+1);
 
398
  Set(n,n-1);
 
399
  i--;
 
400
];
 
401
 
 
402
 
 
403
UnFence("ClosePeep",2);
 
404
ClosePushPops(_list) <--
 
405
[
 
406
//Echo({"ENTER"});
 
407
  Local(i,n);
 
408
  Set(n,Length(list));
 
409
//Echo({"lsit = ",list});
 
410
//Echo({"n = ",n});
 
411
  For(i:=1,i<n,i++)
 
412
  [
 
413
    ClosePeep(list[i],list[i+1]);
 
414
/*
 
415
//Echo({i,list[i]});
 
416
    If(list[i] = POP() And list[i+1] = PUSH(),
 
417
    [
 
418
//      Echo({"deleting at ",list[i]});
 
419
//      Echo({"deleting at ",list[i+1]});
 
420
      DestructiveDelete(list,i);
 
421
      DestructiveDelete(list,i);
 
422
      Set(n,n-2);
 
423
      i--;
 
424
      i--;
 
425
      If(i<1,i:=1);
 
426
    ]);
 
427
*/    
 
428
  ];
 
429
//Echo({list});
 
430
  list;
 
431
];
 
432
 
 
433
 
 
434