private static ISeq sqExpandList(ISeq seq) { IPersistentVector ret = PersistentVector.EMPTY; for (; seq != null; seq = seq.next()) { Object item = seq.first(); //if (item is Unquote) // ret = ret.cons(RT.list(LIST, ((Unquote)item).Obj)); // REV 1184 if (isUnquote(item)) { ret = ret.cons(RT.list(LIST, RT.second(item))); } else if (isUnquoteSplicing(item)) { ret = ret.cons(RT.second(item)); } else { ret = ret.cons(RT.list(LIST, syntaxQuote(item))); } } return(ret.seq()); }
public Expr Parse(ParserContext pcon, object frms) { ISeq forms = (ISeq)frms; if (Util.equals(RT.first(forms), Compiler.DoSym)) { forms = RT.next(forms); } IPersistentVector exprs = PersistentVector.EMPTY; for (; forms != null; forms = forms.next()) { Expr e = (pcon.Rhc != RHC.Eval && (pcon.Rhc == RHC.Statement || forms.next() != null)) ? Compiler.Analyze(pcon.SetRhc(RHC.Statement), forms.first()) : Compiler.Analyze(pcon, forms.first()); exprs = exprs.cons(e); } if (exprs.count() == 0) { exprs = exprs.cons(Compiler.NilExprInstance); } return(new BodyExpr(exprs)); }
private static IPersistentVector flattenMap(object form) { IPersistentVector keyvals = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { IMapEntry e = (IMapEntry)s.first(); keyvals = (IPersistentVector)keyvals.cons(e.key()); keyvals = (IPersistentVector)keyvals.cons(e.val()); } return(keyvals); }
public static Expr Parse(IPersistentMap form) { IPersistentVector keyvals = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { IMapEntry e = (IMapEntry)s.first(); keyvals = (IPersistentVector)keyvals.cons(Compiler.GenerateAST(e.key())); keyvals = (IPersistentVector)keyvals.cons(Compiler.GenerateAST(e.val())); } Expr ret = new MapExpr(keyvals); return(Compiler.OptionallyGenerateMetaInit(form, ret)); }
public Expr Parse(object frm) { ISeq form = (ISeq)frm; IPersistentVector loopLocals = (IPersistentVector)Compiler.LOOP_LOCALS.deref(); if (Compiler.IN_TAIL_POSITION.deref() == null || loopLocals == null) { throw new InvalidOperationException("Can only recur from tail position"); } if (Compiler.IN_CATCH_FINALLY.deref() != null) { throw new InvalidOperationException("Cannot recur from catch/finally."); } IPersistentVector args = PersistentVector.EMPTY; for (ISeq s = form.next(); s != null; s = s.next()) { args = args.cons(Compiler.GenerateAST(s.first())); } if (args.count() != loopLocals.count()) { throw new ArgumentException(string.Format("Mismatched argument count to recur, expected: {0} args, got {1}", loopLocals.count(), args.count())); } return(new RecurExpr(loopLocals, args)); }
void EmitArgsAndCall(int firstArgToEmit, RHC rhc, ObjExpr objx, CljILGen ilg) { for (int i = firstArgToEmit; i < Math.Min(Compiler.MaxPositionalArity, _args.count()); i++) { Expr e = (Expr)_args.nth(i); e.Emit(RHC.Expression, objx, ilg); } if (_args.count() > Compiler.MaxPositionalArity) { IPersistentVector restArgs = PersistentVector.EMPTY; for (int i = Compiler.MaxPositionalArity; i < _args.count(); i++) { restArgs = restArgs.cons(_args.nth(i)); } MethodExpr.EmitArgsAsArray(restArgs, objx, ilg); } //if ( rhc == RHC.Return ) //{ // ObjMethod2 method = (ObjMethod2)Compiler.MethodVar.deref(); // method.EmitClearLocals(context); //} MethodInfo mi = Compiler.Methods_IFn_invoke[Math.Min(Compiler.MaxPositionalArity + 1, _args.count())]; ilg.Emit(OpCodes.Callvirt, mi); }
public Expr Parse(object frm) { ISeq form = (ISeq)frm; // form => (new Typename args ... ) if (form.count() < 2) { throw new Exception("wrong number of arguments, expecting: (new Typename args ...)"); } Type t = Compiler.MaybeType(RT.second(form), false); if (t == null) { throw new ArgumentException("Unable to resolve classname: " + RT.second(form)); } IPersistentVector args = PersistentVector.EMPTY; for (ISeq s = RT.next(RT.next(form)); s != null; s = s.next()) { args = args.cons(Compiler.GenerateAST(s.first())); } return(new NewExpr(t, args)); }
public static Expr Parse(ParserContext pcon, IPersistentMap form) { ParserContext pconToUse = pcon.EvalOrExpr(); bool keysConstant = true; bool valsConstant = true; bool allConstantKeysUnique = true; IPersistentSet constantKeys = PersistentHashSet.EMPTY; IPersistentVector keyvals = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { IMapEntry e = (IMapEntry)s.first(); Expr k = Compiler.Analyze(pconToUse, e.key()); Expr v = Compiler.Analyze(pconToUse, e.val()); keyvals = (IPersistentVector)keyvals.cons(k); keyvals = (IPersistentVector)keyvals.cons(v); if (k is LiteralExpr) { object kval = k.Eval(); if (constantKeys.contains(kval)) { allConstantKeysUnique = false; } else { constantKeys = (IPersistentSet)constantKeys.cons(kval); } } else { keysConstant = false; } if (!(v is LiteralExpr)) { valsConstant = false; } } Expr ret = new MapExpr(keyvals); if (form is IObj iobjForm && iobjForm.meta() != null) { return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret)); }
//static ListReader _listReader = new ListReader(); protected override object Read(PushbackTextReader r, char lparen) { if (ARG_ENV.deref() != null) { throw new InvalidOperationException("Nested #()s are not allowed"); } try { Var.pushThreadBindings(RT.map(ARG_ENV, PersistentTreeMap.EMPTY)); r.Unread('('); ////object form = ReadAux(r, true, null, true); object form = ReadAux(r); //object form = _listReader.invoke(r, '('); IPersistentVector args = PersistentVector.EMPTY; PersistentTreeMap argsyms = (PersistentTreeMap)ARG_ENV.deref(); ISeq rargs = argsyms.rseq(); if (rargs != null) { int higharg = (int)((IMapEntry)rargs.first()).key(); if (higharg > 0) { for (int i = 1; i <= higharg; ++i) { object sym = argsyms.valAt(i); if (sym == null) { sym = garg(i); } args = args.cons(sym); } } object restsym = argsyms.valAt(-1); if (restsym != null) { args = args.cons(Compiler._AMP_); args = args.cons(restsym); } } return(RT.list(Compiler.FN, args, form)); } finally { Var.popThreadBindings(); } }
/// <summary> /// Create a <see cref="PersistentVector">PersistentVector</see> from an <see cref="ISeq">ISeq</see>. /// </summary> /// <param name="items">A sequence of items.</param> /// <returns>An initialized vector.</returns> static public PersistentVector create(ISeq items) { IPersistentVector ret = EMPTY; for (; items != null; items = items.next()) { ret = ret.cons(items.first()); } return((PersistentVector)ret); }
public static Expr Parse(ParserContext pcon, IPersistentMap form) { ParserContext pconToUse = pcon.EvalOrExpr(); bool constant = true; IPersistentVector keyvals = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { IMapEntry e = (IMapEntry)s.first(); Expr k = Compiler.Analyze(pconToUse, e.key()); Expr v = Compiler.Analyze(pconToUse, e.val()); keyvals = (IPersistentVector)keyvals.cons(k); keyvals = (IPersistentVector)keyvals.cons(v); if (!(k is LiteralExpr && v is LiteralExpr)) { constant = false; } } Expr ret = new MapExpr(keyvals); IObj iobjForm = form as IObj; if (iobjForm != null && iobjForm.meta() != null) { return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret)); } else if (constant) { // This 'optimzation' works, mostly, unless you have nested map values. // The nested map values do not participate in the constants map, so you end up with the code to create the keys. // Result: huge duplication of keyword creation. 3X increase in init time to the REPL. //IPersistentMap m = PersistentHashMap.EMPTY; //for (int i = 0; i < keyvals.length(); i += 2) // m = m.assoc(((LiteralExpr)keyvals.nth(i)).Val, ((LiteralExpr)keyvals.nth(i + 1)).Val); //return new ConstantExpr(m); return(ret); } else { return(ret); } }
internal object ParseFile() { IPersistentVector pv = PersistentVector.EMPTY; StringReader sr = new StringReader(_text); PushbackTextReader pr = new PushbackTextReader(sr); pv = pv.cons(Compiler.DO); object eofVal = new object(); object form; while ((form = LispReader.read(pr, false, eofVal, false)) != eofVal) { pv = pv.cons(form); } return(pv.seq()); }
/// <summary> /// Create a <see cref="PersistentVector">PersistentVector</see> from an array of items. /// </summary> /// <param name="items"></param> /// <returns></returns> static public PersistentVector create(params object[] items) { IPersistentVector ret = EMPTY; foreach (object item in items) { ret = ret.cons(item); } return((PersistentVector)ret); }
public object Eval() { IPersistentVector ret = PersistentVector.EMPTY; for (int i = 0; i < _args.count(); i++) { ret = (IPersistentVector)ret.cons(((Expr)_args.nth(i)).Eval()); } return(ret); }
public static Expr Parse(Var v, ISeq args, object tag) { if (!v.isBound || v.get() == null) { //Console.WriteLine("Not bound: {0}", v); return(null); } Type target = v.get().GetType(); MethodInfo[] allMethods = target.GetMethods(); bool variadic = false; int argcount = RT.count(args); MethodInfo method = null; ParameterInfo[] pInfos = null; foreach (MethodInfo m in allMethods) { //Console.WriteLine("Method {0}", m.Name); if (m.IsStatic && m.Name.Equals("invokeStatic")) { pInfos = m.GetParameters(); if (argcount == pInfos.Length) { method = m; variadic = argcount > 0 && pInfos[pInfos.Length - 1].ParameterType == typeof(ISeq); break; } else if (argcount > pInfos.Length && pInfos.Length > 0 && pInfos[pInfos.Length - 1].ParameterType == typeof(ISeq)) { method = m; variadic = true; break; } } } if (method == null) { return(null); } IPersistentVector argv = PersistentVector.EMPTY; for (ISeq s = RT.seq(args); s != null; s = s.next()) { argv = argv.cons(Compiler.Analyze(new ParserContext(RHC.Expression), s.first())); } return(new StaticInvokeExpr(target, method, variadic, argv, tag)); }
public static Expr Parse(ISeq form) { Expr fexpr = Compiler.GenerateAST(form.first()); IPersistentVector args = PersistentVector.EMPTY; for (ISeq s = RT.seq(form.next()); s != null; s = s.next()) { args = args.cons(Compiler.GenerateAST(s.first())); } return(new InvokeExpr(Compiler.TagOf(form), fexpr, args)); }
public static Expr Parse(IPersistentVector form) { IPersistentVector args = PersistentVector.EMPTY; for (int i = 0; i < form.count(); i++) { args = (IPersistentVector)args.cons(Compiler.GenerateAST(form.nth(i))); } Expr ret = new VectorExpr(args); return(Compiler.OptionallyGenerateMetaInit(form, ret)); }
public static Expr Parse(IPersistentSet form) { IPersistentVector keys = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { object e = s.first(); keys = (IPersistentVector)keys.cons(Compiler.GenerateAST(e)); } Expr ret = new SetExpr(keys); return(Compiler.OptionallyGenerateMetaInit(form, ret)); }
public Expr Parse(object frms) { ISeq forms = (ISeq)frms; if (Util.equals(RT.first(forms), Compiler.DO)) { forms = RT.next(forms); } IPersistentVector exprs = PersistentVector.EMPTY; for (ISeq s = forms; s != null; s = s.next()) { if (s.next() == null) { // in tail recurive position try { Var.pushThreadBindings(PersistentHashMap.create(Compiler.IN_TAIL_POSITION, RT.T)); exprs = exprs.cons(Compiler.GenerateAST(s.first())); } finally { Var.popThreadBindings(); } } else { exprs = exprs.cons(Compiler.GenerateAST(s.first())); } } if (exprs.count() == 0) { exprs = exprs.cons(Compiler.NIL_EXPR); } return(new BodyExpr(exprs)); }
public void ConsAddsAtEnd() { IPersistentVector v = LazilyPersistentVector.createOwning(1, 2, 3); IPersistentVector v2 = v.cons(4); Expect(v.count()).To.Equal(3); Expect(v.nth(0)).To.Equal(1); Expect(v.nth(1)).To.Equal(2); Expect(v.nth(2)).To.Equal(3); Expect(v2.count()).To.Equal(4); Expect(v2.nth(0)).To.Equal(1); Expect(v2.nth(1)).To.Equal(2); Expect(v2.nth(2)).To.Equal(3); Expect(v2.nth(3)).To.Equal(4); }
public void ConsAddsAtEnd() { IPersistentVector v = LazilyPersistentVector.createOwning(1, 2, 3); IPersistentVector v2 = v.cons(4); Expect(v.count(), EqualTo(3)); Expect(v.nth(0), EqualTo(1)); Expect(v.nth(1), EqualTo(2)); Expect(v.nth(2), EqualTo(3)); Expect(v2.count(), EqualTo(4)); Expect(v2.nth(0), EqualTo(1)); Expect(v2.nth(1), EqualTo(2)); Expect(v2.nth(2), EqualTo(3)); Expect(v2.nth(3), EqualTo(4)); }
/// <summary> /// Send an action (encapsulated message). /// </summary> /// <param name="action">The action to execute.</param> /// <remarks> /// <para>If there is a transaction running on this thread, /// defer execution until the transaction ends /// (enqueue the action on the transaction).</para> /// <para>If there is already an action running, enqueue it (nested).</para> /// <para>Otherwise, queue it for execution.</para> /// </remarks> internal static void DispatchAction(Action action) { LockingTransaction trans = LockingTransaction.getRunning(); if (trans != null) { trans.enqueue(action); } else if (_nested != null) { _nested = _nested.cons(action); } else { action.Agent.Enqueue(action); } }
public void ConsWorks() { PersistentVector v1 = PersistentVector.create(2, 3, 4); IPersistentVector v2 = v1; for (int i = 3; i < 100000; i++) { v2 = v2.cons(i + 2); } Expect(v1.count(), EqualTo(3)); Expect(v2.count(), EqualTo(100000)); for (int i = 0; i < v2.count(); ++i) { Expect(v2.nth(i), EqualTo(i + 2)); } }
public object Eval() { try { IFn fn = (IFn)_fexpr.Eval(); IPersistentVector argvs = PersistentVector.EMPTY; for (int i = 0; i < _args.count(); i++) { argvs = (IPersistentVector)argvs.cons(((Expr)_args.nth(i)).Eval()); } return(fn.applyTo(RT.seq(Util.Ret1(argvs, argvs = null)))); } catch (Compiler.CompilerException) { throw; } catch (Exception e) { throw new Compiler.CompilerException(_source, Compiler.GetLineFromSpanMap(_spanMap), Compiler.GetColumnFromSpanMap(_spanMap), e); } }
public static Expr Parse(ParserContext pcon, IPersistentVector form) { ParserContext pconToUse = pcon.EvalOrExpr(); bool constant = true; IPersistentVector args = PersistentVector.EMPTY; for (int i = 0; i < form.count(); i++) { Expr v = Compiler.Analyze(pconToUse, form.nth(i)); args = (IPersistentVector)args.cons(v); if (!(v is LiteralExpr)) { constant = false; } } Expr ret = new VectorExpr(args); IObj iobjForm = form as IObj; if (iobjForm != null && iobjForm.meta() != null) { return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret)); } else if (constant) { IPersistentVector rv = PersistentVector.EMPTY; for (int i = 0; i < args.count(); i++) { LiteralExpr ve = (LiteralExpr)args.nth(i); rv = (IPersistentVector)rv.cons(ve.Val); } return(new ConstantExpr(rv)); } else { return(ret); } }
public static Expr Parse(ParserContext pcon, IPersistentSet form) { ParserContext pconToUse = pcon.EvalOrExpr(); bool constant = true; IPersistentVector keys = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { object e = s.first(); Expr expr = Compiler.Analyze(pconToUse, e); keys = (IPersistentVector)keys.cons(expr); if (!(expr is LiteralExpr)) { constant = false; } } Expr ret = new SetExpr(keys); IObj iobjForm = form as IObj; if (iobjForm != null && iobjForm.meta() != null) { return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret)); } else if (constant) { IPersistentSet set = PersistentHashSet.EMPTY; for (int i = 0; i < keys.count(); i++) { LiteralExpr ve = (LiteralExpr)keys.nth(i); set = (IPersistentSet)set.cons(ve.Val); } return(new ConstantExpr(set)); } else { return(ret); } }
public static Expr Parse(ParserContext pcon, IPersistentSet form) { ParserContext pconToUse = pcon.EvalOrExpr(); bool constant = true; IPersistentVector keys = PersistentVector.EMPTY; for (ISeq s = RT.seq(form); s != null; s = s.next()) { object e = s.first(); Expr expr = Compiler.Analyze(pconToUse, e); keys = (IPersistentVector)keys.cons(expr); if (!(expr is LiteralExpr)) { constant = false; } } Expr ret = new SetExpr(keys); if (form is IObj iobjForm && iobjForm.meta() != null) { return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret)); }
public static Expr Parse(ParserContext pcon, IPersistentVector form) { ParserContext pconToUse = pcon.EvalOrExpr(); bool constant = true; IPersistentVector args = PersistentVector.EMPTY; for (int i = 0; i < form.count(); i++) { Expr v = Compiler.Analyze(pconToUse, form.nth(i)); args = (IPersistentVector)args.cons(v); if (!(v is LiteralExpr)) { constant = false; } } Expr ret = new VectorExpr(args); if (form is IObj iobjForm && iobjForm.meta() != null) { return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret)); }
public Expr Parse(ParserContext pcon, object frm) { string source = (string)Compiler.SourceVar.deref(); IPersistentMap spanMap = (IPersistentMap)Compiler.SourceSpanVar.deref(); // Compiler.GetSourceSpanMap(form); ISeq form = (ISeq)frm; IPersistentVector loopLocals = (IPersistentVector)Compiler.LoopLocalsVar.deref(); if (pcon.Rhc != RHC.Return || loopLocals == null) { throw new ParseException("Can only recur from tail position"); } if (Compiler.NoRecurVar.deref() != null) { throw new ParseException("Cannot recur across try"); } IPersistentVector args = PersistentVector.EMPTY; for (ISeq s = RT.seq(form.next()); s != null; s = s.next()) { args = args.cons(Compiler.Analyze(pcon.SetRhc(RHC.Expression).SetAssign(false), s.first())); } if (args.count() != loopLocals.count()) { throw new ParseException(string.Format("Mismatched argument count to recur, expected: {0} args, got {1}", loopLocals.count(), args.count())); } for (int i = 0; i < loopLocals.count(); i++) { LocalBinding lb = (LocalBinding)loopLocals.nth(i); Type primt = lb.PrimitiveType; if (primt != null) { bool mismatch = false; Type pt = Compiler.MaybePrimitiveType((Expr)args.nth(i)); if (primt == typeof(long)) { if (!(pt == typeof(long) || pt == typeof(int) || pt == typeof(short) || pt == typeof(uint) || pt == typeof(ushort) || pt == typeof(ulong) || pt == typeof(char) || pt == typeof(byte) || pt == typeof(sbyte))) { mismatch = true; } } else if (primt == typeof(double)) { if (!(pt == typeof(double) || pt == typeof(float))) { mismatch = true; } } if (mismatch) { lb.RecurMismatch = true; if (RT.booleanCast(RT.WarnOnReflectionVar.deref())) { RT.errPrintWriter().WriteLine("{0}:{1} recur arg for primitive local: {2} is not matching primitive, had: {3}, needed {4}", source, spanMap != null ? (int)spanMap.valAt(RT.StartLineKey, 0) : 0, lb.Name, pt != null ? pt.Name : "Object", primt.Name); } } } } return(new RecurExpr(source, spanMap, loopLocals, args)); }
public static Expr Parse(ParserContext pcon, ISeq form) { pcon = pcon.EvalOrExpr(); Expr fexpr = Compiler.Analyze(pcon, form.first()); VarExpr varFexpr = fexpr as VarExpr; if (varFexpr != null && varFexpr.Var.Equals(Compiler.InstanceVar)) { if (RT.second(form) is Symbol) { Type t = HostExpr.MaybeType(RT.second(form), false); if (t != null) { return(new InstanceOfExpr((string)Compiler.SourceVar.deref(), (IPersistentMap)Compiler.SourceSpanVar.deref(), t, Compiler.Analyze(pcon, RT.third(form)))); } } } if (varFexpr != null && pcon.Rhc != RHC.Eval) { Var v = varFexpr.Var; object arglists = RT.get(RT.meta(v), Compiler.ArglistsKeyword); int arity = RT.count(form.next()); for (ISeq s = RT.seq(arglists); s != null; s = s.next()) { IPersistentVector sargs = (IPersistentVector)s.first(); if (sargs.count() == arity) { string primc = FnMethod.PrimInterface(sargs); if (primc != null) { return(Compiler.Analyze(pcon, RT.listStar(Symbol.intern(".invokePrim"), ((Symbol)form.first()).withMeta(RT.map(RT.TagKey, Symbol.intern(primc))), form.next()))); } break; } } } KeywordExpr kwFexpr = fexpr as KeywordExpr; if (kwFexpr != null && RT.count(form) == 2 && Compiler.KeywordCallsitesVar.isBound) { Expr target = Compiler.Analyze(pcon, RT.second(form)); return(new KeywordInvokeExpr((string)Compiler.SourceVar.deref(), (IPersistentMap)Compiler.SourceSpanVar.deref(), Compiler.TagOf(form), kwFexpr, target)); } IPersistentVector args = PersistentVector.EMPTY; for (ISeq s = RT.seq(form.next()); s != null; s = s.next()) { args = args.cons(Compiler.Analyze(pcon, s.first())); } //if (args.count() > Compiler.MAX_POSITIONAL_ARITY) // throw new ArgumentException(String.Format("No more than {0} args supported", Compiler.MAX_POSITIONAL_ARITY)); return(new InvokeExpr((string)Compiler.SourceVar.deref(), (IPersistentMap)Compiler.SourceSpanVar.deref(), //Compiler.GetSourceSpanMap(form), Compiler.TagOf(form), fexpr, args)); }