private void getexp1(Code c, bool l = false) { if (!l) getTerm(c); while (t.checkNext() != "") { switch (t.checkNext()) { case "*": t.getNext(); getTerm(c); c.PushCode(new Mul()); break; case "/": t.getNext(); getTerm(c); c.PushCode(new Div()); break; case "%": t.getNext(); getTerm(c); c.PushCode(new Mod()); break; default: return; } } }
public bool Parse(Variable v) { var c = new Code(); c.PushCode(new PushVariable("arg")); c.PushCode(new Argument()); while (t.checkNext() != "") ParseStatement(v, c); v.setCode(c); return error != 0; }
private void getexp12(Code c, bool l = false) { getexp11(c, l); while (t.checkNext() != "") { if (t.checkNext() != ",") break; t.getNext(); getexp11(c); c.PushCode(new List()); } }
private void ParseIf(Variable v, Code c) { var i = new Code(); if (t.checkNext() != "(") Error(); else t.getNext(); ParseExpression(i, ")"); OpCode oc = null; int l = i.Length; if (l > 0) { oc = new Jrf(0); i.PushCode(oc); l = i.Length; } else Error(); ParseStatement(v, i); if (t.checkNext() == "identifier" && t.nstr == "else") { t.getNext(); if (oc != null) oc.Set(i.Length + 1 - l); oc = new Jr(0); i.PushCode(oc); l = i.Length; ParseStatement(v, i); oc.Set(i.Length - l); } else { if (oc != null) oc.Set(i.Length - l); } c.PushCodes(i); }
private void getexp3(Code c, bool l = false) { getexp2(c, l); while (t.checkNext() != "") { switch (t.checkNext()) { case "<<": t.getNext(); getexp2(c); c.PushCode(new Shl()); break; case ">>": t.getNext(); getexp2(c); c.PushCode(new Shr()); break; default: return; } } }
public void PushCodes(Code c) { code = code.Concat(c.code).ToList<OpCode>(); }
public virtual void setCode(Code c) { }
public FunctionScope(Code c) : base(c) { }
public LoopScope(Code c, int i) : base(c) { cline = i; }
private void getexp9(Code c, bool l = false) { getexp8(c, l); }
private void getexp8(Code c, bool l = false) { getexp7(c, l); while (t.checkNext() != "") { switch (t.checkNext()) { case "==": t.getNext(); getexp7(c); c.PushCode(new Eq()); break; case "!=": t.getNext(); getexp7(c); c.PushCode(new Ne()); break; default: return; } } }
private void getexp7(Code c, bool l = false) { getexp6(c, l); while (t.checkNext() != "") { switch (t.checkNext()) { case "<": t.getNext(); getexp6(c); c.PushCode(new Lt()); break; case ">": t.getNext(); getexp6(c); c.PushCode(new Gt()); break; case "<=": t.getNext(); getexp6(c); c.PushCode(new Le()); break; case ">=": t.getNext(); getexp6(c); c.PushCode(new Ge()); break; default: return; } } }
private void getexp6(Code c, bool l = false) { getexp5(c, l); while (t.checkNext() != "") { if (t.checkNext() != "|") break; t.getNext(); getexp5(c); c.PushCode(new Or()); } }
private void getexp4(Code c, bool l = false) { getexp3(c, l); while (t.checkNext() != "") { if (t.checkNext() != "&") break; t.getNext(); getexp3(c); c.PushCode(new And()); } }
private void ParseStatement(Variable v, Code c) { var n = t.checkNext(); if (n == ";") { t.getNext(); return; } if (n == "{") { t.getNext(); var x = new Code(); ParseBlock(v, x); c.PushCodes(x); return; } if (n == "identifier") { if (t.nstr == "if") { t.getNext(); ParseIf(v, c); return; } else if (t.nstr == "for") { t.getNext(); ParseFor(v, c); return; } } c.PushCode(new Pop()); ParseExpression(c, ";"); }
private void getexp11(Code c, bool l = false) { getexp10(c, l); }
private void getSuffOp(Code c) { while (t.checkNext() != "") { switch (t.checkNext()) { case "++": t.getNext(); break; case "--": t.getNext(); break; case "(": t.getNext(); if (t.checkNext() == ")") { t.getNext(); c.PushCode(new PushNull()); } else { ParseExpression(c, ")"); } c.PushCode(new Call()); break; case "[": t.getNext(); break; case ".": t.getNext(); break; case "`": t.getNext(); break; default: return; } } }
private void getexp13(Code c, bool l = false) { getexp12(c, l); while (t.checkNext() != "") { switch (t.checkNext()) { case "=": t.getNext(); getexp13(c); c.PushCode(new Substitution()); break; case ":=": t.getNext(); getexp13(c); c.PushCode(new Assignment()); break; case "+=": t.getNext(); getexp13(c); c.PushCode(new SAdd()); break; case "-=": t.getNext(); getexp13(c); c.PushCode(new SSub()); break; case "*=": t.getNext(); getexp13(c); c.PushCode(new SMul()); break; case "/=": t.getNext(); getexp13(c); c.PushCode(new SDiv()); break; case "%=": t.getNext(); getexp13(c); c.PushCode(new SMod()); break; case "&=": t.getNext(); getexp13(c); c.PushCode(new SAnd()); break; case "|=": t.getNext(); getexp13(c); c.PushCode(new SOr()); break; case "^=": t.getNext(); getexp13(c); c.PushCode(new SXor()); break; case "<<=": t.getNext(); getexp13(c); c.PushCode(new SShl()); break; case ">>=": t.getNext(); getexp13(c); c.PushCode(new SShr()); break; default: return; } } }
private void getTerm(Code c) { switch (t.checkNext()) { case "(": t.getNext(); ParseExpression(c, ")"); break; case "+": t.getNext(); getTerm(c); break; case "-": t.getNext(); getTerm(c); c.PushCode(new Minus()); break; case "!": t.getNext(); getTerm(c); c.PushCode(new Not()); break; case "~": t.getNext(); getTerm(c); c.PushCode(new Compl()); break; case "*": t.getNext(); getTerm(c); break; case "&": t.getNext(); getTerm(c); break; case "$": t.getNext(); getTerm(c); break; case "++": t.getNext(); getTerm(c); break; case "--": t.getNext(); getTerm(c); break; case "identifier": t.getNext(); c.PushCode(new PushVariable(t.nstr)); getSuffOp(c); break; case "int": t.getNext(); c.PushCode(new PushInt(t.nint)); getSuffOp(c); break; case "string": t.getNext(); c.PushCode(new PushString(t.nstr)); getSuffOp(c); break; default: Error(); break; } }
public Scope(Code c) { code = c; }
private void ParseBlock(Variable v, Code c) { while (t.checkNext() != "") { if (t.checkNext() == "}") { t.getNext(); return; } ParseStatement(v, c); } Error(); }
internal void setCode(Code c) { x.setCode(c); }
private void ParseExpression(Code c, string e, bool l = false) { getexp13(c, l); if (t.checkNext() == e) t.getNext(); else Error(); }
public override void setCode(Code c) { code = c; }
private void ParseFor(Variable v, Code c) { var f = new Code(); if (t.checkNext() != "(") Error(); else t.getNext(); int l = 0; if (t.checkNext() != ";") { f.PushCode(new Pop()); ParseExpression(f, ";"); l = f.Length; } else t.getNext(); OpCode oc = null; if (t.checkNext() != ";") { ParseExpression(f, ";"); if (f.Length - l > 0) { oc = new Jf(f.Length); f.PushCode(oc); } } else t.getNext(); var x = new Code(); if (t.checkNext() != ")") ParseExpression(x, ")"); else t.getNext(); ParseStatement(v, f); int cline = f.Length; if (x.Length > 0) { f.PushCode(new Pop()); f.PushCodes(x); } f.PushCode(new Jmp(l)); if (oc != null) oc.Set(f.Length); c.PushCode(new For(f, cline)); }
public For(Code c, int i) { code = c; cline = i; }
private void getexp2(Code c, bool l = false) { getexp1(c, l); while (t.checkNext() != "") { switch (t.checkNext()) { case "+": t.getNext(); getexp1(c); c.PushCode(new Add()); break; case "-": t.getNext(); getexp1(c); c.PushCode(new Sub()); break; default: return; } } }