update algebra & precalc demos

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

@ -11,7 +11,7 @@
(define tokenize-1
(lexer
[whitespace (token lexeme #:skip? #t)]
[(from/stop-before "%" "\n") (token 'COMMENT #:skip? #t)]
[(from/stop-before "#" "\n") (token 'COMMENT #:skip? #t)]
[reserved-toks lexeme]
[(:+ alphabetic) (token 'ID (string->symbol lexeme))]
[(:+ (char-set "0123456789")) (token 'INT (string->number lexeme))]))

@ -1,6 +1,6 @@
#lang algebra-demo
fun f(x,y) = x + y
% fun f(x,y) = x * y
# fun f(x,y) = x * y
fun g(zz) = f(zz,zz)
g(10)
g(23)

@ -1,11 +1,11 @@
#lang brag
top : (func-def | func-app)*
func-def : /"fun" var /"(" vars /")" /"=" expr
/vars : [var (/"," var)*]
@expr : sum
sum : [sum ("+" | "-")] product
product : [product ("*" | "/")] value
@value : var | INT | func-app | /"(" expr /")"
func-app : var /"(" [expr (/"," expr)*] /")"
top : (fun | app)*
fun : /"fun" var /"(" argvars /")" /"=" expr
/argvars : [var (/"," var)*]
@expr : s-or-d
s-or-d : [s-or-d ("+" | "-")] p-or-q
p-or-q : [p-or-q ("*" | "/")] value
@value : var | INT | app | /"(" expr /")"
app : var /"(" [expr (/"," expr)*] /")"
@var : ID

@ -1,6 +1,6 @@
#lang br/quicklang
(require brag/support "grammar.rkt")
(provide (all-defined-out) #%module-begin)
(provide top fun app s-or-d p-or-q)
(module+ reader
(provide read-syntax))
@ -8,34 +8,36 @@
(define-lex-abbrev reserved-toks
(:or "fun" "(" ")" "=" "+" "*" "/" "-" ","))
(define tokenize
(lexer
[(:or (from/to "//" "\n") (from/to "/*" "*/")) (token 'COMMENT #:skip? #t)]
[whitespace (tokenize input-port)]
(define-lex-abbrev digits (char-set "0123456789"))
(define tokenize-1
(lexer-srcloc
[whitespace (token lexeme #:skip? #t)]
[(:or (from/stop-before "#" "\n")
(from/to "/*" "*/")) (token 'COMMENT #:skip? #t)]
[reserved-toks lexeme]
[alphabetic (token 'ID (string->symbol lexeme))]
[(:+ (char-set "0123456789")) (token 'INT (string->number lexeme))]))
[(:seq alphabetic (:* (:or alphabetic) digits)) (token 'ID (string->symbol lexeme))]
[(:seq (:? "-") (:+ digits)) (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 (fun VAR (ARGVAR ...) EXPR)
#'(define (VAR ARGVAR ...) EXPR))
(define-macro-cases sum
(define-macro-cases s-or-d
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro-cases product
[(_ LEFT OP-STR RIGHT)
(with-syntax ([OP (string->symbol (syntax->datum #'OP-STR))])
#'(OP LEFT RIGHT))]
(define-macro-cases p-or-q
[(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)]
[(_ 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))))
(define parse-tree (parse src (λ () (tokenize-1 ip))))
(strip-context
(with-syntax ([PT parse-tree])
#'(module mod-name precalc-demo

@ -1,15 +1,15 @@
#lang precalc-demo
fun f(x, y, z) = x + x + x * (y + y) + y * z - z - z
fun g(z) = f(z, z, z) // line comment
g(10) // 300
fun g42(z) = f(z, z, z) # line comment
g42(-10) # = 300
fun h() = g(10)
h() // also 300
fun h() = g42(10)
h() # = 300
fun k(x) = x / 10 / 10 / (x / x)
k(h()) // 3
k(10 * (15 + 3 * 5)) // 3
k(h()) # = 3
k(-10 * (15 + 3 * 5)) # = -3
/*
multiline comment

Loading…
Cancel
Save