From 83a8c55f433c3c18ef0b189042a0ef13ac2598b5 Mon Sep 17 00:00:00 2001 From: Darren Bane Date: Wed, 19 May 2021 00:47:13 +0100 Subject: Fixing issues --- basic.lsp | 103 +++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 25 deletions(-) diff --git a/basic.lsp b/basic.lsp index feec2a6..5209832 100644 --- a/basic.lsp +++ b/basic.lsp @@ -127,52 +127,97 @@ ;;; Lexing (defclass () () (:abstractp t)) -(defclass () ((int :reader int))) -(defclass () ((ident :reader ident))) -(defclass () ((lsymbol :reader lsymbol))) -(defclass () ((lstring :reader lstring))) +(defclass () ((int :initarg i :reader int))) +(defclass () ((ident :initarg i :reader ident))) +(defclass () ((lsymbol :initarg s :reader lsymbol))) +(defclass () ((lstring :initarg s :reader lstring))) (defclass () ()) -(defclass () ((str :initarg s :accessor str) - (curr :initform 0 :accessor curr) +(defclass () ((string :initarg s :accessor string) + (current :initform 0 :accessor current) (size :accessor size))) (defmethod initialize-object :after ((self ) initargs) - (setf (size self) (length (str self)))) + (setf (size self) (length (string self)))) (defgeneric forward (cl &rest args)) (defmethod forward ((cl ) &rest args) (let ((incr (if (null args) 1 (car args)))) - (setf (curr cl) (+ (curr cl) incr)))) + (setf (current cl) (+ (current cl) incr)))) (defgeneric extract (pred cl)) (defmethod extract (pred (cl )) - (let* ((st (string cl)) - (pos (current cl)) - (ext (lambda (n) - (if (and (< n (size cl)) (pred (elt st n))) - (ext (+ n 1)) - n))) - (res (ext pos))) + (flet ((ext (n) + (if (and (< n (size cl)) (apply #'pred (elt st n))) + (ext (+ n 1)) + n))) + (let* ((st (string cl)) + (pos (current cl)) + (res (ext pos))) (setf (current cl) res) - (subseq (string cl) pos (- res pos)))) + (subseq (string cl) pos (- res pos))))) + +;; Some functions from C's ctype.h: +(defun isdigit (c) + (and (char>= x #\0) (char<= x #\9))) +(defun isalpha (c) + (or (and (char>= c #\a) (char<= c #\z)) + (and (char>= c #\A) (char<= c #\Z)))) +(defun isalnum (c) + (or (isalpha c) + (isdigit c))) (defgeneric extract-int (cl)) (defmethod extract-int ((cl )) - (flet ((is-int (x) - (and (char>= x #\0) (char<= x #\9)))) - (convert (extract is-int cl) ))) + (convert (extract #'isdigit cl) )) (defgeneric extract-ident (cl)) (defmethod extract-ident ((cl )) (flet ((is-alpha-num (x) - (or (and (char>= x #\a) (char<= x #\z)) - (and (char>= x #\A) (char<= x #\Z)) - (and (char>= x #\0) (char<= x #\9)) + (or (isalnum x) (char= x #\_)))) - (extract is-alpha-num))) + (extract #'is-alpha-num))) + +(defgeneric lexer (cl)) +(defmethod lexer ((cl )) + (flet ((lexer-char (c) + (cond ((or (char= c #\space) (char= c #\tab)) + (forward cl) + (lexer cl)) + ((isalpha c) + (create (class ) 'i (extract-ident cl))) + ((isdigit c) + (create (class ) 'i (extract-int cl))) + ((char= c #\") + (forward cl) + (let ((res (create (class ) 's (extract (lambda (c) (char/= c #\")) cl)))) + (forward cl) + res)) + ((member c '(#\+ #\- #\* #\/ #\% #\& #\| #\! #\= #\( #\))) + (forward cl) + (create (class ) 's c)) + ((or (char= c #\<) (char= c #\>)) + (forward cl) + (if (>= (current cl) (size cl)) + (crate (class ) 's c) + (let ((cs (elt (string cl) (current cl)))) + (cond ((and (char= c #\<) (char= cs #\=)) + (forward cl) + (create (class ) 's "<=")) + ((and (char= c #\>) (char= cs #\=)) + (forward cl) + (create (class ) 's ">=")) + ((and (char= c #\<) (char= cs #\>)) + (forward cl) + (create (class ) 's "<>")) + (t + (create (class ) c)))))) + (t (error "Lexer error"))))) + (if (>= (current cl) (size cl)) + (create (class )) + (lexer-char (elt (string cl) (current cl)))))) ;;; Parsing (defclass () () (:abstractp t)) @@ -184,7 +229,7 @@ (defun unr-symb (s) (cond ((string= s "!") 'not) ((string= s "-") 'uminus) - (t (error "Parse error")))) + (t (throw 'parse-failure)))) (defun bin-symb (s) (cond ((string= s "+") 'plus) @@ -195,7 +240,15 @@ ((string= s "=") 'equal) ((string= s "<") 'less) ((string= s "<=") 'lesseq) - ((string= s ">") 'great))) + ((string= s ">") 'great) + ((string= s ">=") 'greateq) + ((string= s "<>") 'diff) + ((string= s "&") 'and) + ((string= s "|") 'or) + (t (throw 'parse-failure)))) + +(defun tsymb (s) + (catch 'parse-failure (lambda (defun parse (str) (let* ((cl (init-lex str)) -- cgit 1.4.1-2-gfad0