Added string literals

This commit is contained in:
2024-08-03 19:34:43 +03:00
parent 724519dad1
commit 2cbb10fc6b
3 changed files with 59 additions and 14 deletions

View File

@@ -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))