diff options
-rw-r--r-- | main.lisp | 93 | ||||
-rw-r--r-- | parse-source.lisp | 91 |
2 files changed, 91 insertions, 93 deletions
@@ -19,99 +19,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |# -(defun interpret-character (s x) - "Interpret meanings of characters." - (cond - ((>= x (length s)) - 'end-of-line) - (t - ;; Have character, interpret - (let ((c (char s x))) - (cond - ((eq c #\;) ; Comment - 'comment) - ((eq c #\ ) ; Space - 'whitespace) - (t - 'normal)))))) - -(defun extract-word (line-string index) - "Extract a single word from line-string, starting at index." - (cond - ((member (interpret-character line-string index) - `(end-of-line comment)) - 'end-of-line) - ((equal (interpret-character line-string index) - 'whitespace) - 'whitespace) - (t - (subseq - line-string - index - (do ((end index (incf end))) - ((not (equal 'normal - (interpret-character line-string end))) - end)))))) - -(defun extract-words (line-string &optional (index 0)) - "Return a list the constituent word strings of line-string." - ;; Word Processing Logic - (let ((word (extract-word line-string index))) - (cond - ;; Terminate execution at end-of-line. - ((equal word 'end-of-line) - nil) - ;; If empty, continue on. - ((equal word 'whitespace) - (extract-words line-string (+ index 1))) - ;; Otherwise, extract word and move ahead. - (t - (cons word - (extract-words line-string - (+ index - (length word)))))))) - - -;; Working read-program -(defun read-program (source-filename) - (let ((line (read-line source-filename nil 'EOF))) - (if (equal line 'EOF) - nil - (let ((line-list (extract-words line))) - (if (equal line-list nil) - (progn - (incf *source-line-number*) - (read-program)) - (cons - (list (incf *source-line-number*) - (extract-words line)) - (read-program))))))) - - - -;; Self contained version -(defun program (source-filename) - "Turns an assembly source file into a list, prepended with line numbers." - (let ((source-file (open source-filename)) - (source-line-number 0)) - (labels - ((recurse () - (let ((line (read-line source-file nil 'EOF))) - (if (equal line 'EOF) - nil - (let ((line-list (extract-words line))) - (if (equal line-list nil) - (progn - (incf source-line-number) - (recurse)) - (cons - (list (incf source-line-number) - (extract-words line)) - (recurse)))))))) - (recurse)))) - - - (defun last-char (s) (char s (- (length s) 1))) diff --git a/parse-source.lisp b/parse-source.lisp new file mode 100644 index 0000000..42a57ab --- /dev/null +++ b/parse-source.lisp @@ -0,0 +1,91 @@ +;; -*- mode: common-lisp -*- +#| +clasm-6502: An assembler for the 6502 written in Common Lisp. +Copyright (C) 2024 Aleksei Eaves + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +|# + +(defun interpret-character (s x) + "Interpret meanings of characters." + (cond + ((>= x (length s)) + 'end-of-line) + (t + ;; Have character, interpret + (let ((c (char s x))) + (cond + ((eq c #\;) ; Comment + 'comment) + ((eq c #\ ) ; Space + 'whitespace) + (t + 'normal)))))) + +(defun extract-word (line-string index) + "Extract a single word from line-string, starting at index." + (cond + ((member (interpret-character line-string index) + `(end-of-line comment)) + 'end-of-line) + ((equal (interpret-character line-string index) + 'whitespace) + 'whitespace) + (t + (subseq + line-string + index + (do ((end index (incf end))) + ((not (equal 'normal + (interpret-character line-string end))) + end)))))) + +(defun extract-words (line-string &optional (index 0)) + "Return a list the constituent word strings of line-string." + ;; Word Processing Logic + (let ((word (extract-word line-string index))) + (cond + ;; Terminate execution at end-of-line. + ((equal word 'end-of-line) + nil) + ;; If empty, continue on. + ((equal word 'whitespace) + (extract-words line-string (+ index 1))) + ;; Otherwise, extract word and move ahead. + (t + (cons word + (extract-words line-string + (+ index + (length word)))))))) + +(defun program (source-filename) + "Turns an assembly source file into a list, prepended with line numbers." + (let ((source-file (open source-filename)) + (source-line-number 0)) + (labels + ((recurse () + (let ((line (read-line source-file nil 'EOF))) + (if (equal line 'EOF) + nil + (let ((line-list (extract-words line))) + (if (equal line-list nil) + (progn + (incf source-line-number) + (recurse)) + (cons + (list (incf source-line-number) + (extract-words line)) + (recurse)))))))) + (recurse)))) |