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); }
public virtual Value execute(Value[] parameters = null, Value caller = null) { if (nativeCallback != null) { return(nativeCallback(caller, parameters)); } // we need to store all references in a temporary xfq (execution funqtion) so that the original funqtion is not mutated Log.spam("executing function:\n" + this.ToString()); Funqtion xfq = new Funqtion(parent); if (parameters != null) { for (int i = 0; i < parameters.Length; i++) { if (i >= this.parameters.Count) { throw new Exception("more parameters provided than funqtion accepts"); } Log.spam(this.parameters[i] + " = " + parameters[i].str()); xfq.set(this.parameters[i], new Reference(parameters[i])); } } foreach (Segment s in segments) { Value r = s.execute(xfq); if (s.returning && r != null) { Log.spam("reached return statement, returning " + r.str()); return(r); } } return(null); }
public static void define(ValueType type, string name, Func <Value, Value[], Value> callback) { Funqtion fq = new Funqtion(null, callback); if (!native.ContainsKey(type)) { native.Add(type, new StatiqQontext(null)); } native[type].set(name, new Reference(fq)); }
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); } }
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); }
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); }
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()); }