From 6acea5ed3dd7c25c8b95e926d681dc44c440c729 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 27 Feb 2017 15:37:03 -0800 Subject: [PATCH] tidying --- beautiful-racket-demo/basic-demo-3/expander.rkt | 4 ++-- beautiful-racket-demo/basic-demo-3/expr.rkt | 9 +++++---- beautiful-racket-demo/basic-demo-3/lexer.rkt | 2 +- beautiful-racket-demo/basic-demo-3/misc.rkt | 7 ++++--- beautiful-racket-demo/basic-demo-3/parser.rkt | 12 +++++------- beautiful-racket-demo/basic-demo-3/runtime.rkt | 6 ++++-- beautiful-racket-demo/basic-demo-3/sample-def.rkt | 9 ++++++--- .../basic-demo-3/sample-require.rkt | 4 ++-- 8 files changed, 29 insertions(+), 24 deletions(-) diff --git a/beautiful-racket-demo/basic-demo-3/expander.rkt b/beautiful-racket-demo/basic-demo-3/expander.rkt index b0ac893..0965011 100644 --- a/beautiful-racket-demo/basic-demo-3/expander.rkt +++ b/beautiful-racket-demo/basic-demo-3/expander.rkt @@ -8,13 +8,13 @@ ([((b-line NUM STMT ...) ...) #'(LINE ...)] [(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))] [(VAR-ID ...) (find-property 'b-id #'(LINE ...))] - [(REQ-SPEC ...) (find-property 'b-require-spec #'(LINE ...))] + [(REQ-SPEC ...) (find-property 'b-import-spec #'(LINE ...))] [((SHELL-ID SHELL-VAL) ...) (for/list ([(val idx) (in-indexed (current-command-line-arguments))]) (list (suffix-id #'arg idx #:context caller-stx) val))]) #'(#%module-begin (module configure-runtime br - (require "runtime.rkt") + (require basic-demo-3/runtime) (configure-this!)) (require REQ-SPEC) ... (define VAR-ID 0) ... diff --git a/beautiful-racket-demo/basic-demo-3/expr.rkt b/beautiful-racket-demo/basic-demo-3/expr.rkt index 3dee0ef..94bdca7 100644 --- a/beautiful-racket-demo/basic-demo-3/expr.rkt +++ b/beautiful-racket-demo/basic-demo-3/expr.rkt @@ -23,11 +23,12 @@ [(_ VAL) #'VAL] [(_ LEFT "^" RIGHT) #'(expt LEFT RIGHT)]) -(define-macro (b-def ID VAR-ID ... EXPR) - #'(set! ID (λ (VAR-ID ...) EXPR))) +(define-macro (b-def FUNC-ID VAR-ID ... EXPR) + (syntax-local-lift-expression + #'(set! FUNC-ID (λ (VAR-ID ...) EXPR)))) -(define-macro (b-func ID EXPR ...) - #'(let ([result (ID EXPR ...)]) +(define-macro (b-func FUNC-ID ARG ...) + #'(let ([result (FUNC-ID ARG ...)]) (if (boolean? result) (if result 1 0) result))) diff --git a/beautiful-racket-demo/basic-demo-3/lexer.rkt b/beautiful-racket-demo/basic-demo-3/lexer.rkt index b60b5e6..ee9df28 100644 --- a/beautiful-racket-demo/basic-demo-3/lexer.rkt +++ b/beautiful-racket-demo/basic-demo-3/lexer.rkt @@ -3,7 +3,7 @@ (define-lex-abbrev digits (:+ (char-set "0123456789"))) -(define-lex-abbrev reserved-terms (:or "print" "goto" "end" "+" ":" ";" "let" "=" "input" "-" "*" "/" "^" "mod" "(" ")" "def" "if" "then" "else" "<" ">" "<>" "and" "or" "not" "gosub" "return" "for" "to" "step" "next" "def" "," "require")) +(define-lex-abbrev reserved-terms (:or "print" "goto" "end" "+" ":" ";" "let" "=" "input" "-" "*" "/" "^" "mod" "(" ")" "def" "if" "then" "else" "<" ">" "<>" "and" "or" "not" "gosub" "return" "for" "to" "step" "next" "def" "," "import")) (define basic-lexer (lexer-srcloc diff --git a/beautiful-racket-demo/basic-demo-3/misc.rkt b/beautiful-racket-demo/basic-demo-3/misc.rkt index f487136..409956c 100644 --- a/beautiful-racket-demo/basic-demo-3/misc.rkt +++ b/beautiful-racket-demo/basic-demo-3/misc.rkt @@ -1,6 +1,6 @@ #lang br (require "struct.rkt" "expr.rkt") -(provide b-rem b-print b-let b-input b-repl b-require) +(provide b-rem b-print b-let b-input b-repl b-import) (define (b-rem val) (void)) @@ -16,8 +16,9 @@ (define-macro (b-repl . ARGS) (with-pattern ([STMTS (pattern-case-filter #'ARGS - [(b-expr . EXPR-ARGS) #'(b-print (b-expr . EXPR-ARGS))] + [(b-expr . EXPR-ARGS) + #'(b-print (b-expr . EXPR-ARGS))] [OTHER-STMT #'OTHER-STMT])]) #'(begin . STMTS))) -(define-macro (b-require ID) #'(void)) \ No newline at end of file +(define-macro (b-import VAL) #'(void)) \ No newline at end of file diff --git a/beautiful-racket-demo/basic-demo-3/parser.rkt b/beautiful-racket-demo/basic-demo-3/parser.rkt index d2ecf18..dbbce00 100644 --- a/beautiful-racket-demo/basic-demo-3/parser.rkt +++ b/beautiful-racket-demo/basic-demo-3/parser.rkt @@ -6,7 +6,7 @@ b-rem : REM @b-statement : b-end | b-print | b-goto | b-let | b-input | b-if | b-gosub | b-return | b-for | b-next - | b-def | b-require + | b-def | b-import b-end : /"end" b-print : /"print" [b-printable] (/";" [b-printable])* @b-printable : STRING | b-expr @@ -20,10 +20,9 @@ b-gosub : /"gosub" b-expr b-return : /"return" b-for : /"for" b-id /"=" b-expr /"to" b-expr [/"step" b-expr] b-next : /"next" b-id -b-def : /"def" b-id /"(" b-var-list /")" /"=" b-expr -@b-var-list : ID [/"," ID]* -b-require : /"require" b-require-spec -@b-require-spec : ID | STRING +b-def : /"def" b-id /"(" ID [/"," ID]* /")" /"=" b-expr +b-import : /"import" b-import-spec +@b-import-spec : (ID | STRING) b-expr : b-or-expr b-or-expr : [b-or-expr "or"] b-and-expr b-and-expr : [b-and-expr "and"] b-not-expr @@ -34,7 +33,6 @@ b-product : [b-product ("*"|"/"|"mod")] b-neg b-neg : ["-"] b-expt b-expt : [b-expt ("^")] b-value @b-value : b-number | b-id | /"(" b-expr /")" | b-func -b-func : ID /"(" b-input-list /")" -@b-input-list : (ID | b-expr) [/"," (ID | b-expr)]* +b-func : b-id /"(" b-expr [/"," b-expr]* /")" @b-number : INTEGER | DECIMAL b-repl : (b-statement | b-expr) (/":" [b-repl])* \ No newline at end of file diff --git a/beautiful-racket-demo/basic-demo-3/runtime.rkt b/beautiful-racket-demo/basic-demo-3/runtime.rkt index 62e7d15..4c29eca 100644 --- a/beautiful-racket-demo/basic-demo-3/runtime.rkt +++ b/beautiful-racket-demo/basic-demo-3/runtime.rkt @@ -2,7 +2,8 @@ (require "parser.rkt" "tokenizer.rkt") (provide basic-output-port configure-this!) -(define basic-output-port (make-parameter (open-output-nowhere))) +(define basic-output-port + (make-parameter (open-output-nowhere))) (define repl-parser (make-rule-parser b-repl)) @@ -13,7 +14,8 @@ (define one-line (read-line port)) (if (eof-object? one-line) eof - (repl-parser (make-tokenizer (open-input-string one-line))))) + (repl-parser + (make-tokenizer (open-input-string one-line))))) (current-read-interaction read-one-line)) diff --git a/beautiful-racket-demo/basic-demo-3/sample-def.rkt b/beautiful-racket-demo/basic-demo-3/sample-def.rkt index ac7c683..16eb44a 100644 --- a/beautiful-racket-demo/basic-demo-3/sample-def.rkt +++ b/beautiful-racket-demo/basic-demo-3/sample-def.rkt @@ -1,4 +1,7 @@ #lang basic-demo-3 -10 rem all results should be 1 -20 def f(x) = x * x -30 print f((1+2)*3) = 81 \ No newline at end of file +10 x = 2 : y = 3 : z = 5 +20 print f(3, 4) +30 print f(f(3, g(2)), 2) +40 def f(x, y) = x * y * z +50 def g(i) = i + i +60 print y \ No newline at end of file diff --git a/beautiful-racket-demo/basic-demo-3/sample-require.rkt b/beautiful-racket-demo/basic-demo-3/sample-require.rkt index 67de288..14b3174 100644 --- a/beautiful-racket-demo/basic-demo-3/sample-require.rkt +++ b/beautiful-racket-demo/basic-demo-3/sample-require.rkt @@ -2,7 +2,7 @@ 5 x = 5 10 def f(x, y) = x * y 20 print f(9, f(4,5)) : print x -30 print 42 : require math/number-theory +30 print 42 : import math/number-theory 40 print prime?(15) -50 require racket/base +50 import racket/base 60 print max(2, 4, 10, 8) \ No newline at end of file