-
Notifications
You must be signed in to change notification settings - Fork 6
/
cl.pir
1334 lines (957 loc) · 28.7 KB
/
cl.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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# $Id$
=head1 NAME
cl.pir - Set up the package 'COMMON-LISP'
=cut
.sub _init_cl :init
.local pmc symbol
.local pmc value
.local pmc package
.PACKAGE(package, "COMMON-LISP")
set_global ["PACKAGES"], "COMMON-LISP", package
set_global ["PACKAGES"], "CL", package
.local pmc t
t = package.'_intern_symbol'("T") # Create the T symbol, T meaning true
t.'_set_value'(t)
t.'_set_package'(package)
t.'_set_special'(t)
set_global ["SYMBOLS"], "T", t # Quick alias to T
.local pmc nil
nil = package.'_intern_symbol'("NIL") # Create the NIL symbol
nil.'_set_value'(nil)
nil.'_set_package'(package)
nil.'_set_special'(t)
set_global ["SYMBOLS"], "NIL", nil # Quick alias to NIL
.INTEGER(value,1)
.DEFVAR(symbol, package, "*GENSYM-COUNTER*", value)
.DEFVAR(symbol, package, "*PACKAGE*", package)
.READTABLE(value)
.DEFVAR(symbol, package, "*READTABLE*", value)
.local pmc stream
getstdin stream
.STREAM(value,stream)
.DEFVAR(symbol, package, "*STANDARD-INPUT*", value)
getstdout stream
stream.'buffer_type'('unbuffered')
.STREAM(value,stream)
.DEFVAR(symbol, package, "*STANDARD-OUTPUT*", value)
.local pmc function # this is needed in r20641
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "APPLY", _apply)
.DEFUN(symbol, package, "APPLY", "_apply")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "ATOM", _atom)
.DEFUN(symbol, package, "ATOM", "_atom")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "BOUNDP", _boundp)
.DEFUN(symbol, package, "BOUNDP", "_boundp")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "CAR", _car)
.DEFUN(symbol, package, "CAR", "_car")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "CDR", _cdr)
.DEFUN(symbol, package, "CDR", "_cdr")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "CHAR", _char)
.DEFUN(symbol, package, "CHAR", "_char")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "CONS", _cons)
.DEFUN(symbol, package, "CONS", "_cons")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "EQ", _eq)
.DEFUN(symbol, package, "EQ", "_eq")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "EVAL", _eval)
.DEFUN(symbol, package, "EVAL", "_eval")
.SPECIAL_FORM(symbol, package, "FUNCTION", '_function')
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "GENSYM", _gensym)
.DEFUN(symbol, package, "GENSYM", "_gensym")
.SPECIAL_FORM(symbol, package, "IF", '_if')
.SPECIAL_FORM(symbol, package, "LET", '_let')
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "LIST", _list)
.DEFUN(symbol, package, "LIST", "_list")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "MOD", _modulus)
.DEFUN(symbol, package, "MOD", "_modulus")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "NULL", _null)
.DEFUN(symbol, package, "NULL", "_null")
.DEFUN(symbol, package, "PRINT", "_print")
.SPECIAL_FORM(symbol, package, "PROGN", '_progn')
.SPECIAL_FORM(symbol, package, "QUOTE", '_quote')
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "READ", _read)
.DEFUN(symbol, package, "READ", "_read")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "READ-DELIMITED-LIST",_read_delimited_list)
.DEFUN(symbol, package, "READ-DELIMITED-LIST","_read_delimited_list")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "RPLACA", _rplaca)
.DEFUN(symbol, package, "RPLACA", "_rplaca")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "RPLACD", _rplacd)
.DEFUN(symbol, package, "RPLACD", "_rplacd")
.SPECIAL_FORM(symbol, package, "SETQ", '_setq')
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "TYPE-OF", _type_of)
.DEFUN(symbol, package, "TYPE-OF", "_type_of")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "VALUES", _values)
.DEFUN(symbol, package, "VALUES", "_values")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "QUIT", _quit)
.DEFUN(symbol, package, "QUIT", "_quit")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "/", _divide)
.DEFUN(symbol, package, "/", "_divide")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "-", _subtract)
.DEFUN(symbol, package, "-", "_subtract")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "*", _multiply)
.DEFUN(symbol, package, "*", "_multiply")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "+", _add)
.DEFUN(symbol, package, "+", "_add")
# VALID_IN_PARROT_0_2_0 .DEFUN(symbol, package, "=", _equal)
.DEFUN(symbol, package, "=", "_equal")
.return(1)
.end
.sub _apply
.param pmc args
.ASSERT_MINIMUM_LENGTH(args, 2, ERROR_NARGS)
.local pmc car
.CAR(car, args)
.local pmc args_of_func
.SECOND(args_of_func, args)
.ASSERT_TYPE(args_of_func, "list")
.local string type
type = typeof car
if type == "LispFunction" goto CAR_IS_FUNCTION
if type == "LispSymbol" goto CAR_IS_SYMBOL
goto INVALID_FUNCTION_NAME
CAR_IS_FUNCTION:
.tailcall _FUNCTION_CALL(car, args_of_func)
CAR_IS_SYMBOL:
.local pmc func
func = car.'_get_function'() # Get the function from symbol
if_null func, INVALID_FUNCTION_NAME # Throw an error if undefined
type = typeof func
# print type
# print ' for CAR_IS_SYMBOL'
.tailcall _FUNCTION_CALL(func,args_of_func)
INVALID_FUNCTION_NAME:
.ERROR_1("undefined-function", "%s is not a function name", car)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to APPLY")
goto DONE
ERROR_NONLIST:
.ERROR_0("type-error", "second argument to APPLY must be a proper list")
goto DONE
DONE:
.return() # Call the return continuation
.end
.sub _atom
.param pmc args
.local string type
.local pmc retv
.local pmc a
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(a, args)
type = typeof a # An atom is anything that is
if type != "LispCons" goto ATOM # not a cons.
goto CONS
ATOM:
.TRUE(retv)
goto DONE
CONS:
.NIL(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to ATOM")
goto DONE
DONE:
.return(retv)
.end
.sub _boundp
.param pmc args
.local pmc symbol
.local pmc retv
.local pmc val
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(symbol, args)
.ASSERT_TYPE(symbol, "symbol")
val = symbol.'_get_value'()
if_null val, UNBOUND
.TRUE(retv)
goto DONE
UNBOUND:
.NIL(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to BOUNDP")
goto DONE
DONE:
.return(retv)
.end
.sub _car
.param pmc args
.local pmc retv
.local pmc a
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(a, args)
.ASSERT_TYPE(a, "list")
.CAR(retv, a)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to CAR")
goto DONE
DONE:
.return(retv)
.end
.sub _cdr
.param pmc args
.local pmc retv
.local pmc a
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(a, args)
.ASSERT_TYPE(a, "list")
.CDR(retv, a)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to CDR")
goto DONE
DONE:
.return(retv)
.end
.sub _char
.param pmc args
.local pmc retval
.local pmc ke
.local string str
.local string sstr
.local int k
.local int leng
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
str = args[0]
ke = args[1]
k = ke[0]
length leng, str
if k > leng goto BOUNDS
if k < 0 goto BOUNDS
sstr = substr str, k, 1
retval = new 'LispString'
retval = sstr
goto DONE
BOUNDS:
.NIL(retval)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to CHAR")
goto DONE
DONE:
.return(retval)
.end
.sub _cons
.param pmc args
.local pmc retv
.local pmc a
.local pmc b
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
.CAR(a, args)
.SECOND(b, args)
.CONS(retv, a, b)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to CONS")
goto DONE
DONE:
.return(retv)
.end
.sub _eq
.param pmc args
.local pmc retv
.local pmc a
.local pmc b
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
.CAR(a, args)
.SECOND(b, args)
eq_addr a, b, EQUAL
goto NOT_EQUAL
EQUAL:
.TRUE(retv)
goto DONE
NOT_EQUAL:
.NIL(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to EQ")
goto DONE
DONE:
.return(retv)
.end
.sub _function
.param pmc args
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.local pmc form
.CAR(form, args)
.local pmc retv
.local string type
type = typeof form
if type == "LispSymbol" goto SYMBOL # Retrieve function from symbol
.local int is_lambda_list
is_lambda_list = _IS_ORDINARY_LAMBDA_LIST(form) # Check if it's a lambda form
if is_lambda_list goto LAMBDA_FORM # and build a closure if so
goto INVALID_FUNCTION_NAME
SYMBOL:
.local string symname
symname = form.'_get_name_as_string'() # Retrieve the symbols name
.local pmc package
package = form.'_get_package'() # Retrieve the symbols package name
.local string pkgname
pkgname = package.'_get_name_as_string'()
.local pmc symbol
symbol = _LOOKUP_GLOBAL(pkgname, symname) # Lookup the symbol
.local int found
found = defined symbol # Ensure the symbol was found in
unless found goto FUNCTION_NOT_FOUND # the global namespace
retv = symbol.'_get_function'() # Ensure the symbol had a function
defined found, symbol # defined
unless found goto FUNCTION_NOT_FOUND
goto DONE
LAMBDA_FORM:
retv = _MAKE_LAMBDA(form) # Create a closure PMC
goto DONE
INVALID_FUNCTION_NAME:
.ERROR_1("undefined-function", "%s is not a function name", form)
goto DONE
FUNCTION_NOT_FOUND:
.ERROR_1("undefined-function", "the function %s is undefined", symname)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to FUNCTION")
goto DONE
DONE:
.return(retv)
.end
.sub _gensym
.param pmc args
.local string prefix
.local string gname
.local pmc suffix
.local pmc symbol
.local pmc garg
.local pmc gcnt
.local pmc retv
.local pmc car
.ASSERT_LENGTH_BETWEEN(args, 0, 1, ERROR_NARGS)
symbol = _LOOKUP_GLOBAL("COMMON-LISP", "*GENSYM-COUNTER*")
gcnt = symbol.'_get_value'()
suffix = gcnt
prefix = "G"
.NULL(args, MAKE_SYMBOL)
.CAR(car, args)
goto CHECK_PREFIX
CHECK_PREFIX:
.ASSERT_TYPE_AND_BRANCH(car, "string", CHECK_SUFFIX)
prefix = car
goto MAKE_SYMBOL
CHECK_SUFFIX:
.ASSERT_TYPE(car, "integer")
if car < 0 goto ERROR_NEGINT
suffix = car
goto MAKE_SYMBOL
MAKE_SYMBOL:
garg = new 'Array'
garg = 2
garg[0] = prefix
garg[1] = suffix
sprintf gname, "%s%0.6d", garg
retv = _SYMBOL(gname)
inc gcnt
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to GENSYM")
goto DONE
ERROR_NEGINT:
.ERROR_1("program-error", "%d is negative", car)
goto DONE
DONE:
.return(retv)
.end
.sub _if
.param pmc args
.local pmc retv
.local pmc form
.local pmc earg
.ASSERT_LENGTH_BETWEEN(args, 2, 3, ERROR_NARGS)
.CAR(form, args) # Get the test form
.LIST_1(earg,form)
retv = _eval(earg) # Evaluate the test form.
.NULL(retv, ELSE_CLAUSE) # If test was false, goto else clause
goto THEN_CLAUSE #else goto then clause
THEN_CLAUSE:
.SECOND(form, args)
.LIST_1(earg, form)
retv = _eval(earg)
goto DONE
ELSE_CLAUSE:
.THIRD(form, args)
.LIST_1(earg, form)
retv = _eval(earg)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to IF")
goto DONE
DONE:
.return(retv)
.end
.sub _list
.param pmc args
.local pmc lptr
.local pmc targ
.local pmc retv
.local pmc retp
.local pmc cons
.local pmc nil
.NIL(retv)
.NIL(nil)
lptr = args
LOOP:
.NULL(lptr, DONE)
.CAR(targ, lptr)
.NULL(retv, EMPTY_LIST)
.CONS(cons, targ, nil)
retp[1] = cons
retp = cons
EMPTY_LIST_RETURN:
.CDR(lptr, lptr)
goto LOOP
EMPTY_LIST:
.CONS(retv, targ, nil)
retp = retv
goto EMPTY_LIST_RETURN
DONE:
.return(retv)
.end
.sub _null
.param pmc args
.local pmc retv
.local pmc a
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(a, args)
.NULL(a, IS_NULL)
.NIL(retv)
goto DONE
IS_NULL:
.TRUE(retv)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to NULL")
goto DONE
DONE:
.return(retv)
.end
.sub _let
.param pmc args
.ASSERT_MINIMUM_LENGTH(args, 1, ERROR_NARGS)
.local string name
.local string type
.local pmc package
.local pmc symbol
.local pmc value
.local pmc fargs
.local pmc init
.local pmc body
.local pmc lptr
.local pmc form
.local int test
.local int i
# VALID_IN_PARROT_0_2_0 new_pad -1 # Create new lexical scope
.CAR(init, args) # The variable bindings
.CDR(body, args) # The form to evaluate
.local pmc keyvals
keyvals = new 'ResizablePMCArray' # List for holding init values
.local pmc dynvars
dynvars = new 'ResizablePMCArray' # List for holding dynamic vars
# for exception handling, currently broken
.local pmc error
null error
push_eh CLEANUP_HANDLER # Set a handler for cleanup
.local pmc retv
.NIL(retv) # Initialize return value
INIT_FORM: # Process the init form
type = typeof init
if type == "LispSymbol" goto INIT_SYMBOL
if type == "LispCons" goto INIT_LIST
goto EVAL_BODY
INIT_SYMBOL:
push keyvals, init # Init form was just a symbol -
null value # no value is assigned to it
push keyvals, value
goto INIT_DONE
INIT_LIST:
lptr = init
goto INIT_LIST_LOOP
INIT_LIST_LOOP:
.NULL(lptr, INIT_DONE)
.CAR(form, lptr) # Get the next init form
.ASSERT_TYPE_AND_BRANCH(form, "list", ERROR_BAD_SPEC)
# VALID_IN_PARROT_0_2_0 .ASSERT_LENGTH(form, 2, ERROR_BADSPEC) # Ensure a valid init form
.ASSERT_LENGTH(form, 2, ERROR_BAD_SPEC) # Ensure a valid init form
.CAR(symbol, form) # The symbol we're assigning to
.SECOND(value, form) # The value being assigned
.ASSERT_TYPE_AND_BRANCH(symbol, "symbol", ERROR_BAD_SPEC)
.LIST_1(fargs, value) # Put value into an arg list
value = _eval(fargs) # Evaluate it
push keyvals, symbol # Push symbol onto key/val list
push keyvals, value # Push value onto key/val list
.CDR(lptr, lptr)
goto INIT_LIST_LOOP
INIT_DONE:
# bind the variables in init
.local int nvar
nvar = keyvals
i = 0
BIND_LOOP:
if i >= nvar goto BIND_DONE
symbol = keyvals[i] # Pop symbol of key/val list
inc i
value = keyvals[i] # Pop value of key/val list
name = symbol.'_get_name_as_string'()
test = _IS_SPECIAL(symbol)
if test == 0 goto BIND_LEXICAL
goto BIND_DYNAMIC
BIND_LEXICAL:
# TODO: replace push_pad, pop_pad, do not worry about closures yet
symbol = _LEXICAL_SYMBOL(name, value) # Create a new lexical symbol
inc i
goto BIND_LOOP
BIND_DYNAMIC:
package = symbol.'_get_package'() # Get dynamic symbols package
symbol = package.'_shadow_symbol'(name) # Shadow the symbol
symbol.'_set_value'(value) # Set the new value
push dynvars, symbol # Keep around for tracking
inc i
goto BIND_LOOP
BIND_DONE:
goto EVAL_BODY
EVAL_BODY:
lptr = body # Set pointer to the body form
EVAL_LOOP: # Evaluate each form in order
.NULL(lptr, EVAL_DONE)
.CAR(form, lptr) # Get the next form in the body
.LIST_1(fargs, form) # Put it into an arg list
retv = _eval(fargs) # Evaluate it
.CDR(lptr, lptr) # Get a pointer to next form
goto EVAL_LOOP
EVAL_DONE:
goto CLEANUP
CLEANUP_HANDLER:
.get_results (error) # Caught an exception - save it
goto CLEANUP # and clean up before rethrow
CLEANUP:
# VALID_IN_PARROT_0_2_0 pop_pad # Pop off the lexical scope
nvar = dynvars
i = 0
CLEANUP_LOOP:
if i >= nvar goto CLEANUP_DONE
symbol = dynvars[i] # Symbol to be unshadowed
name = symbol.'_get_name_as_string'()
package = symbol.'_get_package'()
package.'_unshadow_symbol'(name) # Unshadow the symbol
inc i
goto CLEANUP_LOOP
CLEANUP_DONE:
if_null error, DONE # Rethrow an exception if we
rethrow error # need to
goto DONE
CLEANUP_RETHROW:
rethrow error
goto DONE
# VALID_IN_PARROT_0_2_0 ERROR_BADSPEC:
ERROR_BAD_SPEC:
.ERROR_1("program-error", "illegal variable specification %s", form)
goto CLEANUP
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to LET")
goto CLEANUP
DONE:
.return(retv)
.end
.sub _print # This is just a temporary stand-in - it
.param pmc args # doesn't have near enough the amount of
# functionality required.
.local string strval
.local pmc retv
.local pmc obj
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(obj, args)
strval = obj
.STRING(retv, obj)
print retv
print "\n"
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to PRINT")
goto DONE
DONE:
.return(retv)
.end
.sub _progn
.param pmc args
.local pmc eform
.local pmc eargs
.local pmc lptr
.local pmc retv
.NIL(retv)
lptr = args
FORM_LOOP:
.NULL(lptr, DONE)
.CAR(eform, lptr) # Create the arg list for eval
.LIST_1(eargs, eform)
retv = _eval(eargs) # Evaluate form in list
.CDR(lptr, lptr) # Point to next form
goto FORM_LOOP
DONE:
.return(retv)
.end
.sub _quit
.param pmc args
.ASSERT_LENGTH(args, 0, ERROR_NARGS)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to QUIT")
goto DONE
DONE:
end
.end
.sub _quote
.param pmc args
.local pmc retv
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(retv,args)
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to QUOTE")
goto DONE
DONE:
.return(retv)
.end
.sub _rplaca
.param pmc args
.local pmc cons
.local pmc val
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
.CAR(cons, args)
.SECOND(val, args)
.ASSERT_TYPE(cons, "cons")
cons[0] = val # Replace the car with val
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to RPLACA")
goto DONE
DONE:
.return(cons)
.end
.sub _rplacd
.param pmc args
.local pmc cons
.local pmc val
.ASSERT_LENGTH(args, 2, ERROR_NARGS)
.CAR(cons, args)
.SECOND(val, args)
.ASSERT_TYPE(cons, "cons") # Ensure first arg is a cons
cons[1] = val # Replace the cdr with val
goto DONE
ERROR_NARGS:
.ERROR_0("program-error", "wrong number of arguments to RPLACD")
goto DONE
DONE:
.return(cons)
.end
.sub _setq
.param pmc args
.local string name
.local pmc lexical
.local pmc symbol
.local pmc value
.local pmc retv
.local pmc lptr
.local pmc earg
.ASSERT_EVEN_LENGTH(args, ERROR_NARGS)
lptr = args # Pointer to the arguments
.NIL(retv) # Initialize return value
LOOP:
.NULL(lptr, DONE) # If we're at the EOL goto DONE
.CAR(symbol, lptr) # Get the variable to assign to
.SECOND(value, lptr) # Get the value being assigned
.ASSERT_TYPE(symbol, "symbol") # Ensure variable is a symbol
name = symbol.'_get_name_as_string'() # Get the symbols name
lexical = _LOOKUP_LEXICAL(name) # Look for it in lexical env
if_null lexical, SET_SYMBOL_VALUE
symbol = lexical # Lexical variable was found
SET_SYMBOL_VALUE:
.LIST_1(earg, value) # Evaluate the value form
retv = _eval(earg)
symbol.'_set_value'(retv)
.CDR(lptr, lptr)
.CDR(lptr, lptr)
goto LOOP
ERROR_NARGS:
.ERROR_0("program-error", "odd number of arguments to SETQ")
goto DONE
DONE:
.return(retv)
.end
.sub _type_of
.param pmc args
.local string type
.local string name
.local pmc form
.local pmc retv
.local pmc nil
.ASSERT_LENGTH(args, 1, ERROR_NARGS)
.CAR(form, args)
null nil
type = typeof form
if type == "LispCons" goto CONS
if type == "LispFloat" goto FLOAT
if type == "LispFunction" goto FUNCTION
if type == "LispHash" goto HASH
if type == "LispInteger" goto INTEGER
if type == "LispMacro" goto MACRO
if type == "LispPackage" goto PACKAGE
if type == "LispStream" goto STREAM
if type == "LispString" goto STRING
if type == "LispSymbol" goto SYMBOL
goto UNKNOWN_TYPE
CONS:
name = "CONS"
goto LOOKUP_SYMBOL
FLOAT:
name = "FLOAT"
goto LOOKUP_SYMBOL
FUNCTION:
name = "FUNCTON"
goto LOOKUP_SYMBOL
HASH:
name = "HASH-TABLE"
goto LOOKUP_SYMBOL