예제 #1
0
 // Recognize and translate a relational equals
 private void RelEquals()
 {
     Cradle.Match('=');
     Expression();
     Cradle.EmitLn("CMP (SP)+,D0");
     Cradle.EmitLn("SEQ D0");
 }
예제 #2
0
 // Recognize and translate a relational not equals
 private void NotEquals()
 {
     Cradle.Match('#');
     Expression();
     Cradle.EmitLn("CMP (SP)+,D0");
     Cradle.EmitLn("SNE D0");
 }
예제 #3
0
        // Parse and translate an assignment statement
        private void Assignment()
        {
            char Name = Cradle.GetName();

            Cradle.Match('=');
            Table[Name - 'A'] = Expression();
        }
예제 #4
0
 // Recognize and translate a relational less than
 private void Less()
 {
     Cradle.Match('<');
     Expression();
     Cradle.EmitLn("CMP (SP)+,D0");
     Cradle.EmitLn("SGE D0");
 }
예제 #5
0
 // Recognize and translate a relational greater than
 private void Greater()
 {
     Cradle.Match('>');
     Expression();
     Cradle.EmitLn("CMP (SP)+,D0");
     Cradle.EmitLn("SLE D0");
 }
예제 #6
0
        // Parse and translate a for statement
        private void DoFor()
        {
            Cradle.Match('f');
            string L1   = Label.NewLabel();
            string L2   = Label.NewLabel();
            char   Name = Cradle.GetName();

            Cradle.Match('=');
            Expression();
            Cradle.EmitLn("SUBQ #1,D0");
            Cradle.EmitLn("LEA " + Name + "(PC),A0");
            Cradle.EmitLn("MOVE D0,(A0)");
            Expression();
            Cradle.EmitLn("MOVE D0,-(SP)");
            Label.PostLabel(L1);
            Cradle.EmitLn("LEA " + Name + "(PC),A0");
            Cradle.EmitLn("ADDQ #1,D0");
            Cradle.EmitLn("MOVE D0,(A0)");
            Cradle.EmitLn("CMP (SP),D0)");
            Cradle.EmitLn("BGT " + L2);
            Block(L2);
            Cradle.Match('e');
            Cradle.EmitLn("BRA " + L1);
            Label.PostLabel(L2);
            Cradle.EmitLn("ADDQ #2,SP");
        }
예제 #7
0
        // Lexical scanner
        private void Scan()
        {
            int k;

            while (Cradle.Look == Cradle.CR)
            {
                Cradle.Fin();
            }
            if (Cradle.IsAlpha(Cradle.Look))
            {
                GetName();
            }
            else if (Cradle.IsDigit(Cradle.Look))
            {
                GetNum();
            }
            else if (IsOp(Cradle.Look))
            {
                GetOp();
            }
            else
            {
                Value = Cradle.Look.ToString();
                Token = SymType.Operator;
                Cradle.GetChar();
            }
            Cradle.SkipWhite();
        }
예제 #8
0
 // Recognize and translate a divide
 private void Divide()
 {
     Cradle.Match('/');
     Factor();
     Cradle.EmitLn("MOVE(SP)+,D1");
     Cradle.EmitLn("DIVS D1,D0");
 }
예제 #9
0
 // Recognize and translate a subtract
 private void Subtract()
 {
     Cradle.Match('-');
     Term();
     Cradle.EmitLn("SUB (SP)+,D0");
     Cradle.EmitLn("NEG D0");
 }
예제 #10
0
 // Recognize and translate a break
 private void DoBreak(string L)
 {
     Cradle.Match('b');
     if (L.Length > 0)
         Cradle.EmitLn("BRA " + L);
     else
         Cradle.Abort("No loop to break");
 }
예제 #11
0
 // Parse and translate an assignment statement
 private void Assignment()
 {
     char name = Cradle.GetName();
     Cradle.Match('=');
     BoolExpression();
     Cradle.EmitLn("LEA " + name + "(PC),A0");
     Cradle.EmitLn("MOVE D0,(A0)");
 }
예제 #12
0
 // Parse and translate a program
 private void DoProgram()
 {
     Block("");
     if (Cradle.Look != 'e')
     {
         Cradle.Expected("End");
     }
     Cradle.EmitLn("END");
 }
예제 #13
0
 // Skip over a comma
 private void SkipComma()
 {
     Cradle.SkipWhite();
     if (Cradle.Look == ',')
     {
         Cradle.GetChar();
         Cradle.SkipWhite();
     }
 }
예제 #14
0
 // Parse and translate a loop statement
 private void DoLoop()
 {
     Cradle.Match('p');
     string L1 = Label.NewLabel();
     string L2 = Label.NewLabel();
     Label.PostLabel(L1);
     Block(L2);
     Cradle.Match('e');
     Cradle.EmitLn("BRA " + L1);
     Label.PostLabel(L2);
 }
예제 #15
0
 //  Parse and translate a Boolean factor with not
 private void NotFactor()
 {
     if (Cradle.Look == '!')
     {
         Cradle.Match('!');
         BoolFactor();
         Cradle.EmitLn("EOR #-1,D0");
     }
     else
         BoolFactor();
 }
예제 #16
0
 // Parse and translate a Boolean factor
 private void BoolFactor()
 {
     if (Cradle.IsBoolean(Cradle.Look))
     {
         if (Cradle.GetBoolean())
             Cradle.EmitLn("MOVE #-1,D0");
         else
             Cradle.EmitLn("CLR D0");
     }
     Relation();
 }
예제 #17
0
 // Parse and recognize a Boolean term
 private void BoolTerm()
 {
     NotFactor();
     while (Cradle.Look == '&')
     {
         Cradle.EmitLn("MOVE D0,-(SP)");
         Cradle.Match('&');
         NotFactor();
         Cradle.EmitLn("AND (SP)+,D0");
     }
 }
예제 #18
0
 // Parse and translate a repeat block
 private void DoRepeat()
 {
     Cradle.Match('r');
     string L1 = Label.NewLabel();
     string L2 = Label.NewLabel();
     Label.PostLabel(L1);
     Block(L2);
     Cradle.Match('u');
     BoolExpression();
     Cradle.EmitLn("BEQ " + L1);
     Label.PostLabel(L2);
 }
예제 #19
0
 // Parse and translate an identity
 private void Ident()
 {
     char name = Cradle.GetName();
     if (Cradle.Look == '(')
     {
         Cradle.Match('(');
         Expression();
         Cradle.Match(')');
         Cradle.EmitLn("BSR " + name);
     }
     else
         Cradle.EmitLn("MOVE " + name + "(PC),D0");
 }
예제 #20
0
 // Parse and translate a Boolean expression
 private void BoolExpression()
 {
     BoolTerm();
     while (IsOrop(Cradle.Look))
     {
         Cradle.EmitLn("MOVE D0,-(SP)");
         switch (Cradle.Look)
         {
             case '|': BoolOr(); break;
             case '~': BoolXor(); break;
         }
     }
 }
예제 #21
0
 // Parse and translate a math term
 private void Term()
 {
     SignedFactor();
     while ("*/".IndexOf(Cradle.Look) > -1)
     {
         Cradle.EmitLn("MOVE D0,-(SP)");
         switch (Cradle.Look)
         {
             case '*': Multiply(); break;
             case '/': Divide(); break;
         }
     }
 }
예제 #22
0
 // Parse and translate a math factor
 private void Factor()
 {
     if (Cradle.Look == '(')
     {
         Cradle.Match('(');
         Expression();
         Cradle.Match(')');
     }
     else if (Cradle.IsAlpha(Cradle.Look))
         Ident();
     else
         Cradle.EmitLn("MOVE #" + Cradle.GetNum() + ",D0");
 }
예제 #23
0
 // Parse and translate a while statement
 private void DoWhile()
 {
     Cradle.Match('w');
     string L1 = Label.NewLabel();
     string L2 = Label.NewLabel();
     Label.PostLabel(L1);
     BoolExpression();
     Cradle.EmitLn("BEQ " + L2);
     Block(L2);
     Cradle.Match('e');
     Cradle.EmitLn("BRA " + L1);
     Label.PostLabel("L2");
 }
예제 #24
0
 // Get an identifier
 private void GetName()
 {
     Value = "";
     if (!Cradle.IsAlpha(Cradle.Look))
     {
         Cradle.Expected("Name");
     }
     while (Cradle.IsAlNum(Cradle.Look))
     {
         Value += char.ToUpper(Cradle.Look);
         Cradle.GetChar();
     }
     Token = KWCode[Lookup(Value) + 1];
 }
예제 #25
0
 // Get an operator
 private void GetOp()
 {
     Value = "";
     if (!IsOp(Cradle.Look))
     {
         Cradle.Expected("Operator");
     }
     while (IsOp(Cradle.Look))
     {
         Value += Cradle.Look;
         Cradle.GetChar();
     }
     Token = SymType.Operator;
 }
예제 #26
0
 // Get a number
 private void GetNum()
 {
     Value = "";
     if (!Cradle.IsDigit(Cradle.Look))
     {
         Cradle.Expected("Integer");
     }
     while (Cradle.IsDigit(Cradle.Look))
     {
         Value += Cradle.Look;
         Cradle.GetChar();
     }
     Token = SymType.Number;
 }
예제 #27
0
 // Parse and translate a relation
 private void Relation()
 {
     Expression();
     if (IsRelop(Cradle.Look))
     {
         Cradle.EmitLn("MOVE D0,-(SP)");
         switch (Cradle.Look)
         {
             case '=': RelEquals(); break;
             case '#': NotEquals(); break;
             case '<': Less(); break;
             case '>': Greater(); break;
         }
         Cradle.EmitLn("TST D0");
     }
 }
예제 #28
0
 // Parse and translate the first math factor
 private void SignedFactor()
 {
     if (Cradle.Look == '+') Cradle.GetChar();
     if (Cradle.Look == '-')
     {
         Cradle.GetChar();
         if (Cradle.IsDigit(Cradle.Look))
             Cradle.EmitLn("MOVE #-" + Cradle.GetNum() + ",D0");
         else
         {
             Factor();
             Cradle.EmitLn("NEG D0");
         }
     }
     else
         Factor();
 }
예제 #29
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;
                }
            }
        }
예제 #30
0
 // Parse and translate a do statement
 private void DoDo()
 {
     Cradle.Match('d');
     string L1 = Label.NewLabel();
     string L2 = Label.NewLabel();
     Expression();
     Cradle.EmitLn("SUBQ #1,D0");
     Label.PostLabel(L1);
     Cradle.EmitLn("MOVE D0,-(SP)");
     Block(L2);
     Cradle.Match('e');
     Cradle.EmitLn("MOVE (SP)+,D0");
     Cradle.EmitLn("DBRA D0," + L1);
     Cradle.EmitLn("SUBQ #2,SP");
     Label.PostLabel(L2);
     Cradle.EmitLn("ADDQ #2,SP");
 }