From 650a88d875acd4ec7c9cc31dac29fb447a987d31 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 26 Nov 2019 19:25:03 +0100 Subject: Parse lispparser.lisp with lispparser.lisp --- tests/lispparser.lisp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'tests/lispparser.lisp') diff --git a/tests/lispparser.lisp b/tests/lispparser.lisp index b6dfb67..df1ed32 100644 --- a/tests/lispparser.lisp +++ b/tests/lispparser.lisp @@ -26,6 +26,7 @@ (= clr 'green) "32" (= clr 'yellow) "33" (= clr 'blue) "34" + (= clr 'cyan) "36" (error "Unknown color in ansi-color" clr)))) (concat (concat "\x1B[" (concat style "m")) (concat str "\x1B[0m")))) @@ -119,7 +120,7 @@ (parse-string-contents rest (concat yet ch))))) ; deftype token = ('tag ), where the type of depends on the tag -; ('open '()) ('close '()) ('quote '()) ('string "text") ('number 123) ('name "name") +; ('open '()) ('close '()) ('quote '()) ('string "text") ('number 123) ('name "name") ('include "path") ; string -> (token string[rest]) (define next-token (str) (let ((ch (substr 0 1 str)) @@ -137,6 +138,16 @@ (list (list 'close '()) rest) (= ch "'") (list (list 'quote '()) rest) + (= ch "#") + (if (= (substr 0 7 rest) "include") + (let ((pair (next-token (substr 7 -1 rest)))) + (cond + (null? pair) + (error "Expected path after #include") + (= (car (car pair)) 'string) + (list (list 'include (cadr (car pair))) (cadr pair)) + (error "Expected string after #include"))) + (error "Unknown preprocessor directive after '#'")) (= ch "\"") (let ((pair (parse-string-contents rest "")) (text (car pair)) @@ -179,6 +190,8 @@ (ansi-color 'blue (number->string field)) (= tag 'name) field + (= tag 'include) + (concat (concat (ansi-color 'cyan "#include") " ") (ansi-color 'yellow (concat "\"" (concat (string-escape field) "\"")))) (error "Invalid token tag in pretty-token" tag)))) ; tokens -> string @@ -202,7 +215,7 @@ (declare parse-sexpr) ; deftype item = ('tag ), where the type of depends on the tag -; ('string "text") ('number 123) ('name "name") ('quote item) ('sexpr (list item...)) +; ('string "text") ('number 123) ('name "name") ('quote item) ('sexpr (list item...)) ('include "path") ; tokens -> (item tokens[rest]) (define parse-item (logged-token-fn "parse-item" (lambda (tokens) (if (null? tokens) @@ -222,6 +235,8 @@ (list (car tokens) (cdr tokens)) (= tag 'name) (list (car tokens) (cdr tokens)) + (= tag 'include) + (list (car tokens) (cdr tokens)) (error "Invalid token tag?"))))))) ; tokens -> (list[items] tokens[rest]) @@ -276,6 +291,8 @@ (pretty-token ast) (= (car ast) 'name) (pretty-token ast) + (= (car ast) 'include) + (pretty-token ast) (error "Unrecognised AST type in pretty-ast-compact" (car ast)))) ; ast -> string @@ -317,10 +334,12 @@ (pretty-token ast) (= (car ast) 'name) (pretty-token ast) + (= (car ast) 'include) + (pretty-token ast) (error "Unrecognised AST type in pretty-ast" (car ast))))) -(let ((tokens (tokenise (read-file "tests/stdlib.lisp")))) +(let ((tokens (tokenise (read-file "tests/lispparser.lisp")))) (do - (print (pretty-token-list tokens)) + ; (print (pretty-token-list tokens)) ; (print (parse-program tokens)) (print (pretty-ast (parse-program tokens))))) -- cgit v1.2.3-54-g00ecf