summaryrefslogtreecommitdiff
path: root/src/token.lisp
diff options
context:
space:
mode:
authorriton <riton@riton.home>2025-07-14 21:45:47 +0300
committerriton <riton@riton.home>2025-07-14 21:45:47 +0300
commit1c90d9ae3b84f62168337f3c8b1c3854f6198330 (patch)
tree7dcc1dc41c2cfce5548876d53d8e83835b56b624 /src/token.lisp
parent44efb492349025a195a3b402ec580623ad61723f (diff)
More refactoring of tests and packages
Squashed commit of the following: commit c4659d8be4d664ba7fd4b59d613536f2368cff0e Author: riton <riton@riton.home> Date: Mon Jul 14 21:44:48 2025 +0300 fix package name typo commit ec802339b838d059f4bc9e4da7cc370ab4d91a46 Author: riton <riton@riton.home> Date: Mon Jul 14 21:43:42 2025 +0300 seperate deftest from test instances commit 92d20e0b8405a4a51f01ff65f8bd81f4d25c1e21 Author: riton <riton@riton.home> Date: Mon Jul 14 21:29:47 2025 +0300 make a test system definition commit 125e5fe1e8c07230f32e762273c5c0dd259209e6 Author: riton <riton@riton.home> Date: Mon Jul 14 00:39:09 2025 +0300 compiler macro of as-token commit 0e39c32097783aa39e3fed479bb85b412065f597 Author: riton <riton@riton.home> Date: Mon Jul 14 00:21:46 2025 +0300 define new packages
Diffstat (limited to 'src/token.lisp')
-rw-r--r--src/token.lisp28
1 files changed, 22 insertions, 6 deletions
diff --git a/src/token.lisp b/src/token.lisp
index 8d37734..0e82cc7 100644
--- a/src/token.lisp
+++ b/src/token.lisp
@@ -1,4 +1,4 @@
-(in-package :monkey)
+(in-package :token)
(eval-always
(defun process-line (def prefix)
@@ -88,17 +88,24 @@
(defclass token ()
- ((_type :reader _type
+ ((type :reader type
:initarg :type
:type token-type)
(literal :reader literal
- :initarg :literal)))
+ :initarg :literal
+ :type string)))
+
+(defun type= (type1 type2)
+ (eq type1 type2))
+
+(defmethod type-is ((token token) type)
+ (type= (type token) type))
(defmethod token= ((t1 token) (t2 token))
- (and (eq (_type t1) (_type t2))
+ (and (type= (type t1) (type t2))
(string= (literal t1) (literal t2))))
-(defun make-token (type literal)
+(defun make (type literal)
(make-instance 'token :type type :literal (string literal)))
(defmethod print-object ((token token) stream)
@@ -106,10 +113,19 @@
(format stream "\"~a\"" (literal token))))
(defun as-token (token-type)
+ "Returns TOKEN from a TOKEN-TYPE"
(multiple-value-bind (str ok) (token->string token-type)
- (make-token token-type (if (null ok) "" str))))
+ (make token-type (if (null ok) "" str))))
+
+(define-compiler-macro as-token (&whole form token-type)
+ "if TOKEN-TYPE is TOKEN-TYPE, lookup at compile time"
+ (if (typep token-type 'token-type)
+ (let ((token (as-token token-type)))
+ `(make ,(type token) ,(literal token)))
+ form))
(defun lookup-identifier (str)
+ "Returns an identifier or keyword as TOKEN-TYPE from STR"
(multiple-value-bind (val ok) (string->token str)
(if (not (null ok))
val