mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-15 16:57:35 +02:00
Added helper functions for SEXP/AST conversions
Added: ast:ast->sexp and ast:sexp->ast
This commit is contained in:
parent
e6d5de2702
commit
7dbfb64205
1 changed files with 56 additions and 0 deletions
|
@ -25,8 +25,14 @@
|
||||||
ast:set-lambda-has-cont!
|
ast:set-lambda-has-cont!
|
||||||
ast:get-next-lambda-id!
|
ast:get-next-lambda-id!
|
||||||
ast:ast->pp-sexp
|
ast:ast->pp-sexp
|
||||||
|
ast:ast->sexp
|
||||||
|
ast:sexp->ast
|
||||||
)
|
)
|
||||||
(begin
|
(begin
|
||||||
|
(define ast:ast->sexp ast->sexp)
|
||||||
|
|
||||||
|
(define ast:sexp->ast sexp->ast)
|
||||||
|
|
||||||
(define *lambda-id* 0)
|
(define *lambda-id* 0)
|
||||||
|
|
||||||
(define (ast:get-next-lambda-id!)
|
(define (ast:get-next-lambda-id!)
|
||||||
|
@ -78,4 +84,54 @@
|
||||||
((app? exp)
|
((app? exp)
|
||||||
(map ast:ast->pp-sexp exp))
|
(map ast:ast->pp-sexp exp))
|
||||||
(else exp)))
|
(else exp)))
|
||||||
|
|
||||||
|
;; Transform AST back to an equivalent sexp
|
||||||
|
(define (ast->sexp exp)
|
||||||
|
(cond
|
||||||
|
((ast:lambda? exp)
|
||||||
|
(let* ((id (ast:lambda-id exp))
|
||||||
|
(has-cont (ast:lambda-has-cont exp))
|
||||||
|
(sym 'lambda))
|
||||||
|
`(,sym ,(ast:lambda-args exp)
|
||||||
|
,@(map ast->sexp (ast:lambda-body exp)))))
|
||||||
|
((quote? exp) exp)
|
||||||
|
((const? exp) exp)
|
||||||
|
((ref? exp) exp)
|
||||||
|
((define? exp)
|
||||||
|
`(define ,(define->var exp)
|
||||||
|
,@(ast->sexp (define->exp exp))))
|
||||||
|
((set!? exp)
|
||||||
|
`(set! ,(set!->var exp)
|
||||||
|
,(ast->sexp (set!->exp exp))))
|
||||||
|
((if? exp)
|
||||||
|
`(if ,(ast->sexp (if->condition exp))
|
||||||
|
,(ast->sexp (if->then exp))
|
||||||
|
,(ast->sexp (if->else exp))))
|
||||||
|
((app? exp)
|
||||||
|
(map ast->sexp exp))
|
||||||
|
(else exp)))
|
||||||
|
|
||||||
|
;; Transform given SEXP into AST form.
|
||||||
|
(define (sexp->ast exp)
|
||||||
|
(cond
|
||||||
|
((lambda? exp)
|
||||||
|
(ast:make-lambda
|
||||||
|
(lambda->formals exp)
|
||||||
|
(map sexp->ast (lambda->exp exp))))
|
||||||
|
((quote? exp) exp)
|
||||||
|
((const? exp) exp)
|
||||||
|
((ref? exp) exp)
|
||||||
|
((define? exp)
|
||||||
|
`(define ,(define->var exp)
|
||||||
|
,@(sexp->ast (define->exp exp))))
|
||||||
|
((set!? exp)
|
||||||
|
`(set! ,(set!->var exp)
|
||||||
|
,(sexp->ast (set!->exp exp))))
|
||||||
|
((if? exp)
|
||||||
|
`(if ,(sexp->ast (if->condition exp))
|
||||||
|
,(sexp->ast (if->then exp))
|
||||||
|
,(sexp->ast (if->else exp))))
|
||||||
|
((app? exp)
|
||||||
|
(map sexp->ast exp))
|
||||||
|
(else exp)))
|
||||||
))
|
))
|
||||||
|
|
Loading…
Add table
Reference in a new issue