コード例 #1
0
ファイル: Qroup.cs プロジェクト: qrakhen/sqript
 /// <summary>
 /// Qlass.value is a Dictionary with all STATIC references, so a qlass without any static properties won't have anything inside its value.
 /// </summary>
 /// <param name="context"></param>
 public Qroup(
     Qontext parent,
     Dictionary <string, Qlass> qlasses       = null,
     Dictionary <string, Reference> subQroups = null) : base(parent, ValueType.Qlass, subQroups)
 {
     qlasses = (qlasses ?? new Dictionary <string, Qlass>());
 }
コード例 #2
0
        public Value[] parseParameters(Qontext context)
        {
            List <Value> parameters = new List <Value>();
            Token        t          = peek();

            if (t.check(Struqture.Call[OPEN]))
            {
                do
                {
                    var seg = Segmentizer.parseOne(context, readBody(false, ",)"));
                    parameters.Add(seg.execute(context));
                    shift(-1);
                    if (peek().check(Struqture.Call[CLOSE]))
                    {
                        break;
                    }
                    if (peek().check(Struqture.Call[DEL]))
                    {
                        continue;
                    }
                    else
                    {
                        throw new ParseException("unexpected token found when trying to parse funqtion call", t);
                    }
                } while (!endOfStack());
                return(parameters.ToArray());
            }
            else
            {
                throw new ParseException("unexpected funqtion call parameter opening, expected '('", t);
            }
        }
コード例 #3
0
 /// <summary>
 /// Qlass.value is a Dictionary with all STATIC references, so a qlass without any static properties won't have anything inside its value.
 /// </summary>
 /// <param name="context"></param>
 public Qlass(
     Qontext parent,
     Dictionary <string, Property> instanceProperties = null,
     Dictionary <string, Reference> staticReferences  = null) : base(parent, ValueType.Qlass, staticReferences)
 {
     properties = (instanceProperties ?? new Dictionary <string, Property>());
 }
コード例 #4
0
        /************************************
         *  *~ a <~ 5 + (8 / 2);
         *
         *     { <- head node
         *     a <~ {
         *          5 + {
         *              (5 / 2); <- another segment with its own head node
         *              }
         *          }
         *     }
         **/
        public virtual Value execute(Qontext context)
        {
            Log.spam("segment.execute()");
            reset();
            returning = false;
            head      = new Node();
            Node tail = build(context, new Node());

            Log.spam("executing node:\n" + tail);
            Value r = Value.Null;

            if (tail.empty() && !head.empty())
            {
                r = head.execute();
            }
            else
            {
                r = tail.execute();
                if (!head.empty())
                {
                    head.right = r;
                    head.execute();
                }
            }
            if (!head.empty() && head.left is FloatingReference <string> )
            {
                (head.left as FloatingReference <string>).bind();
            }
            else if (!head.empty() && head.left is FloatingReference <int> )
            {
                (head.left as FloatingReference <int>).bind();
            }
            return(r);
        }
コード例 #5
0
        public override Value execute(Qontext context)
        {
            Value p = new Value(true, ValueType.Boolean);

            do
            {
                if (loopType == HEAD)
                {
                    p = base.execute(context);
                }
                if (!p.isType(ValueType.Boolean))
                {
                    throw new ConditionException("expression for loop condition has to return a value of type BOOL, got " + p.type.ToString() + " instead.");
                }
                if (p.getValue <bool>())
                {
                    Funqtion fq = new Funqtion(context, new List <Segment>(body));
                    fq.execute();
                }
                else
                {
                    break;
                }
                if (loopType == FOOT)
                {
                    p = base.execute(context);
                }
            } while (true);
            return(null);
        }
コード例 #6
0
ファイル: Expressionizer.cs プロジェクト: qrakhen/sqript
        public Expression parse(Qontext context)
        {
            Expression head = null;
            Expr       expr = new Expr();

            do
            {
                Token t = peek();
                Value v = null;
                if (t.type == ValueType.OPERATOR)
                {
                    if (head != null)
                    {
                        expr.left = head;
                    }
                    else if (expr.left == null)
                    {
                        throw new Exception("manipulation operators (-1, i++) are not yet implemented. thank you for your patience.", t);
                    }
                    if (expr.right == null)
                    {
                        expr.op = digest().getValue <Operator>();
                    }
                    else
                    {
                        throw new Exception("unexpected operator after left and right token have been read: " + expr.left.ToString() + " " + expr.right.ToString(), t);
                    }
                }
                else
                {
                    v = readNextValue(context);
                    if (v == null)
                    {
                        throw new Exception("expected any value while parsing expression, got '" + v + "' instead.");
                    }
                }

                if (v != null)
                {
                    if (expr.left == null)
                    {
                        expr.left = v;
                    }
                    else if (expr.right == null)
                    {
                        expr.right = v;
                    }
                }

                if (expr.ready() || endOfStack())
                {
                    head = new Expression(expr.op, expr.left, expr.right, context);
                    expr = new Expr();
                }
            } while (!endOfStack());
            reset();
            return(head);
        }
コード例 #7
0
ファイル: Funqtion.cs プロジェクト: qrakhen/sqript
 public Funqtion(
     Qontext parent,
     Dictionary <string, Reference> references,
     List <Segment> segments,
     List <string> parameters = null,
     ValueType type           = ValueType.Funqtion) : base(parent, type, references)
 {
     this.segments   = segments;
     this.parameters = parameters ?? new List <string>();
 }
コード例 #8
0
ファイル: Interpretoken.cs プロジェクト: qrakhen/sqript
 public Reference rrr(Qontext context, int item = 0)
 {
     Reference[] r = rrra(context);
     if (r.Length > item)
     {
         return(r[r.Length - (item + 1)]);
     }
     else
     {
         return(null);
     }
 }
コード例 #9
0
        public Funqtion parse(Qontext context)
        {
            Token t = digest();

            if (t.check(Struqture.Funqtion[OPEN]))
            {
                Funqtion fq = new Funqtion(context);
                do
                {
                    t = peek();
                    if (t.check(Struqture.Context[OPEN]))
                    {
                        break;
                    }
                    else if (t.check(ValueType.Identifier))
                    {
                        fq.parameters.Add(t.str());
                        digest();
                    }
                    else
                    {
                        throw new ParseException("unexpected token found when trying to parse funqtion parameter declaration", t);
                    }
                } while (!endOfStack());
                if (endOfStack())
                {
                    throw new ParseException("unexpected end of stack when trying to parse funqtion parameter declaration", t);
                }
                else
                {
                    Token[] body = readBody();
                    fq.segments.AddRange(new Segmentizer(body).parse(fq));
                    if (peek().check(Struqture.Funqtion[CLOSE]))
                    {
                        return(fq);
                    }
                    else if (peek().check(Struqture.Funqtion[DEL]))
                    {
                        throw new FunqtionizerException("funqtions overloads not yet implemented", peek());
                    }
                    else
                    {
                        throw new ParseException("unexpected token found when trying to parse funqtion body definition", peek());
                    }
                }
            }
            else
            {
                throw new ParseException("unexpected funqtion parameter opening, expected '~('", t);
            }
        }
コード例 #10
0
ファイル: Segmentizer.cs プロジェクト: qrakhen/sqript
 protected Segment parseCondition(Qontext context, Keyword keyword)
 {
     if (keyword == Keyword.CONDITION_IF)
     {
         Token[]   premise = readBody();
         Segment[] body    = parse(context, readBody());
         Log.spam("creating new ifElseSegment");
         IfElseSegment ifElse = new IfElseSegment(body, premise);
         if (peek().check(ValueType.Keyword) && peek().getValue <Keyword>() == Keyword.CONDITION_ELSE)
         {
             digest();
             if (peek().check(ValueType.Keyword) && peek().check(Keyword.CONDITION_IF))
             {
                 digest();
                 Log.spam("appending else if (...) continuation...");
                 ifElse.append((IfElseSegment)parseCondition(context, keyword));
             }
             else
             {
                 Log.spam("appending premise-less else continuation...");
                 ifElse.append(new IfElseSegment(parse(context, readBody()), new Token[0]));
             }
         }
         return(ifElse);
     }
     else if (keyword == Keyword.CONDITION_LOOP)
     {
         if (peek().check(ValueType.Struqture) && peek().check(Struqture.Call[OPEN]))
         {
             Token[]     premise = readBody();
             Segment[]   body    = parse(context, readBody());
             LoopSegment loop    = new LoopSegment(body, LoopSegment.HEAD, premise);
             return(loop);
         }
         else if (peek().check(ValueType.Struqture) && peek().check(Struqture.Context[OPEN]))
         {
             Segment[]   body    = parse(context, readBody());
             Token[]     premise = readBody();
             LoopSegment loop    = new LoopSegment(body, LoopSegment.FOOT, premise);
             return(loop);
         }
         else
         {
             new OperationException("loop condition expected '(' or '{' to start premise or body - got '" + peek().str() + "' instead.");
         }
     }
     throw new OperationException("wanted to parse condition, but could not. debug me!!");
 }
コード例 #11
0
        public override Value execute(Qontext context)
        {
            Value r = stack.Length == 0 ? Value.True : base.execute(context);

            if (!r.isType(ValueType.Boolean))
            {
                throw new ConditionException("expression for loop condition has to return a value of type BOOLEAN, got " + r.type.ToString() + " instead.");
            }
            else if (r.getValue <bool>())
            {
                Funqtion xfq = new Funqtion(context, new List <Segment>(body));
                xfq.execute();
            }
            else if (next != null)
            {
                next.execute(context);
            }
            return(null);
        }
コード例 #12
0
ファイル: Qontext.cs プロジェクト: qrakhen/sqript
        public Reference get(string[] keys)
        {
            Qontext c = this;

            for (int i = 0; i < keys.Length - 1; i++)
            {
                if (c.value.ContainsKey(keys[i]))
                {
                    if (c.value[keys[i]].getValue() is Qontext)
                    {
                        c = c.value[keys[i]].getValue <Qontext>();
                    }
                }
                else
                {
                    return(null);
                }
            }
            if (c.value.ContainsKey(keys[keys.Length - 1]))
            {
                return(c.value[keys[keys.Length - 1]]);
            }
            return(null);
        }
コード例 #13
0
ファイル: Qontext.cs プロジェクト: qrakhen/sqript
 public Qontext(Qontext parent, ValueType type, Dictionary <string, Reference> value, bool extendable = true) : base(type, value)
 {
     this.parent     = parent;
     this.extendable = extendable;
 }
コード例 #14
0
 public Instance(Qlass constructor, Qontext parent, Dictionary <string, Reference> value) : base(parent, value)
 {
     this.constructor = constructor;
 }
コード例 #15
0
ファイル: Qontext.cs プロジェクト: qrakhen/sqript
 public StatiqQontext(Qontext parent) : base(parent)
 {
 }
コード例 #16
0
        protected virtual Node build(Qontext context, Node node = null)
        {
            int step = 0;

            do
            {
                if (node == null)
                {
                    node = new Node();
                }
                Token t = peek();
                if (t.check(ValueType.Keyword))
                {
                    if (t.check(Keyword.REFERENCE))
                    {
                        if (node.left != null)
                        {
                            throw new ParseException("can not declare a new reference here ._.", t);
                        }
                        type = SegmentType.REFERENCE;
                        digest();
                        t = peek();
                        if (t.check(ValueType.Identifier))
                        {
                            head.left = new FloatingReference <string>(digest().str(), context);
                        }
                        else
                        {
                            throw new ParseException("expected identifier after new reference keyword, got '" + t + "' instead", t);
                        }
                    }
                    else if (t.check(Keyword.CURRENT_CONTEXT) || t.check(Keyword.PARENT_CONTEXT))
                    {
                        if (head.empty())
                        {
                            head = new Node(rrr(context), null, null);
                        }
                        else
                        {
                            node.put(rrr(context));
                        }
                    }
                    else if (t.check(Keyword.RETURN))
                    {
                        digest();
                        returning = true;
                    }
                    else
                    {
                        throw new Exception("check me");
                    }
                }
                else if (t.check(ValueType.Operator))
                {
                    Operator op = digest().getValue <Operator>();
                    if (op.symbol == Operator.ASSIGN_VALUE || op.symbol == Operator.ASSIGN_REFERENCE)
                    {
                        if (step == 0)
                        {
                            returning = true;
                        }
                        else if (head.empty() && step == 1)
                        {
                            head.left = node.left;
                            node.left = null;
                        }
                    }
                    if (node.ready())
                    {
                        Log.spam("comparing operators to chain nodes");
                        if (op.compare(node.op) > 0)
                        {
                            node.right = build(context, new Node(node.right, null, op));
                        }
                        else
                        {
                            node = build(context, new Node(node, null, op));
                        }
                    }
                    else if (head.op == null && (
                                 op.symbol == Operator.ASSIGN_VALUE ||
                                 op.symbol == Operator.ASSIGN_REFERENCE))
                    {
                        head.op = op;
                    }
                    else if (node.left == null || (node.op != null && node.right == null))
                    {
                        if (op.symbol == Operator.CALCULATE_ADD)
                        {
                            continue;
                        }
                        else if (op.symbol == Operator.CALCULATE_SUBTRACT)
                        {
                            var neg = new Segment(new Token[] {
                                Token.create(ValueType.Number, "-1"),
                                Token.create(ValueType.Operator, Operator.CALCULATE_MULTIPLY),
                                digest()
                            });
                            Log.spam("created negating segment: " + neg.ToString());
                            if (node.left == null)
                            {
                                node.left = neg.execute(context);
                            }
                            else
                            {
                                node.right = neg.execute(context);
                            }
                        }
                        else
                        {
                            throw new ParseException("unexpected operator " + op.symbol + " at start of expression", t);
                        }
                    }
                    else if (node.left != null)
                    {
                        node.op = op;
                    }
                    else
                    {
                        throw new ParseException("sorry, manipulation operators (operators without left-hand value) are not yet implemented. :/", t);
                    }
                }
                else if (t.check(ValueType.Struqture, "("))
                {
                    if (node.ready())
                    {
                        throw new ParseException("can not open new segment without prior operator.", t);
                    }
                    Segment sub = new Segment(readBody());
                    node.put(sub.execute(context));
                }
                else
                {
                    if (t.check(";") || endOfStack())
                    {
                        Log.spam("end of node chain reached: ';'");
                        digest();
                        break;
                    }
                    if (node.ready())
                    {
                        throw new ParseException("can not add another value to finished new segment node without prior operator.", t);
                    }
                    Value v = readNextValue(context);
                    node.put(v);
                }
                step++;
            } while (!endOfStack());
            return(node);
        }
コード例 #17
0
ファイル: Interface.cs プロジェクト: qrakhen/sqript
 public Funqtion(Qontext parent, Call call) : base(parent)
 {
     this.call = call;
 }
コード例 #18
0
ファイル: Obqect.cs プロジェクト: qrakhen/sqript
 public Obqect(Qontext parent, Dictionary <string, Reference> value, bool extendable = true)
     : base(parent, ValueType.Obqect, value, extendable)
 {
 }
コード例 #19
0
ファイル: Funqtion.cs プロジェクト: qrakhen/sqript
 public Funqtion(Qontext parent, Func <Value, Value[], Value> nativeCallback) : this(parent, null, null, null, ValueType.NativeCall)
 {
     this.nativeCallback = nativeCallback;
 }
コード例 #20
0
ファイル: Funqtion.cs プロジェクト: qrakhen/sqript
 public Funqtion(Qontext parent) : this(parent, new Dictionary <string, Reference>(), new List <Segment>())
 {
 }
コード例 #21
0
ファイル: Funqtion.cs プロジェクト: qrakhen/sqript
 public Funqtion(Qontext parent, ValueType type) : this(parent, null, null, null, type)
 {
 }
コード例 #22
0
ファイル: Segmentizer.cs プロジェクト: qrakhen/sqript
 public Segment parseOne(Qontext context)
 {
     return(parse(context, true)[0]);
 }
コード例 #23
0
ファイル: Segmentizer.cs プロジェクト: qrakhen/sqript
 public static Segment parseOne(Qontext context, Token[] stack)
 {
     return(new Segmentizer(stack).parseOne(context));
 }
コード例 #24
0
ファイル: Segmentizer.cs プロジェクト: qrakhen/sqript
        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());
        }
コード例 #25
0
ファイル: Interpretoken.cs プロジェクト: qrakhen/sqript
        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);
        }
コード例 #26
0
 public static Funqtion parse(Qontext context, Token[] stack)
 {
     return(new Funqtionizer(stack).parse(context));
 }
コード例 #27
0
ファイル: Qontext.cs プロジェクト: qrakhen/sqript
 public Qontext(Qontext parent, bool extendable = true) : base(ValueType.Qontext, new Dictionary <string, Reference>())
 {
     this.parent     = parent;
     this.extendable = extendable;
 }
コード例 #28
0
ファイル: Funqtion.cs プロジェクト: qrakhen/sqript
 public Funqtion(Qontext parent, List <Segment> statements) : this(parent, new Dictionary <string, Reference>(), statements)
 {
 }
コード例 #29
0
 public static Value[] parseParameters(Qontext context, Token[] stack)
 {
     return(new Funqtionizer(stack).parseParameters(context));
 }
コード例 #30
0
ファイル: Obqect.cs プロジェクト: qrakhen/sqript
 public Obqect(Qontext parent, bool extendable = true)
     : this(parent, new Dictionary <string, Reference>(), extendable)
 {
 }