if-then-else syntax plus optional brackets
This commit is contained in:
parent
c3cc1fe8f9
commit
8676f5e081
1 changed files with 40 additions and 3 deletions
|
@ -32,7 +32,14 @@ final class BExpression
|
||||||
// Parse the expression and all subexpression
|
// Parse the expression and all subexpression
|
||||||
public static BExpression parse( BExpressionContext ctx, int level ) throws Exception
|
public static BExpression parse( BExpressionContext ctx, int level ) throws Exception
|
||||||
{
|
{
|
||||||
|
boolean brackets = false;
|
||||||
String operator = ctx.parseToken();
|
String operator = ctx.parseToken();
|
||||||
|
if ( "(".equals( operator ) )
|
||||||
|
{
|
||||||
|
brackets = true;
|
||||||
|
operator = ctx.parseToken();
|
||||||
|
}
|
||||||
|
|
||||||
if ( operator == null )
|
if ( operator == null )
|
||||||
{
|
{
|
||||||
if ( level == 0 ) return null;
|
if ( level == 0 ) return null;
|
||||||
|
@ -49,11 +56,17 @@ final class BExpression
|
||||||
|
|
||||||
BExpression exp = new BExpression();
|
BExpression exp = new BExpression();
|
||||||
int nops = 3;
|
int nops = 3;
|
||||||
|
boolean ifThenElse = false;
|
||||||
|
|
||||||
if ( "switch".equals( operator ) )
|
if ( "switch".equals( operator ) )
|
||||||
{
|
{
|
||||||
exp.typ = SWITCH_EXP;
|
exp.typ = SWITCH_EXP;
|
||||||
}
|
}
|
||||||
|
else if ( "if".equals( operator ) )
|
||||||
|
{
|
||||||
|
exp.typ = SWITCH_EXP;
|
||||||
|
ifThenElse = true;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
nops = 2; // check binary expressions
|
nops = 2; // check binary expressions
|
||||||
|
@ -148,11 +161,35 @@ final class BExpression
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// parse operands
|
// parse operands
|
||||||
if ( nops > 0 ) exp.op1 = BExpression.parse( ctx, level+1 );
|
if ( nops > 0 )
|
||||||
if ( nops > 1 ) exp.op2 = BExpression.parse( ctx, level+1 );
|
{
|
||||||
if ( nops > 2 ) exp.op3 = BExpression.parse( ctx, level+1 );
|
exp.op1 = BExpression.parse( ctx, level+1 );
|
||||||
|
}
|
||||||
|
if ( nops > 1 )
|
||||||
|
{
|
||||||
|
if ( ifThenElse ) checkExpectedToken( ctx, "then" );
|
||||||
|
exp.op2 = BExpression.parse( ctx, level+1 );
|
||||||
|
}
|
||||||
|
if ( nops > 2 )
|
||||||
|
{
|
||||||
|
if ( ifThenElse ) checkExpectedToken( ctx, "else" );
|
||||||
|
exp.op3 = BExpression.parse( ctx, level+1 );
|
||||||
|
}
|
||||||
|
if ( brackets )
|
||||||
|
{
|
||||||
|
checkExpectedToken( ctx, ")" );
|
||||||
|
}
|
||||||
return exp;
|
return exp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void checkExpectedToken( BExpressionContext ctx, String expected ) throws Exception
|
||||||
|
{
|
||||||
|
String token = ctx.parseToken();
|
||||||
|
if ( ! expected.equals( token ) )
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException( "unexpected token: " + token + ", expected: " + expected );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluate the expression
|
// Evaluate the expression
|
||||||
public float evaluate( BExpressionContext ctx )
|
public float evaluate( BExpressionContext ctx )
|
||||||
|
|
Loading…
Reference in a new issue