-
Notifications
You must be signed in to change notification settings - Fork 6
/
internals.pir
555 lines (397 loc) · 10.9 KB
/
internals.pir
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# $Id$
=head1 NAME
internals.pir - lexical and global variables, function call
=head1 Functions
=head2 _LOOKUP_GLOBAL(pkgname, symname)
=cut
.sub _LOOKUP_GLOBAL
.param string pkgname
.param string symname
.local pmc package
.local pmc retv
upcase pkgname, pkgname # Convert names to all upcase
upcase symname, symname
push_eh PACKAGE_NOT_FOUND # Set an error handler
get_global package, ["PACKAGES"], pkgname # Look for the package
pop_eh
retv = package.'_lookup_symbol'(symname) # Lookup the symbol
goto DONE
PACKAGE_NOT_FOUND:
.ERROR_1("package-error", "there is no package with name \"%s\"", pkgname)
goto DONE
DONE:
.return(retv)
.end
=head2 _LOOKUP_LEXICAL(symname)
=cut
.sub _LOOKUP_LEXICAL
.param string symname
push_eh LEXICAL_NOT_FOUND # Set an error handler
.local pmc retv
find_lex retv, symname # Look for the lexical symbol
pop_eh
goto DONE
LEXICAL_NOT_FOUND: # Return null if not found
null retv
goto DONE
DONE:
.return(retv)
.end
=head2 _LOOKUP_SYMBOL(symname)
=cut
.sub _LOOKUP_SYMBOL
.param string symname
.local string pkgname
.local pmc package
.local pmc symbol
.local pmc retv
LEXICAL_SYMBOL:
symbol = _LOOKUP_LEXICAL(symname)
if_null retv, GLOBAL_SYMBOL
goto DONE
GLOBAL_SYMBOL:
symbol = _LOOKUP_GLOBAL("COMMON-LISP", "*PACKAGE*")
if_null symbol, PACKAGE_NOT_FOUND
package = symbol.'_get_value'()
if_null package, PACKAGE_NOT_FOUND
pkgname = package.'_get_name_as_string'()
retv = _LOOKUP_GLOBAL(pkgname, symname)
goto DONE
PACKAGE_NOT_FOUND:
.ERROR_0("internal", "current package not found")
goto DONE
DONE:
.return(retv)
.end
=head2 _INTERN_GLOBAL(symbol, pkgname)
=cut
.sub _INTERN_GLOBAL
.param pmc symbol
.param string pkgname
.local string symname
symname = symbol.'_get_name_as_string'()
set_global pkgname, symname, symbol
.end
=head2 .INTERN_LEXICAL(symbol)
=cut
.sub _INTERN_LEXICAL
.param pmc symbol
.local string symname
symname = symbol.'_get_name_as_string'()
# VALID_IN_PARROT_0_2_0 store_lex -1, symname, symbol
store_lex symname, symbol
.end
=head2 _LEXICAL_SYMBOL
=cut
.sub _LEXICAL_SYMBOL
.param string symname
.param pmc value
.local pmc package
.local pmc symbol
.local int test
symbol = _LOOKUP_GLOBAL("COMMON-LISP", "*PACKAGE*")
package = symbol.'_get_value'()
symbol = _SYMBOL(symname) # Create a new symbol
symbol.'_set_package'(package) # Set the home package
defined test, value # Set a value if provided
if test == 0 goto DONE
symbol.'_set_value'(value)
goto DONE
DONE:
# VALID_IN_PARROT_0_2_0 store_lex -1, symname, symbol
store_lex symname, symbol
.return(symbol)
.end
=head2 _SYMBOL
=cut
.sub _SYMBOL
.param string symname
.local pmc symbol
.local pmc name
symbol = new "LispSymbol"
name = new "LispString"
name = symname
symbol.'_set_name'(name)
.return(symbol)
.end
=head2 _GLOBAL_SYMBOL
=cut
.sub _GLOBAL_SYMBOL
.param string pkgname
.param string symname
.param pmc value
.param pmc function
.local pmc packages
.local pmc package
.local pmc symbol
.local int test
upcase pkgname, pkgname
upcase symname, symname
push_eh PACKAGE_NOT_CREATED
get_global [package], "PACKAGES", pkgname
pop_eh
symbol = package.'_intern_symbol'(symname)
symbol.'_set_package'(package) # Set the home package
defined test, value # Set a value if provided
if test == 0 goto FUNCTION
symbol.'_set_value'(value)
goto FUNCTION
FUNCTION: # Set a function if provided
defined test, function
if test == 0 goto DONE
function.'_set_name'(symname)
symbol.'_set_function'(function)
goto DONE
PACKAGE_NOT_CREATED:
.ERROR_1("package-error", "there is no package with name \"%s\"", pkgname)
goto DONE
DONE:
.return(symbol)
.end
=head2 _FUNCTION_CALL
Call a function.
=cut
.sub _FUNCTION_CALL
.param pmc function
.param pmc args
.local pmc proto
proto = function.'_get_args'()
.local pmc body
body = function.'_get_body'()
.local string type
type = typeof function # Get the function type
# print function
# print " of type "
# print type
type = typeof body # Get the function type
# print " with body "
# print body
# print " with bodytype "
# print type
# print " in _FUNCTION_CALL\n"
type = typeof body # Get the function type
# print type
# print " is the type\n"
if type != 'Sub' goto NOT_A_COMPILED_FUNCTION
.tailcall body( args )
NOT_A_COMPILED_FUNCTION:
if type != 'LispCons' goto NOT_A_LISP_CONS
.local pmc scope
scope = function.'_get_scope'()
# 1st arg - the code to evaluate
# 2nd arg - the arg prototype
# 3rd arg - the args to evaluate
# The closure
# set_args "0,0,0", body, proto, args
.tailcall scope( body, proto, args )
# VALID_IN_PARROT_0_2_0 pushtopp # Save the upper registers
# VALID_IN_PARROT_0_2_0 invokecc # Call the closure
# VALID_IN_PARROT_0_2_0 poptopp # Restore the upper registers
# VALID_IN_PARROT_0_2_0 returncc
NOT_A_LISP_CONS:
.return ()
DONE:
.return()
.end
.sub _IS_SPECIAL
.param pmc symbol
.local int retv
retv = 1
.local pmc special
special = getattribute symbol, "special"
if_null special, NOT_SPECIAL
goto DONE
NOT_SPECIAL:
retv = 0
goto DONE
DONE:
.return(retv)
.end
.sub _IS_ORDINARY_LAMBDA_LIST
.param pmc form
.local string type
.local pmc symbol
.local pmc args
.local int test
.local int retv
.CAR(symbol,form) # Ensure first element is a LAMBDA
if symbol != "LAMBDA" goto NON_LAMBDA_LIST
.SECOND(args,form) # Ensure second element is a lambda-list
.ASSERT_TYPE_AND_BRANCH(args, "list", MISSING_LAMBDA_LIST)
goto LAMBDA_LIST
LAMBDA_LIST:
retv = 1
goto DONE
NON_LAMBDA_LIST:
retv = 0
goto DONE
MISSING_LAMBDA_LIST:
_error("invalid-function", "The lambda-list for LAMBDA is missing")
goto DONE
DONE:
.return(retv)
.end
.sub _MAKE_LAMBDA
.param pmc form
# .FIRST is 'lambda'
# check the parameter declaration
.local pmc args
.SECOND(args, form)
.local pmc lptr
lptr = args
.local pmc symbol
ARG_LOOP_BEGIN:
.NULL(lptr, ARG_LOOP_END)
.CAR(symbol, lptr) # Ensure all the arguments are
.ASSERT_TYPE(symbol, "symbol") # symbol types.
.CDR(lptr, lptr)
goto ARG_LOOP_BEGIN
ARG_LOOP_END:
.local pmc body
.THIRD(body, form)
.const 'Sub' sub_that_calls_eval = 'sub_that_calls_eval'
.local pmc closure
closure = newclosure sub_that_calls_eval # Capture the scope the closure
.local pmc lisp_function
lisp_function = new "LispFunction"
lisp_function.'_set_args'(args)
lisp_function.'_set_body'(body)
lisp_function.'_set_scope'(closure)
.return(lisp_function)
.end
.sub sub_that_calls_eval :outer('_MAKE_LAMBDA') # TODO: what is really :outer ???
.param pmc clbody
.param pmc clprot
.param pmc clargs
# print "sub_that_calls_eval\n body: "
# print clbody
# print "\nproto: "
# print clprot
# print "\nargs: "
# print clargs
.local string clsymname
.local pmc clargsptr
.local pmc clprotptr
.local pmc clbody
.local pmc clprot
.local pmc clargs
.local pmc clarg
.local pmc clval
.local pmc clsym
clargsptr = clargs
clprotptr = clprot
# VALID_IN_PARROT_0_2_0 new_pad -1
goto CLOSURE_ARGS
CLOSURE_ARGS:
.NULL(clprotptr, CLOSURE_CHECK_ARGS)
.NULL(clargsptr, CLOSURE_TOO_FEW_ARGS)
.CAR(clval, clargsptr) # The lexical value
.CAR(clarg, clprotptr) # The lexical arg prototype
clsymname = clarg.'_get_name_as_string'()
clsym = _LEXICAL_SYMBOL(clsymname, clval) # Create a new lexical symbol
.CDR(clargsptr, clargsptr)
.CDR(clprotptr, clprotptr)
goto CLOSURE_ARGS
CLOSURE_CHECK_ARGS:
.NULL(clargsptr, CLOSURE_BODY) # Ensure we didn't have too
goto CLOSURE_TOO_MANY_ARGS # many args
CLOSURE_BODY:
.local pmc clearg
.local pmc clretv
.LIST_1(clearg, clbody)
# VALID_IN_PARROT_0_2_0 pop_pad
.tailcall _eval(clearg)
CLOSURE_TOO_FEW_ARGS:
# VALID_IN_PARROT_0_2_0 pop_pad
.ERROR_0("program-error", "Too few arguments given to LAMBDA")
goto CLOSURE_DONE
CLOSURE_TOO_MANY_ARGS:
# VALID_IN_PARROT_0_2_0 pop_pad
.ERROR_0("program-error", "Too many arguments given to LAMBDA")
goto CLOSURE_DONE
CLOSURE_DONE:
.return()
.end
.sub _LIST_LENGTH
.param pmc args
.local pmc lptr
lptr = args
.local int alen
alen = 0
.local pmc _nilp
.NIL(_nilp)
LOOP:
eq_addr lptr, _nilp, DONE
inc alen
.CDR(lptr, lptr)
goto LOOP
DONE:
.return(alen)
.end
.sub _IS_TYPE
.param pmc args
.param string rtype
.local string atype
.local int retv
atype = typeof args
retv = 1
if rtype == "cons" goto CONS_TYPE
if rtype == "hash" goto HASH_TYPE
if rtype == "integer" goto INTEGER_TYPE
if rtype == "float" goto FLOAT_TYPE
if rtype == "function" goto FUNCTION_TYPE
if rtype == "list" goto LIST_TYPE
if rtype == "number" goto NUMBER_TYPE
if rtype == "package" goto PACKAGE_TYPE
if rtype == "stream" goto STREAM_TYPE
if rtype == "string" goto STRING_TYPE
if rtype == "symbol" goto SYMBOL_TYPE
goto WRONG_TYPE
CONS_TYPE:
if atype != "LispCons" goto WRONG_TYPE
goto DONE
HASH_TYPE:
if atype != "LispHash" goto WRONG_TYPE
goto DONE
INTEGER_TYPE:
if atype != "LispInteger" goto WRONG_TYPE
goto DONE
FLOAT_TYPE:
if atype != "LispFloat" goto WRONG_TYPE
goto DONE
FUNCTION_TYPE:
if atype != "LispFunction" goto WRONG_TYPE
goto DONE
LIST_TYPE:
if atype != "LispSymbol" goto NONEMPTY_LIST
.NULL(args, DONE)
NONEMPTY_LIST:
if atype != "LispCons" goto WRONG_TYPE
goto DONE
NUMBER_TYPE:
if atype == "LispInteger" goto DONE
if atype != "LispFloat" goto WRONG_TYPE
goto DONE
PACKAGE_TYPE:
if atype != "LispPackage" goto WRONG_TYPE
goto DONE
STREAM_TYPE:
if atype != "LispStream" goto WRONG_TYPE
goto DONE
STRING_TYPE:
if atype != "LispString" goto WRONG_TYPE
goto DONE
SYMBOL_TYPE:
if atype != "LispSymbol" goto WRONG_TYPE
goto DONE
WRONG_TYPE:
retv = 0
goto DONE
DONE:
.return(retv)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: