summaryrefslogtreecommitdiff
path: root/src/token.lisp
diff options
context:
space:
mode:
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