From 7ea139ac4faa3507d3881d6e71d5dafd8bdc2e39 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 4 Jul 2019 11:42:02 -0700 Subject: [PATCH] refinement --- .../scriptish-demo/expander.rkt | 22 +++++++++---------- .../scriptish-demo/grammar.rkt | 2 +- beautiful-racket-demo/scriptish-demo/main.rkt | 12 +++++----- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/beautiful-racket-demo/scriptish-demo/expander.rkt b/beautiful-racket-demo/scriptish-demo/expander.rkt index 0c13332..adf6bdf 100644 --- a/beautiful-racket-demo/scriptish-demo/expander.rkt +++ b/beautiful-racket-demo/scriptish-demo/expander.rkt @@ -1,8 +1,7 @@ #lang br (require racket/stxparam) (provide (all-defined-out) - #%top-interaction #%top #%app - (rename-out [my-datum #%datum])) + #%app #%top #%datum #%top-interaction) (define-macro top #'#%module-begin) @@ -18,20 +17,19 @@ [(_ VAL) #'VAL] [(_ L "&&" R) #'(and L R)]) -(define-macro (my-datum . VAL) - (with-syntax ([NEW-VAL (let ([val (syntax->datum #'VAL)]) - (cond - [(and (integer? val) (inexact? val)) - (inexact->exact val)] - [(boolean? val) (if val "True" "False")] - [else val]))]) - #'(#%datum . NEW-VAL))) +(define-macro (my-app ID ARG ...) + #'(error 'boom)) -(define-macro (var ID VAL) #'(define ID VAL)) +(define-macro-cases var + [(_ ID VAL) #'(define ID VAL)] + [(_ ID ... VAL) #'(begin (define ID VAL) ...)]) (define (add/concat . xs) (cond - [(andmap number? xs) (apply + xs)] + [(andmap number? xs) (let ([sum (apply + xs)]) + (if (and (integer? sum) (inexact? sum)) + (inexact->exact sum) + sum))] [(ormap string? xs) (string-join (map ~a xs) "")])) (define-macro-cases add-or-sub diff --git a/beautiful-racket-demo/scriptish-demo/grammar.rkt b/beautiful-racket-demo/scriptish-demo/grammar.rkt index 2818775..7e54f41 100644 --- a/beautiful-racket-demo/scriptish-demo/grammar.rkt +++ b/beautiful-racket-demo/scriptish-demo/grammar.rkt @@ -2,7 +2,7 @@ top : @statement* statement : (var | expr | return | defun) /";" | if-else | while -var : /"var" varname /"=" expr +var : /"var" (varname /"=")+ expr @expr : reassignment reassignment : ID [("+=" | "-=") expr] | ternary ternary : expr /"?" expr /":" expr | logical-or diff --git a/beautiful-racket-demo/scriptish-demo/main.rkt b/beautiful-racket-demo/scriptish-demo/main.rkt index 92e53c0..028e3dd 100644 --- a/beautiful-racket-demo/scriptish-demo/main.rkt +++ b/beautiful-racket-demo/scriptish-demo/main.rkt @@ -4,7 +4,7 @@ (module+ reader (provide read-syntax)) -(define-lex-abbrev reserved-terms +(define-lex-abbrev reserved-toks (:or "var" "=" ";" "{" "}" "//" "/*" "*/" "+" "*" "/" "-" "'" "\"" @@ -20,14 +20,14 @@ (lexer-srcloc [(:or (from/stop-before "//" "\n") (from/to "/*" "*/")) (token 'COMMENT #:skip? #t)] - [reserved-terms lexeme] - [(:+ (:- (:or alphabetic punctuation) "." reserved-terms)) - (token 'ID (string->symbol lexeme))] - [(:+ (:- (:or alphabetic punctuation) reserved-terms)) - (token 'DEREF (map string->symbol (string-split lexeme ".")))] + [reserved-toks lexeme] [(:seq (:? "-") (:or (:seq (:? digits) "." digits) (:seq digits (:? ".")))) (token 'NUMBER (string->number lexeme))] + [(:seq (:+ (:- (:or alphabetic punctuation digits) reserved-toks))) + (if (string-contains? lexeme ".") + (token 'DEREF (map string->symbol (string-split lexeme "."))) + (token 'ID (string->symbol lexeme)))] [(:or (from/to "\"" "\"") (from/to "'" "'")) (token 'STRING (string-trim lexeme (substring lexeme 0 1)))] [whitespace (token 'WHITE #:skip? #t)]