v6.3-exception
Matthew Butterick 6 years ago
parent d05ac2ac6a
commit 9d4292ac85

@ -18,7 +18,7 @@
(define-macro top #'begin) (define-macro top #'begin)
(define-macro (func-def VAR VARS EXPR) (define-macro (func-def VAR VARS EXPR)
#'(define VAR (λ VARS EXPR))) #'(define (VAR . VARS) EXPR))
(define-macro-cases expr (define-macro-cases expr
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)] [(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]

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

@ -18,8 +18,8 @@
(define-macro top #'begin) (define-macro top #'begin)
(define-macro (func-def ID ARGIDS EXPR) (define-macro (func-def VAR VARS EXPR)
#'(define ID (λ ARGIDS EXPR))) #'(define (VAR . VARS) EXPR))
(define-macro-cases sum (define-macro-cases sum
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)] [(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]

@ -9,8 +9,7 @@ h() // also 300
fun k(x) = x / 10 / 10 / (x / x) fun k(x) = x / 10 / 10 / (x / x)
k(h()) // 3 k(h()) // 3
k(10 * (15 + 3 * 5)) // 3
36/6/2 // 3
/* /*
multiline comment multiline comment

@ -1,19 +1,15 @@
#lang brag #lang brag
top : @statement* top : @statement*
statement : assignment | func-def | expr | return | for | if | print statement : func-def | expr | return | if
assignment : ID /"=" expr func-def : /"def" var /"(" ids /")" /":" @block
/ids : [var (/"," var)*]
block : /INDENT @statement* /DEDENT
@expr : comparison @expr : comparison
comparison : [comparison ("<" | ">")] sum comparison : [comparison "<"] value
sum : [sum ("+" | "-")] product @value : var | INTEGER | func-app | STRING
product : [product ("*" | "/")] value func-app : var /"(" @exprs /")"
@value : ID | INTEGER | func-app | STRING
func-app : ID /"(" @exprs /")"
exprs : [expr (/"," expr)*] exprs : [expr (/"," expr)*]
func-def : /"def" ID /"(" ids /")" /":" @block
/ids : [ID (/"," ID)*]
block : /INDENT @statement* /DEDENT
return : /"return" expr return : /"return" expr
for : /"for" ID /"in" expr /":" @block
if : /"if" expr /":" block [/"else" /":" block] if : /"if" expr /":" block [/"else" /":" block]
print : /"print" expr @var : ID

@ -1,15 +1,12 @@
#lang br/quicklang #lang br/quicklang
(require "grammar.rkt" brag/support racket/pretty racket/stxparam) (require "grammar.rkt" brag/support racket/stxparam)
(provide (except-out (all-from-out br/quicklang) for if print) (all-defined-out) pretty-print) (provide #%module-begin (all-defined-out))
(module+ reader (module+ reader
(provide read-syntax)) (provide read-syntax))
(define-lex-abbrev reserved-terms (define-lex-abbrev reserved-terms
(:or "=" "def" "(" ")" ":" "," (:or "def" "(" ")" ":" "," "return" "<" "if" "else"))
"return" "for" "in"
"+" "-" "*" "/" "<" ">" "\""
"if" "else" "print"))
(define-lex-abbrev indent (:: (:+ "\n") (:* " "))) (define-lex-abbrev indent (:: (:+ "\n") (:* " ")))
(define prev-indent 0) (define prev-indent 0)
@ -39,7 +36,7 @@
(set! prev-indent this-indent) (set! prev-indent this-indent)
tok)] tok)]
[(:+ whitespace) (token lexeme #:skip? #t)] [(:+ whitespace) (token lexeme #:skip? #t)]
[reserved-terms (token lexeme (string->symbol lexeme))] [reserved-terms lexeme]
[(:+ (:- (:or alphabetic punctuation) reserved-terms)) [(:+ (:- (:or alphabetic punctuation) reserved-terms))
(token 'ID (string->symbol lexeme))] (token 'ID (string->symbol lexeme))]
[(:+ (char-set "0123456789")) [(:+ (char-set "0123456789"))
@ -52,29 +49,20 @@
[else (inner-lex ip)])) [else (inner-lex ip)]))
(define-macro top #'begin) (define-macro top #'begin)
(define-macro (assignment ID EXPR)
#'(define ID EXPR))
(define-macro-cases comparison (define-macro-cases comparison
[(_ ARG) #'ARG] [(_ LEFT "<" RIGHT) #'(< LEFT RIGHT)]
[(_ LARG OP RARG) #'(OP LARG RARG)]) [(_ OTHER) #'OTHER])
(define-macro sum #'comparison) (define-macro (func-def VAR VARS STMT ...)
(define-macro product #'comparison) #'(define (VAR . VARS)
(define-macro (func-def ID ID-ARGS STMT ...)
#'(define (ID . ID-ARGS)
(let/cc return-cc (let/cc return-cc
(syntax-parameterize ([return (make-rename-transformer #'return-cc)]) (syntax-parameterize ([return (make-rename-transformer #'return-cc)])
STMT ... (void))))) STMT ... (void)))))
(define-syntax-parameter return (λ (stx) (error 'not-parameterized))) (define-syntax-parameter return (λ (stx) (error 'not-parameterized)))
(define-macro func-app #'#%app)
(provide (rename-out [my-for for])) (define-macro func-app #'#%app)
(define-macro (my-for ID EXPR . STMTS)
#'(for ([ID (in-list EXPR)])
. STMTS))
(define-macro block #'begin) (define-macro block #'begin)
@ -83,14 +71,9 @@
[(_ COND TBLOCK) #'(when COND TRUE-BLOCK)] [(_ COND TBLOCK) #'(when COND TRUE-BLOCK)]
[(_ COND TBLOCK FBLOCK) #'(if COND (let () TBLOCK) (let () FBLOCK))]) [(_ COND TBLOCK FBLOCK) #'(if COND (let () TBLOCK) (let () FBLOCK))])
(provide (rename-out [my-print print]))
(define-macro (my-print EXPR)
#'(display EXPR))
(define (read-syntax src ip) (define (read-syntax src ip)
(define parse-tree (parse (λ () (lex ip)))) (define parse-tree (parse (λ () (lex ip))))
(strip-context (strip-context
(with-syntax ([PT parse-tree]) (with-syntax ([PT parse-tree])
#'(module _ pythonesque-demo #'(module _ pythonesque-demo
#;(pretty-print 'PT)
PT)))) PT))))

@ -1,51 +1,27 @@
#lang pythonesque-demo #lang pythonesque-demo
a = 3 "escaped quote: middle \" and end \""
b = 4 "escaped backslash: middle \\ and end \\"
"middle \" escaped quote" def nothing():
"ending escaped quote\"" 42
"middle \\ escaped backslash"
"ending escaped backslash\\"
def ft(): def fortytwo():
return 42 return 42
def gt(x, y): def eightyfour():
return x > y return 84
def noop(): def noop():
return "double dedent here" return "double dedent next"
nothing() # no output
def squaresum(x, y): if fortytwo() < eightyfour():
def add(c, d): "left is less than right"
return c + d
return add(x, y) * add(x, y)
gt(a, b) # #f
squaresum(b, a) # 49
println(a)
expt(2, 4)
range(1, 5)
# keep indented example next to eof
for x in range(1, 5):
println(x * x)
def foo(x):
x
foo(42) # no return value
if a < b:
print "a is less than b"
else: else:
print "a is not less than b" "left is not less than right"
def bar(x, y): def last():
return x > y return 42
def noop(): def noop():
return "double dedent here" return "double dedent next"
Loading…
Cancel
Save