precalc
parent
b13ae90903
commit
d05ac2ac6a
@ -1,9 +1,9 @@
|
||||
#lang brag
|
||||
|
||||
top : (func-def | func-app)*
|
||||
func-def : /"fun" var /"(" boundvars /")" /"=" expr
|
||||
/boundvars : var [/"," var]
|
||||
expr : var "+" var | func-app
|
||||
func-app : var /"(" arg [/"," arg] /")"
|
||||
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
|
||||
@arg : var | INT
|
@ -0,0 +1,11 @@
|
||||
#lang brag
|
||||
|
||||
top : (func-def | expr)*
|
||||
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 /"(" [value [/"," value]*] /")"
|
||||
@var : ID
|
@ -0,0 +1,43 @@
|
||||
#lang br/quicklang
|
||||
(require brag/support "grammar.rkt")
|
||||
(provide (all-defined-out) #%module-begin)
|
||||
|
||||
(module+ reader
|
||||
(provide read-syntax))
|
||||
|
||||
(define-lex-abbrev reserved-toks
|
||||
(:or "fun" "(" ")" "=" "+" "*" "/" "-" ","))
|
||||
|
||||
(define lex
|
||||
(lexer
|
||||
[(:or (from/to "//" "\n") (from/to "/*" "*/")) (token 'COMMENT #:skip? #t)]
|
||||
[whitespace (lex input-port)]
|
||||
[reserved-toks lexeme]
|
||||
[alphabetic (token 'ID (string->symbol lexeme))]
|
||||
[(:+ (char-set "0123456789")) (token 'INT (string->number lexeme))]))
|
||||
|
||||
(define-macro top #'begin)
|
||||
|
||||
(define-macro (func-def ID ARGIDS EXPR)
|
||||
#'(define ID (λ ARGIDS EXPR)))
|
||||
|
||||
(define-macro-cases sum
|
||||
[(_ 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))]
|
||||
[(_ OTHER) #'OTHER])
|
||||
|
||||
(define-macro func-app #'#%app)
|
||||
|
||||
(define (read-syntax src ip)
|
||||
(define token-thunk (λ () (lex ip)))
|
||||
(define parse-tree (parse token-thunk))
|
||||
(strip-context
|
||||
(with-syntax ([PT parse-tree])
|
||||
#'(module mod-name precalc-demo
|
||||
PT))))
|
@ -0,0 +1,18 @@
|
||||
#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 h() = g(10)
|
||||
h() // also 300
|
||||
|
||||
fun k(x) = x / 10 / 10 / (x / x)
|
||||
k(h()) // 3
|
||||
|
||||
36/6/2 // 3
|
||||
|
||||
/*
|
||||
multiline comment
|
||||
0 / 0 / 0
|
||||
*/
|
Loading…
Reference in New Issue