/// <summary> /// Expression ::= ["+" | "-"] Term {("+"|"-") Term}. /// </summary> /// <returns></returns> private EXPRESSION parseExpression() { bool sign = true; // positive Token token = lexer.get(); if ( token.code == TokenCode.PLUS ) { lexer.forget(); } else if ( token.code == TokenCode.MINUS ) { sign = false; lexer.forget(); } EXPRESSION result = new EXPRESSION(); result.positive = sign; result.term = parseTerm(); while ( true ) { token = lexer.get(); switch ( token.code ) { case TokenCode.PLUS : case TokenCode.MINUS : lexer.forget(); result.ops.Add(token.code); result.terms.Add(parseTerm()); break; default : // Don't forget() return result; } } }
/// <summary> /// Expression ::= ["+" | "-"] Term {("+"|"-") Term}. /// </summary> private void generateExpression(ILGenerator il, EXPRESSION expression) { generateTerm(il,expression.term); if ( !expression.positive ) il.Emit(OpCodes.Neg); for ( int i=0; i<expression.terms.Count; i++ ) { generateTerm(il,expression.terms[i]); switch ( expression.ops[i] ) { case TokenCode.PLUS : il.Emit(OpCodes.Add); break; case TokenCode.MINUS : il.Emit(OpCodes.Sub); break; } } }