[honu] reparse honu expressions into racket expressions

This commit is contained in:
Jon Rafkind 2011-11-09 11:13:50 -07:00
parent 5aa6b0b06e
commit 8fe87bd2a9
4 changed files with 33 additions and 11 deletions

View File

@ -41,7 +41,8 @@
(syntax-parse code #:literal-sets (cruft) (syntax-parse code #:literal-sets (cruft)
#:literals (honu-=) #:literals (honu-=)
[(_ name:id honu-= one:honu-expression . rest) [(_ name:id honu-= one:honu-expression . rest)
(values #'(%racket (define name one.result)) (values (with-syntax ([one-parsed (parse-all #'one.result)])
#'(%racket (define name one-parsed)))
#'rest #'rest
#t)]))) #t)])))
@ -75,7 +76,10 @@
#:literals (else honu-then) #:literals (else honu-then)
[(_ condition:honu-expression honu-then true:honu-expression else false:honu-expression . rest) [(_ condition:honu-expression honu-then true:honu-expression else false:honu-expression . rest)
(values (values
#'(%racket-expression (if condition.result true.result false.result)) (with-syntax ([condition-parsed (parse-all #'condition.result)]
[true-parsed (parse-all #'true.result)]
[false-parsed (parse-all #'false.result)])
#'(%racket-expression (if condition-parsed true-parsed false-parsed)))
#'rest #'rest
#f)]))) #f)])))

View File

@ -228,10 +228,10 @@
(do-parse #'(rest ...) (do-parse #'(rest ...)
precedence left precedence left
#'racket))] #'racket))]
[(%racket-expression racket) [(%racket-expression racket rest ...)
(if current (if current
(values (left current) stream) (values (left current) stream)
(do-parse #'() (do-parse #'(rest ...)
precedence left precedence left
#'racket))] #'racket))]
[(head rest ...) [(head rest ...)
@ -304,9 +304,14 @@
[(#%brackets stuff ...) [(#%brackets stuff ...)
(syntax-parse #'(stuff ...) #:literal-sets (cruft) (syntax-parse #'(stuff ...) #:literal-sets (cruft)
[(work:honu-expression colon (~seq variable:id honu-<- list:honu-expression (~optional honu-comma)) ...) [(work:honu-expression colon (~seq variable:id honu-<- list:honu-expression (~optional honu-comma)) ...)
(define comprehension #'(for/list ([variable list.result] (define comprehension
...) (with-syntax ([(list-parsed ...) (map (lambda (list)
work.result)) (parse-all list))
(syntax->list #'(list.result ...)))]
[work-parsed (parse-all #'work.result)])
#'(for/list ([variable list-parsed]
...)
work-parsed)))
(if current (if current
(error 'parse "a list comprehension cannot follow an expression") (error 'parse "a list comprehension cannot follow an expression")
(do-parse #'(rest ...) precedence left comprehension))] (do-parse #'(rest ...) precedence left comprehension))]

View File

@ -17,8 +17,14 @@
[(_ (~seq clause:honu-expression colon body:honu-expression (~optional honu-comma)) ... [(_ (~seq clause:honu-expression colon body:honu-expression (~optional honu-comma)) ...
. rest) . rest)
(values (values
#'(%racket-expression (cond (with-syntax ([(clause-parsed ...) (map (lambda (clause)
[clause.result body.result] (parse-all clause))
...)) (syntax->list #'(clause.result ...)))]
[(body-parsed ...) (map (lambda (body)
(parse-all body))
(syntax->list #'(body.result ...)))])
#'(%racket-expression (cond
[clause-parsed body-parsed]
...)))
#'rest #'rest
#t)]))) #t)])))

View File

@ -38,7 +38,8 @@
;; (printf "output '~a'\n" stuff) ;; (printf "output '~a'\n" stuff)
(apply string-append "" (append stuff (list "\n")))) (apply string-append "" (append stuff (list "\n"))))
(define (test input output) (define (test name input output)
(printf "Running test ~a\n" name)
(define final (run-honu input)) (define final (run-honu input))
(when (not (same? final output)) (when (not (same? final output))
(printf "Not the same!\n'~a'\nvs\n'~a'\n" final output))) (printf "Not the same!\n'~a'\nvs\n'~a'\n" final output)))
@ -47,6 +48,7 @@
(apply string-append "#lang honu\n" stuff)) (apply string-append "#lang honu\n" stuff))
(test (test
"basic numbers"
@input{ @input{
5 5
6 6
@ -57,6 +59,7 @@
}) })
(test (test
"basic math"
@input{ @input{
1 + 1 1 + 1
} }
@ -65,6 +68,7 @@
}) })
(test (test
"function call"
@input{ @input{
foo(x){ foo(x){
x * 2 x * 2
@ -76,6 +80,7 @@
}) })
(test (test
"cond"
@input{ @input{
var n = 5; var n = 5;
cond cond
@ -87,6 +92,7 @@
}) })
(test (test
"if"
@input{ @input{
if 2 > 1 then if 2 > 1 then
1 1
@ -98,6 +104,7 @@
}) })
(test (test
"list comprehension"
@input{ @input{
[x + 1: x <- [1, 2, 3]]; [x + 1: x <- [1, 2, 3]];
[x + y: x <- [1, 2, 3], y <- [4, 5, 6]] [x + y: x <- [1, 2, 3], y <- [4, 5, 6]]