pull/10/head
Matthew Butterick 8 years ago
parent 3dbddf6d1d
commit 6acea5ed3d

@ -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) ...

@ -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)))

@ -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

@ -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))
(define-macro (b-import VAL) #'(void))

@ -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])*

@ -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))

@ -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
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

@ -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)
Loading…
Cancel
Save