diff options
author | mRnea <akannemre@gmail.com> | 2024-08-03 19:34:43 +0300 |
---|---|---|
committer | mRnea <akannemre@gmail.com> | 2024-08-03 19:34:43 +0300 |
commit | 2cbb10fc6b1daacfc331880ef39245307e976b1d (patch) | |
tree | 593c37fa03c17ce6ac49653fd4c198355804b575 /cl-forth.lisp | |
parent | 724519dad14aeb62571866923c46bf47d5963d02 (diff) |
Added string literals
Diffstat (limited to 'cl-forth.lisp')
-rw-r--r-- | cl-forth.lisp | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/cl-forth.lisp b/cl-forth.lisp index 7cee812..d935956 100644 --- a/cl-forth.lisp +++ b/cl-forth.lisp @@ -1,7 +1,7 @@ (in-package :cl-forth) (defun make-token (sym? line col) - (if (or (numberp sym?) (is-identifier sym?)) + (if (or (numberp sym?) (stringp sym?) (is-identifier sym?)) (values (list sym? :line line :col col) nil) (values (list sym? :line line :col col :error t) t))) @@ -9,6 +9,17 @@ (car token)) ;;; LEXER +(defun read-string (stream) + ;; TODO: Handle unmatched " and give proper error message + (with-output-to-string (str) + (iter (for ch = (read-char stream)) + (cond ((char= ch #\") + (finish)) + ((and (char= ch #\\) (char= (peek-char nil stream) #\n)) + (read-char stream) + (write-char #\Newline str)) + (t (write-char ch str)))))) + (defun lex-line (line &optional (line-num 0)) (iter (with line-stream = (make-string-input-stream line)) (with col = 0) @@ -25,6 +36,11 @@ ((char= #\Space next-char) (read-char line-stream)) ((char= #\; next-char) ;; and not in string (finish)) + ((char= #\" next-char) + (read-char line-stream) + (collect (make-token (read-string line-stream) + line-num col) + into tokens)) (t (setf flag nil))) (when flag (incf col) @@ -95,7 +111,10 @@ (for token in tokens) (let ((op (token-op token))) (cond ((numberp op) - (vector-push-extend `(push ,op) ops)) + (vector-push-extend `(push-int ,op) ops)) + ((stringp op) + (vector-push-extend `(push-str ,(length op) ,i ,op) + ops)) ((string= 'ise op) (push (list 'ise i) stack) (vector-push-extend (list 'ise nil) ops)) |