// Parse and translate a math factor private int Factor() { if (Cradle.Look == '(') { Cradle.Match('('); int value = Expression(); Cradle.Match(')'); return(value); } else if (Cradle.IsAlpha(Cradle.Look)) { return(Table[Cradle.GetName() - 'A']); } else { return(Cradle.GetNum()); } }
// Recognize and translate an if construct private void DoIf(string L) { Cradle.Match('i'); BoolExpression(); string L1 = Label.NewLabel(); string L2 = L1; Cradle.EmitLn("BEQ " + L1); Block(L); if (Cradle.Look == 'l') { Cradle.Match('l'); L2 = Label.NewLabel(); Cradle.EmitLn("BRA " + L2); Label.PostLabel(L1); Block(L); } Cradle.Match('e'); Label.PostLabel(L2); }
// Parse and translate an expression private int Expression() { int Value = (Cradle.IsAddop(Cradle.Look)) ? 0 : Term(); while (Cradle.IsAddop(Cradle.Look)) { switch (Cradle.Look) { case '+': Cradle.Match('+'); Value += Term(); break; case '-': Cradle.Match('-'); Value -= Term(); break; } } return(Value); }
// Parse and translate a math term private int Term() { int Value = Factor(); while ("*/".IndexOf(Cradle.Look) > -1) { switch (Cradle.Look) { case '*': Cradle.Match('*'); Value *= Factor(); break; case '/': Cradle.Match('/'); Value /= Factor(); break; } } return(Value); }
// Recognize and translate a Boolean or private void BoolOr() { Cradle.Match('|'); BoolTerm(); Cradle.EmitLn("OR (SP)+,D0"); }
// Recognize and translate an add private void Add() { Cradle.Match('+'); Term(); Cradle.EmitLn("ADD (SP)+,D0"); }
// Recognize and translate a multiply private void Multiply() { Cradle.Match('*'); Factor(); Cradle.EmitLn("MULS (SP)+,D0"); }
// Recognize and translate an exclusive or private void BoolXor() { Cradle.Match('~'); BoolTerm(); Cradle.EmitLn("EOR (SP)+,D0"); }
// Output routine private void Output() { Cradle.Match('!'); Console.WriteLine(Table[Cradle.GetName() - 'A']); }
// Input routine private void Input() { Cradle.Match('?'); Assignment(); }