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

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

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

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

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

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

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

@ -1,51 +1,27 @@
#lang pythonesque-demo
a = 3
b = 4
"escaped quote: middle \" and end \""
"escaped backslash: middle \\ and end \\"
"middle \" escaped quote"
"ending escaped quote\""
"middle \\ escaped backslash"
"ending escaped backslash\\"
def nothing():
42
def ft():
def fortytwo():
return 42
def gt(x, y):
return x > y
def eightyfour():
return 84
def noop():
return "double dedent here"
return "double dedent next"
nothing() # no output
def squaresum(x, y):
def add(c, d):
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"
if fortytwo() < eightyfour():
"left is less than right"
else:
print "a is not less than b"
"left is not less than right"
def bar(x, y):
return x > y
def last():
return 42
def noop():
return "double dedent here"
return "double dedent next"
Loading…
Cancel
Save