Exemple #1
0
    void WriteGeneral(P6any obj)
    {
        if (FailSoft && !(obj is P6opaque))
        {
            o.AppendFormat("\"UNSERIALIZABLE {0}\"", obj.mo.name);
            return;
        }

        int a = nextanchor++;

        anchors[obj] = a;
        P6opaque dyo = (P6opaque)obj;
        STable   mo  = dyo.mo;

        o.Append('{');
        contUsed = true;
        o.AppendFormat("\"&\":\"A{0}\",\"!\":", a);
        WriteStr(true, "!perl6/" + mo.name);

        for (int i = 0; i < mo.nslots; i++)
        {
            o.Append(',');
            WriteStr(true, mo.all_slot[i]);
            o.Append(':');
            WriteObj(((Variable)dyo.slots[i]).Fetch());
        }

        o.Append('}');
    }
Exemple #2
0
    Variable GetFromArray()
    {
        SkipCharWS('[');
        VarDeque kids = new VarDeque();
        P6opaque obj  = new P6opaque(Kernel.ArrayMO);

        obj.SetSlot("items", kids);
        obj.SetSlot("rest", new VarDeque());
        bool   comma = false;
        string a_tag = null;

        if (SkipWhite(true) == '"')
        {
            GetString();
            comma = true;
        }

        if (comma)
        {
            if (s_content_type == NONE)
            {
                a_tag = s_tag;
                if (s_anchor != null)
                {
                    AddAnchor(s_anchor, obj);
                }
            }
            else
            {
                kids.Push(GetFromString());
            }
        }

        while (true)
        {
            if (SkipWhite(true) == ']')
            {
                break;
            }
            if (comma)
            {
                SkipChar(',');
            }
            kids.Push(GetObj());
            comma = true;
        }
        SkipCharWS(']');
        if (a_tag != null)
        {
            Err("Typed arrays are NYI in Niecza Perl 6");
        }
        return(Kernel.NewRWScalar(Kernel.AnyMO, obj));
    }
Exemple #3
0
 private Variable FixupList(VarDeque caps)
 {
     if (caps.Count() != 0 && caps[0] == null) {
         caps.Shift();
         P6opaque l = new P6opaque(Kernel.ListMO);
         l.slots[0 /*items*/] = caps;
         l.slots[1 /*rest*/ ] = new VarDeque();
         return Kernel.NewROScalar(l);
     } else {
         return caps.Count() != 0 ? caps[0] : Kernel.AnyMO.typeVar;
     }
 }
Exemple #4
0
    public static Variable cursor_allcaps(Variable cv)
    {
        Cursor c = (Cursor) cv.Fetch();
        VarDeque dq = new VarDeque();

        for (CapInfo it = c.captures; it != null; it = it.prev) {
            if (it.names[0] == null || it.cap == null)
                continue; // special node
            if (!it.cap.Fetch().Isa(Kernel.MatchMO))
                continue;
            foreach (string name in it.names)
                dq.Unshift(pair(MakeStr(name), it.cap));
        }

        P6opaque lst = new P6opaque(Kernel.ListMO);
        lst.slots[0 /*items*/] = dq;
        lst.slots[1 /*rest*/ ] = new VarDeque();
        return Kernel.NewRWListVar(lst);
    }
Exemple #5
0
    public Frame Backtrack(Frame th)
    {
        // throw away cut or mark-only frames
        while (bt != rootf && (bt.ip < 0))
            bt = bt.prev;
        if (st.pos > global.highwater)
            global.IncHighwater(st.pos);
        if (bt == rootf) {
            if (return_one) {
                if (Cursor.Trace)
                    Console.WriteLine("Failing {0}@{1} after no matches",
                            name, from);
                return Kernel.Take(th, Kernel.NewROScalar(Kernel.EMPTYP));
            } else {
                if (Cursor.Trace)
                    Console.WriteLine("Failing {0}@{1} after some matches",
                            name, from);
                if (EmptyList == null) {
                    P6opaque lst = new P6opaque(Kernel.ListMO);
                    lst.slots[0 /*items*/] = new VarDeque();
                    lst.slots[1 /*rest*/ ] = new VarDeque();
                    EmptyList = Kernel.NewRWListVar(lst);
                }
                th.caller.resultSlot = EmptyList;
            }

            return th.caller;
        } else {
            th.ip = bt.ip;
            st = bt.st;
            bt = bt.prev;
            return th;
        }
    }
Exemple #6
0
 // currently just used for protoregex
 public Frame EndWith(Frame th, Cursor m)
 {
     if (st.pos > global.highwater)
         global.IncHighwater(st.pos);
     if ((flags & RETURN_ONE) != 0) {
         return Kernel.Take(th, Kernel.NewROScalar(m));
     } else {
         th.MarkSharedChain();
         flags |= RETURN_ONE;
         VarDeque ks = new VarDeque();
         ks.Push(Kernel.NewROScalar(m));
         th.coro_return = th;
         P6opaque it  = new P6opaque(Kernel.GatherIteratorMO);
         it.slots[0 /*frame*/] = Kernel.NewMuScalar(th);
         it.slots[1 /*reify*/] = Kernel.NewMuScalar(Kernel.AnyP);
         VarDeque iss = new VarDeque();
         iss.Push(Kernel.NewROScalar(it));
         P6opaque lst = new P6opaque(Kernel.ListMO);
         lst.slots[0 /*items*/] = ks;
         lst.slots[1 /*rest*/ ] = iss;
         th.caller.resultSlot = Kernel.NewRWListVar(lst);
     }
     return th.Return();
 }
Exemple #7
0
 // This function implements the actual looping part of autothreading
 public static Variable AutoThread(P6any j, Func<Variable,Variable> dgt)
 {
     P6opaque j_ = (P6opaque)j;
     P6any listObj = (P6any) j_.slots[1];
     Variable[] list = Kernel.UnboxAny<Variable[]>(listObj);
     Variable[] nlist = new Variable[list.Length];
     for (int i = 0; i < list.Length; i++) {
         nlist[i] = dgt(list[i]);
     }
     P6any newList = Kernel.BoxRaw(nlist, Kernel.ParcelMO);
     P6opaque newJunc = new P6opaque(Kernel.JunctionMO);
     newJunc.slots[0] = j_.slots[0];
     newJunc.slots[1] = newList;
     return Kernel.NewROScalar(newJunc);
 }
Exemple #8
0
 public static Variable HashIter(int mode, Variable v)
 {
     VarDeque lv = HashIterRaw(mode, v);
     P6opaque l = new P6opaque(Kernel.ListMO);
     l.slots[0] = lv;
     l.slots[1] = new VarDeque();
     return Kernel.NewRWListVar(l);
 }
Exemple #9
0
 public static Variable pair(Variable key, Variable value)
 {
     P6any l = new P6opaque(Kernel.PairMO);
     l.SetSlot("key", key);
     l.SetSlot("value", value);
     return Kernel.NewROScalar(l);
 }
Exemple #10
0
    internal static Frame CommonMEMap_C(Frame th)
    {
        ItemSource src = (ItemSource) th.lex0;
        VarDeque outq = (VarDeque) th.lex1;
        object fnc = th.lex2;
        int tailmode = th.lexi0;

        switch (th.ip) {
            case 0:
                Variable[] pen;
                if (!src.TryGet(out pen, tailmode != 0)) {
                    P6opaque thunk = new P6opaque(Kernel.GatherIteratorMO);
                    th.coro_return = th;
                    th.MarkSharedChain();
                    thunk.slots[0] = Kernel.NewMuScalar(th);
                    thunk.slots[1] = Kernel.NewMuScalar(Kernel.AnyP);
                    P6opaque lst = new P6opaque(Kernel.ListMO);
                    lst.slots[0] = outq;
                    lst.slots[1] = new VarDeque(Kernel.NewROScalar(thunk));
                    th.caller.resultSlot = Kernel.NewRWListVar(lst);
                    th.lexi0 = 1;
                    return th.Return();
                }
                if (pen == null) {
                    if (tailmode != 0)
                        return Kernel.Take(th, Kernel.NewROScalar(Kernel.EMPTYP));
                    P6opaque lst = new P6opaque(Kernel.ListMO);
                    lst.slots[0] = outq;
                    lst.slots[1] = new VarDeque();
                    th.caller.resultSlot = Kernel.NewRWListVar(lst);
                    return th.Return();
                }
                th.lex3 = pen;
                th.ip = 1;
                goto case 1;
            case 1:
                th.ip = 2;
                if (fnc is P6any) {
                    return ((P6any)fnc).Invoke(th, (Variable[])th.lex3, null);
                } else if (fnc == null) {
                    th.resultSlot = MakeParcel((Variable[]) th.lex3);
                    goto case 2;
                } else {
                    th.resultSlot = ((Func<Variable,Variable>)fnc).Invoke(
                        ((Variable[])th.lex3)[0]);
                    goto case 2;
                }
            case 2:
                if (tailmode != 0) {
                    th.ip = 0;
                    return Kernel.Take(th, (Variable)th.resultSlot);
                } else {
                    outq.Push((Variable) th.resultSlot);
                    th.ip = 0;
                    goto case 0;
                }
            case 3:
                th.lex0 = src = ItemSource.Empty;
                th.ip = 0;
                goto case 0;
            default:
                return Kernel.Die(th, "Invalid IP");
        }
    }
Exemple #11
0
    Variable GetFromJson(bool top_level)
    {
        char look = SkipWhite(true);

        if (look == '[')
        {
            VarDeque q = new VarDeque();
            SkipChar('[');
            while (true)
            {
                look = SkipWhite(true);
                if (look == ']')
                {
                    break;
                }
                if (q.Count() != 0)
                {
                    SkipChar(',');
                }
                q.Push(GetFromJson(false));
            }
            SkipWhite(true);
            SkipChar(']');
            P6any i = new P6opaque(Kernel.ArrayMO);
            i.SetSlot("items", q);
            i.SetSlot("rest", new VarDeque());
            return(Kernel.NewROScalar(i));
        }
        else if (look == '{')
        {
            VarHash q  = new VarHash();
            int     ct = 0;
            SkipChar('{');
            while (true)
            {
                look = SkipWhite(true);
                if (look == '}')
                {
                    break;
                }
                if (ct != 0)
                {
                    SkipCharWS(',');
                }
                ct++;
                string key = GetJsonString();
                SkipWhite(true);
                SkipChar(':');
                q[key] = GetFromJson(false);
            }
            SkipWhite(true);
            SkipChar('}');
            return(BoxRW <VarHash>(q, Kernel.HashMO));
        }
        else if (top_level)
        {
            Err("Top-level scalar found");
            return(null);
        }
        else if (look == '"')
        {
            return(BoxRW <string>(GetJsonString(), Kernel.StrMO));
        }
        else if (look == 'n')
        {
            SkipToken("null");
            return(Kernel.NewMuScalar(Kernel.AnyP));
        }
        else if (look == 't')
        {
            SkipToken("true");
            return(Kernel.NewMuScalar(Kernel.TrueV.Fetch()));
        }
        else if (look == 'f')
        {
            SkipToken("false");
            return(Kernel.NewMuScalar(Kernel.FalseV.Fetch()));
        }
        else
        {
            double d;
            string tx = GetJsonNumber();
            if (!Utils.S2NB(tx, out d))
            {
                Err("Unparsable number " + tx);
            }
            return(BoxRW <double>(d, Kernel.NumMO));
        }
    }
Exemple #12
0
        public object ObjRef()
        {
            var tag = (SerializationCode)Byte();

            if (Config.SerTrace)
            {
                Console.WriteLine("Reading {0} from {1}...", tag, rpointer - 1);
            }
            int i, j;

            switch (tag)
            {
            case SerializationCode.Null:
                return(null);

            case SerializationCode.ForeignRef:
                i = Int();
                j = Int();
                return(unit_map[i].bynum[j]);

            case SerializationCode.SelfRef:
                i = Int();
                return(unit.bynum[i]);

            case SerializationCode.NewUnitRef:
                return(LoadNewUnit());

            case SerializationCode.RuntimeUnit:
                return(RuntimeUnit.Thaw(this));

            case SerializationCode.SubInfo:
                return(SubInfo.Thaw(this));

            case SerializationCode.STable:
                return(STable.Thaw(this));

            case SerializationCode.StashEnt:
                return(StashEnt.Thaw(this));

            case SerializationCode.Rat:
                return(Rat.Thaw(this));

            case SerializationCode.FatRat:
                return(FatRat.Thaw(this));

            case SerializationCode.Complex:
                return(Complex.Thaw(this));

            case SerializationCode.BigInteger:
                return(BigInteger.Thaw(this));

            case SerializationCode.VarDeque:
                return(VarDeque.Thaw(this));

            case SerializationCode.VarHash:
                return(VarHash.Thaw(this));

            case SerializationCode.DispatchEnt:
                return(DispatchEnt.Thaw(this));

            //case SerializationCode.RxFrame:
            //    return RxFrame.Thaw(this);
            case SerializationCode.P6how:
                return(P6how.Thaw(this));

            case SerializationCode.CC:
                return(CC.Thaw(this));

            case SerializationCode.AltInfo:
                return(AltInfo.Thaw(this));

            case SerializationCode.Signature:
                return(Signature.Thaw(this));

            case SerializationCode.Parameter:
                return(Parameter.Thaw(this));

            case SerializationCode.ReflectObj:
                return(ReflectObj.Thaw(this));

            case SerializationCode.P6opaque:
                return(P6opaque.Thaw(this));

            case SerializationCode.Frame:
                return(Frame.Thaw(this));

            //Cursor,

            case SerializationCode.String:
                return(Register(String()));

            case SerializationCode.ArrP6any:
                return(RefsARegister <P6any>());

            case SerializationCode.ArrVariable:
                return(RefsARegister <Variable>());

            case SerializationCode.ArrString:
                return(RefsARegister <string>());

            case SerializationCode.ArrCC:
                return(RefsARegister <CC>());

            case SerializationCode.Boolean:
                return(Register(Byte() != 0));

            case SerializationCode.Int:
                return(Register(Int()));

            case SerializationCode.Double:
                return(Register(Double()));

            case SerializationCode.Type:
                return(Register(Type.GetType(String(), true)));

            case SerializationCode.SimpleVariable:
            case SerializationCode.SimpleVariable_1:
            case SerializationCode.SimpleVariable_2:
            case SerializationCode.SimpleVariable_3:
                return(SimpleVariable.Thaw(this,
                                           (int)tag - (int)SerializationCode.SimpleVariable));

            case SerializationCode.SubstrLValue:
                return(SubstrLValue.Thaw(this));

            case SerializationCode.TiedVariable:
                return(TiedVariable.Thaw(this));

            case SerializationCode.SubViviHook:
                return(SubViviHook.Thaw(this));

            case SerializationCode.ArrayViviHook:
                return(ArrayViviHook.Thaw(this));

            case SerializationCode.NewArrayViviHook:
                return(NewArrayViviHook.Thaw(this));

            case SerializationCode.HashViviHook:
                return(HashViviHook.Thaw(this));

            case SerializationCode.NewHashViviHook:
                return(NewHashViviHook.Thaw(this));

            case SerializationCode.LADNone:
                return(Register(new LADNone()));

            case SerializationCode.LADNull:
                return(Register(new LADNull()));

            case SerializationCode.LADDot:
                return(Register(new LADDot()));

            case SerializationCode.LADDispatcher:
                return(Register(new LADDispatcher()));

            case SerializationCode.LADImp:
                return(Register(new LADImp()));

            case SerializationCode.LADStr:
                return(LADStr.Thaw(this));

            case SerializationCode.LADStrNoCase:
                return(LADStrNoCase.Thaw(this));

            case SerializationCode.LADMethod:
                return(LADMethod.Thaw(this));

            case SerializationCode.LADParam:
                return(LADParam.Thaw(this));

            case SerializationCode.LADQuant:
                return(LADQuant.Thaw(this));

            case SerializationCode.LADSequence:
                return(LADSequence.Thaw(this));

            case SerializationCode.LADAny:
                return(LADAny.Thaw(this));

            case SerializationCode.LADCC:
                return(LADCC.Thaw(this));

            default:
                throw new ThawException("unexpected object tag " + tag);
            }
        }
Exemple #13
0
 public static P6any MakePair(Variable key, Variable value)
 {
     P6any l = new P6opaque(Kernel.PairMO);
     l.SetSlot(Kernel.EnumMO, "$!key", key);
     l.SetSlot(Kernel.EnumMO, "$!value", value);
     return l;
 }
Exemple #14
0
 public static P6any MakeList(VarDeque items, VarDeque rest)
 {
     P6any l = new P6opaque(Kernel.ListMO);
     l.SetSlot(Kernel.ListMO, "$!rest", rest);
     l.SetSlot(Kernel.ListMO, "$!items", items);
     return l;
 }
Exemple #15
0
    public Variable SimpleWS()
    {
        string backing = global.orig_s;
        char[] backing_ca = global.orig_a;
        int l = backing_ca.Length;
        int p = pos;

        VarDeque ks = new VarDeque();

        P6opaque lst = new P6opaque(Kernel.ListMO);
        lst.slots[0 /*items*/] = ks;
        lst.slots[1 /*rest*/ ] = new VarDeque();

        if (p != 0 && p != l && CC.Word.Accepts(backing[p]) &&
                CC.Word.Accepts(backing[p-1])) {
            if (Trace)
                Console.WriteLine("! no match <ws> at {0}", pos);
        } else {
            while (p != l && Char.IsWhiteSpace(backing, p)) { p++; }
            if (Trace)
                Console.WriteLine("* match <ws> at {0} to {1}", pos, p);
            ks.Push(Kernel.NewROScalar(At(p)));
        }

        return Kernel.NewRWListVar(lst);
    }
Exemple #16
0
    public Variable O(VarHash caps)
    {
        Cursor nw = At(pos);
        foreach (KeyValuePair<string,Variable> kv in caps)
            nw.captures = new CapInfo(nw.captures, new string[] { kv.Key },
                    Kernel.NewMuScalar(kv.Value.Fetch()));
        VarDeque ks = new VarDeque();

        P6opaque lst = new P6opaque(Kernel.ListMO);
        lst.slots[0 /*items*/] = ks;
        lst.slots[1 /*rest*/ ] = new VarDeque();

        ks.Push(Kernel.NewROScalar(nw));
        return Kernel.NewRWListVar(lst);
    }
Exemple #17
0
 // currently just used for protoregex
 public Frame EndWith(Frame th, Cursor m)
 {
     if (st.pos > global.highwater)
         global.IncHighwater(st.pos);
     if (return_one) {
         return Kernel.Take(th, Kernel.NewROScalar(m));
     } else {
         th.MarkSharedChain();
         return_one = true;
         VarDeque ks = new VarDeque();
         ks.Push(Kernel.NewROScalar(m));
         th.lex = new Dictionary<string,object>();
         th.lex["!return"] = null;
         P6opaque it  = new P6opaque(Kernel.GatherIteratorMO);
         it.slots[0 /*frame*/] = Kernel.NewMuScalar(th);
         it.slots[1 /*reify*/] = Kernel.NewMuScalar(Kernel.AnyP);
         VarDeque iss = new VarDeque();
         iss.Push(Kernel.NewROScalar(it));
         P6opaque lst = new P6opaque(Kernel.ListMO);
         lst.slots[0 /*items*/] = ks;
         lst.slots[1 /*rest*/ ] = iss;
         th.caller.resultSlot = Kernel.NewRWListVar(lst);
     }
     return th.caller;
 }
Exemple #18
0
 public static Variable MakeJunction(int type, Variable[] elems)
 {
     if (type >= 8) {
         type -= 8;
         foreach (Variable e in elems)
             if (e.islist) goto need_flatten;
         goto flat_enough;
     need_flatten:;
         VarDeque iter = new VarDeque(elems);
         VarDeque into = new VarDeque();
         while (Kernel.IterHasFlat(iter, true))
             into.Push(iter.Shift());
         elems = into.CopyAsArray();
     flat_enough:;
     }
     P6opaque nj = new P6opaque(Kernel.JunctionMO);
     nj.slots[0] = Kernel.BoxRaw(type, Kernel.IntMO);
     nj.slots[1] = Kernel.BoxRaw(elems, Kernel.ParcelMO);
     return Kernel.NewROScalar(nj);
 }
Exemple #19
0
    Variable GetFromHash()
    {
        SkipCharWS('{');
        // we can't make any assumptions about ordering here, as JSON
        // emitters can blindly reorder hashes
        string h_tag    = null;
        string h_anchor = null;
        Dictionary <string, string> h_key_ind = null;
        VarHash h_val_ind = null;
        VarHash zyg       = new VarHash();
        bool    comma     = false;

        while (true)
        {
            if (SkipWhite(true) == '}')
            {
                break;
            }
            if (comma)
            {
                SkipChar(',');
            }
            comma = true;
            SkipWhite(false);
            GetString();
            if (s_content_type == NONE && s_anchor == null && s_tag == "")
            {
                if (h_tag != null)
                {
                    Err("Tag specified twice");
                }
                h_tag = GetSimpleStringValue();
            }
            else if (s_content_type == NONE && s_tag == null && s_anchor == "")
            {
                if (h_anchor != null)
                {
                    Err("Anchor specified twice");
                }
                h_anchor = GetSimpleStringValue();
            }
            else if (s_content_type == NONE)
            {
                if (s_anchor == null || s_tag != null)
                {
                    Err("Invalid hash key form");
                }
                string k1 = s_anchor;
                if (h_key_ind == null)
                {
                    h_key_ind = new Dictionary <string, string>();
                }
                if (h_key_ind.ContainsKey(k1))
                {
                    Err("Key alias &" + k1 + " specified twice");
                }
                SkipCharWS(':');
                if (SkipWhite(true) != '"')
                {
                    Err("Non-scalar hash keys NYI in Niecza Perl 6");
                }
                GetString();
                if (s_tag != null || s_anchor != null || s_content_type != SCALAR)
                {
                    Err("Typed hash keys NYI in Niecza Perl 6");
                }
                h_key_ind[k1] = s_content;
            }
            else if (s_content_type == ALIAS)
            {
                string k1 = s_content;
                if (h_val_ind == null)
                {
                    h_val_ind = new VarHash();
                }
                if (h_val_ind.ContainsKey(k1))
                {
                    Err("Key alias *" + k1 + " used twice");
                }
                SkipCharWS(':');
                h_val_ind[k1] = GetObj();
            }
            else if (s_content_type == DIRECTIVE)
            {
                Err("Got directive in hash key position");
            }
            else
            {
                if (s_tag != null || s_anchor != null)
                {
                    Err("Typed hash keys NYI in Niecza Perl 6");
                }
                string k1 = s_content;
                SkipCharWS(':');
                zyg[k1] = GetObj();
            }
        }

        SkipChar('}');

        if (h_key_ind != null || h_val_ind != null)
        {
            h_key_ind = h_key_ind ?? new Dictionary <string, string>();
            h_val_ind = h_val_ind ?? new VarHash();

            foreach (KeyValuePair <string, string> kv in h_key_ind)
            {
                if (!h_val_ind.ContainsKey(kv.Key))
                {
                    Err("No value provided for indirect key *" + kv.Key);
                }
                Variable val = h_val_ind[kv.Key];
                h_val_ind.Remove(kv.Key);
                if (zyg.ContainsKey(kv.Value))
                {
                    Err("Indirect key &" + kv.Value + " collides with non-indirect key");
                }
                zyg[kv.Value] = val;
            }

            foreach (string k in h_val_ind.Keys)
            {
                Err("Indirect key &" + k + " is unused");
            }
        }

        Variable obj;

        if (h_tag != null)
        {
            if (!Utils.StartsWithInvariant("!perl6/", h_tag))
            {
                Err("Unsupported hash tag " + h_tag);
            }
            string s2  = "::GLOBAL::" + h_tag.Substring(7);
            int    cut = s2.LastIndexOf("::");

            Variable v_cursor = setting.GetVar(s2.Substring(0, cut),
                                               s2.Substring(cut + 2)).v;

            if (v_cursor.Rw)
            {
                Err(s2.Substring(2) + " does not name a loaded global class");
            }
            P6any p_cursor = v_cursor.Fetch();

            if (p_cursor.Isa(setting.HashMO))
            {
                if (p_cursor.mo.nslots != 0)
                {
                    Err("Cannot thaw Hash subclass " + p_cursor.mo.name + "; it has attributes");
                }
                obj = BoxRW <VarHash>(zyg, p_cursor.mo);
            }
            else
            {
                P6opaque dyo = new P6opaque(p_cursor.mo);
                for (int i = 0; i < dyo.mo.nslots; i++)
                {
                    string sn = dyo.mo.all_slot[i];
                    if (!zyg.ContainsKey(sn))
                    {
                        Err("No value for attribute " + sn + " in thawed value of class " + dyo.mo.name);
                    }
                    dyo.slots[i] = zyg[sn];
                    zyg.Remove(sn);
                }
                foreach (string key in zyg.Keys)
                {
                    Err("Attribute " + key + " not present in " + dyo.mo.name);
                }
                obj = Kernel.NewMuScalar(dyo);
            }
        }
        else
        {
            obj = BoxRW <VarHash>(zyg, setting.HashMO);
        }
        if (h_anchor != null)
        {
            AddAnchor(h_anchor, obj.Fetch());
        }
        return(obj);
    }
Exemple #20
0
    internal static Frame CommonGrep_C(Frame th)
    {
        VarDeque src = (VarDeque) th.lex0;
        VarDeque outq = (VarDeque) th.lex1;
        Variable flt = (Variable) th.lex2;
        int tailmode = th.lexi0;

        switch (th.ip) {
            case 0:
                Variable pen = null;
                while (pen == null) {
                    if (tailmode != 0) {
                        if (!Kernel.IterHasFlat(src, false)) break;
                    } else {
                        if (src.Count() == 0) break;
                        if (src[0].Fetch().mo.HasMRO(Kernel.IterCursorMO)) {
                            P6opaque thunk = new P6opaque(Kernel.GatherIteratorMO);
                            th.coro_return = th;
                            th.MarkSharedChain();
                            thunk.slots[0] = Kernel.NewMuScalar(th);
                            thunk.slots[1] = Kernel.NewMuScalar(Kernel.AnyP);
                            P6opaque lst = new P6opaque(Kernel.ListMO);
                            lst.slots[0] = outq;
                            lst.slots[1] = new VarDeque(Kernel.NewROScalar(thunk));
                            th.caller.resultSlot = Kernel.NewRWListVar(lst);
                            th.lexi0 = 1;
                            return th.Return();
                        }
                    }
                    pen = src.Shift();
                }
                if (pen == null) {
                    if (tailmode != 0)
                        return Kernel.Take(th, Kernel.NewROScalar(Kernel.EMPTYP));
                    P6opaque lst = new P6opaque(Kernel.ListMO);
                    lst.slots[0] = outq;
                    lst.slots[1] = new VarDeque();
                    th.caller.resultSlot = Kernel.NewRWListVar(lst);
                    return th.Return();
                }
                th.lex3 = pen;
                th.ip = 1;
                goto case 1;
            case 1:
                th.ip = 2;
                return flt.Fetch().InvokeMethod(th, "ACCEPTS",
                        new Variable[] { flt, (Variable)th.lex3 }, null);
            case 2:
                Variable r = (Variable) th.resultSlot;
                if (!r.Fetch().mo.mro_raw_Bool.Get(r)) {
                    th.ip = 0;
                    goto case 0;
                }
                if (tailmode != 0) {
                    th.ip = 0;
                    return Kernel.Take(th, (Variable)th.lex3);
                } else {
                    outq.Push((Variable) th.lex3);
                    th.ip = 0;
                    goto case 0;
                }
            case 3:
                th.lex0 = src = new VarDeque();
                th.ip = 0;
                goto case 0;
            default:
                return Kernel.Die(th, "Invalid IP");
        }
    }
Exemple #21
0
    public static VarDeque HashIterRaw(int mode, Variable v)
    {
        P6any o = NominalCheck("$x", v);
        VarHash d = Kernel.UnboxAny<VarHash>(o);
        var s = o.mo.setting;

        VarDeque lv = new VarDeque();

        foreach (KeyValuePair<string,Variable> kv in d) {
            switch (mode) {
                case 0:
                    lv.Push(s.MakeStr(kv.Key));
                    break;
                case 1:
                    lv.Push(kv.Value);
                    break;
                case 2:
                    lv.Push(s.MakeStr(kv.Key));
                    lv.Push(kv.Value);
                    break;
                case 3:
                    P6opaque p = new P6opaque(s.PairMO);
                    p.slots[0] = s.MakeStr(kv.Key);
                    p.slots[1] = kv.Value;
                    lv.Push(p);
                    break;
            }
        }
        return lv;
    }
Exemple #22
0
 public static Variable array_constructor(Variable bits)
 {
     VarDeque rest  = start_iter(bits);
     VarDeque items = new VarDeque();
     while (Kernel.IterHasFlat(rest, true))
         items.Push(Kernel.NewMuScalar(rest.Shift().Fetch()));
     P6any l = new P6opaque(Kernel.ArrayMO);
     l.SetSlot("rest", rest);
     l.SetSlot("items", items);
     return Kernel.NewROScalar(l);
 }
Exemple #23
0
 // This function implements the actual looping part of autothreading
 internal static Variable AutoThread(Compartment c, P6any j,
         Func<Variable,Variable> dgt)
 {
     P6opaque j_ = (P6opaque)j;
     P6any listObj = (P6any) j_.slots[1];
     Variable[] list = Kernel.UnboxAny<Variable[]>(listObj);
     Variable[] nlist = new Variable[list.Length];
     for (int i = 0; i < list.Length; i++) {
         nlist[i] = dgt(list[i]);
     }
     P6any newList = Kernel.BoxRaw(nlist, c.ParcelMO);
     P6opaque newJunc = new P6opaque(c.JunctionMO);
     newJunc.slots[0] = j_.slots[0];
     newJunc.slots[1] = newList;
     return newJunc;
 }
Exemple #24
0
 public static Variable BoxLoS(string[] los)
 {
     VarDeque items = new VarDeque();
     foreach (string i in los)
         items.Push(Kernel.BoxAnyMO(i, Kernel.StrMO));
     P6any l = new P6opaque(Kernel.ListMO);
     l.SetSlot("rest", new VarDeque());
     l.SetSlot("items", items);
     return Kernel.NewRWListVar(l);
 }
Exemple #25
0
    public Frame Backtrack(Frame th)
    {
        // throw away cut or mark-only frames
        while (bt != rootf && (bt.ip < 0)) {
            if (bt.ip == -1) {
                // Special frame that does $*GOAL cleanup ...
                th.LexicalBind("$*GOAL", (Variable)bt.st.subrule_iter);
            }
            bt = bt.prev;
        }
        if (st.pos > global.highwater)
            global.IncHighwater(st.pos);
        if (bt == rootf) {
            if ((flags & RETURN_ONE) != 0) {
                if (Cursor.Trace)
                    Console.WriteLine("Failing {0}@{1} after no matches",
                            name, from);
                return Kernel.Take(th, Kernel.NewROScalar(Kernel.EMPTYP));
            } else {
                if (Cursor.Trace)
                    Console.WriteLine("Failing {0}@{1} after some matches",
                            name, from);
                if (EmptyList == null) {
                    P6opaque lst = new P6opaque(Kernel.ListMO);
                    lst.slots[0 /*items*/] = new VarDeque();
                    lst.slots[1 /*rest*/ ] = new VarDeque();
                    EmptyList = Kernel.NewRWListVar(lst);
                }
                th.caller.resultSlot = EmptyList;
            }

            return th.Return();
        } else {
            th.ip = bt.ip;
            st = bt.st;
            bt = bt.prev;
            return th;
        }
    }
Exemple #26
0
    public static VarDeque HashIterRaw(int mode, Variable v)
    {
        P6any o = NominalCheck("$x", Kernel.AnyMO, v);
        VarHash d = Kernel.UnboxAny<VarHash>(o);

        VarDeque lv = new VarDeque();

        foreach (KeyValuePair<string,Variable> kv in d) {
            switch (mode) {
                case 0:
                    lv.Push(Kernel.BoxAnyMO<string>(kv.Key, Kernel.StrMO));
                    break;
                case 1:
                    lv.Push(kv.Value);
                    break;
                case 2:
                    lv.Push(Kernel.BoxAnyMO<string>(kv.Key, Kernel.StrMO));
                    lv.Push(kv.Value);
                    break;
                case 3:
                    P6opaque p = new P6opaque(Kernel.PairMO);
                    p.slots[0] = Kernel.BoxAnyMO<string>(kv.Key, Kernel.StrMO);
                    p.slots[1] = kv.Value;
                    lv.Push(Kernel.NewROScalar(p));
                    break;
            }
        }
        return lv;
    }
 private Variable FixupList(VarDeque caps) {
     if (caps.Count() != 0 && caps[0] == null) {
         caps.Shift();
         P6opaque l = new P6opaque(global.setting.ListMO);
         l.slots[0 /*items*/] = caps;
         l.slots[1 /*rest*/ ] = new VarDeque();
         return l;
     } else {
         return caps.Count() != 0 ? caps[0] : global.setting.AnyP;
     }
 }