some arrangements to fix quirks of symbols in the executable program

note that (eq 'baz:foo bar:foo) is not true
so some stuff that works in the repl fails in executable
This commit is contained in:
2024-07-29 23:15:38 +03:00
parent 49b58b2d57
commit 004c2b5628
4 changed files with 35 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
(in-package :cl-forth) (in-package :cl-forth)
(defparameter *operations* (make-hash-table)) (defparameter *operations* (make-hash-table :test 'equal))
(eval-always (eval-always
(defun normalize-op-list (asm-list) (defun normalize-op-list (asm-list)
@@ -27,7 +27,7 @@
(defmacro defop (op-name+args (&key (indent 4)) &body body) (defmacro defop (op-name+args (&key (indent 4)) &body body)
(with-gensyms (out-stream) (with-gensyms (out-stream)
(destructuring-bind (op-name . args) (mklist op-name+args) (destructuring-bind (op-name . args) (mklist op-name+args)
`(setf (gethash ',op-name *operations*) `(setf (gethash ,(string op-name) *operations*)
(lambda (,out-stream ,@args) (lambda (,out-stream ,@args)
,(if (or (stringp (car body)) (stringp (caar body))) ,(if (or (stringp (car body)) (stringp (caar body)))
`(defop-format ,out-stream ,indent `(defop-format ,out-stream ,indent
@@ -141,7 +141,7 @@
(format str " ;; -- ~s --~%" op)) (format str " ;; -- ~s --~%" op))
(defun gen-code (op str) (defun gen-code (op str)
(let ((op-fn (gethash (car op) *operations*))) (let ((op-fn (gethash (string (car op)) *operations*)))
(if (null op-fn) (if (null op-fn)
(error "~s is not a valid op" op) (error "~s is not a valid op" op)
(apply op-fn str (cdr op))))) (apply op-fn str (cdr op)))))

View File

@@ -4,6 +4,7 @@
:author "Emre Akan" :author "Emre Akan"
:licence "MIT" :licence "MIT"
:depends-on ("iterate") :depends-on ("iterate")
:serial t
:components ((:file "package") :components ((:file "package")
(:file "util") (:file "util")
(:file "assembly") (:file "assembly")

View File

@@ -6,10 +6,10 @@
syscall-1 syscall-2 syscall-3 syscall-4 syscall-5 syscall-6 syscall-1 syscall-2 syscall-3 syscall-4 syscall-5 syscall-6
bel oku yaz)) bel oku yaz))
(defun is-identifier (sym) (defun is-identifier (sym)
(find sym *identifiers*))) (find sym *identifiers* :test #'string=)))
(defun make-token (sym? line col) (defun make-token (sym? line col)
(if (or (is-identifier sym?) (numberp sym?)) (if (or (numberp sym?) (is-identifier sym?))
(values (list sym? :line line :col col) nil) (values (list sym? :line line :col col) nil)
(values (list sym? :line line :col col :error t) t))) (values (list sym? :line line :col col :error t) t)))
@@ -49,6 +49,28 @@
(let ((has-error nil)) (let ((has-error nil))
(values (values
(with-open-file (str file-name) (with-open-file (str file-name)
(iter outer
(for line = (read-line str nil nil))
(until (null line))
(for line-num from 1)
(multiple-value-bind (tokens has-err)
(lex-line line line-num)
(when has-err
(setf has-error t)
(when report-errors
(format t "~a~%" line)
(let ((err-token (find-if (lambda (tok) (find :error tok))
tokens)))
(format t "~a^~%"
(make-string (getf (cdr err-token) :col)
:initial-element #\Space)))))
(appending tokens))))
has-error)))
(defun lex-string (string &optional report-errors)
(let ((has-error nil))
(values
(let ((str (make-string-input-stream string)))
(iter outer (iter outer
(for line = (read-line str nil nil)) (for line = (read-line str nil nil))
(until (null line)) (until (null line))
@@ -159,7 +181,7 @@
(- (vector-push-extend (let ((top (vector-pop stack))) (- (vector-push-extend (let ((top (vector-pop stack)))
(- (vector-pop stack) top)) (- (vector-pop stack) top))
stack)) stack))
(|.| (print (vector-pop stack))) (dump (print (vector-pop stack)))
(= (vector-push-extend (if (= (vector-pop stack) (= (vector-push-extend (if (= (vector-pop stack)
(vector-pop stack)) (vector-pop stack))
1 0) 1 0)
@@ -211,15 +233,15 @@
(format out "~a ~a~%" "bel: resb" mem-cap)) (format out "~a ~a~%" "bel: resb" mem-cap))
(defun generate-program (program &key (path "output.asm") (compile nil) (defun generate-program (program &key (path "output.asm") (compile nil)
(mem-cap 640000)) (mem-cap 640000) (silence nil))
(with-open-file (out path :direction :output (with-open-file (out path :direction :output
:if-exists :supersede) :if-exists :supersede)
(write-program program out :mem-cap mem-cap)) (write-program program out :mem-cap mem-cap))
(when compile (when compile
(run `("nasm" "-felf64" ,path) :output t) (run `("nasm" "-felf64" ,path) :output t :silence silence)
(let ((name (first (uiop:split-string path :separator '(#\.))))) (let ((name (first (uiop:split-string path :separator '(#\.)))))
(run `("ld" "-o" ,name ,(concatenate 'string name ".o")) (run `("ld" "-o" ,name ,(concatenate 'string name ".o"))
:output t)))) :output t :silence silence))))
(defun compile-program (path) (defun compile-program (path)
(generate-program (make-program path) :compile t)) (generate-program (make-program path) :compile t))

View File

@@ -20,8 +20,9 @@
(defun mklist (form) (defun mklist (form)
(if (listp form) form (list form))) (if (listp form) form (list form)))
(defun run (args &rest options) (defun run (args &rest options &key &allow-other-keys)
(format t "~{~a~^ ~}~%" args) (unless (eq t (getf options :silence))
(format t "~{~a~^ ~}~%" args))
(apply #'uiop:run-program args options)) (apply #'uiop:run-program args options))
(defun from-root (path) (defun from-root (path)