private BinaryOperatorExpression balanceBinaryExpression(SimpleExpression exp, Token.TokenValue op, BinaryOperatorExpression binExp) { if (tokenPriority(op) >= tokenPriority(binExp.op)) { return new BinaryOperatorExpression(op, exp, binExp); } if (binExp.e1 is BinaryOperatorExpression) { return new BinaryOperatorExpression(binExp.op, balanceBinaryExpression(exp, op, (BinaryOperatorExpression)binExp.e1), binExp.e2); } return new BinaryOperatorExpression(binExp.op, new BinaryOperatorExpression(op, exp, binExp.e1), binExp.e2); }
public ObjectExpression evalSimpleExpression(SimpleExpression exp) { if (exp is LiteralExpression) { lit = (LiteralExpression)exp; switch (lit.value.type) { case ParserUtils.Token.TokenValue.STRING: return new ObjectExpression(ObjectType.stringType, lit.value.stringValue); case ParserUtils.Token.TokenValue.INT: return new ObjectExpression(ObjectType.intType, lit.value.intValue); case ParserUtils.Token.TokenValue.CHAR: return new ObjectExpression(ObjectType.charType, lit.value.charValue); case ParserUtils.Token.TokenValue.BOOLEAN: return new ObjectExpression(ObjectType.booleanType, lit.value.booleanValue); default: break; } } else if (exp is SubSimpleExpression) { return evalSimpleExpression(((SubSimpleExpression)exp).exp); } else if (exp is NullExpression) { return null; } else if (exp is BinaryOperatorExpression) { BinaryOperatorExpression binExp = (BinaryOperatorExpression)exp; ObjectExpression oLeft = evalSimpleExpression(binExp.e1), oRight; switch (binExp.op) { case ParserUtils.Token.TokenValue.AND: if ((bool)oLeft.value) { oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.booleanType, (bool)oRight.value); } return new ObjectExpression(ObjectType.booleanType, false); case ParserUtils.Token.TokenValue.OR: if ((bool)oLeft.value) { return new ObjectExpression(ObjectType.booleanType, true); } oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.booleanType, (bool)oRight.value); case ParserUtils.Token.TokenValue.PLUS: oRight = evalSimpleExpression(binExp.e2); if (oLeft.type == ObjectType.stringType || oRight.type == ObjectType.stringType) { return new ObjectExpression(ObjectType.stringType, oLeft.value.ToString() + oRight.value.ToString()); } if (oLeft.type == ObjectType.intType && oRight.type == ObjectType.intType) { return new ObjectExpression(ObjectType.intType, (int)oLeft.value + (int)oRight.value); } break; case ParserUtils.Token.TokenValue.MINUS: oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.intType, (int)oLeft.value - (int)oRight.value); case ParserUtils.Token.TokenValue.TIMES: oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.intType, (int)oLeft.value * (int)oRight.value); case ParserUtils.Token.TokenValue.DIV: oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.intType, (int)oLeft.value / (int)oRight.value); case ParserUtils.Token.TokenValue.LT: oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.booleanType, (int)oLeft.value < (int)oRight.value); case ParserUtils.Token.TokenValue.GT: oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.booleanType, (int)oLeft.value > (int)oRight.value); case ParserUtils.Token.TokenValue.LEQ: oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.booleanType, (int)oLeft.value <= (int)oRight.value); case ParserUtils.Token.TokenValue.GEQ: oRight = evalSimpleExpression(binExp.e2); return new ObjectExpression(ObjectType.booleanType, (int)oLeft.value >= (int)oRight.value); case ParserUtils.Token.TokenValue.EQBOOL: oRight = evalSimpleExpression(binExp.e2); if (oLeft.type == ObjectType.intType) { return new ObjectExpression(ObjectType.booleanType, (int)oLeft.value == (int)oRight.value); } if (oLeft.type == ObjectType.stringType) { return new ObjectExpression(ObjectType.booleanType, ((string)oLeft.value).Equals((string)oRight.value)); } if (oLeft.type == ObjectType.booleanType) { return new ObjectExpression(ObjectType.booleanType, (bool)oLeft.value == (bool)oRight.value); } if (oLeft.type == ObjectType.charType) { return new ObjectExpression(ObjectType.booleanType, (char)oLeft.value == (char)oRight.value); } break; default: break; } } else if (exp is UnaryOperatorExpression) { unExp = (UnaryOperatorExpression)exp; switch (unExp.op) { case ParserUtils.Token.TokenValue.NEG: o1 = evalSimpleExpression(((UnaryOperatorExpression)exp).exp); return new ObjectExpression(ObjectType.booleanType, !((bool)o1.value)); } } else if (exp is VariableExpression) { return variables[((VariableExpression)exp).name]; } else if (exp is CharacterVariableExpression) { return new ObjectExpression(ObjectType.intType, IrisData.getPlayerVariable(((CharacterVariableExpression)exp).name)); } return null; }
public UnaryOperatorExpression(Token.TokenValue operatorToken, SimpleExpression expr) { op = operatorToken; exp = expr; }
public ElseIfExpression(SimpleExpression conditionIf, Expression corpsIf) { this.condition = conditionIf; this.corps = corpsIf; }
public BinaryOperatorExpression(Token.TokenValue opValue, SimpleExpression left, SimpleExpression right) { this.op = opValue; this.e1 = left; this.e2 = right; }
public ValueExpression(ObjectType varType, string varName, SimpleExpression varValue) { this.type = varType; this.name = varName; this.value = varValue; }
public IfExpression(SimpleExpression conditionIf, Expression ifBody) { this.condition = conditionIf; this.body = ifBody; }
public SubSimpleExpression(SimpleExpression e) { exp = e; }
public AssignExpression(string varName, SimpleExpression varValue) { }