Ejemplo n.º 1
0
 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;
         }
     }
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 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;
         }
     }
 }
Ejemplo n.º 4
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());
     }
 }
Ejemplo n.º 5
0
 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, ";");
 }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
0
 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));
 }
Ejemplo n.º 8
0
 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;
     }
 }
Ejemplo n.º 9
0
 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;
         }
     }
 }
Ejemplo n.º 10
0
 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;
         }
     }
 }
Ejemplo n.º 11
0
 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;
         }
     }
 }
Ejemplo n.º 12
0
 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());
     }
 }
Ejemplo n.º 13
0
 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());
     }
 }
Ejemplo n.º 14
0
 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;
         }
     }
 }
Ejemplo n.º 15
0
 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;
         }
     }
 }