(module hoist mzscheme
(require (planet "contract-utils.ss" ("cobbe" "contract-utils.plt" 1 0))
(all-except (planet "list.ss" ("dherman" "list.plt" 1 0)) any)
(lib "contract.ss")
(lib "match.ss")
(lib "struct.ss")
"../syntax/ast.ss"
"../syntax/token.ss")
(define-struct (FunctionDeclaration/hoisted FunctionDeclaration) (functions variables))
(define-struct (FunctionExpression/hoisted FunctionExpression) (functions variables))
(define-struct (BlockStatement/hoisted BlockStatement) (functions variables))
(define-struct hoisting (functions vars let-functions let-vars) #f)
(define null-hoisting (make-hoisting null null null null))
(define (append-hoisting . hs)
(match hs
[(($ hoisting funss varss let-funss let-varss) ...)
(make-hoisting (apply append funss)
(apply append varss)
(apply append let-funss)
(apply append let-varss))]))
(define (cons-function-hoisting fun h)
(copy-struct hoisting h [hoisting-functions (cons fun (hoisting-functions h))]))
(define (cons-var-hoisting var h)
(copy-struct hoisting h [hoisting-vars (cons var (hoisting-vars h))]))
(define (cons-let-function-hoisting fun h)
(copy-struct hoisting h [hoisting-let-functions (cons fun (hoisting-let-functions h))]))
(define (cons-let-var-hoisting var h)
(copy-struct hoisting h [hoisting-let-vars (cons var (hoisting-let-vars h))]))
(define (continuation/c c)
(hoisting? c . -> . any))
(define-syntax provide/contract/k
(syntax-rules (->k)
[(_ [name (domain . ->k . range)] ...)
(provide/contract
[name (domain (continuation/c range) . -> . any)]
...)]))
(define (unique-vars funs . varss)
(lset-difference Identifier=?
(delete-duplicates (apply append varss) Identifier=?)
(map FunctionDeclaration-name funs)))
(define (hoist-function-declaration decl)
(match decl
[($ FunctionDeclaration location name args body)
(hoist-source-elements body
(lambda (h stmts)
(match h
[($ hoisting funs vars let-funs let-vars)
(make-FunctionDeclaration/hoisted location name args stmts funs (unique-vars funs vars let-vars))])))]))
(define (hoist-script elts)
(hoist-source-elements elts
(lambda (h stmts)
(match h
[($ hoisting funs vars let-funs let-vars)
(values funs (unique-vars funs vars let-vars) stmts)]))))
(define (map-k hoist1 elts k)
(if (null? elts)
(k null-hoisting null)
(hoist1 (car elts)
(lambda (hoisting1 result)
(map-k hoist1 (cdr elts)
(lambda (hoisting2 results)
(k (append-hoisting hoisting1 hoisting2)
(cons result results))))))))
(define (filter-map-k hoist1 elts k)
(if (null? elts)
(k null-hoisting null)
(hoist1 (car elts)
(lambda (hoisting1 result)
(filter-map-k hoist1 (cdr elts)
(lambda (hoisting2 results)
(k (append-hoisting hoisting1 hoisting2)
(if result (cons result results) results))))))))
(define (append-map-k hoist1 elts k)
(if (null? elts)
(k null-hoisting null)
(hoist1 (car elts)
(lambda (hoisting1 results1)
(append-map-k hoist1 (cdr elts)
(lambda (hoisting2 results2)
(k (append-hoisting hoisting1 hoisting2)
(append results1 results2))))))))
(define (hoist-source-elements elts k)
(map-k hoist-source-element elts k))
(define (hoist-expressions exprs k)
(map-k hoist-expression exprs k))
(define (hoist-optional-expression expr k)
(if (not expr)
(k null-hoisting #f)
(hoist-expression expr k)))
(define (hoist-optional-expressions exprs k)
(map-k hoist-optional-expression exprs k))
(define (hoist-substatements stmts k)
(filter-map-k hoist-substatement stmts k))
(define (hoist-variable-bindings decls k)
(filter-map-k hoist-variable-binding decls k))
(define (hoist-case-clauses cases k)
(map-k hoist-case-clause cases k))
(define (hoist-catch-clauses catches k)
(map-k hoist-catch-clause catches k))
(define (hoist-source-element src k)
(if (Declaration? src)
(hoist-declaration src k)
(hoist-statement src k)))
(define (hoist-declaration decl0 k)
(cond
[(FunctionDeclaration? decl0)
(k (cons-function-hoisting (hoist-function-declaration decl0) null-hoisting)
(make-EmptyStatement (Term-location decl0)))]
[(VariableDeclaration? decl0)
(hoist-variable-declaration decl0
(lambda (h expr)
(k h (make-ExpressionStatement (Term-location decl0) expr))))]))
(define (hoist-variable-declaration decl0 k)
(match decl0
[($ VariableDeclaration loc bindings)
(hoist-variable-bindings bindings
(lambda (h exprs)
(k h (if (and (pair? exprs)
(null? (cdr exprs)))
(car exprs)
(make-ListExpression loc exprs)))))]))
(define (hoist-substatement src0 k)
(if (FunctionDeclaration? src0)
(k (cons-function-hoisting (hoist-function-declaration src0) null-hoisting) #f)
(hoist-statement src0 k)))
(define (hoist-variable-binding decl k)
(match decl
[($ VariableInitializer loc id #f)
(k (cons-var-hoisting id null-hoisting) #f)]
[($ VariableInitializer loc id init)
(hoist-expression init
(lambda (h init)
(k (cons-var-hoisting id h)
(make-AssignmentExpression loc (make-VarReference (Term-location id) id) '= init))))]))
(define (hoist-case-clause case k)
(match case
[($ CaseClause loc #f answer)
(hoist-substatements answer
(lambda (h answer)
(k h (make-CaseClause loc #f answer))))]
[($ CaseClause loc question answer)
(hoist-expression question
(lambda (h1 question)
(hoist-substatements answer
(lambda (h2 answer)
(k (append-hoisting h1 h2)
(make-CaseClause loc question answer))))))]))
(define (hoist-catch-clause catch k)
(match catch
[($ CatchClause loc id body0)
(hoist-statement body0
(lambda (h body)
(k h (make-CatchClause loc id body))))]))
(define (to-location x)
(cond
[(not x) #f]
[(position? x) x]
[else (ast-location x)]))
(define (optional-statement->statement stmt loc)
(or stmt (make-EmptyStatement (to-location loc))))
(define (hoist-statement stmt k)
(match stmt
[($ BlockStatement loc stmts)
(hoist-substatements stmts
(lambda (h stmts)
(k h (make-BlockStatement loc stmts))))]
[($ ExpressionStatement loc expr)
(hoist-expression expr
(lambda (h expr)
(k h (make-ExpressionStatement loc expr))))]
[($ IfStatement loc test consequent0 alternate0)
(hoist-expression test
(lambda (h1 test)
(hoist-substatement consequent0
(lambda (h2 consequent)
(if alternate0
(hoist-substatement alternate0
(lambda (h3 alternate)
(k (append-hoisting h1 h2 h3)
(make-IfStatement loc
test
(optional-statement->statement consequent consequent0)
alternate))))
(k (append-hoisting h1 h2)
(make-IfStatement loc test (optional-statement->statement consequent consequent0) #f)))))))]
[($ DoWhileStatement loc body0 test)
(hoist-substatement body0
(lambda (h1 body)
(hoist-expression test
(lambda (h2 test)
(k (append-hoisting h1 h2)
(make-DoWhileStatement loc (optional-statement->statement body body0) test))))))]
[($ WhileStatement loc test body0)
(hoist-expression test
(lambda (h1 test)
(hoist-substatement body0
(lambda (h2 body)
(k (append-hoisting h1 h2)
(make-WhileStatement loc test (optional-statement->statement body body0)))))))]
[($ ForStatement loc init test incr body0)
(let ([hoist (if (VariableDeclaration? init)
hoist-variable-declaration
hoist-optional-expression)])
(hoist init
(lambda (h1 init)
(hoist-optional-expression test
(lambda (h2 test)
(hoist-optional-expression incr
(lambda (h3 incr)
(hoist-substatement body0
(lambda (h4 body)
(k (append-hoisting h1 h2 h3 h4)
(make-ForStatement loc init test incr (optional-statement->statement body body0))))))))))))]
[($ ForInStatement loc (and lhs (? Expression?)) container body0)
(hoist-expression lhs
(lambda (h1 lhs)
(hoist-expression container
(lambda (h2 container)
(hoist-substatement body0
(lambda (h3 body)
(k (append-hoisting h1 h2 h3)
(make-ForInStatement loc lhs container (optional-statement->statement body body0)))))))))]
[($ ForInStatement loc ($ VariableDeclaration _ ($ VariableInitializer v-loc id #f)) container body0)
(hoist-expression container
(lambda (h1 container)
(hoist-substatement body0
(lambda (h2 body)
(k (cons-var-hoisting id (append-hoisting h1 h2))
(make-ForInStatement loc
(make-VarReference v-loc id)
container
(optional-statement->statement body body0)))))))]
[($ ReturnStatement loc (and expr (? Expression?)))
(hoist-expression expr
(lambda (h expr)
(k h (make-ReturnStatement loc expr))))]
[($ WithStatement loc test body0)
(hoist-expression test
(lambda (h1 test)
(hoist-substatement body0
(lambda (h2 body)
(k (append-hoisting h1 h2)
(make-WithStatement loc test (optional-statement->statement body body0)))))))]
[($ SwitchStatement loc expr cases)
(hoist-expression expr
(lambda (h1 expr)
(hoist-case-clauses cases
(lambda (h2 cases)
(k (append-hoisting h1 h2)
(make-SwitchStatement loc expr cases))))))]
[($ LabelledStatement loc label stmt0)
(hoist-substatement stmt0
(lambda (h stmt)
(k h (make-LabelledStatement loc label (optional-statement->statement stmt stmt0)))))]
[($ ThrowStatement loc expr)
(hoist-expression expr
(lambda (h expr)
(k h (make-ThrowStatement loc expr))))]
[($ TryStatement loc body0 catch0 finally0)
(hoist-statement body0
(lambda (h1 body)
(hoist-catch-clauses catch0
(lambda (h2 catch)
(if finally0
(hoist-statement finally0
(lambda (h3 finally)
(k (append-hoisting h1 h2 h3)
(make-TryStatement loc body catch finally))))
(k (append-hoisting h1 h2)
(make-TryStatement loc body catch #f)))))))]
[_ (k null-hoisting stmt)]))
(define (hoist-expression expr k)
(match expr
[($ ArrayLiteral loc elts)
(hoist-optional-expressions elts
(lambda (h elts)
(k h (make-ArrayLiteral loc elts))))]
[($ ObjectLiteral loc ([props . vals] ...))
(hoist-expressions vals
(lambda (h vals)
(k h (make-ObjectLiteral loc (map cons props vals)))))]
[($ BracketReference loc container key)
(hoist-expression container
(lambda (h1 container)
(hoist-expression key
(lambda (h2 key)
(k (append-hoisting h1 h2)
(make-BracketReference loc container key))))))]
[($ DotReference loc container id)
(hoist-expression container
(lambda (h container)
(k h (make-DotReference loc container id))))]
[($ NewExpression loc constructor args)
(hoist-expression constructor
(lambda (h1 constructor)
(hoist-expressions args
(lambda (h2 args)
(k (append-hoisting h1 h2)
(make-NewExpression loc constructor args))))))]
[($ PostfixExpression loc expr op)
(hoist-expression expr
(lambda (h expr)
(k h (make-PostfixExpression loc expr op))))]
[($ PrefixExpression loc op expr)
(hoist-expression expr
(lambda (h expr)
(k h (make-PrefixExpression loc op expr))))]
[($ InfixExpression loc left op right)
(hoist-expression left
(lambda (h1 left)
(hoist-expression right
(lambda (h2 right)
(k (append-hoisting h1 h2)
(make-InfixExpression loc left op right))))))]
[($ ConditionalExpression loc test consequent alternate)
(hoist-expression test
(lambda (h1 test)
(hoist-expression consequent
(lambda (h2 consequent)
(hoist-expression alternate
(lambda (h3 alternate)
(k (append-hoisting h1 h2 h3)
(make-ConditionalExpression loc test consequent alternate))))))))]
[($ AssignmentExpression loc left op right)
(hoist-expression left
(lambda (h1 left)
(hoist-expression right
(lambda (h2 right)
(k (append-hoisting h1 h2)
(make-AssignmentExpression loc left op right))))))]
[($ FunctionExpression loc name args body)
(k null-hoisting (hoist-source-elements body
(lambda (h body)
(match h
[($ hoisting funs vars let-funs let-vars)
(make-FunctionExpression/hoisted loc name args body funs (unique-vars funs vars let-vars))]))))]
[($ CallExpression loc method args)
(hoist-expression method
(lambda (h1 method)
(hoist-expressions args
(lambda (h2 args)
(k (append-hoisting h1 h2)
(make-CallExpression loc method args))))))]
[($ ParenExpression loc expr)
(hoist-expression expr
(lambda (h expr)
(k h (make-ParenExpression loc expr))))]
[($ ListExpression loc exprs)
(hoist-expressions exprs
(lambda (h exprs)
(k h (make-ListExpression loc exprs))))]
[_ (k null-hoisting expr)]))
(provide/contract
[continuation/c ((union flat-contract? predicate/c) . -> . contract?)])
(provide/contract/k
[hoist-source-elements ((listof SourceElement?) . ->k . (listof Statement?))]
[hoist-declaration (Declaration? . ->k . Statement?)]
[hoist-variable-declaration (VariableDeclaration? . ->k . Expression?)]
[hoist-expressions ((listof Expression?) . ->k . (listof Expression?))]
[hoist-optional-expressions ((listof (optional/c Expression?)) . ->k . (listof (optional/c Expression?)))]
[hoist-source-element (SourceElement? . ->k . Statement?)]
[hoist-statement (Statement? . ->k . Statement?)]
[hoist-expression (Expression? . ->k . Expression?)]
[hoist-substatement (SubStatement? . ->k . (optional/c Statement?))]
[hoist-substatements ((listof SubStatement?) . ->k . (listof Statement?))]
[hoist-variable-binding (VariableInitializer? . ->k . (optional/c Expression?))]
[hoist-variable-bindings ((listof VariableInitializer?) . ->k . (listof Expression?))]
[hoist-case-clause (CaseClause? . ->k . CaseClause?)]
[hoist-case-clauses ((listof CaseClause?) . ->k . (listof CaseClause?))]
[hoist-catch-clause (CatchClause? . ->k . CatchClause?)]
[hoist-catch-clauses ((listof CatchClause?) . ->k . (listof CatchClause?))])
(provide/contract
[hoist-function-declaration (FunctionDeclaration? . -> . FunctionDeclaration/hoisted?)]
[hoist-script ((listof SourceElement?) . -> . (values (listof FunctionDeclaration/hoisted?)
(listof Identifier?)
(listof Statement?)))])
(provide/contract
(struct (FunctionDeclaration/hoisted FunctionDeclaration) ([location (optional/c region?)]
[name Identifier?]
[args (listof Identifier?)]
[body (listof Statement?)]
[functions (listof FunctionDeclaration/hoisted?)]
[vars (listof Identifier?)]))
(struct (FunctionExpression/hoisted FunctionExpression) ([location (optional/c region?)]
[name (optional/c Identifier?)]
[args (listof Identifier?)]
[body (listof Statement?)]
[functions (listof FunctionDeclaration/hoisted?)]
[vars (listof Identifier?)]))
(struct (LetExpression/hoisted LetExpression) ([location (optional/c region?)]
[bindings (listof VariableDeclaration?)]
[body Statement?]
[functions (listof FunctionDeclaration/hoisted?)]
[vars (listof Identifier?)]))
))