public AST Apply(Token operata, AST op1, AST op2) { op1 = this.Convert(op1); if (!operata.Is(TokenType.FULL_STOP)) { op2 = this.Convert(op2); } switch (operata.Type) { case TokenType.TIMES: return(new Tree.Number((op1.Evaluate(this.env).Value *op2.Evaluate(this.env).Value).ToString())); case TokenType.DIVIDE: return(new Tree.Number((op1.Evaluate(this.env).Value / op2.Evaluate(this.env).Value).ToString())); case TokenType.PLUS: return(this.ApplyPLUS(operata, op1, op2)); case TokenType.MINUS: return(new Tree.Number((op1.Evaluate(this.env).Value - op2.Evaluate(this.env).Value).ToString())); case TokenType.MODULUS: return(new Tree.Number((op1.Evaluate(this.env).Value % op2.Evaluate(this.env).Value).ToString())); case TokenType.EQUAL_TO: return(new Tree.Boolean((op1.Evaluate(this.env).Value == op2.Evaluate(this.env).Value).ToString())); case TokenType.NOT_EQUAL_TO: return(new Tree.Boolean((op1.Evaluate(this.env).Value != op2.Evaluate(this.env).Value).ToString())); case TokenType.LESS_THAN: return(new Tree.Boolean((op1.Evaluate(this.env).Value < op2.Evaluate(this.env).Value).ToString())); case TokenType.LESS_THAN_OR_EQUAL_TO: return(new Tree.Boolean((op1.Evaluate(this.env).Value <= op2.Evaluate(this.env).Value).ToString())); case TokenType.GREATER_THAN: return(new Tree.Boolean((op1.Evaluate(this.env).Value > op2.Evaluate(this.env).Value).ToString())); case TokenType.GREATER_THAN_OR_EQUAL_TO: return(new Tree.Boolean((op1.Evaluate(env).Value >= op2.Evaluate(env).Value).ToString())); case TokenType.LOGICAL_AND: var op1Res = op1.Evaluate(this.env).Value; var op2Res = op2.Evaluate(this.env).Value; return(new Tree.Boolean((op1Res && op2Res).ToString())); case TokenType.LOGICAL_OR: var operationOneRes = op1.Evaluate(this.env).Value; var operationTwoRes = op2.Evaluate(this.env).Value; return(new Tree.Boolean((operationOneRes || operationTwoRes).ToString())); case TokenType.FULL_STOP: return(this.AccessModifier(op1, op2)); default: this.Error(operata.Type + " cannot be used as an expression operator", operata.Row, operata.Column); return(null); } }
/* * Helper functions */ private bool checkType(AST oparand, TreeType type) { return(oparand.Type == type); }
public Assignment(string left, AST right) : base(TreeType.ASSIGNMENT) { this.right = right; this.left = left; }