public UnaryOperandNode VisitExpressionOperand(ExpressionOperandNode eon) { int temp = this.depth; string spaces = IncreaseDepth(); this.io.WriteLine($"{spaces}ExpressionOperand: ("); this.io.WriteLine($"{spaces} Negative: {eon.Negative},"); this.io.WriteLine($"{spaces} Not: {eon.Not},"); eon.Expression.Visit(this); this.io.WriteLine($"{spaces})"); this.depth = temp; return(new UnaryOperandNode("", SymbolType.Invalid)); }
public UnaryOperandNode VisitExpressionOperand(ExpressionOperandNode eon) { UnaryOperandNode node = eon.Expression.Visit(this); if (node.Type == SymbolType.IntegerValue) { if (eon.Not) { throw new Error($"Can not have ! in front of an expression that returns {node.Type}", node.Token); } if (eon.Negative) { int v = ConvertStringToInteger(node.Value, node.Token); node.Value = OppositeOfInteger(v); } } else if (node.Type == SymbolType.Boolean) { if (eon.Negative) { throw new Error($"Can not have a - in front of an expression that returns {node.Type}", node.Token); } if (eon.Not) { if (node.Value == "true") { node.Value = "false"; } else { node.Value = "true"; } } } else { if (eon.Negative) { throw new Error($"Can not have a - in front of an expression that returns {node.Type}", node.Token); } if (eon.Not) { throw new Error($"Can not have ! in front of an expression that returns {node.Type}", node.Token); } } return(node); }
private Operand Operand() { // <opnd> ::= [Minus] <int> | <string> | [Minus | Exclamation] <var_ident> | [Minus | Exclamation] "(" expr ")" bool negative = false; bool not = false; Token minus = this.token; if (this.token.SymbolType == SymbolType.Minus) { negative = true; this.token = this.scanner.NextToken(); } else if (this.token.SymbolType == SymbolType.Exclamation) { not = true; this.token = this.scanner.NextToken(); } Operand o; switch (this.token.SymbolType) { case SymbolType.IntegerValue: if (not) { throw new Error($"! is not allowed in front of an IntegerValue", minus); } if (negative) { o = new UnaryOperandNode($"{Integer.minus}{this.token.Value}", this.token.SymbolType, minus); } else { o = new UnaryOperandNode(this.token.Value, this.token.SymbolType, this.token); } this.token = this.scanner.NextToken(); return(o); case SymbolType.StringValue: if (not) { throw new Error($"! is not allowed in front of a StringValue", minus); } if (negative) { throw new Error($"- is not allowed in front of a StringValue", minus); } o = new UnaryOperandNode(this.token.Value, this.token.SymbolType, this.token); this.token = this.scanner.NextToken(); return(o); case SymbolType.Identifier: if (not) { o = new UnaryOperandNode(this.token.Value, this.token.SymbolType, this.token, false, true); } else if (negative) { o = new UnaryOperandNode(this.token.Value, this.token.SymbolType, this.token, true); } else { o = new UnaryOperandNode(this.token.Value, this.token.SymbolType, this.token); } this.token = this.scanner.NextToken(); return(o); case SymbolType.LeftParenthesis: this.token = this.scanner.NextToken(); Expression e = Expression(); if (negative) { o = new ExpressionOperandNode(e, true); } else if (not) { o = new ExpressionOperandNode(e, false, true); } else { o = new ExpressionOperandNode(e); } Match(SymbolType.RightParenthesis); return(o); default: throw new Error($"Unexpected token: {this.token.SymbolType}", this.token); } }