This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
system.s
5079 lines (4515 loc) · 84.3 KB
/
system.s
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
[bits 16]
[org 0]
[cpu 386]
; Memory map:
; 0x500-0x8000: all list (SS)
; 0x8000-0x10000: stack (SS)
; 0x10000-0x18000: system code (CS/DS)
; 0x18000-0x20000: system data (DS)
; 0x20000-0x30000: objects (FS)
; 0x30000-0x40000: strings (GS)
; 0x40000-0x50000: environment relinking
%define ADDITIONAL_DATA (0x8000)
%define INPUT_BUFFER_SIZE (0x200)
%define INPUT_BUFFER (ADDITIONAL_DATA)
%define MAX_OPEN_FILES (8)
%define SECTOR_SIZE (0x200)
%define OPEN_FILE_BUFFER (INPUT_BUFFER + INPUT_BUFFER_SIZE)
%define SECTOR_TABLE_BUFFER (OPEN_FILE_BUFFER + SECTOR_SIZE * MAX_OPEN_FILES)
%define FS_HEADER_BUFFER (SECTOR_TABLE_BUFFER + SECTOR_SIZE)
%define TYPE_BUFFER_SIZE (0x100)
%define TYPE_BUFFER (FS_HEADER_BUFFER + SECTOR_SIZE)
%define PRIOR_INPUT_BUFFER (TYPE_BUFFER + TYPE_BUFFER_SIZE)
; Standard file open modes:
%define FILE_READ (1)
%define FILE_WRITE (2)
%define FILE_APPEND (3)
; Special file open modes:
%define FILE_RENAME (4)
%define FILE_DELETE (5)
; File flags.
%define FILE_ERROR (0x80)
; 0 position in root directory
; 2 (remaining) file size low
; 4 (remaining) file size high
; 6 offset into sector
; 8 current sector
; 10 access mode (0 if handle unused)
; 11 checksum
%define DATA_PER_OPEN_FILE (12)
%define ROOT_HANDLE ((MAX_OPEN_FILES - 1) * DATA_PER_OPEN_FILE + open_file_table)
%define SCREEN_COLOR (0x0700)
%define HIGHLIGHT_COLOR (0x1700)
%define ALT_COLOR (0x2F00)
%define ALT2_COLOR (0x3F00)
%define ALT3_COLOR (0x1F00)
%define ERROR_COLOR (0x4F00)
; If the stack pointer goes below this value then we assume it's about to overflow.
; We need a reasonable margin before the all list begins,
; since the stack is also used by the BIOS's interrupt handlers.
%macro CHECK_STACK_OVERFLOW 0
cmp sp,0x8200
jb error_stack_overflow
%endmacro
; Call the garbage collector before every object/string allocation.
; This is used to ensure that the all list is correct,
; so that only truly inaccessible objects are freed.
; %define ALWAYS_GC
; The all list is a stack of objects that shouldn't be freed.
; It grows down and uses SS, much like the actual stack.
; Its position is kept in BP, throughout all functions that need it.
; The symbol table is not kept in the all list.
%define ALL_LIST_TOP (0x8000)
%define ALL_POP(x) add bp,2*x
%macro ALL_PUSH 1
sub bp,2
cmp bp,0x500
je error_stack_overflow
mov [ss:bp],%1
%endmacro
; Each object is 4 bytes. Object 0 is nil.
; Bit 0 of the first word is used by the garbage collector.
; Bit 1 of the first word is set if the object is not a pair.
; The first word is used by pairs to store car, otherwise to store the object type.
; The second word stores object data. For pairs, this is cdr.
%define OBJ_SIZE (4)
%define TYPE_FREE (0x06)
%define TYPE_INT (0x0A)
%define TYPE_STRING (0x0E)
%define TYPE_LAMBDA (0x12)
%define TYPE_SYMBOL (0x16)
%define TYPE_BUILTIN (0x1A)
%define TYPE_NIL (0x1E)
%define TYPE_MACRO (0x22)
%define CAR(d,s) mov d,[fs:s + 0]
%define CDR(d,s) mov d,[fs:s + 2]
%define SETCAR(d,s) mov [fs:d + 0],s
%define SETCDR(d,s) mov [fs:d + 2],s
; Each string section is 8 bytes. Section 0 is used to terminate a string.
; Bit 0 of the first word is used by the garbage collector.
; Bit 1 of the first word is set if the section is unused.
; The first word indicates the identifier of the next section.
; The other words in the section store 6 ASCII characters of the string.
; If the string length is not a multiple of 6, then the last section is padded with 0s.
%define STRING_SIZE (8)
%define STRING_DATA (6)
%define STRING_NEXT(d, s) mov d,[gs:s + 0]
%define MAX_SYMBOL_LENGTH (24)
%define BUILTIN_NIL (0)
; Flags for next_argument:
%define NEXT_ARG_ANY (1 << 8) ; Match any type.
%define NEXT_ARG_QUOTE (1 << 9) ; Don't evaluate the argument.
%define NEXT_ARG_FINAL (1 << 10) ; Check this is the last argument.
%define NEXT_ARG_TAIL (1 << 11) ; Tail call evaluate. Implies _ANY and _BX; incompatible with _QUOTE and _KEEP.
%define NEXT_ARG_KEEP (1 << 12) ; Add the result to the all list.
%define NEXT_ARG_BX (1 << 13) ; Return the result in _BX. Use with _FINAL.
%define NEXT_ARG_NIL (1 << 14) ; Allow nil as well as any matched types.
start:
; Setup segment registers and the stack.
cli
xor ax,ax
mov ss,ax
mov ax,0x1000
mov ds,ax
mov ax,0x2000
mov fs,ax
mov ax,0x3000
mov gs,ax
mov sp,0
cld
sti
; Save the BIOS drive number.
mov [drive_number],dl
; Clear the screen.
call clear_screen
; Install exception handlers.
call install_exception_handlers
; Initialize IO.
call initialize_io
; Initialize the interpreter.
call initialize_interpreter
; Run the REPL.
mov word [recover],repl
call repl
cli
hlt
repl:
; Reset stack.
mov sp,0
; Allow the garbage collector to run.
mov byte [gc_ready],1
; Close any open files.
mov cx,MAX_OPEN_FILES - 1
mov si,open_file_table
.close_file_loop:
cmp byte [si + 10],0
jz .handle_unused
push si
push cx
call close_file
pop cx
pop si
.handle_unused:
add si,DATA_PER_OPEN_FILE
loop .close_file_loop
; Get user input.
mov word [print_callback],terminal_print_string
cmp byte [run_startup_command],0
je .do_startup
mov si,prompt_message
call print_string
call get_user_input
jmp .got_input
.do_startup:
mov byte [run_startup_command],1
mov cx,[startup_command_length]
mov si,startup_command
mov ax,ds
mov es,ax
mov di,INPUT_BUFFER
rep movsb
mov word [print_callback],output_null
.got_input:
; Reset read information.
mov byte [next_character],0
mov word [input_line],1
mov word [input_offset],0
mov word [input_handle],0
; Set the environment.
cmp word [.environment],0
jne .environment_set
mov bx,[obj_builtins]
mov [.environment],bx
.environment_set:
; Tidy the environment.
mov bx,[.environment]
call tidy_environment
; Read and evaluate all the objects in the input buffer.
.evaluate_loop:
mov bp,ALL_LIST_TOP
mov bx,[.environment]
ALL_PUSH(bx)
call print_newline
call read_object
cmp bx,0xFFFF
je .last_object
ALL_PUSH(bx)
mov si,[.environment]
push si
mov di,sp
call evaluate_object
pop si
mov [.environment],si
or bx,bx
jz .evaluate_loop
mov cx,-100
xor dx,dx
call print_object
jmp .evaluate_loop
.last_object:
or al,al
jne error_unexpected_character
jmp repl
.environment: dw 0
get_user_input:
; Save the position where user input began.
call get_caret_position
mov [user_input_start],bx
; Reset last scancode.
mov byte [last_scancode],0
; Add entropy to RNG.
xor ah,ah
int 0x1A
add [do_builtin_random.seed],dx
add [do_builtin_random.seed],cx
xor bx,bx
.loop:
; Highlight the first unmatched brace.
push bx
call highlight_first_unmatched_brace
pop bx
; Read a character from the keyboard.
xor ax,ax
push bx
int 0x16
pop bx
cmp ah,0x48
je .up
cmp al,8
je .backspace
cmp al,13
je .done
cmp al,32
jb .loop
cmp al,127
jae .loop
; Append the character to the end of the buffer.
cmp bx,INPUT_BUFFER_SIZE - 1
je .loop
mov [INPUT_BUFFER + bx],al
inc bx
; Echo the character to the screen.
push ax
push bx
call print_character
pop bx
pop ax
jmp .loop
; Move the caret back.
.backspace:
or bx,bx
jz .loop
push bx
call print_backspace
; Clear the cell.
mov al,' '
call print_character
call print_backspace
pop bx
dec bx
jmp .loop
; Copy the prior input buffer to the current input buffer.
.up:
mov ax,ds
mov es,ax
mov di,INPUT_BUFFER
mov si,PRIOR_INPUT_BUFFER
mov cx,INPUT_BUFFER_SIZE
rep movsb
; Clear the already typed text.
mov cx,bx
or cx,cx
jz .no_text_to_clear
.clear_loop:
push cx
call print_backspace
mov al,' '
call print_character
call print_backspace
pop cx
loop .clear_loop
.no_text_to_clear:
; Print the new text.
mov si,INPUT_BUFFER
call print_string
; Update bx to the length of the new input.
xor bx,bx
mov si,INPUT_BUFFER
.count_loop:
lodsb
or al,al
jz .loop
inc bx
jmp .count_loop
.done:
; Zero terminate the result.
mov byte [INPUT_BUFFER + bx],0
; Copy to the prior input buffer.
mov cx,bx
inc cx
mov ax,ds
mov es,ax
mov di,PRIOR_INPUT_BUFFER
mov si,INPUT_BUFFER
rep movsb
; Remove any highlighting on an unmatched brace.
mov bx,[previously_unmatched_brace]
or bx,bx
jz .cleared_old_formatting
mov ax,0xB800
mov es,ax
mov byte [es:bx],SCREEN_COLOR >> 8
.cleared_old_formatting:
ret
; returns position in bx
; preserves cx, si, di, bp
get_caret_position:
mov ax,[caret_row]
mov dx,160
mul dx
mov bx,[caret_column]
shl bx,1
add bx,ax
ret
; bx - input buffer position
highlight_first_unmatched_brace:
mov ax,0xB800
mov es,ax
mov cx,bx
; Clear the formatting on the previously unmatched brace.
mov bx,[previously_unmatched_brace]
or bx,bx
jz .cleared_old_formatting
mov byte [es:bx],SCREEN_COLOR >> 8
.cleared_old_formatting:
; Find the first unmatched brace.
call get_caret_position
xor cx,cx
.search_loop:
sub bx,2
cmp bx,[user_input_start]
jb .at_start
mov al,[es:bx]
cmp al,']'
je .right_brace
cmp al,'['
je .left_brace
jmp .search_loop
.right_brace:
inc cx
jmp .search_loop
.left_brace:
or cx,cx
jz .found
dec cx
jmp .search_loop
; Set the formatting for the matching brace.
.found:
inc bx
mov [previously_unmatched_brace],bx
mov byte [es:bx],HIGHLIGHT_COLOR >> 8
ret
; Check if there were more closing braces than opening braces.
.at_start:
or cx,cx
jz .done
; Highlight the last unmatched closing brace.
call get_caret_position
.search_loop2:
sub bx,2
cmp bx,[user_input_start]
jb .done
mov al,[es:bx]
cmp al,']'
jne .search_loop2
inc bx
mov [previously_unmatched_brace],bx
mov byte [es:bx],ERROR_COLOR >> 8
ret
; All braces were matched.
.done:
xor bx,bx
mov [previously_unmatched_brace],bx
ret
initialize_interpreter:
; The first object and string are reserved to indicate nil.
mov ax,1
mov word [first_free_string],STRING_SIZE
mov word [first_free_object],OBJ_SIZE
; Create a list of free strings.
mov cx,(65536 / STRING_SIZE - 1)
mov bx,STRING_SIZE
mov ax,(STRING_SIZE * 2 + 2)
.free_string_loop:
mov [gs:bx],ax
add bx,STRING_SIZE
add ax,STRING_SIZE
loop .free_string_loop
; Create a list of free objects.
mov cx,(65536 / OBJ_SIZE - 1)
mov bx,OBJ_SIZE
mov ax,(OBJ_SIZE * 2)
mov dx,TYPE_FREE
.free_obj_loop:
SETCAR(bx, dx)
SETCDR(bx, ax)
add bx,OBJ_SIZE
add ax,OBJ_SIZE
loop .free_obj_loop
; Initial the nil object and string.
mov word [gs:0],0
mov word [gs:2],0
mov word [gs:4],0
mov word [gs:6],0
mov word [fs:0],TYPE_NIL
mov word [fs:2],0
; Start the symbol table and builtins as the nil object.
mov word [obj_symbol_table],0
mov word [obj_builtins],0
; Add builtins.
call add_builtins
ret
add_builtins:
mov si,builtin_strings
mov dx,0
.builtin_loop:
; Check if we're done.
mov al,[si]
or al,al
jz .done
push si
push dx
xor bx,bx
cmp dx,BUILTIN_NIL
je .made_object
; Make the builtin object itself.
mov ax,TYPE_BUILTIN
call new_object
.made_object:
; Get the symbol object.
push bx
mov bp,ALL_LIST_TOP
call find_symbol
; Create the pair of symbol-builtin.
mov ax,bx
pop dx
call new_object
; Create the list object.
mov ax,bx
mov dx,[obj_builtins]
call new_object
mov [obj_builtins],bx
; Advance to the next builtin.
pop dx
pop si
inc dx
.string_end_loop:
lodsb
or al,al
jne .string_end_loop
jmp .builtin_loop
.done:
ret
; bp - all list (preserved)
; returns object in bx (bx != 0xFFFF), or character in al (bx = 0xFFFF)
read_object:
CHECK_STACK_OVERFLOW
; Read a non-whitespace character.
.try_again:
call read_next_character
cmp al,10
je .try_again
cmp al,13
je .try_again
cmp al,9
je .try_again
cmp al,' '
je .try_again
; End of input?
mov bx,0xFFFF
or al,al
je .done
; Check for a comment.
cmp al,';'
jne .not_comment
.comment_loop:
call read_next_character
or al,al
jz read_object
cmp al,10
je read_object
jmp .comment_loop
.not_comment:
; Check for single-character tokens, ']' and '.'.
mov bx,0xFFFF
cmp al,']'
je .done
cmp al,'.'
je .done
; Check for string.
cmp al,'"'
je read_string_object
; Check for list.
cmp al,'['
je read_list_object
; It must be either a symbol or integer.
jmp read_symbol_object
.done:
ret
; bp - all list (preserved)
read_string_object:
; Create the object.
mov ax,TYPE_STRING
xor dx,dx ; new_string could gc, so this should be valid
call new_object
ALL_PUSH(bx)
; Create the first string section.
push bx
call new_string
pop di
SETCDR(di,bx)
push di
; Read characters until the string is closed.
.add_loop:
call read_next_character
or al,al
jz error_unexpected_eoi
cmp al,'"'
je .done
; Handle escape codes.
cmp al,'\'
jne .append
call read_next_character
cmp al,'n'
jne .e1
mov al,10
.e1:
; Append the character.
.append:
call string_append_character
jmp .add_loop
.done:
pop bx
ALL_POP(1)
ret
; bp - all list (preserved)
; al - first character of symbol
read_symbol_object:
xor bx,bx
; Read characters into the buffer.
.loop:
cmp al,'['
je .end_symbol
cmp al,']'
je .end_symbol
cmp al,';'
je .end_symbol
cmp al,'.'
je .end_symbol
cmp al,'"'
je .end_symbol
cmp al,' '
je .end_symbol
cmp al,9
je .end_symbol
cmp al,10
je .end_symbol
or al,al
jz .end_symbol
; Store the characer, and read the next one.
cmp bx,MAX_SYMBOL_LENGTH
je error_symbol_too_long
mov [.buffer + bx],al
inc bx
call read_next_character
jmp .loop
.end_symbol:
mov byte [.buffer + bx],0
mov [next_character],al
; Try to parse the symbol as an integer.
mov si,.buffer
call read_integer_object
jc .done
; Find the symbol.
mov si,.buffer
call find_symbol
; Create the object.
mov ax,TYPE_SYMBOL
mov dx,bx
call new_object
.done: ret
.buffer: times (MAX_SYMBOL_LENGTH + 1) db 0
; bp - all list (preserved)
; si - buffer containing string
; returns object in bx, carry clear if not an integer
read_integer_object:
; Is it negative?
mov dx,32767
mov al,[si]
cmp al,'-'
jne .positive
mov dx,32768
inc si
mov al,[si]
.positive:
; Is it an empty string?
or al,al
jz .not_integer
; Iterate through each digit
xor cx,cx
.digit_loop:
lodsb
or al,al
jz .done
cmp al,'0'
jb .not_integer
cmp al,'9'
ja .not_integer
; Check for overflow.
cmp cx,3276
ja error_integer_too_large
; Multiply by 10.
push ax
push dx
mov ax,cx
mov bx,10
mul bx
mov cx,ax
pop dx
pop ax
; Check for overflow.
mov bx,dx
xor ah,ah
sub bx,ax
add bx,'0'
cmp cx,bx
ja error_integer_too_large
; Add the digit.
add cx,ax
sub cx,'0'
jmp .digit_loop
; Negate the final result.
.done:
cmp dx,32768
jne .negated
neg cx
.negated:
; Create the object.
mov dx,cx
mov ax,TYPE_INT
call new_object
stc
ret
.not_integer:
clc
ret
; bp - all list (preserved)
read_list_object:
sub sp,8
mov di,sp
mov [ss:di+0],bp ; all
mov byte [ss:di+2],1 ; first
mov word [ss:di+4],0 ; result
mov word [ss:di+6],0 ; tail
; Loop until the list is closed.
.loop:
mov bp,[ss:di+0]
mov bx,[ss:di+4]
ALL_PUSH(bx)
call read_object
mov di,sp
; Check for end of list and dotted lists.
cmp bx,0xFFFF
jne .next_item
or al,al
jz error_unexpected_eoi
cmp al,']'
je .done
cmp al,'.'
je .dotted
jmp error_unknown
; Save the item.
.next_item:
ALL_PUSH(bx)
; Is this the first item in the list?
cmp byte [ss:di+2],1
jne .not_first
; Create the pair and set it as the tail.
mov byte [ss:di+2],0
mov ax,bx
xor dx,dx
call new_object
mov di,sp
mov [ss:di+4],bx
mov [ss:di+6],bx
jmp .loop
; Create the pair and add it to the tail.
.not_first:
mov ax,bx
xor dx,dx
call new_object
mov di,sp
mov si,[ss:di+6]
SETCDR(si, bx)
mov [ss:di+6],bx
jmp .loop
; Restore context and return.
.done:
mov bp,[ss:di+0]
mov bx,[ss:di+4]
add sp,8
ret
; Dotted list.
.dotted:
cmp byte [ss:di+2],1
je error_invalid_dot
; Read the final item.
call read_object
mov di,sp
cmp bx,0xFFFF
je error_invalid_dot
mov si,[ss:di+6]
SETCDR(si, bx)
; Read the closing brace.
call read_object
cmp bx,0xFFFF
jne error_invalid_dot
cmp al,']'
jne error_invalid_dot
jmp .done
; returns next character in al
; overwrites si only
read_next_character:
mov al,[next_character]
mov byte [next_character],0
or al,al
jnz .return
cmp word [input_handle],0
je .from_input_buffer
jmp .from_file
.process:
cmp al,10
jne .return
inc word [input_line]
.return: ret
.from_input_buffer:
mov si,[input_offset]
mov al,[si + INPUT_BUFFER]
or al,al
jz .process
inc si
mov [input_offset],si
jmp .process
.from_file:
pusha
mov si,[input_handle]
mov cx,1
mov ax,ds
mov es,ax
mov di,.destination
call read_file
call has_error_file
jc error_read_file
or cx,cx
jnz .e1
mov byte [.destination],0
.e1:
popa
mov al,[.destination]
jmp .process
.destination: db 1
; bp - all list
; si - environment
; bx - object
; di - stack address to write updated enviornment, else 0
; evaluated object returned in bx
; no registers preserved
evaluate_object:
CHECK_STACK_OVERFLOW
; Keep our environment.
ALL_PUSH(si)
; Check for Ctrl+C.
inc byte [check_break]
jnz .no_break
mov ah,1
int 0x16
jz .no_break
cmp ah,0x2E
jne .remove_key
mov ah,2
int 0x16
test al,(1 << 2)
jnz error_break
.remove_key:
mov [last_scancode],ah
xor ah,ah
int 0x16
.no_break:
; Is the object a list?
CAR(ax, bx)
test ax,2
jz .list
; Is the object a symbol?
cmp ax,TYPE_SYMBOL
je .symbol
; Otherwise, the object evalutes to itself.
cmp ax,TYPE_FREE
je error_free_accessible
ret
; Lookup the value of the symbol in the environment.
.symbol:
CDR(bx, bx)
call lookup_symbol
CDR(bx, bx)
ret
; Evaluate the function.
.list:
push bx
push bp
push si
push di
xor di,di
CAR(bx, bx)
call evaluate_object
pop di
pop si
pop bp
ALL_PUSH(bx)
; Is the function a builtin?
CAR(ax, bx)
cmp ax,TYPE_BUILTIN
je .builtin
; Is the function a lambda or macro?
cmp ax,TYPE_LAMBDA
je evaluate_lambda
cmp ax,TYPE_MACRO
je evaluate_macro
; Otherwise, the function object is not callable.
jmp error_not_callable
; Get the builtin ID and the start of the arguments list.
.builtin:
CDR(ax, bx)
pop bx
CDR(bx, bx)
; Call the builtin.
push di
mov di,ax
shl di,1
mov ax,[di + builtin_functions]
or ax,ax
jz error_unimplemented_builtin
pop di