示例#1
0
 // Recognize and translate a divide
 private void Divide()
 {
     Cradle.Match('/');
     Factor();
     Cradle.EmitLn("MOVE(SP)+,D1");
     Cradle.EmitLn("DIVS D1,D0");
 }
示例#2
0
 // Recognize and translate a subtract
 private void Subtract()
 {
     Cradle.Match('-');
     Term();
     Cradle.EmitLn("SUB (SP)+,D0");
     Cradle.EmitLn("NEG D0");
 }
示例#3
0
 public Compiler()
 {
     Assignment();
     if (Cradle.Look != Cradle.CR)
     {
         Cradle.Expected("Newline");
     }
 }
示例#4
0
        // Parse and translate an assignment statement
        private void Assignment()
        {
            string name = Cradle.GetName();

            Cradle.Match('=');
            Expression();
            Cradle.EmitLn("LEA " + name + "(PC),A0");
            Cradle.EmitLn("MOVE D0,(A0)");
        }
示例#5
0
 // Parse and translate a math factor
 private void Factor()
 {
     if (Cradle.Look == '(')
     {
         Cradle.Match('(');
         Expression();
         Cradle.Match(')');
     }
     else
     {
         Cradle.EmitLn("MOVE #" + Cradle.GetNum() + ",D0");
     }
 }
示例#6
0
        // Parse and translate a math term
        private void Term()
        {
            Factor();
            while ("*/".IndexOf(Cradle.Look) > -1)
            {
                Cradle.EmitLn("MOVE D0,-(SP)");
                switch (Cradle.Look)
                {
                case '*': Multiply(); break;

                case '/': Divide(); break;
                }
            }
        }
示例#7
0
        // Parse and translate an identity
        private void Ident()
        {
            string name = Cradle.GetName();

            if (Cradle.Look == '(')
            {
                Cradle.Match('(');
                Expression();
                Cradle.Match(')');
                Cradle.EmitLn("BSR " + name);
            }
            else
            {
                Cradle.EmitLn("MOVE " + name + "(PC),D0");
            }
        }
示例#8
0
        // Parse and translate an expression
        private void Expression()
        {
            if (Cradle.IsAddop(Cradle.Look))
            {
                Cradle.EmitLn("CLR D0");
            }
            else
            {
                Term();
            }

            while (Cradle.IsAddop(Cradle.Look))
            {
                Cradle.EmitLn("MOVE D0,-(SP)");
                switch (Cradle.Look)
                {
                case '+': Add(); break;

                case '-': Subtract(); break;
                }
            }
        }
示例#9
0
 // Recognize and translate an add
 private void Add()
 {
     Cradle.Match('+');
     Term();
     Cradle.EmitLn("ADD (SP)+,D0");
 }
示例#10
0
 // Recognize and translate a multiply
 private void Multiply()
 {
     Cradle.Match('*');
     Factor();
     Cradle.EmitLn("MULS (SP)+,D0");
 }