private short EvaluateExpression()
{
var operations = new Stack<ExpressionOperation>();
var stack = new Stack<int>();
BitwiseExpression(operations);
operations = new Stack<ExpressionOperation>(operations);
while (operations.Count != 0)
{
ExpressionOperation operation = operations.Pop();
switch (operation.Operation)
{
case TokenType.Number:
stack.Push(operation.Value);
break;
case TokenType.BitwiseNot:
stack.Push(~stack.Pop());
break;
case TokenType.BitwiseAnd:
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b & a);
break;
}
case TokenType.BitwiseOr:
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b | a);
break;
}
case TokenType.BitwiseXor:
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b ^ a);
break;
}
case TokenType.Add:
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b + a);
break;
}
case TokenType.Subtract:
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b - a);
break;
}
case TokenType.Multiply:
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b * a);
break;
}
case TokenType.Divide:
{
try
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b / a);
}
catch (Exception)
{
throw new AssemblerException(String.Format("Division by 0 in inline expression at {0}:{1}.", tokens[pos].Filename,
tokens[pos].Line));
}
break;
}
case TokenType.Modulo:
{
try
{
int a = stack.Pop();
int b = stack.Pop();
stack.Push(b % a);
}
catch (Exception)
{
throw new AssemblerException(String.Format("Module by 0 in inline expression at {0}:{1}.", tokens[pos].Filename,
tokens[pos].Line));
}
break;
}
}
}
return (short)stack.Pop();
}