// --- public IValue Visit(BinOperatorStatement binaryOperatorExpression) { var left = binaryOperatorExpression.left.Accept(this); var right = binaryOperatorExpression.right.Accept(this); var opType = MapOpExpr[binaryOperatorExpression.type]; if (opType == OperatorExpression.INTEGER) { if (left.Type == ValueType.Number && right.Type == ValueType.Number) { var x = ((NumberValue)left).Number; var y = ((NumberValue)right).Number; switch (binaryOperatorExpression.type) { case BinOperatorStatement.Type.ADD: return(new NumberValue(x + y)); case BinOperatorStatement.Type.SUB: return(new NumberValue(x - y)); case BinOperatorStatement.Type.MUL: return(new NumberValue(x * y)); case BinOperatorStatement.Type.DIV: return(new NumberValue(x / y)); } } else { var err = new ErrorMessage(); throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.BOP_1, binaryOperatorExpression)); } } else if (opType == OperatorExpression.LOGICAL) { if (left.Type == ValueType.Bool && right.Type == ValueType.Bool) { var x = ((BoolValue)left).Bool; var y = ((BoolValue)right).Bool; switch (binaryOperatorExpression.type) { case BinOperatorStatement.Type.AND: return(new BoolValue(x && y)); case BinOperatorStatement.Type.OR: return(new BoolValue(x || y)); } } else { var err = new ErrorMessage(); ErrorMessage.ErrorCode code = (binaryOperatorExpression.type == BinOperatorStatement.Type.OR) ? ErrorMessage.ErrorCode.BOP_2 : ErrorMessage.ErrorCode.BOP_3; throw new EvaluationError(err.ErrorOutput(code, binaryOperatorExpression)); } } else if (opType == OperatorExpression.EQUALITY) { if (left.Type == ValueType.Number && right.Type == ValueType.Number) { var x = ((NumberValue)left).Number; var y = ((NumberValue)right).Number; switch (binaryOperatorExpression.type) { case BinOperatorStatement.Type.EQ: return(new BoolValue(x == y)); case BinOperatorStatement.Type.NEQ: return(new BoolValue(x != y)); } } else if (left.Type == ValueType.Bool && right.Type == ValueType.Bool) { var x = ((BoolValue)left).Bool; var y = ((BoolValue)right).Bool; switch (binaryOperatorExpression.type) { case BinOperatorStatement.Type.EQ: return(new BoolValue(x == y)); case BinOperatorStatement.Type.NEQ: return(new BoolValue(x != y)); } } else { var err = new ErrorMessage(); throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.BOP_4, binaryOperatorExpression)); } } else if (opType == OperatorExpression.INEQUALITY) { if (left.Type == ValueType.Number && right.Type == ValueType.Number) { var x = ((NumberValue)left).Number; var y = ((NumberValue)right).Number; switch (binaryOperatorExpression.type) { case BinOperatorStatement.Type.GR: return(new BoolValue(x > y)); case BinOperatorStatement.Type.LE: return(new BoolValue(x < y)); case BinOperatorStatement.Type.GEQ: return(new BoolValue(x >= y)); case BinOperatorStatement.Type.LEQ: return(new BoolValue(x <= y)); } } else { var err = new ErrorMessage(); throw new EvaluationError(err.ErrorOutput(ErrorMessage.ErrorCode.BOP_5, binaryOperatorExpression)); } } else { return(null); //error } return(null); }
/** * @param ec The ErrorCode for this message * @param message the string in the message */ public ErrorMessage(ErrorMessage.ErrorCode ec, string message) { _ec = ec;; _message = message; }