-
Notifications
You must be signed in to change notification settings - Fork 6
/
system.pir
696 lines (492 loc) · 12.5 KB
/
system.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# $Id$
=head1 NAME
system.pir - implementation specific package SYSTEM
=head1 DESCRIPTION
Used in bootstrapping.
=cut
.sub _init_system :init
.local pmc package
.PACKAGE(package, "SYSTEM")
set_global ["PACKAGES"], "SYSTEM", package
set_global ["PACKAGES"], "SYS", package
_init_reader_macros( package )
.local pmc symbol, nil
.NIL(nil)
.DEFVAR(symbol, package, "*INSIDE-BACKQUOTE*", nil) # not used yet
.DEFVAR(symbol, package, "*INSIDE-BACKQUOTE-LIST*", nil) # not used yet
.DEFUN(symbol, package, "%GET-OBJECT-ATTRIBUTE", "_get_object_attr")
.DEFUN(symbol, package, "%SET-OBJECT-ATTRIBUTE", "_set_object_attr")
.DEFUN(symbol, package, "%MAKE-HASH-TABLE", "_make_hash_table")
.DEFUN(symbol, package, "%SET-HASH", "_set_hash")
.DEFUN(symbol, package, "%GET-HASH", "_get_hash")
.DEFUN(symbol, package, "%ALIAS-PACKAGE", "_alias_package")
.DEFUN(symbol, package, "%FIND-PACKAGE", "_find_package")
.DEFUN(symbol, package, "%PACKAGE-NAME", "_package_name")
.DEFUN(symbol, package, "%MAKE-PACKAGE", "_make_package")
.DEFUN(symbol, package, "%USE-PACKAGE", "_use_package")
.DEFUN(symbol, package, "%EXPORT", "_export")
.DEFUN(symbol, package, "%OPEN-FILE", "_open_file")
.DEFUN(symbol, package, "%PEEK", "_peek")
.DEFUN(symbol, package, "%CLOSE", "_close")
.DEFUN(symbol, package, "%STRING-EQUAL", "_string_equal")
.DEFUN(symbol, package, "%MAKE-MACRO", "_make_macro")
# XXX - THESE SHOULD BE REMOVED AND CONVERTED TO PROPER LISP FUNCTIONS
.DEFUN(symbol, package, "ERROR", "_raise_error")
.DEFUN(symbol, package, "LOAD", "_load")
.return(1)
.end
.sub _init_reader_macros
.param pmc package
.local pmc function, reader_macros
.HASH(reader_macros)
.FUNCTION(function, "_left_paren_macro" )
reader_macros["("] = function
.FUNCTION(function, "_right_paren_macro" )
reader_macros[")"] = function
.FUNCTION(function, "_single_quote_macro" )
reader_macros["'"] = function
.FUNCTION(function, "_semicolon_macro" )
reader_macros[";"] = function
.FUNCTION(function, "_double_quote_macro" )
reader_macros['"'] = function
.FUNCTION(function, "_backquote_macro" )
reader_macros["`"] = function
.FUNCTION(function, "_comma_macro" )
reader_macros[","] = function
.FUNCTION(function, "_sharpsign_macro" )
reader_macros["#"] = function
.local pmc symbol
.DEFVAR(symbol, package, "*READER-MACROS*", reader_macros)
.return(1)
.end
.sub _set_hash
.param pmc args
.ASSERT_LENGTH(args,3,ERROR_NARGS)
.local pmc hash
.CAR(hash,args)
.ASSERT_TYPE(hash, "hash")
.local pmc key
.SECOND(key,args)
.ASSERT_TYPE(key, "string")
.local pmc val
.THIRD(val,args)
.local string key_str
key_str = key
hash[key_str] = val
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %SET-HASH")
goto DONE
DONE:
.return(val)
.end
.sub _get_hash
.param pmc args
.ASSERT_LENGTH(args,2,ERROR_NARGS)
.local pmc hash
.CAR(hash,args)
.ASSERT_TYPE(hash, "hash")
.local pmc key
.SECOND(key,args)
.ASSERT_TYPE(key, "string")
.local string key_str
key_str = key # Convert the key to a string
.local pmc val
val = hash[key_str]
if_null val, NO_VALUE_SET
goto DONE
NO_VALUE_SET:
.NIL(val)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %GET-HASH")
goto DONE
DONE:
.return(val)
.end
.sub _package_name
.param pmc args
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.local pmc pkg
.CAR(pkg, args)
.ASSERT_TYPE(pkg, "package")
.local pmc pkgname
pkgname = pkg.'_get_name'()
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to SYS:%PACKAGE-NAME")
goto DONE
DONE:
.return(pkgname)
.end
.sub _find_package
.param pmc args
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.local pmc pkgname
.CAR(pkgname, args)
.ASSERT_TYPE(pkgname, "string")
.local string pkgname_str
pkgname_str = pkgname
upcase pkgname_str
push_eh PACKAGE_NOT_FOUND
.local pmc retv
retv = get_global ["PACKAGES"], pkgname_str
if_null retv, PACKAGE_NOT_FOUND
pop_eh
goto DONE
PACKAGE_NOT_FOUND:
.NIL(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %FIND-PACKAGE")
goto DONE
DONE:
.return(retv)
.end
.sub _alias_package
.param pmc args
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
.local pmc package
.CAR(package, args)
.ASSERT_TYPE(package, "package")
.local pmc pkgname
.SECOND(pkgname, args)
.ASSERT_TYPE(pkgname, "string")
.local string pkgname_str
pkgname_str = pkgname
upcase pkgname_str
set_global ["PACKAGES"], pkgname_str, package
.local pmc retv
.TRUE(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %ALIAS-PACKAGE")
goto DONE
DONE:
.return(retv)
.end
.sub _make_package
.param pmc args
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.local pmc pkgname
.CAR(pkgname, args)
.ASSERT_TYPE(pkgname, "string")
.local pmc package
.PACKAGE(package, pkgname)
.local string pkgname_str
pkgname_str = pkgname
upcase pkgname_str
set_global ["PACKAGES"], pkgname_str, package
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %MAKE-PACKAGE")
goto DONE
DONE:
.return(package)
.end
.sub _use_package
.param pmc args
.local string symnames
.local pmc frompkg
.local pmc intopkg
.local pmc exports
.local pmc symname
.local pmc symbol
.local pmc retv
.local pmc i
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
.CAR(intopkg, args)
.SECOND(frompkg, args)
.ASSERT_TYPE(intopkg, "package")
.ASSERT_TYPE(frompkg, "package")
exports = frompkg.'_get_exports'()
iter i, exports
push_eh DONE
LOOP:
shift symname, i
symnames = symname
symbol = frompkg.'_lookup_symbol'(symnames)
intopkg.'_import_symbol'(symbol)
goto LOOP
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %USE-PACKAGE")
goto DONE
DONE:
.TRUE(retv)
.return(retv)
.end
.sub _export
.param pmc args
.local string symname
.local pmc package
.local pmc symbols
.local pmc symbol
.local pmc retv
.ASSERT_MINIMUM_LENGTH(args, 1, ERROR_NARGS)
.CAR(package, args)
.ASSERT_TYPE(package, "package")
.CDR(symbols, args)
# TODO: looks like find-package is called twice, problem in eval.pir ?
.CDR(symbols, symbols)
LOOP:
.NULL(symbols, DONE)
.CAR(symbol, symbols)
.ASSERT_TYPE(symbol, "string")
symname = symbol
package.'_export_symbol'(symname)
.CDR(symbols, symbols)
goto LOOP
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %EXPORT")
goto DONE
DONE:
.TRUE(retv)
.return(retv)
.end
.sub _make_hash_table
.param pmc args
.ASSERT_LENGTH(args,0,ERROR_NARGS)
.local pmc retv
.HASH(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %MAKE-HASH-TABLE")
goto DONE
DONE:
.return(retv)
.end
.sub _raise_error
.param pmc args
.local string types
.local string mesgs
.local pmc type
.local pmc mesg
.local pmc retv
.ASSERT_LENGTH(args,2,ERROR_NARGS)
.CAR(type,args)
.SECOND(mesg,args)
.NIL(retv)
types = type
mesgs = mesg
.ERROR_0(types, mesgs)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %ERROR")
goto DONE
DONE:
.return(retv)
.end
.sub _load
.param pmc args
.local string fname1
.local pmc stream
.local pmc fname2
.local pmc farg
.local pmc rretv
.local pmc eretv
.local pmc retv
.local pmc fd
.ASSERT_LENGTH(args, 1,ERROR_NARGS)
.CAR(fname2,args)
fname1 = fname2
open fd, fname1, "r"
unless fd, OPEN_FAILED
.STREAM(stream, fd)
.TRUE(retv)
LOAD_LOOP:
.LIST_1(farg,stream)
rretv = _read(farg)
.NULL(rretv, CLEANUP)
.LIST_1(farg,rretv)
eretv = _eval(farg)
goto LOAD_LOOP
OPEN_FAILED:
.NIL(retv)
goto DONE
CLEANUP:
close fd
.TRUE(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %LOAD")
goto DONE
DONE:
.return(retv)
.end
.sub _get_object_attr
.param pmc args
.ASSERT_LENGTH(args,3,ERROR_NARGS)
.local pmc symbol
.CAR(symbol,args)
.local pmc obj_type
.SECOND(obj_type,args)
.ASSERT_TYPE(obj_type, "string")
# TODO: check type of symbol
.local pmc attr_name
.THIRD(attr_name,args)
.ASSERT_TYPE(attr_name, "string")
.local string attr_name_str
attr_name_str = attr_name
.local pmc retv
retv = getattribute symbol, attr_name_str
if_null retv, NO_VALUE
goto DONE
NO_VALUE:
.NIL(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error","wrong number of arguments to %GET-OBJECT-ATTRIBUTE")
goto DONE
DONE:
.return(retv)
.end
.sub _set_object_attr
.param pmc args
.ASSERT_LENGTH(args,4,ERROR_NARGS)
.local pmc symbol
.CAR(symbol,args)
.local pmc obj_type
.SECOND(obj_type,args)
.ASSERT_TYPE(obj_type, "string")
# TODO: check type of symbol
.local pmc attr_name
.THIRD(attr_name,args)
.ASSERT_TYPE(attr_name, "string")
.local string attr_name_str
attr_name_str = attr_name
.local pmc value
.FOURTH(value,args)
setattribute symbol, attr_name_str, value
goto DONE
ERROR_NARGS:
.ERROR_0("program-error","wrong number of arguments to %SET-SYMBOL-ATTRIBUTE")
goto DONE
DONE:
.return(value)
.end
.sub _open_file
.param pmc args
.local string modes
.local string names
.local pmc stream
.local pmc name
.local pmc mode
.local pmc retv
.local int test
.ASSERT_LENGTH(args,2,ERROR_NARGS)
.CAR(name, args)
.SECOND(mode, args)
.ASSERT_TYPE(name, "string")
.ASSERT_TYPE(mode, "string")
names = name
modes = mode
open stream, names, modes
defined test, stream
if test != 1 goto FILE_NOT_FOUND
.STREAM(retv, stream)
goto DONE
FILE_NOT_FOUND:
.ERROR_1("file-error", "error opening file %s", name)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %OPEN-FILE")
goto DONE
DONE:
.return(retv)
.end
.sub _peek
.param pmc args
.local string char
.local pmc stream
.local pmc retv
.local pmc io
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(stream, args)
.ASSERT_TYPE(stream, "stream")
io = stream.'_get_io'()
peek char, io
if char == "" goto ERROR_EOF
.STRING(retv, char)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %PEEK")
goto DONE
ERROR_EOF:
.ERROR_0("end-of-file", "EOF on input stream reached.")
goto DONE
DONE:
.return(retv)
.end
.sub _close
.param pmc args
.local pmc stream
.local pmc retv
.local pmc io
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(stream, args)
.ASSERT_TYPE(stream, "stream")
io = stream.'_get_io'()
close io
.TRUE(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %CLOSE")
goto DONE
DONE:
.return(retv)
.end
.sub _string_equal
.param pmc args
.local string val1
.local string val2
.local pmc str1
.local pmc str2
.local pmc retv
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
.CAR(str1, args)
.SECOND(str2, args)
.ASSERT_TYPE(str1, "string")
.ASSERT_TYPE(str2, "string")
val1 = str1
val2 = str2
if val1 == val2 goto STRING_EQUAL
.NIL(retv)
goto DONE
STRING_EQUAL:
.TRUE(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %STRING-EQUAL")
goto DONE
DONE:
.return(retv)
.end
.sub _make_macro
.param pmc args
.local int type
.local pmc macro
.local pmc val
.local pmc form
.local pmc retv
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(form, args)
# XXX - This is pretty hackish - should probably use the __morph method
macro = new "LispMacro"
val = form.'_get_args'()
macro.'_set_args'(val)
val = form.'_get_scope'()
macro.'_set_scope'(val)
val = form.'_get_body'()
macro.'_set_body'(val)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to %MAKE-MACRO")
goto DONE
DONE:
.return(macro)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: