I have the following function in Scheme (Using Dr. Racket), it's a vague translator from Scheme to Javascript.
I can't seem to figure out why #void is being printed.
I'm guessing it has something to do with the return value of fprintf, but I'm not sure how can this behavior be overcome.
Any ideas?
(define unparse->js
(lambda (ast output-port)
(cond ((def-exp? ast) (fprintf output-port "const ~a = ~a;" (unparse->js
(def-exp->var ast) output-port) (unparse->js (def-exp->val ast) output-port)))
((cexp? ast)
(cond ((num-exp? ast) (number->string (num-exp->val ast)))
((bool-exp? ast) (if (eq? (bool-exp->val ast) #t) "true" "false"))
((str-exp? ast) (str-exp->val ast))
((var-exp? ast) (symbol->string (var-exp->var ast)))
((literal-exp? ast) (list 'quote (literal-exp->val ast)))
((proc-exp? ast) (fprintf output-port "(~a) => { ~a }"
(string-join
(map (lambda (b)
(unparse->js b output-port)
)
(proc-exp->params ast)
)
",")
(string-join
(map (lambda (ast)
(unparse->js ast output-port)
)
(proc-exp->body ast))
";")))
((if-exp? ast) (fprintf output-port "~a ? ~a : ~a"
(unparse->js (if-exp->test ast) output-port)
(unparse->js (if-exp->then ast) output-port)
(unparse->js (if-exp->else ast) output-port)))
((let-exp? ast) (fprintf output-port "let ~a; ~a;"
(string-join
(map (lambda (b)
(fprintf output-port "~a = ~a"
(unparse->js (binding->var b) output-port)
(unparse->js (binding->val b) output-port)
)
)
(let-exp->bindings ast)
)
",")
(string-join
(map (lambda (b)
(unparse->js b output-port)
)
(let-exp->body ast))
";")
)
)
((app-exp? ast) (fprintf output-port "~a(~a)"
(unparse->js (app-exp->rator ast) output-port)
(string-join
(map (
lambda (rand)
(unparse->js rand output-port)
)
(app-exp->rands ast)
)
",")
)
)
(else (error "Unknown exp type: " ast))))
(else (error "Unknown exp type: " ast)))
)
)
I'm calling it this way:
(unparse->js (parse '(if (eq? 5 3) 1 0)) (current-output-port))
The expected result is:
eq?(5,3) ? 1 : 0
However, the result I'm getting is:
eq?(5,3) #< void > ? 1 : 0