diff --git a/beautiful-racket-demo/basic-demo-3/expander.rkt b/beautiful-racket-demo/basic-demo-3/expander.rkt index 9e31ffa..1487b9a 100644 --- a/beautiful-racket-demo/basic-demo-3/expander.rkt +++ b/beautiful-racket-demo/basic-demo-3/expander.rkt @@ -1,5 +1,5 @@ #lang br/quicklang -(require "struct.rkt" "run.rkt" "elements.rkt") +(require "struct.rkt" "run.rkt" "elements.rkt" "runtime.rkt") (provide (rename-out [b-module-begin #%module-begin]) (all-from-out "elements.rkt")) @@ -7,26 +7,29 @@ (with-pattern ([((b-line NUM STMT ...) ...) #'(LINE ...)] [(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))] - [(VAR-ID ...) (find-unique-var-ids #'(LINE ...))]) + [(VAR-ID ...) (find-property 'b-id #'(LINE ...))] + [(REQ-SPEC ...) (find-property 'b-require-spec #'(LINE ...))]) #'(#%module-begin (module configure-runtime br - (require basic-demo-3/runtime) - (current-basic-port (current-output-port))) + (require "runtime.rkt") + (current-basic-port (current-output-port)) + (configure-repl!)) + (require REQ-SPEC) ... (define VAR-ID 0) ... (provide VAR-ID ...) LINE ... (define line-table (apply hasheqv (append (list NUM LINE-FUNC) ...))) - (void (parameterize ([current-output-port + (parameterize ([current-output-port (or (current-basic-port) (open-output-nowhere))]) - (void (run line-table))))))) + (void (run line-table)))))) (begin-for-syntax (require racket/list) - (define (find-unique-var-ids line-stxs) + (define (find-property which line-stxs) (remove-duplicates (for/list ([stx (in-list (stx-flatten line-stxs))] - #:when (syntax-property stx 'b-id)) + #:when (syntax-property stx which)) stx) #:key syntax->datum))) diff --git a/beautiful-racket-demo/basic-demo-3/expr.rkt b/beautiful-racket-demo/basic-demo-3/expr.rkt index ecef2a7..3dee0ef 100644 --- a/beautiful-racket-demo/basic-demo-3/expr.rkt +++ b/beautiful-racket-demo/basic-demo-3/expr.rkt @@ -1,5 +1,5 @@ #lang br -(provide b-expr b-sum b-product b-neg b-expt) +(provide b-expr b-sum b-product b-neg b-expt b-def b-func) (define (b-expr expr) (if (integer? expr) (inexact->exact expr) expr)) @@ -21,4 +21,13 @@ (define-macro-cases b-expt [(_ VAL) #'VAL] - [(_ LEFT "^" RIGHT) #'(expt LEFT RIGHT)]) \ No newline at end of file + [(_ LEFT "^" RIGHT) #'(expt LEFT RIGHT)]) + +(define-macro (b-def ID VAR-ID ... EXPR) + #'(set! ID (λ (VAR-ID ...) EXPR))) + +(define-macro (b-func ID EXPR ...) + #'(let ([result (ID EXPR ...)]) + (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 e5f6456..b60b5e6 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")) +(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 basic-lexer (lexer-srcloc @@ -12,7 +12,7 @@ [whitespace (token lexeme #:skip? #t)] [(from/stop-before "rem" "\n") (token 'REM lexeme)] [reserved-terms (token lexeme lexeme)] - [(:seq alphabetic (:* (:or alphabetic numeric "$"))) + [(:seq alphabetic (:* (:or alphabetic numeric "$" "/" "-" "?"))) (token 'ID (string->symbol lexeme))] [digits (token 'INTEGER (string->number lexeme))] [(:or (:seq (:? digits) "." digits) diff --git a/beautiful-racket-demo/basic-demo-3/misc.rkt b/beautiful-racket-demo/basic-demo-3/misc.rkt index 963910e..00e5b02 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") -(provide b-rem b-print b-let b-input) +(provide b-rem b-print b-let b-input b-require) (define (b-rem val) (void)) @@ -12,4 +12,6 @@ (define-macro (b-input ID) #'(b-let ID (let* ([str (read-line)] [num (string->number (string-trim str))]) - (or num str)))) \ No newline at end of file + (or num str)))) + +(define-macro (b-require ID) #'(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 646d3e0..826a5e9 100644 --- a/beautiful-racket-demo/basic-demo-3/parser.rkt +++ b/beautiful-racket-demo/basic-demo-3/parser.rkt @@ -7,6 +7,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-end : /"end" b-print : /"print" [b-printable] (/";" [b-printable])* @b-printable : STRING | b-expr @@ -20,6 +21,10 @@ 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-expr : b-or-expr b-or-expr : [b-or-expr "or"] b-and-expr b-and-expr : [b-and-expr "and"] b-not-expr @@ -29,5 +34,7 @@ b-sum : [b-sum ("+"|"-")] b-product 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-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-number : INTEGER | DECIMAL \ 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 17e5656..229a3d5 100644 --- a/beautiful-racket-demo/basic-demo-3/runtime.rkt +++ b/beautiful-racket-demo/basic-demo-3/runtime.rkt @@ -1,3 +1,12 @@ #lang br -(provide current-basic-port) -(define current-basic-port (make-parameter #f)) \ No newline at end of file +(require (prefix-in basic: (submod "main.rkt" reader))) +(provide current-basic-port configure-repl!) + +(define current-basic-port (make-parameter #f)) + +(define (configure-repl!) + ;; wrap REPL interactions with pollen expression support + (define racket-read (current-read-interaction)) + (define (basic-read src in) + (basic:read-syntax src in)) + (current-read-interaction basic-read)) \ No newline at end of file diff --git a/beautiful-racket-demo/basic-demo-3/sample-export.rkt b/beautiful-racket-demo/basic-demo-3/sample-export.rkt index 24d365a..77ed7da 100644 --- a/beautiful-racket-demo/basic-demo-3/sample-export.rkt +++ b/beautiful-racket-demo/basic-demo-3/sample-export.rkt @@ -1,5 +1,6 @@ #lang basic-demo-3 -10 a = 1 : a = 5 +5 def f(x, y) = x * y +10 a = 1 : a = 5 : b = 10 20 gosub 150 30 a = 25 40 gosub 150 diff --git a/beautiful-racket-demo/basic-demo-3/sample-import.rkt b/beautiful-racket-demo/basic-demo-3/sample-import.rkt index fa726eb..a1922cb 100644 --- a/beautiful-racket-demo/basic-demo-3/sample-import.rkt +++ b/beautiful-racket-demo/basic-demo-3/sample-import.rkt @@ -1,3 +1,6 @@ #lang br -(require basic-demo-3/sample-var) -(* a a) \ No newline at end of file +(require "sample-export.rkt") +f +a +b +(f a b) \ 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 new file mode 100644 index 0000000..67de288 --- /dev/null +++ b/beautiful-racket-demo/basic-demo-3/sample-require.rkt @@ -0,0 +1,8 @@ +#lang basic-demo-3 +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 +40 print prime?(15) +50 require racket/base +60 print max(2, 4, 10, 8) \ No newline at end of file