summaryrefslogtreecommitdiff
path: root/src/parse.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/parse.lisp')
-rw-r--r--src/parse.lisp42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/parse.lisp b/src/parse.lisp
index 8bd8c50..3052583 100644
--- a/src/parse.lisp
+++ b/src/parse.lisp
@@ -1,9 +1,20 @@
-(in-package #:parse)
+helper(in-package #:parse)
+
+(define-condition parser-error (error)
+ ((message :initarg :message
+ :initform nil
+ :reader message))
+ (:report (lambda (condition stream)
+ (format stream "~A" (message condition))))
+ (:documentation "Dedicated error for an invalid parse."))
(defun tokens->ast (program)
- (let ((program (remove nil (mapcar #'extract-label program))))
- ;; TODO
- program))
+ "Given PROGRAM, which is a list of lists of symbols,
+filters out the labels and parses."
+ ;; TODO add directives
+ (let ((program (remove nil (mapcar #'extract-label program)))
+ (i 0))
+ (mapcar (lambda (l) (extract-instruction l i)) program)))
(let ((i 0))
(defun extract-label (line)
@@ -19,3 +30,26 @@ processing."
(satisfies (lambda (x) (equal x 'lex::colon))))
(progn (push (cons (read-from-string id) i) util:label-loc) nil))
(_ (progn (incf i) line)))))
+
+(defun extract-instruction (line i)
+ "Given instruction LINE, determines the expected type format and passes
+LINE and the index I to the the respective function."
+ ;; TODO add pseudo-ops (i.e., nop, mov, ...)
+ (let* ((type-map '((r-type . extract-r-type)
+ (i-type . extract-i-type)
+ (j-type . extract-j-type)))
+ (keyword (car line))
+ (type-fn (cdr (assoc keyword type-map))))
+ (if type-fn
+ (funcall type-fn line i)
+ (error 'parser-error
+ (format nil "PARSE failed--~a is not a known keyword.~%" (keyword))))))
+
+(defun extract-r-type (line i)
+ 'r)
+
+(defun extract-i-type (line i)
+ 'i)
+
+(defun extract-j-type (line i)
+ 'j)