예제 #1
0
        public Value readNextValue(Qontext context, ValueType expected = ValueType.Any)
        {
            Log.spam("Interpretoken.readNextValue()");
            Token t      = peek();
            Value result = null;

            if ((t.check(ValueType.Identifier)) ||
                (t.check(ValueType.Keyword) &&
                 (t.check(Keyword.CURRENT_CONTEXT.name) ||
                  t.check(Keyword.PARENT_CONTEXT.name))))
            {
                Log.spam("detected possible reference / identifier");
                Reference[] _r = rrra(context);
                Reference   r  = _r[_r.Length - 1];
                if (peek().check(Struqture.Call[OPEN]))
                {
                    Log.spam("hey, it's a funqtion call!");
                    if (r.getValueType() == ValueType.Funqtion || r.getValueType() == ValueType.NativeCall)
                    {
                        Value[] p = Funqtionizer.parseParameters(context, readBody(true));
                        result = (r.getTrueValue() as Funqtion).execute(p, (_r.Length > 1 ? _r[_r.Length - 2] : null));
                    }
                    else
                    {
                        throw new ParseException("can not call " + peek(-1) + ": not a funqtion.");
                    }
                }
                else
                {
                    result = r;
                }
            }
            else if (t.check(ValueType.Struqture))
            {
                Log.spam("detected possible structure");
                if (t.check(Struqture.Funqtion[OPEN]))
                {
                    Log.spam("...it's a funqtion");
                    Funqtion f = Funqtionizer.parse(context, readBody(true));
                    if (peek().check(Struqture.Call[OPEN]))
                    {
                        Value[] p = Funqtionizer.parseParameters(context, readBody(true));
                        result = f.execute(p);
                    }
                    else
                    {
                        result = f;
                    }
                }
                else if (t.check(Struqture.Context[OPEN]))
                {
                    Obqect o = Colleqtionizer.readObqect(context, readBody(true));
                    result = o;
                }
                else if (t.check(Struqture.Collection[OPEN]))
                {
                    Array a = Colleqtionizer.readArray(context, readBody(true));
                    result = a;
                }
                else if (t.check(Struqture.Call[OPEN]))
                {
                    Segment s = Segmentizer.parseOne(context, readBody());
                    result = s.execute(context);
                }
            }
            else if (t.check(ValueType.Primitive))
            {
                result = digest().makeValue();
            }
            else if (t.check(ValueType.Operator))
            {
                throw new ParseException("next token was unreadable as value: " + t, t);
            }

            return(result);
        }
예제 #2
0
        public Segment[] parse(Qontext context, bool first = false)
        {
            Log.spam("Statementizer.execute()");
            if (stack.Length == 0)
            {
                return(new Segment[0]);
            }
            List <Token>   buffer   = new List <Token>();
            List <Segment> segments = new List <Segment>();
            int            level    = 0;

            do
            {
                Token t = digest();
                if (t.check(ValueType.Keyword) && t.getValue <Keyword>().isType(Keyword.KeywordType.DECLARATION))
                {
                    if (buffer.Count > 0)
                    {
                        throw new ParseException("unexpected declaration keyword '" + t.str() + "' while parsing segment.", t);
                    }
                    Keyword keyword = t.getValue <Keyword>();
                    if (keyword == Keyword.FUNQTION)
                    {
                        t = peek();
                        if (!t.check(ValueType.Identifier))
                        {
                            throw new ParseException("expected identifier for declaration of '" + keyword.name + "', got '" + t.str() + "' instead.", t);
                        }
                        string   name = digest().str();
                        Funqtion fq   = Funqtionizer.parse(context, readBody(true));
                        context.set(name, new Reference(fq));
                    }
                    if (peek().check(";"))
                    {
                        digest();
                    }
                }
                else if (t.check(ValueType.Keyword) && t.getValue <Keyword>().isType(Keyword.KeywordType.CONDITION))
                {
                    segments.Add(parseCondition(context, t.getValue <Keyword>()));
                }
                else
                {
                    if (t.check(";") && level == 0)
                    {
                        if (first)
                        {
                            return new Segment[] { new Segment(buffer.ToArray()) }
                        }
                        ;
                        else
                        {
                            segments.Add(new Segment(buffer.ToArray()));
                        }
                        buffer.Clear();
                    }
                    else
                    {
                        if (t.check(Struqture.Context[OPEN]))
                        {
                            level++;
                        }
                        else if (t.check(Struqture.Context[CLOSE]))
                        {
                            level--;
                        }
                        buffer.Add(t);
                        if (endOfStack())
                        {
                            segments.Add(new Segment(buffer.ToArray()));
                        }
                    }
                }
            } while (!endOfStack());
            return(segments.ToArray());
        }