From 0fe2cc70abacc7c9e7aa2602836c8226bb1a1dc3 Mon Sep 17 00:00:00 2001 From: bd Date: Mon, 7 Apr 2025 23:58:29 -0400 Subject: Add label processing, mnemonic and label lookup maps --- src/package.lisp | 11 ++++++++++- src/parse.lisp | 21 +++++++++++++++++++++ src/util.lisp | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/parse.lisp (limited to 'src') diff --git a/src/package.lisp b/src/package.lisp index 670ed02..d999783 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -4,7 +4,10 @@ (defpackage #:util (:use #:cl) - (:export #:asm-extension?)) + (:export #:asm-extension? + #:format-as-binary + #:label-loc + #:mnemonic-loc)) (defpackage #:lex (:use #:cl) @@ -12,3 +15,9 @@ ;; exported for testing only #:read-token #:invalid-immediate-or-keyword)) + +(defpackage #:parse + (:use #:cl) + (:export #:tokens->ast + ;; exported for testing only + #:extract-label)) diff --git a/src/parse.lisp b/src/parse.lisp new file mode 100644 index 0000000..8bd8c50 --- /dev/null +++ b/src/parse.lisp @@ -0,0 +1,21 @@ +(in-package #:parse) + +(defun tokens->ast (program) + (let ((program (remove nil (mapcar #'extract-label program)))) + ;; TODO + program)) + +(let ((i 0)) + (defun extract-label (line) + "Given a series of tokens LINE, determines if LINE is +in the form STRING {colon}. If it is, then it is treated as a +label, and pushed onto the stack with the line index. + +Note that this function is intended to be called using mapcar, +so that labels can be added to a map and otherwise removed from +processing." + (trivia:match line + ((list (and id (type string)) + (satisfies (lambda (x) (equal x 'lex::colon)))) + (progn (push (cons (read-from-string id) i) util:label-loc) nil)) + (_ (progn (incf i) line))))) diff --git a/src/util.lisp b/src/util.lisp index 87e4df9..1ea6dfc 100644 --- a/src/util.lisp +++ b/src/util.lisp @@ -3,3 +3,35 @@ (defun asm-extension? (file) "Returns t if FILE is extended with .asm, nil otherwise." (string= (pathname-type file) "asm")) + +;; TODO this won't work for negative numbers of odd sizes quite yet. +(defun format-as-binary (num len) + "Formats NUM as a binary number, and pads to LEN with zeros." + (declare (type number num)) + (declare (type (integer 0 *) len)) + (format nil "~V,'0b" len num)) + +(defmacro generate-type-map (type opsize ops) + "Generates an alist where the key corresponds to an element in +OPS, while the value is the index of that key (padded to OPSIZE) +concatenated with TYPE." + `(let ((i 0)) + (mapcar (lambda (x) + (incf i) + (cons x (concatenate 'string ,type + (format-as-binary i ,opsize)))) + ,ops))) + +(defparameter label-loc '() + "A symbol table mapping label names to line indices.") + +(defparameter mnemonic-loc + `(,@(generate-type-map "00" 5 + '(ADD SUB MUL QUOT REM SFTR SFTL AND OR NOT + XOR ADDV SUBV MULV DIVV CMP CEV)) + ,@(generate-type-map "01" 4 + '(LOAD LOADV ADDI SUBI SFTRI SFTLI ANDI ORI + XORI STORE STOREV)) + ,@(generate-type-map "10" 4 + '(JMP JRL JAL BEQ BGT BUF BOF PUSH POP))) + "An alist mapping known mnemonics to their binary representation.") -- cgit v1.2.3