   # types: typeEnumaration ctx
   errors: errors
 
-compileModule = (moduleName = '@unnamed') ->
+compileModuleWithDependencies = (moduleName = '@unnamed') ->
   js: library + immutable + (listOfLines map lookupJs, (setToArray runtimeDependencies moduleName))
 
 compileExpression = (source, moduleName = '@unnamed') ->
+  {js} = parsed = parseExpression source, moduleName
+  extend parsed,
+    (js:  library + immutable + (listOfLines map lookupJs, (setToArray runtimeDependencies moduleName)) + '\n;' + js)
+
+parseTopLevel = (source, moduleName = '@unnamed') ->
   ast = (astFromSource "(#{source})", -1, -1)
-  if _empty _validTerms ast
+  parseWith ast, ast, topLevel, moduleName
+
+parseExpression = (source, moduleName = '@unnamed') ->
+  ast = (astFromSource "(#{source})", -1, -1)
+  [expression] = _terms ast
+  compilationFn = (topLevelExpressionInModule importsFor moduleDependencies moduleName)
+  parseWith ast, expression, compilationFn, moduleName
+
+parseWith = (originalAst, ast, compilationFn, moduleName) ->
+  if _empty _validTerms originalAst
     {
-      ast: ast
+      ast: originalAst
       js: ''
     }
   else
     {modules, ctx} = contextWithDependencies moduleDependencies moduleName
-    [expression] = _terms ast
-    compilationFn = (topLevelExpressionInModule importsFor moduleDependencies moduleName)
-    {js} = compileCtxAstToJs compilationFn, ctx, expression
+    {js} = compileCtxAstToJs compilationFn, ctx, ast
     errors = checkTypes ctx
-    (finalizeTypes ctx, expression)
-    js: library + immutable + (listOfLines map lookupJs, (setToArray runtimeDependencies moduleName)) + '\n;' + js
-    ast: ast
+    (finalizeTypes ctx, ast)
+    ast: originalAst
     errors: errors
     malformed: ctx.isMalformed
+    js: js
 
 expandCall = (moduleName, call) ->
   {modules, ctx} = contextWithDependencies moduleDependencies moduleName

__ = (fna, fnb) ->
  (x) -> fna fnb x

# end of Utils

# Unit tests
test = (testName, teaSource, result) ->

  {request, ast, ir, js} = compileCtxAstToJs compilationFn, ctx, (astFromSource "(#{source})", -1, -1)
  if request
    if not allInjected request, requiredMap
      return compileModuleTopLevel source, moduleName, request
    else
      {js} = compileCtxIrToJs ctx, ir
  errors = checkTypes ctx
