-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabels.lisp
29 lines (25 loc) · 995 Bytes
/
labels.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
(in-package :pc)
(defun handle-instruction (instr labels-table)
"Replaces labels in arguments to the appropriate instruction's numbers"
(unless (symbolp instr)
(mapcar (lambda (arg)
(let ((address (gethash arg labels-table)))
(if address
address
arg)))
instr)))
(defun get-labels-table (program)
"Returns mapping from label name to appropriate instruction's number"
(let ((labels-table (make-hash-table :test #'eq))
(next-instr 0))
(mapc (lambda (instr) (if (symbolp instr)
(setf (gethash instr labels-table) next-instr)
(incf next-instr)))
program)
labels-table))
(defun transform-labels (program)
"Transform labels to instruction's numbers"
(let ((labels-table (get-labels-table program)))
(remove nil
(mapcar (lambda (x) (handle-instruction x labels-table))
program))))