private void AddArgsToState(FunctionDef fd) { for (int i = 0; i < fd.Params.Count; i++) { //suspicious copying - needs testing VarState vs = new VarState(fd.Params[i].Name, fd.Params[i].Type, argList[i]); State.Add(fd.Params[i].Name, vs); } }
private bool Foreach(Dospiz loop, bool r) { VarState index; VarState item; //See if your index and variable name are in state if (!State.ContainsKey(loop.ItemName)) { index = new VarState(loop.ItemName + "_i", TokenType.Int, 0); if (!State.ContainsKey(loop.Collection)) { throw new Exception("spiz must have collection '" + loop.Collection + "' defined spiz"); } if (State[loop.Collection].Type != TokenType.Lint && State[loop.Collection].Type != TokenType.Lstr) { throw new Exception("spiz '" + loop.Collection + "' must be a collection spiz"); } List <object> list = (List <object>)State[loop.Collection].Value; item = new VarState(loop.ItemName, loop.ItemType, list[0]); State.Add(index.Name, index); State.Add(item.Name, item); } else { //Grab the index and item from the state item = State[loop.ItemName]; index = State[loop.ItemName + "_i"]; index.Value = (int)index.Value + 1; List <object> list = (List <object>)State[loop.Collection].Value; if (list.Count == (int)index.Value) { r = false; } else { item.Value = list[(int)index.Value]; } } return(r); }
private object Traverse(Node node) { bool conditionCompleted = false; object ret = null; while (true) { if (swordOfKhali) { return(ret); } if (node is FunctionDef) { FunctionDef fd = (node as FunctionDef); ValidateArgs(fd); AddArgsToState(fd); if (node.Children.Count == 0) { throw new Exception("MASSIVE spiz DOESNT HAVE ANYTHING IN HIS DOspiz STATEMENT"); } node = node.Children[0]; continue; } else if (node is spizdun) { if ((node as spizdun).Exp == null) { return(null); } ret = (node as spizdun).Exp.Eval(ast, State); swordOfKhali = true; return(ret); } else if (node is spizout) { if ((node as spizout).Exp == null) { spizoutLevel = 1; } else { spizoutLevel = Convert.ToInt32((node as spizout).Exp.Eval(ast, State)); } } else if (node is FunctionCall) { (node as FunctionCall).Exp.Eval(ast, State); } else if (node is Declaration) { Declaration d = (node as Declaration); object r = null; if (d.Exp != null) { r = d.Exp.Eval(ast, State); } VarState vs = new VarState(d.VarName, d.Type, r); State.Add(vs.Name, vs); } else if (node is Assignment) { Assignment a = (node as Assignment); object r = a.RightExpression.Eval(ast, State); if (a.IsListIndexAssignment()) { object leftIndex = a.LeftIndexExpression.Eval(ast, State); int i = int.Parse(leftIndex.ToString()); var list = (List <object>)State[a.VarName].Value; list[i] = r; } else { State[a.VarName].Value = r; } } else if (node is Spif) { Spif spif = (node as Spif); object r = spif.Exp.Eval(ast, State); if (!(r is bool)) { throw new Exception("spiz spif must evaluate to bool spiz"); } bool rb = (bool)r; if (rb) { if (node.Children.Count == 0) { throw new Exception("MASSIVE spiz DOESNT HAVE ANYTHING IN HIS IF STATEMENT"); } node = node.Children[0]; conditionCompleted = true; continue; } else { conditionCompleted = false; } } else if (node is Spelzif) { Spelzif spelzIf = (node as Spelzif); var prevSib = GetPrevChild(node); if (!(prevSib is Spif || prevSib is Spelzif)) { throw new Exception("spiz cant have else if without previous spif or spelzif spiz"); } if (!(conditionCompleted)) { object r = spelzIf.Exp.Eval(ast, State); if (!(r is bool)) { throw new Exception("spiz spelzif must evaluate to bool spiz"); } bool rb = (bool)r; if (rb) { if (node.Children.Count == 0) { throw new Exception("MASSIVE spiz DOESNT HAVE ANYTHING IN HIS SPELZIF STATEMENT"); } node = node.Children[0]; conditionCompleted = true; continue; } } } else if (node is Spelz) { var prevSib = GetPrevChild(node); if (!(prevSib is Spif || prevSib is Spelzif)) { throw new Exception("spiz cant have spelz without previous spif or elseif spiz"); } if (!(conditionCompleted)) { if (node.Children.Count == 0) { throw new Exception("MASSIVE spiz DOESNT HAVE ANYTHING IN HIS SPELZ STATEMENT"); } node = node.Children[0]; continue; } } else if (node is Dospiz) { Dospiz loop = (node as Dospiz); bool r = true; if (loop.Type == DospizType.While) { r = (bool)loop.Exp.Eval(ast, State); } else if (loop.Type == DospizType.Foreach) { r = Foreach(loop, r); } conditionCompleted = false; if (r) { if (node.Children.Count == 0) { throw new Exception("MASSIVE spiz DOESNT HAVE ANYTHING IN HIS DOspiz STATEMENT"); } node = node.Children[0]; continue; } } //Regular execution Node next = GetNextChild(node); if (next != null && !swordOfKhali) { node = next; } else { return(null); } } }