resume in runtime repl

pull/10/head
Matthew Butterick 8 years ago
parent 841f12e09a
commit 29f13113f7

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

@ -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)])
[(_ 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)))

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

@ -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))))
(or num str))))
(define-macro (b-require ID) #'(void))

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

@ -1,3 +1,12 @@
#lang br
(provide current-basic-port)
(define current-basic-port (make-parameter #f))
(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))

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

@ -1,3 +1,6 @@
#lang br
(require basic-demo-3/sample-var)
(* a a)
(require "sample-export.rkt")
f
a
b
(f a b)

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