forked from dllaurence/Nil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nil.llm
1258 lines (1017 loc) · 38.3 KB
/
nil.llm
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
;**********************************************************************
; nil.llm
;
; Nil main program
; Coding conventions: all native quantities that have a direct correspondence
; with something in lisp are named "nil_<name>" where <name> is the name
; recognized by Nil.
;
; Similarly, all quantities imported from C source have a "c_" prefix
; (obviously quantites defined in libc, such as printf, have their standard
; name).
;
; The above rules apply to macros, but in all caps.
;
; Everything else is an implementation-defined quantity not visible to
; Nil. They are in my C++ camel case convention (initial cap is a type
; or function, initial small is a variable) to further distinguish them
; from the above. Also, I am using a 'p' prefix to denote pointers where
; it seems to clarify the code, but probably not consistently.
;
; Finally, to avoid horrible confusion, when it appear by itself or in English
; text the name of the language Nil is always capitalized and the name of the
; empty list nil is always in all lowercase.
;
; Organization: basically like C, with .llh #include files for public module
; declarations and .llm module implementation files. Note that for ease of
; development I have not always divided the project into as many modules as
; it would have for a finished work, however the modules should be obvious and
; easily broken out as becomes convenient. Also note that the c_defs and
; system modules have different rules.
;
; Cheats:
;
; * Using malloc()
; * Using getchar and other stdio functions instead of read() and write().
; * Using the C string-handling functions. Would prefer counted
; strings anyway.
;
; Copyright 2009-2010 by Dustin Laurence. Distributed under the terms of
; the LGPLv3.
;
;**********************************************************************
#include "nil.llh"
#include "system.llh"
#include "exp.llh"
#include "memory.llh"
#include "parse.llh"
; For now, we'll leave the definition here in the main module.
; For now, we are maintaining the definition here and the declarations
; in other files by hand because LLVM would call this a redefinition of
; an external declaration. :-(
@exceptionCode = global %Word 0
; Needed public declarations
declare NILCC %Exp @Eval(%Exp %exp, %Exp %env)
; Evaluator parameters
@selfQuote = internal global i1 0
; We're caching the value of t both for efficiency and so insane
; redefinitions can't change the evaluator behavior. Arguably
; we should follow scheme and make this a magic value instead of
; an ordinary symbol.
; Initialize this value or there will be trouble!
@tPtr = internal global %Exp NIL_VALUE
%TArrayType = type [2 x %c_char]
@tArray = internal constant %TArrayType c"t\00"
@tStrPtr = internal constant %c_char*
getelementptr (%TArrayType* @tArray, i64 0, i64 0)
;**********************************************************************
; Commands
;
; While formally no different than language primitives, conceptually
; these operators manipulate the interpreter rather than being part
; of the regular programming vocabulary.
;
;**********************************************************************
;**********************************************************************
; nil_quit
;
;**********************************************************************
%QuitArrayType = type [5 x %c_char]
@quitArray = internal constant %QuitArrayType c"quit\00"
@quitStrPtr = internal constant %c_char*
getelementptr (%QuitArrayType* @quitArray, i64 0, i64 0)
define NILCC %Exp
@nil_quit(%Exp %argList, %Exp %env)
{
call ccc void @exit(%c_int 0)
unreachable
ret %Exp NIL_VALUE
}
;**********************************************************************
; nil_selfquote
;
;**********************************************************************
%SelfQuoteArrayType = type [10 x %c_char]
@selfQuoteArray = internal constant %SelfQuoteArrayType c"selfquote\00"
@selfQuoteStrPtr = internal constant %c_char*
getelementptr (%SelfQuoteArrayType* @selfQuoteArray, i64 0, i64 0)
define NILCC %Exp
@nil_selfquote(%Exp %argList, %Exp %env)
{
store i1 1, i1* @selfQuote
; Useless return value
ret %Exp NIL_VALUE
}
;**********************************************************************
; nil_noselfquote
;
;**********************************************************************
%NoSelfQuoteArrayType = type [12 x %c_char]
@noSelfQuoteArray = internal constant %NoSelfQuoteArrayType c"noselfquote\00"
@noSelfQuoteStrPtr = internal constant %c_char*
getelementptr (%NoSelfQuoteArrayType* @noSelfQuoteArray, i64 0, i64 0)
define NILCC %Exp
@nil_noselfquote(%Exp %argList, %Exp %env)
{
store i1 0, i1* @selfQuote
; Useless return value
ret %Exp NIL_VALUE
}
;**********************************************************************
; nil_symbols
;
;**********************************************************************
%SymbolsArrayType = type [8 x %c_char]
@symbolsArray = internal constant %SymbolsArrayType c"symbols\00"
@symbolsStrPtr = internal constant %c_char*
getelementptr (%SymbolsArrayType* @symbolsArray, i64 0, i64 0)
define NILCC %Exp
@nil_symbols(%Exp %argList, %Exp %env)
{
%envIsNil = call NILCC i1 @IsNil(%Exp %env)
br i1 %envIsNil, label %EnvIsNil, label %EnvNotNil
EnvIsNil:
ret %Exp NIL_VALUE
EnvNotNil:
#ifndef NDEBUG
%isCell = call NILCC i1 @IsCell(%Exp %env)
assert(%isCell)
#endif
%car = call NILCC %Exp @Car(%Exp %env)
%carIsAtom = call NILCC i1 @IsAtom(%Exp %car)
br i1 %carIsAtom, label %CARIsAtom, label %CARIsCell
CARIsAtom:
ret %Exp %car
CARIsCell:
%carSyms = call NILCC %Exp @nil_symbols(%Exp %argList, %Exp %car)
%cdr = call NILCC %Exp @Cdr(%Exp %env)
%cdrSyms = call NILCC %Exp @nil_symbols(%Exp %argList, %Exp %cdr)
%cons = call NILCC %Exp @Cons(%Exp %carSyms, %Exp %cdrSyms)
ret %Exp %cons
}
;**********************************************************************
; nil_env
;
;**********************************************************************
%EnvArrayType = type [4 x %c_char]
@envArray = internal constant %EnvArrayType c"env\00"
@envStrPtr = internal constant %c_char*
getelementptr (%EnvArrayType* @envArray, i64 0, i64 0)
define NILCC %Exp
@nil_env(%Exp %argList, %Exp %env)
{
ret %Exp %env
}
;**********************************************************************
; nil_strings
;
;**********************************************************************
%StringsArrayType = type [8 x %c_char]
@stringsArray = internal constant %StringsArrayType c"strings\00"
@stringsStrPtr = internal constant %c_char*
getelementptr (%StringsArrayType* @stringsArray, i64 0, i64 0)
define NILCC %Exp
@nil_strings(%Exp %argList, %Exp %env)
{
%stringList = call NILCC %Exp @Strings()
ret %Exp %stringList
}
;**********************************************************************
; nil_help
;
;**********************************************************************
%HelpArrayType = type [5 x %c_char]
@helpArray = internal constant %HelpArrayType c"help\00"
@helpStrPtr = internal constant %c_char*
getelementptr (%HelpArrayType* @helpArray, i64 0, i64 0)
%HelpHelpType = type [41 x %c_char]
@helpHelpArray = internal constant %HelpHelpType
c" (help) display this help message\00"
%HelpSymbolsType = type [65 x %c_char]
@helpSymbolsArray = internal constant %HelpSymbolsType
c" (symbols) display all defined symbols (without definitions)\00"
%HelpEnvType = type [67 x %c_char]
@helpEnvArray = internal constant %HelpEnvType
c" (env) display the complete environment (with definitions)\00"
%HelpStringsType = type [57 x %c_char]
@helpStringsArray = internal constant %HelpStringsType
c" (strings) display all known strings (for debugging)\00"
%HelpMsgsType = type [5 x %c_char*]
@helpMsgs = internal constant %HelpMsgsType [
%c_char* getelementptr (%HelpHelpType* @helpHelpArray,
i64 0, i64 0 ),
%c_char* getelementptr (%HelpSymbolsType* @helpSymbolsArray,
i64 0, i64 0 ),
%c_char* getelementptr (%HelpEnvType* @helpEnvArray,
i64 0, i64 0 ),
%c_char* getelementptr (%HelpStringsType* @helpStringsArray,
i64 0, i64 0 ),
%c_char* null
]
define NILCC %Exp
@nil_help(%Exp %argList, %Exp %env)
{
Entry:
call %c_int @putchar(%c_int ASCII_NEWLINE)
%startPtr = getelementptr %HelpMsgsType* @helpMsgs, i64 0, i64 0
%startMsg = load %c_char** %startPtr
br label %Loop
Loop:
%msgPtr = phi %c_char** [%startPtr, %Entry], [%nextPtr, %Loop]
%msg = phi %c_char* [%startMsg, %Entry], [%nextMsg, %Loop]
call %c_int @puts(%c_char* %msg)
%nextPtr = getelementptr %c_char** %msgPtr, i64 1
%nextMsg = load %c_char** %nextPtr
%notNull = icmp ne %c_char* %nextMsg, null
br i1 %notNull, label %Loop, label %Finished
Finished:
call %c_int @putchar(%c_int ASCII_NEWLINE)
ret %Exp NIL_VALUE
}
;**********************************************************************
; The seven basic primitives
;
; These functions implement the seven basic primitives in Graham's
; (and therefore McCarthy's) paper.
;
;**********************************************************************
;**********************************************************************
; nil_quote (#1)
;
;**********************************************************************
%QuoteArrayType = type [6 x %c_char]
@quoteArray = internal constant %QuoteArrayType c"quote\00"
@quoteStrPtr = constant %c_char*
getelementptr (%QuoteArrayType* @quoteArray, i64 0, i64 0)
define NILCC %Exp
@nil_quote(%Exp %argList, %Exp %env)
{
%car = call NILCC %Exp @Car(%Exp %argList)
ret %Exp %car
}
;**********************************************************************
; nil_atom (#2)
;
;**********************************************************************
%AtomArrayType = type [5 x %c_char]
@atomArray = internal constant %AtomArrayType c"atom\00"
@atomStrPtr = constant %c_char*
getelementptr (%AtomArrayType* @atomArray, i64 0, i64 0)
define NILCC %Exp
@nil_atom(%Exp %argList, %Exp %env)
{
%arg = call NILCC %Exp @Car(%Exp %argList)
%isAtom = call NILCC i1 @IsAtom(%Exp %arg)
br i1 %isAtom, label %True, label %False
True:
%t = load %Exp* @tPtr
ret %Exp %t
False:
ret %Exp NIL_VALUE
}
;**********************************************************************
; nil_eq (#3)
;
;**********************************************************************
%EqArrayType = type [3 x %c_char]
@eqArray = internal constant %EqArrayType c"eq\00"
@eqStrPtr = constant %c_char*
getelementptr (%EqArrayType* @eqArray, i64 0, i64 0)
define NILCC %Exp
@nil_eq(%Exp %argList, %Exp %env)
{
%lhs = call NILCC %Exp @Car(%Exp %argList)
%rest = call NILCC %Exp @Cdr(%Exp %argList)
%rhs = call NILCC %Exp @Car(%Exp %rest)
%isEq = call NILCC i1 @Eq(%Exp %lhs, %Exp %rhs)
br i1 %isEq, label %True, label %False
True:
%t = load %Exp* @tPtr
ret %Exp %t
False:
ret %Exp NIL_VALUE
}
;**********************************************************************
; nil_car (#4)
;
;**********************************************************************
%CARArrayType = type [4 x %c_char]
@carArray = internal constant %CARArrayType c"car\00"
@carStrPtr = internal constant %c_char*
getelementptr (%CARArrayType* @carArray, i64 0, i64 0)
%CARExpectedListType = type [19 x %c_char]
@carExpectedListArray = internal constant %CARExpectedListType
c"car: expected list\00"
define NILCC %Exp
@nil_car(%Exp %exp, %Exp %env)
{
%arg = call NILCC %Exp @Car(%Exp %exp)
%isCell = call NILCC i1 @IsCell(%Exp %arg)
br i1 %isCell, label %HaveCell, label %NoCell
NoCell:
; This is an error, throw an exception
%carExpectedListPtr = getelementptr %CARExpectedListType*
@carExpectedListArray, i64 0, i64 0
call %c_int @puts(%c_char* %carExpectedListPtr)
%xpn = call NILCC %Exp @Exception2Exp(%Word NIL_EXPECTED_LIST)
ret %Exp %xpn
HaveCell:
%car = call NILCC %Exp @Car(%Exp %arg)
ret %Exp %car
}
;**********************************************************************
; nil_cdr (#5)
;
;**********************************************************************
%CDRArrayType = type [4 x %c_char]
@cdrArray = internal constant %CDRArrayType c"cdr\00"
@cdrStrPtr = internal constant %c_char*
getelementptr (%CDRArrayType* @cdrArray, i64 0, i64 0)
%CDRExpectedListType = type [19 x %c_char]
@cdrExpectedListArray = internal constant %CDRExpectedListType
c"cdr: expected list\00"
define NILCC %Exp
@nil_cdr(%Exp %exp, %Exp %env)
{
%arg = call NILCC %Exp @Car(%Exp %exp)
%isCell = call NILCC i1 @IsCell(%Exp %arg)
br i1 %isCell, label %HaveCell, label %NoCell
NoCell:
; This is an error, throw an exception
%cdrExpectedListPtr = getelementptr %CDRExpectedListType*
@cdrExpectedListArray, i64 0, i64 0
call %c_int @puts(%c_char* %cdrExpectedListPtr)
%xpn = call NILCC %Exp @Exception2Exp(%Word NIL_EXPECTED_LIST)
ret %Exp %xpn
HaveCell:
%cdr = call NILCC %Exp @Cdr(%Exp %arg)
ret %Exp %cdr
}
;**********************************************************************
; nil_cons (#6)
;
;**********************************************************************
%ConsArrayType = type [5 x %c_char]
@consArray = internal constant %ConsArrayType c"cons\00"
@consStrPtr = internal constant %c_char*
getelementptr (%ConsArrayType* @consArray, i64 0, i64 0)
define NILCC %Exp
@nil_cons(%Exp %exp, %Exp %env)
{
%car = call NILCC %Exp @Car(%Exp %exp)
%cdr = call NILCC %Exp @Cdr(%Exp %exp)
%cadr = call NILCC %Exp @Car(%Exp %cdr)
%cons = call NILCC %Exp @Cons(%Exp %car, %Exp %cadr)
ret %Exp %cons
}
;**********************************************************************
; nil_cond (#7)
;
;**********************************************************************
%CondArrayType = type [5 x %c_char]
@condArray = internal constant %CondArrayType c"cond\00"
@condStrPtr = internal constant %c_char*
getelementptr (%CondArrayType* @condArray, i64 0, i64 0)
%CondExpectedListType = type [45 x %c_char]
@condExpectedListArray = internal constant %CondExpectedListType
c"cond: expected clause to be a non-empty list\00"
%CondMalformedArgsType = type [49 x %c_char]
@condMalformedArgsArray = internal constant %CondMalformedArgsType
c"cond: each clause must have exactly two elements\00"
define NILCC %Exp
@nil_cond(%Exp %argList, %Exp %env)
{
%isNil = call NILCC i1 @IsNil(%Exp %argList)
br i1 %isNil, label %NoClause, label %HaveClause
NoClause:
ret %Exp NIL_VALUE
HaveClause:
%clause = call NILCC %Exp @Car(%Exp %argList)
%clauseIsCell = call NILCC i1 @IsCell(%Exp %clause)
br i1 %clauseIsCell, label %HaveCell, label %NotCell
NotCell:
; Each clause must be a list--throw an exception
%condExpectedListPtr = getelementptr %CondExpectedListType*
@condExpectedListArray, i64 0, i64 0
call %c_int @puts(%c_char* %condExpectedListPtr)
%cellXpn = call NILCC %Exp @Exception2Exp(%Word NIL_EXPECTED_LIST)
ret %Exp %cellXpn
HaveCell:
%clauseLen = call NILCC %Word @Len(%Exp %clause)
%wellFormed = icmp eq %Word %clauseLen, 2
br i1 %wellFormed, label %WellFormed, label %Malformed
Malformed:
; Throw exception!
%condMalformedArgsPtr = getelementptr
%CondMalformedArgsType* @condMalformedArgsArray, i64 0, i64 0
call %c_int @puts(%c_char* %condMalformedArgsPtr)
%argXpn = call NILCC %Exp @Exception2Exp(%Word NIL_MALFORMED_ARGS)
ret %Exp %argXpn
WellFormed:
%test = call NILCC %Exp @Car(%Exp %clause)
%testResult = call NILCC %Exp @Eval(%Exp %test, %Exp %env)
; FIXME: could easily optimize away intermediate boolean
; boxing/unboxing with a separate Cond() function that returns
; an i1
%failed = call NILCC i1 @IsNil(%Exp %testResult)
br i1 %failed, label %Failed, label %Succeeded
Failed:
%nextCond = call NILCC %Exp @Cdr(%Exp %argList)
%nextResult = call NILCC %Exp @nil_cond(%Exp %nextCond, %Exp %env)
ret %Exp %nextResult
Succeeded:
%rest = call NILCC %Exp @Cdr(%Exp %clause)
%exp = call NILCC %Exp @Car(%Exp %rest)
%thisResult = call NILCC %Exp @Eval(%Exp %exp, %Exp %env)
ret %Exp %thisResult
; ret %Exp %exp
}
;**********************************************************************
; AddBinding
;
; Add a binding to a context.
;
;**********************************************************************
define NILCC %Exp
@AddBinding(%c_char** %namePtr, %Exp %value, %Exp %context)
{
%name = load %c_char** %namePtr
%sym = call NILCC %Exp @NewSymbol(%c_char* %name)
%cdr = call NILCC %Exp @Cons(%Exp %value, %Exp NIL_VALUE)
%binding = call NILCC %Exp @Cons(%Exp %sym, %Exp %cdr)
%newContext = call NILCC %Exp @Cons(%Exp %binding,
%Exp %context)
ret %Exp %newContext
}
;**********************************************************************
; AddPrimitiveBinding
;
;**********************************************************************
define NILCC %Exp
@AddPrimitiveBinding
(
%c_char** %namePtr,
%PrimFn* %fn,
%Word %nArgs,
%Exp %context,
i1 %magic
)
{
%name = load %c_char** %namePtr
%prim = call NILCC %Exp @NewPrimitive(%PrimFn* %fn, %Word %nArgs,
%c_char* %name, i1 %magic)
%newContext = call NILCC %Exp @AddBinding(%c_char** %namePtr,
%Exp %prim,
%Exp %context)
ret %Exp %newContext
}
;**********************************************************************
; GetEnv
;
; Constructs the top-level environment
;
; FIXME: re-write in a table-driven fashion
;
;**********************************************************************
define NILCC %Exp
@GetEnv()
{
; Command primitives--these are conceptually ways to control the
; interpreter rather than language elements per se, though in
; reality there is no difference
; quit
%commandContext1 = call NILCC %Exp @AddPrimitiveBinding(%c_char** @quitStrPtr,
%PrimFn* @nil_quit, %Word 0, %Exp NIL_VALUE, i1 1)
; selfquote
%commandContext2 = call NILCC %Exp @AddPrimitiveBinding(
%c_char** @selfQuoteStrPtr, %PrimFn* @nil_selfquote,
%Word 0, %Exp %commandContext1, i1 1)
; noselfquote
%commandContext3 = call NILCC %Exp @AddPrimitiveBinding(
%c_char** @noSelfQuoteStrPtr, %PrimFn* @nil_noselfquote,
%Word 0, %Exp %commandContext2, i1 1)
; symbols
%commandContext4 = call NILCC %Exp @AddPrimitiveBinding(
%c_char** @symbolsStrPtr, %PrimFn* @nil_symbols,
%Word 0, %Exp %commandContext3, i1 1)
; env
%commandContext5 = call NILCC %Exp @AddPrimitiveBinding(
%c_char** @envStrPtr, %PrimFn* @nil_env,
%Word 0, %Exp %commandContext4, i1 1)
; strings
%commandContext6 = call NILCC %Exp @AddPrimitiveBinding(
%c_char** @stringsStrPtr, %PrimFn* @nil_strings,
%Word 0, %Exp %commandContext5, i1 1)
; help
%commandContext = call NILCC %Exp @AddPrimitiveBinding(
%c_char** @helpStrPtr, %PrimFn* @nil_help,
%Word 0, %Exp %commandContext6, i1 1)
%env1 = call NILCC %Exp @Cons(%Exp %commandContext, %Exp NIL_VALUE)
; Language primitives
; t
; Unfortunately must get the symbol so we can create the binding;
; if we do more of these there should be a 'AddSelfBinding' function
%tStr = load %c_char** @tStrPtr
%t = call NILCC %Exp @NewSymbol(%c_char* %tStr)
; Cache the value of t for efficiency and so redefinitions by insane
; programmers won't affect the evaluator code
store %Exp %t, %Exp* @tPtr
%langContext0 = call NILCC %Exp @AddBinding(%c_char** @tStrPtr,
%Exp %t, %Exp NIL_VALUE)
; quote
%langContext1 = call NILCC %Exp @AddPrimitiveBinding(%c_char** @quoteStrPtr,
%PrimFn* @nil_quote, %Word 1, %Exp %langContext0, i1 1)
; atom
%langContext2 = call NILCC %Exp @AddPrimitiveBinding(%c_char** @atomStrPtr,
%PrimFn* @nil_atom, %Word 1, %Exp %langContext1, i1 0)
; eq
%langContext3 = call NILCC %Exp @AddPrimitiveBinding(%c_char** @eqStrPtr,
%PrimFn* @nil_eq, %Word 2, %Exp %langContext2, i1 0)
; car
%langContext4 = call NILCC %Exp @AddPrimitiveBinding(%c_char** @carStrPtr,
%PrimFn* @nil_car, %Word 1, %Exp %langContext3, i1 0)
; cdr
%langContext5 = call NILCC %Exp @AddPrimitiveBinding(%c_char** @cdrStrPtr,
%PrimFn* @nil_cdr, %Word 1, %Exp %langContext4, i1 0)
; cons
%langContext6 = call NILCC %Exp @AddPrimitiveBinding(%c_char** @consStrPtr,
%PrimFn* @nil_cons, %Word 2, %Exp %langContext5, i1 0)
; cond
%langContext = call NILCC %Exp @AddPrimitiveBinding(%c_char** @condStrPtr,
%PrimFn* @nil_cond, %Word NIL_ARGS_ANY, %Exp %langContext6,
i1 1)
%env = call NILCC %Exp @Cons(%Exp %langContext, %Exp %env1)
ret %Exp %env
}
;**********************************************************************
; nil_assoc
;
; Takes a list of associations, where each association is a list with
; the key in the first position. This is probably the same as assoc
; in standard lisp.
;
; We'll only use two-element associations, but the code is general for
; re-use in-language. For that reason, this should perhaps move to
; the exp module, or a separate library of primitives.
;
;**********************************************************************
%AssocExpectedListType = type [21 x %c_char]
@assocExpectedListArray = internal constant %AssocExpectedListType
c"assoc: expected list\00"
%ExpectedBindingType = type [24 x %c_char]
@expectedBindingArray = internal constant %ExpectedBindingType
c"assoc: expected binding\00"
define NILCC %Exp
@nil_assoc(%Exp %key, %Exp %assocList)
{
%isList = call NILCC i1 @IsCell(%Exp %assocList)
br i1 %isList, label %HaveList, label %HaveAtomTag
HaveAtomTag:
%haveNil = call NILCC i1 @IsNil(%Exp %assocList)
br i1 %haveNil, label %HaveNil, label %HaveAtom
HaveNil:
; The empty list is a list that contains no bindings, so valid
; This is our "not found" return value
ret %Exp NIL_VALUE
HaveAtom:
; This is an error, throw an exception
%assocExpectedListPtr = getelementptr %AssocExpectedListType*
@assocExpectedListArray, i64 0, i64 0
call %c_int @puts(%c_char* %assocExpectedListPtr)
br label %Exception
Exception:
%xpn = call NILCC %Exp @Exception2Exp(%Word NIL_EXPECTED_LIST)
ret %Exp %xpn
HaveList:
%firstBinding = call NILCC %Exp @Car(%Exp %assocList)
; Is this really a binding? It needs to be a cell, but
; can be arbitrary otherwise
%isBinding = call NILCC i1 @IsCell(%Exp %firstBinding)
br i1 %isBinding, label %HaveBinding, label %NotBinding
NotBinding:
; we can't interpret this object, so throw an exception
; This is an error, throw an exception
%expectedBindingPtr = getelementptr %ExpectedBindingType*
@expectedBindingArray, i64 0, i64 0
call %c_int @puts(%c_char* %expectedBindingPtr)
br label %Exception
HaveBinding:
; Does the first entry have the key?
%thisKey = call NILCC %Exp @Car(%Exp %firstBinding)
%notFound = call NILCC i1 @NotEq(%Exp %key, %Exp %thisKey)
br i1 %notFound, label %NotFound, label %Found
Found:
; Note that we return the entire association--this allows,
; among other things, distinguishing between finding a nil
; value and finding no value
ret %Exp %firstBinding
NotFound:
%restAssoc = call NILCC %Exp @Cdr(%Exp %assocList)
; Trivial tail call
%binding = call NILCC %Exp @nil_assoc(%Exp %key, %Exp %restAssoc)
ret %Exp %binding
}
;**********************************************************************
; EnvLookup
;
;**********************************************************************
%UnknownSymbolType = type [16 x %c_char]
@unknownSymbolArray = internal constant %UnknownSymbolType
c"unknown symbol \00"
define NILCC %Exp
@EnvLookup(%Exp %exp, %Exp %env)
{
%cellp = call NILCC i1 @IsCell(%Exp %env)
br i1 %cellp, label %IsCell, label %NotCell
NotCell:
%nilp = call NILCC i1 @IsNil(%Exp %env)
br i1 %nilp, label %UnknownSymbol, label %NotNil
NotNil:
; This can't happen if the nil code is correct
cant_happen()
br label %NotNil
UnknownSymbol:
%selfquote = load i1* @selfQuote
br i1 %selfquote, label %SelfQuote, label %ThrowException
SelfQuote:
ret %Exp %exp
ThrowException:
%unknownSymbolPtr = getelementptr
%UnknownSymbolType* @unknownSymbolArray, i64 0, i64 0
call %c_int @putstring(%c_char* %unknownSymbolPtr)
call NILCC void @PrintExp(%Exp %exp)
call %c_int @putchar(%c_int ASCII_NEWLINE)
%xpn = call NILCC %Exp @Exception2Exp(%Word NIL_UNKNOWN_SYMBOL)
ret %Exp %xpn
IsCell:
%context = call NILCC %Exp @Car(%Exp %env)
%binding = call NILCC %Exp @nil_assoc(%Exp %exp, %Exp %context)
#ifndef NDEBUG
; When used internally, should be impossible for assoc
; to throw an exception
%notException = call NILCC i1 @NotException(%Exp %binding)
assert(%notException)
#endif
%found = call NILCC i1 @NotNil(%Exp %binding)
br i1 %found, label %FoundSymbol, label %NotFound
NotFound:
; Recurse and check next context
%outerEnv = call NILCC %Exp @Cdr(%Exp %env)
%outerBinding = call NILCC %Exp @EnvLookup(%Exp %exp, %Exp %outerEnv)
ret %Exp %outerBinding
FoundSymbol:
%cdr = call NILCC %Exp @Cdr(%Exp %binding)
#ifndef NDEBUG
; Must be a list
%isCell = call NILCC i1 @IsCell(%Exp %cdr)
assert(%isCell)
; Must be a 1-element list--we have no good use for anything
; else!
%cddr = call NILCC %Exp @Cdr(%Exp %cdr)
%isNil = call NILCC i1 @IsNil(%Exp %cddr)
assert(%isNil)
#endif
%cadr = call NILCC %Exp @Car(%Exp %cdr)
ret %Exp %cadr
}
;**********************************************************************
; Eval
;
;**********************************************************************
define NILCC %Exp
@Eval(%Exp %exp, %Exp %env)
{
;call %c_int @putchar(%c_int ASCII_E)
%tag = call NILCC %Tag @GetTag(%Exp %exp)
; Remember that here we will only see types produced by the
; parser, not all types in the language, and the top level
; caller will filter out parser exceptions for us.
switch %Tag %tag, label %CantHappen [
%Tag CELL_TAG, label %Cell
%Tag SYMBOL_TAG, label %SymbolTag
]
CantHappen:
cant_happen()
br label %CantHappen
Cell:
; I claim apply should control the evaluation of its arguments,
; so contrary to SICP we just pass the expression on
%return = call NILCC %Exp @Apply(%Exp %exp, %Exp %env)
ret %Exp %return
SymbolTag:
;%evalExp = call NILCC %Exp @EnvLookup(%Exp %exp, %Exp %env)
;ret %Exp %evalExp
%notNil = call NILCC i1 @NotNil(%Exp %exp)
br i1 %notNil, label %Symbol, label %SelfEvaluate
; for now
;br label %IsNil
SelfEvaluate:
; nil is self-evaluating
ret %Exp %exp
Symbol:
; May be an exception
%value = call NILCC %Exp @EnvLookup(%Exp %exp, %Exp %env)
ret %Exp %value
}
;**********************************************************************
; Apply
;
;**********************************************************************
%CannotApplyType = type [14 x %c_char]
@cannotApplyArray = internal constant %CannotApplyType
c"cannot apply \00"
%BadArgNumType = type [35 x %c_char]
@badArgNumArray = internal constant %BadArgNumType
c"\0Ahad %ld arguments, required %ld\0A\0A\00"
define NILCC %Exp
@Apply(%Exp %combination, %Exp %env)
{
Initial:
#ifndef NDEBUG
%isCell = call NILCC i1 @IsCell(%Exp %combination)
assert(%isCell)
#endif
%car = call NILCC %Exp @Car(%Exp %combination)
; For now, always evaluate the function. This may have to
; change when we get to implementing user functions and
; lambda, we'll see
%fn = call NILCC %Exp @Eval(%Exp %car, %Exp %env)
%tag = call NILCC %Tag @GetTag(%Exp %fn)
; Remember that here we will only see types produced by the
; parser, not all types in the language, and the top level
; caller will filter out parser exceptions for us.
switch %Tag %tag, label %CantHappen [
%Tag EXCEPTION_TAG, label %Exception
%Tag CELL_TAG, label %CannotApply
%Tag SYMBOL_TAG, label %CannotApply
%Tag PRIMITIVE_TAG, label %Primitive
]
CantHappen:
cant_happen()
br label %CantHappen
Exception:
ret %Exp %fn
CannotApply:
%cannotApplyPtr = getelementptr %CannotApplyType* @cannotApplyArray,
i64 0, i64 0
call %c_int @putstring(%c_char* %cannotApplyPtr)
call NILCC void @PrintExp(%Exp %fn)
call %c_int @putchar(%c_int ASCII_NEWLINE)
%xpn = call NILCC %Exp @Exception2Exp(%Word NIL_CANNOT_APPLY)
ret %Exp %xpn
Primitive:
; FIXME: break this out into a separate 'ApplyPrimitive()' function
%prim = call NILCC %nil_primitive* @Exp2nil_primitive(%Exp %fn)
%primFnPtr = getelementptr %nil_primitive* %prim, i64 0, i32 0
%primFn = load %PrimFn** %primFnPtr
%primArgNumPtr = getelementptr %nil_primitive* %prim, i64 0, i32 1
%primArgNum = load %Word* %primArgNumPtr
%anyArgs = icmp eq %Word %primArgNum, NIL_ARGS_ANY
%cdr = call NILCC %Exp @Cdr(%Exp %combination)
br i1 %anyArgs, label %GoodArgNum, label %CheckArgs
CheckArgs:
%argNum = call NILCC %Word @Len(%Exp %cdr)
%goodArgNum = icmp eq %Word %argNum, %primArgNum
br i1 %goodArgNum, label %GoodArgNum, label %BadArgNum
BadArgNum:
%badArgNumStr = getelementptr %BadArgNumType* @badArgNumArray, i64 0, i64 0
call %c_int (%c_char*, ...)* @printf(%c_char* %badArgNumStr, %Word %argNum,
%Word %primArgNum)
%badArgsXpn = call NILCC %Exp @Exception2Exp(%Word NIL_BAD_ARG_NUM)
ret %Exp %badArgsXpn
GoodArgNum:
%magicPtr = getelementptr %nil_primitive* %prim, i64 0, i32 3
%magic = load i1* %magicPtr
br i1 %magic, label %IsMagic, label %NotMagic
NotMagic:
%evalArgs = call NILCC %Exp @EvalList(%Exp %cdr, %Exp %env)
%argException = call NILCC i1 @IsException(%Exp %evalArgs)
br i1 %argException, label %ArgException, label %IsMagic
ArgException:
ret %Exp %evalArgs
IsMagic:
%args = phi %Exp [%evalArgs, %NotMagic], [%cdr, %GoodArgNum]
%result = call NILCC %Exp %primFn(%Exp %args, %Exp %env)
ret %Exp %result
}
;**********************************************************************
; EvalList
;
;**********************************************************************
%DotPairType = type [37 x %c_char]
@dotPairArray = internal constant %DotPairType
c"Error: can't evaluate a dotted pair\0A\00"
define NILCC %Exp
@EvalList(%Exp %exp, %Exp %env)
{
%isAtom = call NILCC i1 @IsAtom(%Exp %exp)
br i1 %isAtom, label %Atom, label %Cell
Atom:
%isNil = call NILCC i1 @IsNil(%Exp %exp)
br i1 %isNil, label %IsNil, label %NotNil
IsNil:
ret %Exp %exp
NotNil:
; FIXME: when we have the ability to produce dotted pairs,