public static bool IsList(this ISExpression sexpr) { var c = sexpr as Cons; return(sexpr.IsNil() || (c != null && (c.Cdr.IsNil() || c.Cdr.IsList()))); }
protected static void CheckNotIllegalSymbol(ISExpression expr) { if (CompilerConstants.IllegalVariables.Contains(expr)) { throw new SchemeCompilerException("compiler", "name cannot be used as a variable", expr.ToString()); } }
public static ISExpression[] Parse(string input) { AntlrInputStream inputStream = new AntlrInputStream(input); SchemeLexer lexer = new SchemeLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); SchemeParser parser = new SchemeParser(commonTokenStream); var context = parser.body(); var visitor = new SchemeVisitor(); var expr = visitor.Visit(context); ISExpression[] arr; if (expr is ISExpression) { arr = new ISExpression[] { (ISExpression)expr } } ; else if (expr is ISExpression[]) { arr = (ISExpression[])expr; } else { throw new InvalidOperationException("parser error, unknown type: " + expr.GetType().Name); } return(arr); }
public static IEnumerable <ISExpression> Flatten(this ISExpression s) { var ss = new List <ISExpression>(); if (s.IsNil()) { ss.Add(s); return(ss); } var ok = s is Cons; while (ok) { var c = s as Cons; ss.Add(c.Car); s = c.Cdr; ok = s is Cons; } if (s != AtomHelper.Nil) { throw new InvalidOperationException("list is not flat"); } return(ss); }
private static void RecursivePrettyPrint(ISExpression x, int depth) { Console.Write(new String('\t', depth)); if ((x.Type & Types.Atom) == Types.Atom) { if ((x.Type & Types.String) == Types.String) { Console.WriteLine(String.Format("string: \"{0}\"", x.ToString())); } else if ((x.Type & Types.Symbol) == Types.Symbol) { Console.WriteLine(String.Format("symbol: {0}", x.ToString())); } else if ((x.Type & Types.Number) == Types.Number) { Console.WriteLine(String.Format("number: {0}", x.ToString())); } } else if ((x.Type & Types.List) == Types.List) { Console.WriteLine("list"); SExprList list = x as SExprList; foreach (ISExpression y in list) { SExpression.RecursivePrettyPrint(y, depth + 1); } } }
private IDictionary <SymbolAtom, ISExpression> CreateMap(ISExpression vars, Stack <ISExpression> rib) { var map = new Dictionary <SymbolAtom, ISExpression>(); var vflat = vars.Flatten(); if (vflat.Count() == 1 && vflat.First() == AtomHelper.Nil) { vflat = Enumerable.Empty <ISExpression>(); } // this gets weird because native needs the args // and we are wrapping all natives in a zero arg lambda if (vflat.Count() != rib.Count) { ThrowErr("apply", "attempted application of function with incorrect number of parameters", $"({vars.String()})"); } if (vars != AtomHelper.Nil) { foreach (var v in vflat) { if (!v.IsSymbol()) { ThrowErr("apply", "attempted application of function with incorrectly defined bindings", $"({vars.String()})"); } map[v as SymbolAtom] = rib.Pop(); } } return(map); }
public static bool IsInteger(this ISExpression sexpr) { var c = (sexpr as NumberAtom)?.Val; return(IsRational(sexpr) && (c == Complex.CreateExactReal(0) || c.IsInteger)); }
public static IInstruction Compile(ISchemeVM vm, ISExpression expr) { var p1 = DoPass <RewriteLetPass, ISExpression, ISExpression>(vm, expr); var p2 = DoPass <RewriteTwoArmIfPass, ISExpression, ISExpression>(vm, p1); var p3 = DoPass <InstructionPass, ISExpression, IInstruction>(vm, p2); return(p3); }
public static ISExpression GetUnsafeCdr(this ISExpression e) { if (e.IsCons()) { return((e as Cons).Cdr); } throw new InvalidOperationException("expression was not cons"); }
public bool Equals(ISExpression other) { if (other is Procedure) { var p = other as Procedure; return(Name == p.Name && Proc == p.Proc && Primitive == p.Primitive); } return(false); }
public void DefineHere(ISExpression sym, ISExpression val) { var s = sym as SymbolAtom; Map[s] = new EnvThunk() { Val = val }; }
public bool Equals(ISExpression other) { if (other is Cons) { var c = other as Cons; return(Car.Equals(c.Car) && Cdr.Equals(c.Cdr)); } return(false); }
public bool Equals(ISExpression other) { if (other is Closure) { var c = other as Closure; return(Body == c.Body && Vars == c.Vars); } return(false); }
public bool Equals(ISExpression other) { if (other is AtomBase <T> ) { var a = other as AtomBase <T>; return(Val.Equals(a.Val)); } return(false); }
public bool Set(ISExpression sym, ISExpression val) { var s = sym as SymbolAtom; if (Map.ContainsKey(s)) { Map[s].Val = val; return(true); } return(false); }
public static ISExpression Unflatten(this IEnumerable <ISExpression> ss) { ISExpression c = AtomHelper.Nil; ss = ss.Reverse(); foreach (var s in ss) { c = AtomHelper.CreateCons(s, c); } return(c); }
public ISExpression Lookup(ISExpression sym) { if (!sym.IsSymbol()) { throw new InvalidOperationException(); } if (IsDefined(sym)) { return(Map[sym as SymbolAtom].Val); } return(null); }
public bool Set(SymbolAtom sym, ISExpression sexpr) { if (IsDefinedHere(sym)) { DefineHere(sym, sexpr); return(true); } else if (Parent == null) { return(false); } else { return(Parent.Set(sym, sexpr)); } }
public static IEnumerable <ISExpression> FlattenImproper(this ISExpression s) { var ss = new List <ISExpression>(); var ok = s is Cons; while (ok) { var c = s as Cons; ss.Add(c.Car); s = c.Cdr; ok = s is Cons; } ss.Add(s); return(ss); }
private static int ListCount(ISExpression e, int sum) { if (!e.IsList()) { throw new InvalidOperationException("can't list count something that isn't a list"); } var c = e as Cons; if (c.Cdr == AtomHelper.Nil) { return(sum + 1); } else { return(ListCount(c.Cdr, sum + 1)); } }
public bool Equals(ISExpression other) { if (other is Vector) { var v = other as Vector; if (Values.Count() != v.Values.Count()) { return(false); } for (var i = 0; i < Values.Count(); i++) { if (!Values[i].Equals(v.Values[i])) { return(false); } } return(true); } return(false); }
public static bool IsComplex(this ISExpression sexpr) { return(IsNumber(sexpr)); }
public void DefineHere(SymbolAtom sym, ISExpression sexpr) { Data[sym] = sexpr; }
public bool Equals(ISExpression other) { return(other is NilAtom); }
public static bool IsCons(this ISExpression sexpr) { return(sexpr is Cons); }
public static bool IsRational(this ISExpression sexpr) { return(IsReal(sexpr) && ((NumberAtom)(sexpr)).IsReal()); }
public static bool IsReal(this ISExpression sexpr) { return(IsComplex(sexpr) && ((NumberAtom)(sexpr)).Val.Imag.IsZero); }
public static bool IsVector(this ISExpression sexpr) { return(sexpr is Vector); }
public static bool IsEnvironment(this ISExpression sexpr) { return(sexpr is VirtualMachine.Environment); }
public static bool IsNil(this ISExpression sexpr) { return(sexpr is NilAtom); }