private void EmitRel(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
        {
            int jmpLab = ctv.JumpLabel++;

            codes.Add(new HRMCode(HRMInstr.Lab, jmpLab));
            cond.Emit(codes, vtab, ctv);
            int jnLab = ctv.JumpLabel++;

            codes.Add(new HRMCode(HRMInstr.Jn, jnLab));
            int jzLab = int.MaxValue;

            if (!cond.Comparer.Contains("="))
            {
                jzLab = ctv.JumpLabel++;
                codes.Add(new HRMCode(HRMInstr.Jz, jzLab));
            }
            loop.Emit(codes, vtab, ctv);
            foreach (var cj in ctv.ContinueLoopJump)
            {
                codes.Add(new HRMCode(HRMInstr.Lab, cj));
            }
            codes.Add(new HRMCode(HRMInstr.Jmp, jmpLab));
            if (!cond.Comparer.Contains("="))
            {
                codes.Add(new HRMCode(HRMInstr.Lab, jzLab));
            }
            codes.Add(new HRMCode(HRMInstr.Lab, jnLab));
            foreach (var bkj in ctv.BreakLoopJump)
            {
                codes.Add(new HRMCode(HRMInstr.Lab, bkj));
            }
        }
 public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
 {
     if (IsConstantExpression)
     {
         return;
     }
     if (elements[0].Variable == "hand")
     {
         if (!elements[0].Positive)
         {
             throw new CompileException(ErrorHelper.MinusHand);
         }
     }
     else
     {
         int addr = Helper.GetAddress(vtab, elements[0].Variable);
         codes.Add(new HRMCode(HRMInstr.Cpyf, addr));
         if (!elements[0].Positive)
         {
             // get zero
             codes.Add(new HRMCode(HRMInstr.Sub, addr));
             // get -var
             codes.Add(new HRMCode(HRMInstr.Sub, addr));
         }
     }
     for (int i = 1; i < elements.Count; i++)
     {
         codes.Add(
             new HRMCode(
                 elements[i].Positive ? HRMInstr.Add : HRMInstr.Sub,
                 Helper.GetAddress(vtab, elements[i].Variable)));
     }
 }
        private void EmitEqu(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
        {
            cond.Emit(codes, vtab, ctv);
            int jzLab = ctv.JumpLabel++;

            codes.Add(new HRMCode(HRMInstr.Jz, jzLab));
            if (cond.Comparer == "!=")
            {
                ifBlock?.Emit(codes, vtab, ctv);
            }
            else
            {
                elseBlock?.Emit(codes, vtab, ctv);
            }
            int jmpLab = ctv.JumpLabel++;

            codes.Add(new HRMCode(HRMInstr.Jmp, jmpLab));
            codes.Add(new HRMCode(HRMInstr.Lab, jzLab));
            if (cond.Comparer == "!=")
            {
                elseBlock?.Emit(codes, vtab, ctv);
            }
            else
            {
                ifBlock?.Emit(codes, vtab, ctv);
            }
            codes.Add(new HRMCode(HRMInstr.Lab, jmpLab));
        }
 public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
 {
     foreach (var stmt in stmts)
     {
         stmt.Emit(codes, vtab, ctv);
     }
 }
 public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
 {
     exp.Emit(codes, vtab, ctv);
     if (dest != "hand")
     {
         codes.Add(new HRMCode(HRMInstr.Cpyt, Helper.GetAddress(vtab, dest)));
     }
 }
Пример #6
0
        public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
        {
            if (ctv.ContinueLoopJump == null)
            {
                throw new CompileException(ErrorHelper.InvailedBC);
            }
            int jmpLab = ctv.JumpLabel++;

            ctv.ContinueLoopJump.Add(jmpLab);
            codes.Add(new HRMCode(HRMInstr.Jmp, jmpLab));
        }
Пример #7
0
        public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
        {
            HashSet <int> prevBreakJmp    = ctv.BreakLoopJump;
            HashSet <int> prevContinueJmp = ctv.ContinueLoopJump;

            ctv.BreakLoopJump    = new HashSet <int>();
            ctv.ContinueLoopJump = new HashSet <int>();

            if (cond.IsConstantExpression)
            {
                if (cond.ConstantResult)
                {
                    int jmpLab = ctv.JumpLabel++;
                    codes.Add(new HRMCode(HRMInstr.Lab, jmpLab));
                    loop.Emit(codes, vtab, ctv);
                    foreach (var cj in ctv.ContinueLoopJump)
                    {
                        codes.Add(new HRMCode(HRMInstr.Lab, cj));
                    }
                    codes.Add(new HRMCode(HRMInstr.Jmp, jmpLab));
                    foreach (var bkj in ctv.BreakLoopJump)
                    {
                        codes.Add(new HRMCode(HRMInstr.Lab, bkj));
                    }
                }
                else
                {
                    loop.Emit(codes, vtab, ctv);
                    foreach (var cj in ctv.ContinueLoopJump)
                    {
                        codes.Add(new HRMCode(HRMInstr.Lab, cj));
                    }
                    foreach (var bkj in ctv.BreakLoopJump)
                    {
                        codes.Add(new HRMCode(HRMInstr.Lab, bkj));
                    }
                }
            }
            else
            {
                //if (cond.Comparer == "==" || cond.Comparer == "!=")
                //    EmitEqu(codes, vtab, ctv);
                //else
                //    EmitRel(codes, vtab, ctv);
            }

            ctv.BreakLoopJump    = prevBreakJmp;
            ctv.ContinueLoopJump = prevContinueJmp;
        }
        public HRMProgram(string code)
        {
            parser = new Parser(code);
            ctv    = new CompileTimeVars(code);
            string token = parser.GetNextToken();

            if (token == "var")
            {
                CreateVarTab();
            }
            block = new StmtBlock(parser);
            token = parser.GetNextToken();
            if (!string.IsNullOrWhiteSpace(token))
            {
                throw new CompileException(ErrorHelper.UnexpectedEndOfProgram);
            }
        }
 private void EmitEqu(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
 {
     if (cond.Comparer == "!=")
     {
         int jmpLab = ctv.JumpLabel++;
         codes.Add(new HRMCode(HRMInstr.Lab, jmpLab));
         cond.Emit(codes, vtab, ctv);
         int jzLab = ctv.JumpLabel++;
         codes.Add(new HRMCode(HRMInstr.Jz, jzLab));
         loop.Emit(codes, vtab, ctv);
         foreach (var cj in ctv.ContinueLoopJump)
         {
             codes.Add(new HRMCode(HRMInstr.Lab, cj));
         }
         codes.Add(new HRMCode(HRMInstr.Jmp, jmpLab));
         codes.Add(new HRMCode(HRMInstr.Lab, jzLab));
         foreach (var bkj in ctv.BreakLoopJump)
         {
             codes.Add(new HRMCode(HRMInstr.Lab, bkj));
         }
     }
     else
     {
         int jmpLab = ctv.JumpLabel++;
         codes.Add(new HRMCode(HRMInstr.Lab, jmpLab));
         cond.Emit(codes, vtab, ctv);
         int jzLab = ctv.JumpLabel++;
         codes.Add(new HRMCode(HRMInstr.Jz, jzLab));
         int joutLab = ctv.JumpLabel++;
         codes.Add(new HRMCode(HRMInstr.Jmp, joutLab));
         codes.Add(new HRMCode(HRMInstr.Lab, jzLab));
         loop.Emit(codes, vtab, ctv);
         foreach (var cj in ctv.ContinueLoopJump)
         {
             codes.Add(new HRMCode(HRMInstr.Lab, cj));
         }
         codes.Add(new HRMCode(HRMInstr.Jmp, jmpLab));
         codes.Add(new HRMCode(HRMInstr.Lab, joutLab));
         foreach (var bkj in ctv.BreakLoopJump)
         {
             codes.Add(new HRMCode(HRMInstr.Lab, bkj));
         }
     }
 }
 public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
 {
     if (cond.IsConstantExpression)
     {
         if (cond.ConstantResult)
         {
             ifBlock?.Emit(codes, vtab, ctv);
         }
         else
         {
             elseBlock?.Emit(codes, vtab, ctv);
         }
     }
     else if (cond.Comparer == "==" || cond.Comparer == "!=")
     {
         EmitEqu(codes, vtab, ctv);
     }
     else
     {
         EmitRel(codes, vtab, ctv);
     }
 }
 public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
 {
     if (cmd == "read")
     {
         codes.Add(new HRMCode(HRMInstr.In));
         if (var != "hand")
         {
             codes.Add(new HRMCode(HRMInstr.Cpyt, Helper.GetAddress(vtab, var)));
         }
     }
     else if (cmd == "write")
     {
         if (var != "hand")
         {
             codes.Add(new HRMCode(HRMInstr.Cpyf, Helper.GetAddress(vtab, var)));
         }
         codes.Add(new HRMCode(HRMInstr.Out));
     }
     else
     {
         throw new CompileException(ErrorHelper.Unknown);
     }
 }
        private void EmitRel(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
        {
            cond.Emit(codes, vtab, ctv);
            int jnLab = ctv.JumpLabel++, jzLab = int.MaxValue;

            codes.Add(new HRMCode(HRMInstr.Jn, jnLab));
            if (!cond.Comparer.Contains("="))
            {
                jzLab = ctv.JumpLabel++;
                codes.Add(new HRMCode(HRMInstr.Jz, jzLab));
            }
            ifBlock?.Emit(codes, vtab, ctv);
            int jmpLab = ctv.JumpLabel++;

            codes.Add(new HRMCode(HRMInstr.Jmp, jmpLab));
            if (!cond.Comparer.Contains("="))
            {
                codes.Add(new HRMCode(HRMInstr.Lab, jzLab));
            }
            codes.Add(new HRMCode(HRMInstr.Lab, jnLab));
            elseBlock?.Emit(codes, vtab, ctv);
            codes.Add(new HRMCode(HRMInstr.Lab, jmpLab));
        }
 public void Emit(List <HRMCode> codes, Dictionary <string, int> vtab, CompileTimeVars ctv)
 {
     codes.Add(new HRMCode(HRMInstr.Dec, Helper.GetAddress(vtab, variable)));
 }