From e0944fea241f9e8224732ce8dbb299dc9cc878f2 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 1 Jul 2019 16:41:22 -0700 Subject: [PATCH] update scriptish --- .../scriptish-demo/expander.rkt | 24 +++++++++++++++---- .../scriptish-demo/grammar.rkt | 5 ++-- beautiful-racket-demo/scriptish-demo/main.rkt | 10 +++++--- .../scriptish-demo/subtest.rkt | 21 ++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 beautiful-racket-demo/scriptish-demo/subtest.rkt diff --git a/beautiful-racket-demo/scriptish-demo/expander.rkt b/beautiful-racket-demo/scriptish-demo/expander.rkt index 4b25c1b..f658696 100644 --- a/beautiful-racket-demo/scriptish-demo/expander.rkt +++ b/beautiful-racket-demo/scriptish-demo/expander.rkt @@ -1,9 +1,19 @@ -#lang br/quicklang +#lang br (require racket/stxparam) -(provide (all-defined-out)) +(provide (all-defined-out) + #%top-interaction #%top + (rename-out [my-datum #%datum])) (define-macro top #'#%module-begin) +(define-macro (my-datum . VAL) + (with-syntax ([NEW-VAL (let ([val (syntax->datum #'VAL)]) + (if (and (integer? val) + (inexact? val)) + (inexact->exact val) + val))]) + #'(#%datum . NEW-VAL))) + (define-macro (var ID VAL) #'(define ID VAL)) (define (add/concat . xs) @@ -12,8 +22,14 @@ [(ormap string? xs) (string-join (map ~a xs) "")])) (define-macro-cases add-or-sub - [(_ VAL) #'VAL] - [(_ . VALS) #'(add/concat . VALS)]) + [(_ LEFT "+" RIGHT) #'(add/concat LEFT RIGHT)] + [(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)] + [(_ OTHER) #'OTHER]) + +(define-macro-cases mult-or-div + [(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)] + [(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT)] + [(_ OTHER) #'OTHER]) (define-macro (object (K V) ...) #'(make-hash (list (cons K V) ...))) diff --git a/beautiful-racket-demo/scriptish-demo/grammar.rkt b/beautiful-racket-demo/scriptish-demo/grammar.rkt index 251e641..2ffa39a 100644 --- a/beautiful-racket-demo/scriptish-demo/grammar.rkt +++ b/beautiful-racket-demo/scriptish-demo/grammar.rkt @@ -5,8 +5,9 @@ statement : (var | expr | return) /";" | if | while var : /"var" id /"=" expr @expr : comparison comparison : [comparison ("!=" | "==")] add-or-sub -add-or-sub : [@add-or-sub /"+"] value -@value : id | INTEGER | STRING | object +add-or-sub : [add-or-sub ("+" | "-")] mult-or-div +mult-or-div : [mult-or-div ("*" | "/")] value +@value : id | NUMBER | STRING | object | fun | app | increment increment : id /"++" object : /"{" @kvs /"}" diff --git a/beautiful-racket-demo/scriptish-demo/main.rkt b/beautiful-racket-demo/scriptish-demo/main.rkt index f6b0377..4bb1095 100644 --- a/beautiful-racket-demo/scriptish-demo/main.rkt +++ b/beautiful-racket-demo/scriptish-demo/main.rkt @@ -5,10 +5,13 @@ (provide read-syntax)) (define-lex-abbrev reserved-terms - (:or "var" "=" ";" "+" "{" "}" "'" "\"" + (:or "var" "=" ";" "+" "*" "/" + "-" "{" "}" "'" "\"" ":" "," "(" ")" "//" "/*" "*/" "if" "else" "while" "==" "!=" "function" "return" "++")) +(define-lex-abbrev digits (:+ (char-set "0123456789"))) + (define tokenize-1 (lexer-srcloc [(:or (from/stop-before "//" "\n") @@ -18,8 +21,9 @@ (token 'ID (string->symbol lexeme))] [(:+ (:- (:or alphabetic punctuation) reserved-terms)) (token 'DEREF (map string->symbol (string-split lexeme ".")))] - [(:+ (char-set "0123456789")) - (token 'INTEGER (string->number lexeme))] + [(:seq (:? "-") (:or (:seq (:? digits) "." digits) + (:seq digits (:? ".")))) + (token 'NUMBER (string->number lexeme))] [(:or (from/to "\"" "\"") (from/to "'" "'")) (token 'STRING (string-trim lexeme (substring lexeme 0 1)))] [whitespace (token 'WHITE #:skip? #t)] diff --git a/beautiful-racket-demo/scriptish-demo/subtest.rkt b/beautiful-racket-demo/scriptish-demo/subtest.rkt new file mode 100644 index 0000000..1f8b42d --- /dev/null +++ b/beautiful-racket-demo/scriptish-demo/subtest.rkt @@ -0,0 +1,21 @@ +#lang scriptish-demo + +-1; // line comment + 10 ; + +/* multi +// line +comment */ +2; + +var al = -.123; +var bo = "foo"; +var cy = 1.; +var da = 'bar'; +al;bo;cy;da; + +cy + cy + cy; +al+cy*al-cy/100; +cy++; +cy + cy + bo + da; +bo+cy+cy+da; \ No newline at end of file