Skip to content

Commit

Permalink
Changed greater than to consider type
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 17, 2016
1 parent 9b6f81f commit 4e8e93a
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 46 deletions.
Expand Up @@ -115,7 +115,7 @@ public void addDoubles( Expression lhs, Expression rhs )
} }


@Override @Override
public void gt( Expression lhs, Expression rhs ) public void gt( Expression lhs, Expression rhs, TypeReference type )
{ {


} }
Expand Down
Expand Up @@ -34,14 +34,14 @@ public void accept( ExpressionVisitor visitor )


public abstract void accept( ExpressionVisitor visitor ); public abstract void accept( ExpressionVisitor visitor );


public static Expression gt( final Expression lhs, final Expression rhs ) public static Expression gt( final Expression lhs, final Expression rhs, TypeReference type )
{ {
return new Expression() return new Expression()
{ {
@Override @Override
public void accept( ExpressionVisitor visitor ) public void accept( ExpressionVisitor visitor )
{ {
visitor.gt( lhs, rhs ); visitor.gt( lhs, rhs, type );
} }
}; };
} }
Expand Down
Expand Up @@ -179,7 +179,7 @@ private void add( Expression lhs, Expression rhs )
} }


@Override @Override
public void gt( Expression lhs, Expression rhs ) public void gt( Expression lhs, Expression rhs, TypeReference ignored )
{ {
result.append( "gt(" ); result.append( "gt(" );
lhs.accept( this ); lhs.accept( this );
Expand Down
Expand Up @@ -51,7 +51,7 @@ public interface ExpressionVisitor


void addDoubles( Expression lhs, Expression rhs ); void addDoubles( Expression lhs, Expression rhs );


void gt( Expression lhs, Expression rhs ); void gt( Expression lhs, Expression rhs, TypeReference type );


void subtractInts( Expression lhs, Expression rhs ); void subtractInts( Expression lhs, Expression rhs );


Expand Down
Expand Up @@ -226,7 +226,6 @@ public void ternary( Expression test, Expression onTrue, Expression onFalse )
@Override @Override
public void eq( Expression lhs, Expression rhs, TypeReference type ) public void eq( Expression lhs, Expression rhs, TypeReference type )
{ {

switch ( type.simpleName() ) switch ( type.simpleName() )
{ {
case "int": case "int":
Expand Down Expand Up @@ -310,18 +309,10 @@ public void addDoubles( Expression lhs, Expression rhs )
} }


@Override @Override
public void gt( Expression lhs, Expression rhs ) public void gt( Expression lhs, Expression rhs, TypeReference type )
{ {


TypeReference lhsType = findType( lhs ); switch ( type.simpleName() )
TypeReference rhsType = findType( rhs );

if ( !lhsType.equals( rhsType ) )
{
throw new IllegalStateException( "Cannot compare values of different types" );
}

switch ( lhsType.simpleName() )
{ {
case "int": case "int":
case "byte": case "byte":
Expand Down
Expand Up @@ -436,7 +436,7 @@ private void add( Expression lhs, Expression rhs )
} }


@Override @Override
public void gt( Expression lhs, Expression rhs ) public void gt( Expression lhs, Expression rhs, TypeReference ignored )
{ {
lhs.accept( this ); lhs.accept( this );
append( " > " ); append( " > " );
Expand Down
Expand Up @@ -1057,43 +1057,66 @@ public void shouldHandleEquality() throws Throwable
@Test @Test
public void shouldHandleGreaterThan() throws Throwable public void shouldHandleGreaterThan() throws Throwable
{ {
assertTrue( compareForType( float.class, 43F, 42F, Expression::gt ) ); assertTrue( compareForType( float.class, 43F, 42F,
assertTrue( compareForType( long.class, 43L, 42L, Expression::gt ) ); ( a, b ) -> Expression.gt( a, b, typeReference( float.class ) ) ) );
assertTrue( compareForType( long.class, 43L, 42L,
( a, b ) -> Expression.gt( a, b, typeReference( long.class ) ) ) );


// byte // byte
assertTrue( compareForType( byte.class, (byte) 43, (byte) 42, Expression::gt ) ); assertTrue( compareForType( byte.class, (byte) 43, (byte) 42,
assertFalse( compareForType( byte.class, (byte) 42, (byte) 42, Expression::gt ) ); ( a, b ) -> Expression.gt( a, b, typeReference( byte.class ) ) ) );
assertFalse( compareForType( byte.class, (byte) 42, (byte) 43, Expression::gt ) ); assertFalse( compareForType( byte.class, (byte) 42, (byte) 42,
( a, b ) -> Expression.gt( a, b, typeReference( byte.class ) ) ) );
assertFalse( compareForType( byte.class, (byte) 42, (byte) 43,
( a, b ) -> Expression.gt( a, b, typeReference( byte.class ) ) ) );


// short // short
assertTrue( compareForType( short.class, (short) 43, (short) 42, Expression::gt ) ); assertTrue( compareForType( short.class, (short) 43, (short) 42,
assertFalse( compareForType( short.class, (short) 42, (short) 42, Expression::gt ) ); ( a, b ) -> Expression.gt( a, b, typeReference( short.class ) ) ) );
assertFalse( compareForType( short.class, (short) 42, (short) 43, Expression::gt ) ); assertFalse( compareForType( short.class, (short) 42, (short) 42,
( a, b ) -> Expression.gt( a, b, typeReference( short.class ) ) ) );
assertFalse( compareForType( short.class, (short) 42, (short) 43,
( a, b ) -> Expression.gt( a, b, typeReference( short.class ) ) ) );


// char // char
assertTrue( compareForType( char.class, (char) 43, (char) 42, Expression::gt ) ); assertTrue( compareForType( char.class, (char) 43, (char) 42,
assertFalse( compareForType( char.class, (char) 42, (char) 42, Expression::gt ) ); ( a, b ) -> Expression.gt( a, b, typeReference( char.class ) ) ) );
assertFalse( compareForType( char.class, (char) 42, (char) 43, Expression::gt ) ); assertFalse( compareForType( char.class, (char) 42, (char) 42,
( a, b ) -> Expression.gt( a, b, typeReference( char.class ) ) ) );
assertFalse( compareForType( char.class, (char) 42, (char) 43,
( a, b ) -> Expression.gt( a, b, typeReference( char.class ) ) ) );


//int //int
assertTrue( compareForType( int.class, 43, 42, Expression::gt ) ); assertTrue(
assertFalse( compareForType( int.class, 42, 42, Expression::gt ) ); compareForType( int.class, 43, 42, ( a, b ) -> Expression.gt( a, b, typeReference( int.class ) ) ) );
assertFalse( compareForType( int.class, 42, 43, Expression::gt ) ); assertFalse(
compareForType( int.class, 42, 42, ( a, b ) -> Expression.gt( a, b, typeReference( int.class ) ) ) );
assertFalse(
compareForType( int.class, 42, 43, ( a, b ) -> Expression.gt( a, b, typeReference( int.class ) ) ) );


//long //long
assertTrue( compareForType( long.class, 43L, 42L, Expression::gt ) ); assertTrue( compareForType( long.class, 43L, 42L,
assertFalse( compareForType( long.class, 42L, 42L, Expression::gt ) ); ( a, b ) -> Expression.gt( a, b, typeReference( long.class ) ) ) );
assertFalse( compareForType( long.class, 42L, 43L, Expression::gt ) ); assertFalse( compareForType( long.class, 42L, 42L,
( a, b ) -> Expression.gt( a, b, typeReference( long.class ) ) ) );
assertFalse( compareForType( long.class, 42L, 43L,
( a, b ) -> Expression.gt( a, b, typeReference( long.class ) ) ) );


//float //float
assertTrue( compareForType( float.class, 43F, 42F, Expression::gt ) ); assertTrue( compareForType( float.class, 43F, 42F,
assertFalse( compareForType( float.class, 42F, 42F, Expression::gt ) ); ( a, b ) -> Expression.gt( a, b, typeReference( float.class ) ) ) );
assertFalse( compareForType( float.class, 42F, 43F, Expression::gt ) ); assertFalse( compareForType( float.class, 42F, 42F,
( a, b ) -> Expression.gt( a, b, typeReference( float.class ) ) ) );
assertFalse( compareForType( float.class, 42F, 43F,
( a, b ) -> Expression.gt( a, b, typeReference( float.class ) ) ) );


//double //double
assertTrue( compareForType( double.class, 43D, 42D, Expression::gt ) ); assertTrue( compareForType( double.class, 43D, 42D,
assertFalse( compareForType( double.class, 42D, 42D, Expression::gt ) ); ( a, b ) -> Expression.gt( a, b, typeReference( double.class ) ) ) );
assertFalse( compareForType( double.class, 42D, 43D, Expression::gt ) ); assertFalse( compareForType( double.class, 42D, 42D,
( a, b ) -> Expression.gt( a, b, typeReference( double.class ) ) ) );
assertFalse( compareForType( double.class, 42D, 43D,
( a, b ) -> Expression.gt( a, b, typeReference( double.class ) ) ) );
} }


@Test @Test
Expand Down
Expand Up @@ -540,7 +540,7 @@ private case class Method(fields: Fields, generator: CodeBlock, aux:AuxGenerator
val keyVar = keyVars.head val keyVar = keyVars.head
val times = generator.declare(typeRef[Int], context.namer.newVarName()) val times = generator.declare(typeRef[Int], context.namer.newVarName())
generator.assign(times, Expression.invoke(generator.load(tableVar), Methods.countingTableGet, generator.load(keyVar))) generator.assign(times, Expression.invoke(generator.load(tableVar), Methods.countingTableGet, generator.load(keyVar)))
using(generator.whileLoop(Expression.gt(times, Expression.constant(0)))) { body => using(generator.whileLoop(Expression.gt(times, Expression.constant(0), typeRef[Int]))) { body =>
block(copy(generator=body)) block(copy(generator=body))
body.assign(times, Expression.subtractInts(times, Expression.constant(1))) body.assign(times, Expression.subtractInts(times, Expression.constant(1)))
} }
Expand All @@ -555,7 +555,7 @@ private case class Method(fields: Fields, generator: CodeBlock, aux:AuxGenerator
Expression.eq(generator.load(intermediate.name()), Expression.constant(null)), Expression.eq(generator.load(intermediate.name()), Expression.constant(null)),
Expression.constant(-1), generator.load(intermediate.name()))) Expression.constant(-1), generator.load(intermediate.name())))


using(generator.whileLoop(Expression.gt(times, Expression.constant(0)))) { body => using(generator.whileLoop(Expression.gt(times, Expression.constant(0), typeRef[Int]))) { body =>
block(copy(generator=body)) block(copy(generator=body))
body.assign(times, Expression.subtractInts(times, Expression.constant(1))) body.assign(times, Expression.subtractInts(times, Expression.constant(1)))
} }
Expand Down
Expand Up @@ -554,7 +554,7 @@ private case class Method(fields: Fields, generator: CodeBlock, aux:AuxGenerator
val keyVar = keyVars.head val keyVar = keyVars.head
val times = generator.declare(typeRef[Int], context.namer.newVarName()) val times = generator.declare(typeRef[Int], context.namer.newVarName())
generator.assign(times, Expression.invoke(generator.load(tableVar), Methods.countingTableGet, generator.load(keyVar))) generator.assign(times, Expression.invoke(generator.load(tableVar), Methods.countingTableGet, generator.load(keyVar)))
using(generator.whileLoop(Expression.gt(times, Expression.constant(0)))) { body => using(generator.whileLoop(Expression.gt(times, Expression.constant(0), typeRef[Int]))) { body =>
block(copy(generator=body)) block(copy(generator=body))
body.assign(times, Expression.subtractInts(times, Expression.constant(1))) body.assign(times, Expression.subtractInts(times, Expression.constant(1)))
} }
Expand All @@ -569,7 +569,7 @@ private case class Method(fields: Fields, generator: CodeBlock, aux:AuxGenerator
Expression.eq(generator.load(intermediate.name()), Expression.constant(null)), Expression.eq(generator.load(intermediate.name()), Expression.constant(null)),
Expression.constant(-1), generator.load(intermediate.name()))) Expression.constant(-1), generator.load(intermediate.name())))


using(generator.whileLoop(Expression.gt(times, Expression.constant(0)))) { body => using(generator.whileLoop(Expression.gt(times, Expression.constant(0), typeRef[Int]))) { body =>
block(copy(generator=body)) block(copy(generator=body))
body.assign(times, Expression.subtractInts(times, Expression.constant(1))) body.assign(times, Expression.subtractInts(times, Expression.constant(1)))
} }
Expand Down
Expand Up @@ -581,7 +581,7 @@ private case class Method(fields: Fields, generator: CodeBlock, aux:AuxGenerator
val times = generator.declare(typeRef[Int], context.namer.newVarName()) val times = generator.declare(typeRef[Int], context.namer.newVarName())
generator.assign(times, Expression generator.assign(times, Expression
.invoke(generator.load(tableVar), Methods.countingTableGet, generator.load(keyVar))) .invoke(generator.load(tableVar), Methods.countingTableGet, generator.load(keyVar)))
using(generator.whileLoop(Expression.gt(times, Expression.constant(0)))) { body => using(generator.whileLoop(Expression.gt(times, Expression.constant(0), typeRef[Int]))) { body =>
block(copy(generator=body)) block(copy(generator=body))
body.assign(times, Expression.subtractInts(times, Expression.constant(1))) body.assign(times, Expression.subtractInts(times, Expression.constant(1)))
} }
Expand All @@ -596,7 +596,7 @@ private case class Method(fields: Fields, generator: CodeBlock, aux:AuxGenerator
Expression.eq(generator.load(intermediate.name()), Expression.constant(null)), Expression.eq(generator.load(intermediate.name()), Expression.constant(null)),
Expression.constant(-1), generator.load(intermediate.name()))) Expression.constant(-1), generator.load(intermediate.name())))


using(generator.whileLoop(Expression.gt(times, Expression.constant(0)))) { body => using(generator.whileLoop(Expression.gt(times, Expression.constant(0), typeRef[Int]))) { body =>
block(copy(generator=body)) block(copy(generator=body))
body.assign(times, Expression.subtractInts(times, Expression.constant(1))) body.assign(times, Expression.subtractInts(times, Expression.constant(1)))
} }
Expand Down

0 comments on commit 4e8e93a

Please sign in to comment.