update algebra demo

pull/21/head
Matthew Butterick 6 years ago
parent 6d4f0a7b69
commit 684abfc3e6

@ -1,9 +1,6 @@
#lang brag
top : (func-def | func-app)*
func-def : /"fun" var /"(" vars /")" /"=" expr
/vars : var [/"," var]
expr : var "+" var | value
@value : var | INT | func-app
func-app : var /"(" value [/"," value] /")"
@var : ID
top : (fun | app)*
fun : /"fun" ID /"(" ID [/"," ID] /")" /"=" expr
expr : ID "+" ID | app
app : ID /"(" (ID | INT) [/"," ID] /")"

@ -1,6 +1,6 @@
#lang br/quicklang
(require brag/support "grammar.rkt")
(provide (all-defined-out) #%module-begin)
(provide top fun expr app)
(module+ reader
(provide read-syntax))
@ -8,27 +8,29 @@
(define-lex-abbrev reserved-toks
(:or "fun" "(" ")" "=" "+" ","))
(define tokenize
(define tokenize-1
(lexer
[whitespace (tokenize input-port)]
[whitespace (token lexeme #:skip? #t)]
[(from/stop-before "%" "\n") (token 'COMMENT #:skip? #t)]
[reserved-toks lexeme]
[alphabetic (token 'ID (string->symbol lexeme))]
[(:+ alphabetic) (token 'ID (string->symbol lexeme))]
[(:+ (char-set "0123456789")) (token 'INT (string->number lexeme))]))
(define-macro top #'begin)
(define-macro top #'#%module-begin)
(define-macro (func-def VAR VARS EXPR)
#'(define (VAR . VARS) EXPR))
(define-macro-cases fun
[(_ VAR ARG0 EXPR) #'(define (VAR ARG0) EXPR)]
[(_ VAR ARG0 ARG1 EXPR) #'(define (VAR ARG0 ARG1) EXPR)])
(define-macro-cases expr
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro func-app #'#%app)
(define-macro app #'#%app)
(define (read-syntax src ip)
(define parse-tree (parse (λ () (tokenize ip))))
(strip-context
(define parse-tree (parse src (λ () (tokenize-1 ip))))
(strip-bindings
(with-syntax ([PT parse-tree])
#'(module mod-name algebra-demo
#'(module algebra-mod algebra-demo
PT))))

@ -1,4 +1,6 @@
#lang algebra-demo
fun f(x,y) = x + y
fun g(z) = f(z,z)
g(10)
fun f(x,y) = x + y
% fun f(x,y) = x * y
fun g(zz) = f(zz,zz)
g(10)
g(23)
Loading…
Cancel
Save