     {type: getType(new Type "func"), access: @func.label}
 
   compileFunc: (o) ->
-    code = "; TODO function body here"
-    ":#{@func.label}\n#{code}"
+    o = {namespace: o.namespace.createLocalNamespace()}
+    code = @body.compileStatement o
+    returnCode = "set pc, pop"
+    ":#{@func.label}\n#{code}\n#{returnCode}"
 
   children: ['params', 'body']
 

    value = @value.unwrapAll().compileExpression o
    variable = o.namespace.find name
    if @context is ":="
      throw SyntaxError "variable \"#{name}\" is already declared" if variable?
      variable = o.namespace.createVariable name, value.type
      variable.asm = ":#{variable.mangledName()} dat 0"
    else

  constructor: (@label, @code) ->

class Namespace
  constructor: (@parent) ->
    @names = {}
    @root = @parent?.root
  find: (name) ->
    @names[name] or @parent?.find name
  createLocalNamespace: ->
    new LocalNamespace this
  scopeNameMangle: ->
    return "" unless @parent?
    return @parent.scopeNameMangle() + @index + ".."
class LocalNamespace extends Namespace
  constructor: (args...) ->
    super(args...)
    createRootRedirect = (name) =>
      @[name] = (args...) ->
        @root[name](args...)
    createRootRedirect "createLiteralString"
    createRootRedirect "createFunc"
  createVariable: (name, type) ->
    variable = new Variable name, type
class GlobalNamespace extends Namespace
  constructor: ->
    super(null)
    @root = this
    @variables = []
    @funcs = []
    labelCount = 0
    @nextLabel = -> "_#{labelCount++}"
  createVariable: (name, type) ->
    variable = new Variable name, type, this
    @names[name] = variable
