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 void ValidateArgs(FunctionDef fd) { if (fd.Params.Count > argList.Count) { throw new Exception("spiz DOESNT HAVE ENOUGH ARGUMENTS just like you dont have enough chromosomes"); } if (fd.Params.Count < argList.Count) { throw new Exception("spiz HAS TOO MANY ARGUMENTS just like you have too many chromosomes"); } }
public string Compile(FunctionDef fd) { string header = CompileHeader(fd); List <Instruction> list = new List <Instruction>(); CompileBody(fd, list); list.Add(new Instruction(InstructionType.Ret)); string body = ToMSIL(list); return(header + Environment.NewLine + body + "}"); }
private string CompileHeader(FunctionDef fd) { StringBuilder sb = new StringBuilder(); string methodSig = ".method public static [ret_type] [func_name]([params]) cil managed {"; string entryPoint = ".entrypoint"; string locals = ".locals init ([decs])"; methodSig = methodSig.Replace("[ret_type]", MapTypeToMSIL(fd.ReturnType)); methodSig = methodSig.Replace("[func_name]", fd.Name); string prms = ""; foreach (var param in fd.Params) { prms += MapTypeToMSIL(param.Type) + " " + param.Name + ","; } prms = prms.TrimEnd(','); methodSig = methodSig.Replace("[params]", prms); sb.AppendLine(methodSig); if (fd.Name == "spiz") { sb.AppendLine(entryPoint); } List <Declaration> decList = new List <Declaration>(); FindAllDeclarations(fd, decList); string decs = ""; foreach (var dec in decList) { decs += MapTypeToMSIL(dec.Type) + " " + dec.VarName + ","; } decs = decs.TrimEnd(','); locals = locals.Replace("[decs]", decs); sb.AppendLine(locals); return(sb.ToString()); }
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); } } }