[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)
#:literals (honu-=)
[(_ 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
#t)])))
@ -75,7 +76,10 @@
#:literals (else honu-then)
[(_ condition:honu-expression honu-then true:honu-expression else false:honu-expression . rest)
(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
#f)])))

View File

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

View File

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

View File

@ -38,7 +38,8 @@
;; (printf "output '~a'\n" stuff)
(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))
(when (not (same? 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))
(test
"basic numbers"
@input{
5
6
@ -57,6 +59,7 @@
})
(test
"basic math"
@input{
1 + 1
}
@ -65,6 +68,7 @@
})
(test
"function call"
@input{
foo(x){
x * 2
@ -76,6 +80,7 @@
})
(test
"cond"
@input{
var n = 5;
cond
@ -87,6 +92,7 @@
})
(test
"if"
@input{
if 2 > 1 then
1
@ -98,6 +104,7 @@
})
(test
"list comprehension"
@input{
[x + 1: x <- [1, 2, 3]];
[x + y: x <- [1, 2, 3], y <- [4, 5, 6]]