-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc-006.lisp
2112 lines (1841 loc) · 56.2 KB
/
rc-006.lisp
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
;;;; rosetta/ros-006
;;;; Contains one task:
;;;; Compiler/Virtual Machine Interpreter
(in-package #:ros-01)
#| Compiler/Virtual Machine Interpreter
A virtual machine implements a computer in
software.
Virtual Machine Interpreter
Write a virtual machine interpreter. This
interpreter should be able to run virtual
assembly language programs created via the
task. This is a byte-coded, 32-bit word
stack based virtual machine.
The program should read input from a file
and/or stdin, and write output to a file
and/or stdout.
Input format
Given the following Given the following
program:
count = 1;
while (count < 10) {
print("count is: ", count, "\n");
count = count + 1;
}
The output from the Code generator is a
virtual assembly code program:
Output from gen, input to VM
Datasize: 1 Strings: 2
"count is: "
"\n"
0 push 1
5 store [0]
10 fetch [0]
15 push 10
20 lt
21 jz (43) 65
26 push 0
31 prts
32 fetch [0]
37 prti
38 push 1
43 prts
44 fetch [0]
49 push 1
54 add
55 store [0]
60 jmp (-51) 10
65 halt
The first line of the input specifies the
datasize required and the number of constant
strings, in the order that they are
reference via the code.
The data can be stored in a separate array,
or the data can be stored at the beginning
of the stack. Data is addressed starting at
0. If there are 3 variables, the 3rd one if
referenced at address 2.
If there are one or more constant strings,
they come next. The code refers to these
strings by their index. The index starts at
0. So if there are 3 strings, and the code
wants to reference the 3rd string, 2 will be
used.
Next comes the actual virtual assembly code.
The first number is the code address of that
instruction. After that is the instruction
mnemonic, followed by optional operands,
depending on the instruction.
Registers
sp:
the stack pointer - points to the next
top of stack. The stack is a 32-bit
integer array.
pc:
the program counter - points to the
current instruction to be performed.
The code is an array of bytes.
Data:
data
string pool
Instructions
Each instruction is one byte. The following
instructions also have a 32-bit integer
operand:
fetch [index]
where index is an index into the data array.
store [index]
where index is an index into the data array.
push n
where value is a 32-bit integer that will be
pushed onto the stack.
jmp (n) addr
where (n) is a 32-bit integer specifying the
distance between the current location and
the desired location. addr is an unsigned
value of the actual code address.
jz (n) addr
where (n) is a 32-bit integer specifying the
distance between the current location and
the desired location. addr is an unsigned
value of the actual code address.
The following instructions do not have an
operand. They perform their operation
directly against the stack:
For the following instructions,
the operation is performed against the top
two entries in the stack:
add
sub
mul
div
mod
lt
gt
le
ge
eq
ne
and
or
For the following instructions, the
operation is performed against the top
entry in the stack:
neg
not
Print the word at stack top as a character.
prtc
Print the word at stack top as an integer.
prti
Stack top points to an index into the string
pool. Print that entry.
prts
Unconditional stop.
halt
A simple example virtual machine
def run_vm(data_size)
int stack[data_size + 1000]
set stack[0..data_size - 1] to 0
int pc = 0
while True:
op = code[pc]
pc += 1
if op == FETCH:
stack.append(stack[bytes_to_int(code[pc:pc+word_size])[0]]);
pc += word_size
elif op == STORE:
stack[bytes_to_int(code[pc:pc+word_size])[0]] = stack.pop();
pc += word_size
elif op == PUSH:
stack.append(bytes_to_int(code[pc:pc+word_size])[0]);
pc += word_size
elif op == ADD: stack[-2] += stack[-1]; stack.pop()
elif op == SUB: stack[-2] -= stack[-1]; stack.pop()
elif op == MUL: stack[-2] *= stack[-1]; stack.pop()
elif op == DIV: stack[-2] /= stack[-1]; stack.pop()
elif op == MOD: stack[-2] %= stack[-1]; stack.pop()
elif op == LT: stack[-2] = stack[-2] < stack[-1]; stack.pop()
elif op == GT: stack[-2] = stack[-2] > stack[-1]; stack.pop()
elif op == LE: stack[-2] = stack[-2] <= stack[-1]; stack.pop()
elif op == GE: stack[-2] = stack[-2] >= stack[-1]; stack.pop()
elif op == EQ: stack[-2] = stack[-2] == stack[-1]; stack.pop()
elif op == NE: stack[-2] = stack[-2] != stack[-1]; stack.pop()
elif op == AND: stack[-2] = stack[-2] and stack[-1]; stack.pop()
elif op == OR: stack[-2] = stack[-2] or stack[-1]; stack.pop()
elif op == NEG: stack[-1] = -stack[-1]
elif op == NOT: stack[-1] = not stack[-1]
elif op == JMP: pc += bytes_to_int(code[pc:pc+word_size])[0]
elif op == JZ: if stack.pop() then pc += word_size else pc += bytes_to_int(code[pc:pc+word_size])[0]
elif op == PRTC: print stack[-1] as a character; stack.pop()
elif op == PRTS: print the constant string referred to by stack[-1]; stack.pop()
elif op == PRTI: print stack[-1] as an integer; stack.pop()
elif op == HALT: break
Additional examples
Your solution should pass all the test cases
above and the additional tests found Here.
Reference
The C and Python versions can be considered
reference implementations.
See compiler.txt (use the shell to find it)
to see the various compiler tasks.
|#
#| My notes
1. all code and data, except string
constants, is stored and operated on
as regular integers, whose values
are constrained to those that fit
in 32 bits. Overflow results in
rollover. (Note that in CL, integers
are automatically given arbitrary
precision, with type conversion performed
implicitly. At least one implementation
has facilities for raw 32-bit arithmetic,
but this is not portable. The use of
bit-vectors was considered and
rejected. CL does not provide native
functions to perform arithmetic on
bit-vectors. The 3rd party bit-smasher
library offers this facility for
non-negative values, only.
2. Registers/Memory. All registers/memory
except the string array
are Common Lisp arrays of constrained
integers, each limited to 32 bits.
2A. We need a temporary register.
Temp is a zero-dimensional array
(i.e. it can hold only one value at
a time.)
2B. Stack
This will be an adjustable array with
a Lisp fill-pointer. The fill-pointer
will be used as the sp (stack pointer)
2C. Data
A simple vector (i.e. of fixed length)
2D. Strings
A simple vector. Contains strings,
not integers.
3. Instructions
3A FETCH n
Retrieve nth value from data array
and place into the register
3B STORE n
Place the value in the register into
the nth cell of the data array.
3C PUSH n
Push n onto the stack
3D JMP (offset) address
Add offset to the code pointer.
3E JZ (offset) address
Iff the value in the register is
binary zero, add offset to the code
pointer.
3F ADD
Pop stack to temp.
Pop stack, temp.
Normalize temp
Push to stack.
3G SUB
Pop stack
Exchange popped value and top value
of stack.
Use hand-rolled subtraction: pop
stack, subtracting from temp.
Normalize temp
Push to stack
3H. MUL
Pop stack to temp.
Pop stack, multiplying with temp
Normalize temp
Push to stack.
3I. DIV
Pop stack
Exchange
Specialized divide: divide temp
by value popped from stack
Normalize temp
Push stack
3J. MOD
Pop stack
Exchange
Find modulus of temp and pop
Normalize temp
Push
3K. LT
Pop
Exchange
If temp < pop,
Push b1,
else b0.
3L. GT
Pop
Exchange.
If temp > pop
push b1
else b0
3M. LE
Pop
Exchange
If temp <= pop,
push b1
else b0
3N. GE
Pop
Exchange
If temp >= pop
push b1
else b0
30. EQ
Pop
If temp = pop
push b1
else b0
3P. NE
Pop
If temp /= pop
push b1
else b0
3Q. AND
Pop
Bitwise AND temp with pop
normalize temp
push
3R. OR
Pop
Bitwise OR temp and pop
normalize temp
push
3S. NEG
pop
negate temp
normalize temp
push
3T. NOT
pop
bitwise NOT temp
normalize temp
push
3U. PRTC
peek
convert temp to character
print character
3V. PRTI
pop
convert temp to integer
print integer
3W. PRTS
pop
convert temp to integer
access and print string at strings[temp]
3X. HALT
Exit program loop
|#
(defparameter *instructions*
'(fetch store push jmp jz add sub mul
div mod lt gt le ge eq ne and or
neg not prtc prti prts halt)
"The instruction set for the vm")
(defparameter *code-table*
(let ((table (make-hash-table :size 24)))
(do ((i 1 (1+ i))
(tokens *instructions* (rest tokens)))
((null tokens) (progn
(setf (gethash 0 table)
'none)
table))
(setf (gethash i table)
(first tokens))))
"Table mapping bit-vectors to instructions.
Instruction codes start at binary 1,
as we wish binary 0 to be a neutral
place-holder.")
;; The opposite mapping will be on each
;; instruction symbol's plist.
(maphash
#'(lambda (code instruction)
(setf (get instruction 'code) code))
*code-table*)
(defun int->instruction (code)
(gethash code *code-table* 'none))
(defun instruction->int (instruction)
(let ((trial (get instruction 'code)))
(if trial
trial
0)))
(defparameter *word-size* 4
"The number of cells allowed for an
argument in code, or, the length
in bytes of a computer word.")
(defparameter *max-bits* (* *word-size* 8))
(defparameter *max-val*
(1- (expt 2 (1- *max-bits*)))
"The maximum value that a signed
2s complement *max-bits* integer can hold.
When *max-bits* is 32,
value is 2,147,483,647")
(defparameter *min-val*
(- (expt 2 (1- *max-bits*)))
"The minimum value that a signed 2s
complement *max-bits* integer can hold.
With *max-bits* = 32,
the value is -2,147,483,648.")
(defparameter *rand-limit*
(expt 2 *max-bits*)
"used for creating chaotic overflows.")
(defun chaotic-overflow ()
"Simulates an overflow of two
operands whose signs are opposite.
Since this is undefined behavior,
returns a random integer within the
limits."
(+ *min-val* (random *rand-limit*)))
(defun raw-add (a b)
"Simulates raw fixed-bit adding. If the
result fits within the range, return
the result. If not, and both a and b
have the same sign, return an overflowed
result. If the result is out of range and
the integers have opposite signs, then
the result is undefined, and so we return
a random integer in range."
(let ((true-sum (+ a b)))
(if (<= *min-val* true-sum *max-val*)
true-sum
(cond
((= 1 (signum a) (signum b))
(+ *min-val*
(mod
true-sum
(1+ *max-val*))))
((= -1 (signum a) (signum b))
(+ *max-val*
(mod
true-sum
*min-val*)))
(t
(chaotic-overflow))))))
(defun raw-sub (a b)
"Simulates raw fixed-bit subtraction.
See raw-add for details."
(let ((true-difference (- a b)))
(if (<= *min-val*
true-difference
*max-val*)
true-difference
;; if there is an overflow,
;; the operands must have had
;; different signs
(chaotic-overflow))))
(defun raw-mul (a b)
"Simulate fixed-bit multiplication of
two integers. See raw-add for details."
(let ((true-product (* a b)))
(cond
((<= *min-val* true-product *max-val*)
true-product)
((/= (signum a) (signum b))
(chaotic-overflow))
;; if the signs are the same, true-product
;; must be positive.
(t
(do* ((abs-a (abs a))
(abs-b (abs b))
(i (truncate (1+ *max-val*) abs-a)
(1+ i))
(result (* abs-a i)
(+ result abs-a)))
((= i abs-b) result)
(when (> result *max-val*)
(setf result
(+ *min-val*
(mod
result
(1+ *max-val*))))))))))
(defun raw-div (a b)
"In truncated integer division,
there can be no overflows. However, if
an attempt is made to divide by zero,
the cpu gets stuck in an endless loop.
I will not do that here. I will simply
end the program with a note that the
error occurred."
(cond
((zerop b)
;; rather than signal an error,
;; I mimic an infinite recursion and
;; then send control
;; out of the program loop.
(dotimes (_ 10)
(format t "DIV0~%"))
(format t "X-{~%")
(throw 'div0 nil))
(t
(truncate a b))))
(defun raw-mod (a b)
"a modulo b. If b is 0, crash. See
raw-div for details."
(cond
((zerop b)
(dotimes (_ 10)
(format t "DIV0~%"))
(format t "X-{~%")
(throw 'div0 nil))
(t (mod a b))))
(defun raw-neg (a)
"Simulate negation of a fixed-bit
integer. Given twos-complement
fixed-bit integers, an attempt to
negate the minimum legal value
results in the value being
returned unchanged."
(if (= a *min-val*)
a
(- a)))
(defstruct (registers (:conc-name reg-))
"Struct which holds the registers of
the virtual machine. This will be
populated by our assembler."
(stack
(make-array 20
:element-type 'fixnum
:initial-element 0
:adjustable t
:fill-pointer 0)
:type (vector fixnum))
(data nil
:type (or (vector fixnum) null))
(strings nil
:type (or (vector string) null))
(code nil
:type (or (vector fixnum) null)))
(defun vector-peek (vec)
(aref vec (1- (length vec))))
(defparameter *loop-limit* most-positive-fixnum)
(defun c-or (a b)
(if (not (zerop a))
1
(if (not (zerop b))
1
0)))
(defun c-and (a b)
(if (zerop a)
0
(if (zerop b)
0
1)))
(defun c-not (a)
(if (zerop a)
1
0))
(defun vm-run (reg)
"Runs code as a virtual machine. The
machine-language code and the
other registers are stored in a
registers struct."
(catch 'div0
(let ((code (reg-code reg))
(stack (reg-stack reg))
(data (reg-data reg))
(strings (reg-strings reg))
(temp-reg 0)
(instruction 'none))
(loop with halted = nil
for cp from 0
for counter from 0 to *loop-limit*
until halted
while (< cp (length code)) do
(setf instruction
(int->instruction
(aref code cp)))
(case instruction
(fetch
(vector-push-extend
(aref data
(aref code (1+ cp)))
stack)
(incf cp *word-size*))
(store
(setf
(aref data
(aref code (1+ cp)))
(vector-pop stack))
(incf cp *word-size*))
(push
(vector-push-extend
(aref code (1+ cp))
stack)
(incf cp *word-size*))
(jmp
(incf cp
(aref code (1+ cp))))
(jz
(incf cp
(if (zerop (vector-pop stack))
(aref code (1+ cp))
*word-size*)))
(add
(setf temp-reg
(vector-pop stack))
(vector-push
(raw-add
(vector-pop stack)
temp-reg)
stack))
(sub
(setf temp-reg
(vector-pop stack))
(vector-push
(raw-sub
(vector-pop stack)
temp-reg)
stack))
(mul
(setf temp-reg
(vector-pop stack))
(vector-push
(raw-mul
(vector-pop stack)
temp-reg)
stack))
(div
(setf temp-reg
(vector-pop stack))
(vector-push
(raw-div
(vector-pop stack)
temp-reg)
stack))
(mod
(setf temp-reg
(vector-pop stack))
(vector-push
(raw-mod
(vector-pop stack)
temp-reg)
stack))
(lt
(setf temp-reg
(vector-pop stack))
(vector-push
(if (< (vector-pop stack)
temp-reg)
1
0)
stack))
(gt
(setf temp-reg
(vector-pop stack))
(vector-push
(if (> (vector-pop stack)
temp-reg)
1
0)
stack))
(le
(setf temp-reg
(vector-pop stack))
(vector-push
(if (<= (vector-pop stack)
temp-reg)
1
0)
stack))
(ge
(setf temp-reg
(vector-pop stack))
(vector-push
(if (<= (vector-pop stack)
temp-reg)
1
0)
stack))
(eq
(setf temp-reg
(vector-pop stack))
(vector-push
(if (= (vector-pop stack)
temp-reg)
1
0)
stack))
(ne
(setf temp-reg
(vector-pop stack))
(vector-push
(if (/= (vector-pop stack)
temp-reg)
1
0)
stack))
(and
(setf temp-reg
(vector-pop stack))
(vector-push
(c-and
(vector-pop stack)
temp-reg)
stack))
(or
(setf temp-reg
(vector-pop stack))
(vector-push
(c-or
(vector-pop stack)
temp-reg)
stack))
(neg
(vector-push
(raw-neg
(vector-pop stack))
stack))
(not
(vector-push
(c-not
(vector-pop stack))
stack))
(prtc
(format t "~C"
(code-char (vector-peek stack))))
(prti
(format t "~D"
(vector-peek stack)))
(prts
(let ((str
(aref strings
(vector-pop stack))))
(if (string= str "\"\\n\"")
(terpri)
(format t "~A" str))))
(halt
(setf halted t))))))
(values))
(defun emptyp (seq)
(zerop (length seq)))
(defun load-file (file-path)
"Returns contents of text file
as a string, containing the same
line structure as the file."
(with-output-to-string (s)
(with-open-file
(f :direction :input
:element-type :character
:if-does-not-exist :error)
(do ((line
#1=(read-line f nil 'eof nil)
#1#))
((eq line 'eof))
(format s "~A~%"
(string-trim " " line))))))
(defun load-input ()
"Loads a code string from user input."
(format t "~&Please enter code, line by line.~%")
(format t "Enter q on a separate line to stop.~2%")
(with-output-to-string (s)
(do ((line
#7=(read-line)
#7#))
((find line
'("q" "quit" "end" "stop" "eof")
:test #'string-equal))
(format s "~A~%"
(string-trim " " line)))))
(defparameter *c-char->lisp-char*
`(("\\\\a" . "@")
("\\\\b" . ,(format nil "~C" #\Backspace))
("\\\\e" . "@")
("\\\\f" . ,(format nil "~C" #\Page))
("\\\\n" . ,(format nil "~%"))
("\\\\r" . ,(format nil "~C" #\Return))
("\\\\t" . ,(format nil "~C" #\Tab))
("\\\\v" . "@")
("\\\\'" . "'")
("\\\\" . "\\")))
(defun clean-string (str)
"Removes superfluous quotes from a string.
Replaces C escape characters with common
Lisp equivalents."
(let* ((adjustable
(make-array (length str)
:element-type 'character
:initial-contents str
:adjustable t))
(no-quotes
(remove #\" adjustable)))
(reduce
#'(lambda (new-str entry)
(regex-replace-all
(car entry)
new-str
(cdr entry)))
*c-char->lisp-char*
:initial-value no-quotes)))
(defun process-raw (code-str)
"Takes a code string, as loaded
by load-file or load-input. Places each
line in a list. The first line
is divided into tokens, which
contain the labels 'Datasize:' and
'Strings:', each followed by a number.
The labels are discarded and the arguments
are coerced to ints. The second arg, the
number of strings, is used to skip over
the following lines, which contain those
strings. After the string lines, the
code itself begins. For each code line,the
list is split into tokens.
The numbers are coerced to ints, other
numbers are extracted from brackets and
parentheses, are coerced to ints,
and the brackets and parentheses are
discarded.
the instructions are coerced to symbols.
Once the raw string has been proceseed,
we are left with a nested list in
which the inner lists are in reverse order.
The last line number (first item in
first list) is grabbed. Then, the list
is reversed, a list containing only
the last line number is added to the
front, and resulting nested list is
returned."
(let* ((line-list
(uiop:split-string
code-str
:separator (list #\Newline)))
(param-line (first line-list))
(processed nil)
string-count)
(do* ((rest-line
(uiop:split-string
(first line-list)
:separator " ")
(rest rest-line))
(token (first rest-line)
(first rest-line))
(new-first-line nil))
((null rest-line) (push (nreverse new-first-line)
processed))
(when
(and
(not (emptyp token))
(every
#'digit-char-p token))
(push
(parse-integer token)
new-first-line)
(when (null (cdr rest-line))
(setf string-count
(first
new-first-line)))))
(do* ((rest-lines
(rest line-list)
(rest rest-lines))
(line-ptr 1 (1+ line-ptr))
(cur-line
#3=(first rest-lines)
#3#))
((null rest-lines))
(if (<= line-ptr string-count)
(push (list (clean-string cur-line))
processed)
(let ((tokens
(uiop:split-string
cur-line
:separator " "))
(processed-line nil))
(when tokens
(dolist (token
tokens
(push
(nreverse
processed-line)
processed))
(cond
((emptyp token) nil)
((every #'digit-char-p token)
(push
(parse-integer token)
processed-line))
((char=
(char token 0)
#\[)
(push
(parse-integer
(remove #\]
(remove #\[ token)))
processed-line))
((char=
(char token 0)
#\()
(push
(parse-integer
(remove #\)
(remove #\( token)))
processed-line))
(t
(let
((sym (find-symbol
(string-upcase
token))))
(if
(and
sym
(position sym *instructions*))
(push sym processed-line)
(push token processed-line))))))))))
(let ((code-line-count (first (first processed))))
(setf processed (nreverse processed))
(push (list code-line-count) processed))))
(defun assemble (line-list)
"Assembles a nested list (from
process-raw) into a registers structure,
to be fed to our virtual machine.
We process the list as follows:
Take the first line. It contains only
the line number of the last code
instruction. We use this line number
to create a code array, and populate
it with zeros.
The next line contains two integers:
the size of the data array, and the size
of the string array. We use these to
create the arrays, populating the first