import Debug, Global, Lazy, Scope
export Function, new_function

Function : set of Entity := {}
formals : map from Function to list of Entity
body : map from Function to Lazy
enclosing_scope : map from Function to Scope

func new_function(f_ids : list of Entity, f_body : Lazy) -> Function
  result := new(Function)
  formals(result) := f_ids
  body(result) := f_body
  enclosing_scope(result) := last(scope.Stack)
  
func call(self; params : list of Entity) -> Entity
  scope.Stack +:= [new_scope(enclosing_scope(self))]
  for i in [1..#formals(self)]
    junk := assign(formals(self)(i), params(i))
  loop
  result := value(body(self))
  if result = Undefined
    debug.Message(`Function did not return a value')
    debug.Interact
  end
  scope.Stack := trim_right(scope.Stack)
  Must_return := False
