public override int Evaluate(WarriorParser parser, int currentAddress) { int l = left.Evaluate(parser, currentAddress); int r = right.Evaluate(parser, currentAddress); switch (operation) { case BinaryOperation.Plus: return l + r; case BinaryOperation.Minus: return l - r; case BinaryOperation.Multiply: return l * r; case BinaryOperation.Divide: if (r == 0) { parser.WriteError("Divide by zero during evaluation of " + ToString() + " at " + Location, Location); return 0; } return l / r; case BinaryOperation.Modulo: if (r == 0) { parser.WriteError("Divide by zero during evaluation of " + ToString() + " at " + Location, Location); return 0; } return l % r; case BinaryOperation.BinOr: return l | r; case BinaryOperation.BinXor: return l ^ r; case BinaryOperation.BinAnd: return l & r; case BinaryOperation.Shl: return l << r; case BinaryOperation.Shr: return l >> r; case BinaryOperation.Or: return ((l != 0) || (r != 0)) ? 1 : 0; case BinaryOperation.And: return ((l != 0) && (r != 0)) ? 1 : 0; case BinaryOperation.CompareGt: return (l > r) ? 1 : 0; case BinaryOperation.CompareGe: return (l >= r) ? 1 : 0; case BinaryOperation.CompareLe: return (l <= r) ? 1 : 0; case BinaryOperation.CompareLt: return (l < r) ? 1 : 0; case BinaryOperation.CompareEq: return (l == r) ? 1 : 0; case BinaryOperation.CompareNe: return (l != r) ? 1 : 0; default: throw new InvalidOperationException(); } }
public override Mode GetMode(WarriorParser parser, int currentAddress) { string fullName = GetFullName(parser, currentAddress); if (parser.variables.ContainsKey(fullName)) { Expression ex = parser.variables[fullName]; if (ex == this) { parser.WriteError("Label not yet resolved: " + fullName + " at " + Location, Location); return 0; } return ex.GetMode(parser, currentAddress); } else { parser.WriteError("Label not defined: " + fullName + " at " + Location, Location); return 0; } }
public override int Evaluate(WarriorParser parser, int currentAddress) { if (inEval) { parser.WriteError("Cyclic definition of function : " + name + " at " + Location, Location); return 0; } try { return EvaluateInternal(parser, currentAddress); } finally { inEval = false; } }
protected virtual int EvaluateInternal(WarriorParser parser, int currentAddress) { string fullName = GetFullName(parser, currentAddress); if (parser.variables.ContainsKey(fullName)) { Expression ex = parser.variables[fullName]; if (ex == this) { parser.WriteError("Label not yet resolved: " + fullName + " at " + Location, Location); return 0; } return ex.Evaluate(parser, currentAddress); } else { parser.WriteError("Label not defined: " + fullName + " at " + Location, Location); return 0; } }