public bool Equal(IScheminType type) { if (type == this) { return true; } return false; }
public ScheminLambda(ScheminPair definition, Environment closure) { this.Arguments = definition.Car; ScheminPair def = definition.ListCdr(); def = def.Cons(new ScheminPrimitive("begin")); this.Definition = def; this.Closure = closure; }
public void AddBinding(ScheminAtom symbol, IScheminType type) { if (bindings.ContainsKey(symbol.Name)) { bindings[symbol.Name] = type; } else { bindings.Add(symbol.Name, type); } }
public bool Equals(IScheminType type) { if ((type as ScheminLambda) != null) { if (type == this) { return true; } } return false; }
public IScheminType ExpandAll(IScheminType ast) { bool modified = false; IScheminType expanded = Expand(ast, ref modified); while (modified == true) { modified = false; expanded = Expand(expanded, ref modified); } return expanded; }
private IScheminType Expand(IScheminType ast, ref bool modified) { if ((ast as ScheminPair) != null && ((ScheminPair) ast).Proper) { ScheminPair astPair = (ScheminPair) ast; if ((astPair.Car as ScheminAtom) != null) { ScheminAtom functionPosition = (ScheminAtom) astPair.Car; IScheminType bound = null; if (functionPosition.Closed) bound = functionPosition.Closure.GetValue(functionPosition); else bound = this.eval.GlobalEnv.GetValue(functionPosition); if ((bound as ScheminRewriter) != null) { ScheminRewriter rewriter = (ScheminRewriter)bound; ScheminPair macroCall = rewriter.Rewrite(astPair); IScheminType expanded = this.eval.EvaluateInternal(macroCall); modified = true; return expanded; } else if ((bound as ScheminPrimitive) != null) { ScheminPrimitive boundPrim = (ScheminPrimitive)bound; if (boundPrim.Definition == PrimitiveFactory.Get("define-rewriter")) { modified = true; return this.eval.EvaluateInternal(astPair); } else if (boundPrim.Name == "quote" || boundPrim.Name == "quasiquote") { return astPair; } } } ScheminPair rewritten = new ScheminPair(); foreach (IScheminType type in astPair) { rewritten = rewritten.Append(Expand(type, ref modified)); } return rewritten; } else { return ast; } }
public bool Equals(IScheminType type) { if (this.GetType() != type.GetType()) { return false; } ScheminDecimal temp = (ScheminDecimal) type; if (this.Value == temp.Value) { return true; } return false; }
public bool Equals(IScheminType type) { if ((type as ScheminVector) == null) { return false; } ScheminVector temp = (ScheminVector) type; if (this.List == temp.List) { return true; } return false; }
public IScheminType EvalAtom(IScheminType ast, Environment env) { ScheminAtom temp = (ScheminAtom) ast; IScheminType bound = null; if (temp.Closed) bound = temp.Closure.GetValue(temp); else bound = env.GetValue(temp); if (bound == null) throw new UnboundAtomException(string.Format("Unbound atom: {0}", temp)); return bound; }
public bool Equivalent(IScheminType type) { if ((type as ScheminAtom) == null) { return false; } ScheminAtom temp = (ScheminAtom) type; if (this.Name == temp.Name) { return true; } return false; }
public bool Equivalent(IScheminType type) { if ((type as ScheminString) == null) { return false; } ScheminString temp = (ScheminString) type; if (this.Value == temp.Value) { return true; } return false; }
public ScheminLambda(ScheminList definition, Environment closure) { this.Arguments = definition.Car(); if (definition.Cdr().Length == 1) { this.Definition = definition.Cdr().Car(); } else { ScheminList def = definition.Cdr(); def.Cons(new ScheminPrimitive("begin")); this.Definition = def; } this.Closure = closure; }
private bool EqualsHelper(IScheminType first, IScheminType second) { if ((first as ScheminPair) != null && (second as ScheminPair) != null) { ScheminPair firstPair = (ScheminPair) first; ScheminPair secondPair = (ScheminPair) second; if (firstPair.Length != secondPair.Length) return false; for (int i = 0; i < firstPair.Length; i++) { if (!EqualsHelper(firstPair.ElementAt(i), secondPair.ElementAt(i))) return false; } return true; } else if ((first as ScheminVector) != null && (second as ScheminVector) != null) { ScheminVector firstVec = (ScheminVector) first; ScheminVector secondVec = (ScheminVector) second; if (firstVec.List.Count != secondVec.List.Count) return false; for (int i = 0; i < firstVec.List.Count; i++) { if (!EqualsHelper(firstVec.List[i], secondVec.List[i])) return false; } return true; } return first.Equivalent(second); }
private ScheminList CombineStackFrame(ScheminList before, ScheminList after, IScheminType result) { ScheminList complete = new ScheminList(); complete.UnQuote(); if (before != null && !before.Empty) { complete.Append(before.Head); var restBefore = before.Rest; while (restBefore != null) { complete.Append(restBefore.Head); restBefore = restBefore.Rest; } } if (result != null) { complete.Append(result); } if (after != null && !after.Empty) { complete.Append(after.Head); var restAfter = after.Rest; while (restAfter != null) { complete.Append(restAfter.Head); restAfter = restAfter.Rest; } } return complete; }
public bool Equivalent(IScheminType type) { return Equal(type); }
public StackFrame() { this.Evaluated = new ScheminPair(); this.Unevaluated = new ScheminPair(); }
public StackFrame(StackFrame original) { this.Evaluated = original.Evaluated; this.Unevaluated = original.Unevaluated; this.CurrentEnv = original.CurrentEnv; }
private IScheminType EvalAtom(IScheminType ast, Environment env) { ScheminAtom temp = (ScheminAtom) ast; IScheminType bound = env.GetValue(temp); if (bound == null) { throw new UnboundAtomException(string.Format("Unbound atom: {0}", temp)); } return bound; }
public IScheminType EvaluateInternal(IScheminType ast) { StackFrame start = new StackFrame(); start.Unevaluated = ast; start.CurrentEnv = this.GlobalEnv; this.Stack.Clear(); this.Stack.Push(start); StackStart: while (this.Stack.Count > 0) { StackFrame current = new StackFrame(this.Stack.Pop()); Environment currentEnv = current.CurrentEnv; IScheminType unevaled = current.Unevaluated; if ((unevaled as ScheminAtom) != null) { if (this.Stack.Count < 1) { return EvalAtom(unevaled, currentEnv); } else { StackFrame previous = this.Stack.Pop(); StackFrame combinedPrevious = new StackFrame(previous); combinedPrevious.Evaluated = ((ScheminPair)combinedPrevious.Evaluated).Append(EvalAtom(unevaled, currentEnv)); this.Stack.Push(combinedPrevious); continue; } } else if (IsEmptyList(current.Unevaluated) && IsEmptyList(current.Evaluated)) { if (this.Stack.Count < 1) { return unevaled; } else { StackFrame previous = this.Stack.Pop(); StackFrame combinedPrevious = new StackFrame(previous); combinedPrevious.Evaluated = ((ScheminPair)combinedPrevious.Evaluated).Append(unevaled); this.Stack.Push(combinedPrevious); continue; } } else if ((unevaled as ScheminPair) != null) { ScheminPair unevaluated = (ScheminPair) unevaled; ScheminPair evaluatedList = (ScheminPair) current.Evaluated; IScheminType function; int currentArg = 0; if (!evaluatedList.Empty) { function = evaluatedList.Car; currentArg += evaluatedList.Length; } else { function = unevaluated.Car; } ScheminPrimitive currentPrimitive = null; if ((function as ScheminAtom) != null) { IScheminType evaledFunction = EvalAtom(function, currentEnv); if ((evaledFunction as ScheminPrimitive) != null) currentPrimitive = (ScheminPrimitive) evaledFunction; } else if ((function as ScheminPrimitive) != null) { currentPrimitive = (ScheminPrimitive)function; } ScheminPair fullArgs = (ScheminPair)current.Evaluated; foreach (IScheminType restArg in (ScheminPair) unevaluated) { fullArgs = fullArgs.Append(restArg); } ScheminPair evaluated = new ScheminPair(); while (!unevaluated.Empty) { IScheminType type = unevaluated.Car; if (currentPrimitive != null) { if ((!EvaluateNextArg(currentPrimitive, currentArg, fullArgs.ListCdr()) && currentArg > 0)) { evaluated = evaluated.Append(type); unevaluated = unevaluated.ListCdr(); currentArg++; continue; } } if ((type as ScheminAtom) != null) { IScheminType atomResult = EvalAtom(type, currentEnv); evaluated = evaluated.Append(atomResult); } else if ((type as ScheminPair) != null) { ScheminPair tempList = (ScheminPair)type; if (tempList.Empty) { evaluated = evaluated.Append(type); unevaluated = unevaluated.ListCdr(); currentArg++; continue; } StackFrame next = new StackFrame(); next.Unevaluated = tempList; next.CurrentEnv = currentEnv; StackFrame newSublist = new StackFrame(current); ScheminPair doneArgs = (ScheminPair)newSublist.Evaluated; foreach (IScheminType evaled in evaluated) { doneArgs = doneArgs.Append(evaled); } newSublist.Evaluated = doneArgs; newSublist.Unevaluated = unevaluated.ListCdr(); this.Stack.Push(newSublist); this.Stack.Push(next); goto StackStart; } else { evaluated = evaluated.Append(type); } unevaluated = unevaluated.ListCdr(); currentArg++; } foreach (IScheminType type in evaluated) { evaluatedList = evaluatedList.Append(type); } current.Evaluated = evaluatedList; current.Unevaluated = unevaluated; IScheminType waiting = evaluatedList.Car; if ((waiting as ScheminAtom) != null) { waiting = EvalAtom(waiting, currentEnv); } if ((waiting as ScheminPrimitive) != null) { ScheminPrimitive prim = (ScheminPrimitive)waiting; this.Stack.Push(current); IScheminType result = EvaluatePrimitive((ScheminPrimitive)waiting, evaluatedList.ListCdr(), currentEnv); this.Stack.Pop(); if (prim.Rewriter) { StackFrame rewritten = new StackFrame(current); rewritten.Unevaluated = result; rewritten.Evaluated = new ScheminPair(); this.Stack.Push(rewritten); continue; } if (this.Stack.Count < 1) { return result; } else { StackFrame previous = this.Stack.Pop(); StackFrame combinedPrevious = new StackFrame(previous); ScheminPair previousDone = (ScheminPair)combinedPrevious.Evaluated; previousDone = previousDone.Append(result); combinedPrevious.Evaluated = previousDone; this.Stack.Push(combinedPrevious); continue; } } else if ((waiting as ScheminLambda) != null) { ScheminLambda lam = (ScheminLambda)waiting; StackFrame next = new StackFrame(); next.Unevaluated = lam.Definition; next.CurrentEnv = lam.MakeEnvironment(evaluatedList.ListCdr(), this); this.Stack.Push(next); continue; } else if ((waiting as ScheminContinuation) != null) { ScheminContinuation con = (ScheminContinuation) waiting; this.Stack = new Stack<StackFrame>(con.PreviousStack); StackFrame continuationStart = new StackFrame(this.Stack.Pop()); if (this.Stack.Count < 1) { return evaluatedList.ListCdr().Car; } else { StackFrame previous = this.Stack.Pop(); StackFrame combinedPrevious = new StackFrame(previous); ScheminPair previousDone = (ScheminPair) combinedPrevious.Evaluated; previousDone = previousDone.Append(evaluatedList.ListCdr().Car); combinedPrevious.Evaluated = previousDone; this.Stack.Push(combinedPrevious); continue; } } else { throw new InvalidOperationException("Non-function in function position: " + waiting.ToString()); } } else { if (this.Stack.Count < 1) { return unevaled; } else { StackFrame previous = this.Stack.Pop(); StackFrame combinedPrevious = new StackFrame(previous); combinedPrevious.Evaluated = ((ScheminPair)combinedPrevious.Evaluated).Append(unevaled); this.Stack.Push(combinedPrevious); continue; } } } throw new Exception("Control escaped evaluator"); }
public bool IsEmptyList(IScheminType type) { if ((type as ScheminPair) != null) { ScheminPair temp = (ScheminPair) type; if (temp.Empty == true) { return true; } } return false; }
public IScheminType EvaluateInternal(IScheminType ast) { if ((ast as ScheminAtom) != null) { return EvalAtom(ast, this.GlobalEnv); } else if ((ast as ScheminList) != null) { return EvaluateList((ScheminList) ast); } else { return ast; } }
private bool IsEmptyList(IScheminType type) { if (type.GetType() == typeof(ScheminList)) { ScheminList temp = (ScheminList) type; if (temp.Empty == true) { return true; } } return false; }