Skip to content

Commit

Permalink
Make sure variables are properly loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Oct 5, 2018
1 parent 61c5fc2 commit 46f5378
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 304 deletions.
Expand Up @@ -543,10 +543,7 @@ public void cast( TypeReference type, Expression expression )
public void instanceOf( TypeReference type, Expression expression )
{
expression.accept( this );
if ( !type.equals( expression.type() ) )
{
methodVisitor.visitTypeInsn( INSTANCEOF, byteCodeName( type ) );
}
methodVisitor.visitTypeInsn( INSTANCEOF, byteCodeName( type ) );
}

@Override
Expand Down
Expand Up @@ -211,7 +211,7 @@ case class CachedNodePropertyExists(cachedNodeProperty: Expression) extends Pred
trait StringOperator {
self: Predicate =>
override def isMatch(m: ExecutionContext, state: QueryState) = (lhs(m, state), rhs(m, state)) match {
case (l: TextValue, r: TextValue) => Some(compare(l, r)
case (l: TextValue, r: TextValue) => Some(compare(l, r))
case (_, _) => None
}

Expand Down Expand Up @@ -271,7 +271,9 @@ case class RegularExpression(lhsExpr: Expression, regexExpr: Expression)
val lValue = lhsExpr(m, state)
val rValue = regexExpr(m, state)
(lValue, rValue) match {
case (lhs: TextValue, rhs) if rhs != Values.NO_VALUE => Some(CypherBoolean.regex(lhs, rhs).booleanValue())
case (lhs: TextValue, rhs) if rhs != Values.NO_VALUE =>
val rhsAsRegexString = converter(CastSupport.castOrFail[TextValue](rhs))
Some(CypherBoolean.regex(lhs, rhsAsRegexString).booleanValue())
case _ => None
}
}
Expand Down
Expand Up @@ -95,74 +95,24 @@ public static Value notEquals( AnyValue lhs, AnyValue rhs )
return compare ? FALSE : TRUE;
}

public static Value regex( AnyValue lhs, AnyValue rhs )
public static BooleanValue regex( TextValue lhs, TextValue rhs )
{
String regexString = CypherFunctions.asString( rhs );
if ( lhs instanceof TextValue )
{
try
{
boolean matches = Pattern.compile( regexString ).matcher( ((TextValue) lhs).stringValue() ).matches();
return matches ? TRUE : FALSE;
}
catch ( PatternSyntaxException e )
{
throw new InvalidSemanticsException( "Invalid Regex: " + e.getMessage() );
}
}
else
String regexString = rhs.stringValue();
try
{
return NO_VALUE;
}
}

public static Value regex( AnyValue text, Pattern pattern )
{
if ( text instanceof TextValue )
{
boolean matches = pattern.matcher( ((TextValue) text).stringValue() ).matches();
boolean matches = Pattern.compile( regexString ).matcher( lhs.stringValue() ).matches();
return matches ? TRUE : FALSE;
}
else
{
return NO_VALUE;
}
}

public static Value startsWith( AnyValue lhs, AnyValue rhs )
{
if ( lhs instanceof TextValue && rhs instanceof TextValue )
{
return ((TextValue) lhs).stringValue().startsWith( ((TextValue) rhs).stringValue() ) ? TRUE : FALSE;
}
else
catch ( PatternSyntaxException e )
{
return NO_VALUE;
throw new InvalidSemanticsException( "Invalid Regex: " + e.getMessage() );
}
}

public static Value endsWith( AnyValue lhs, AnyValue rhs )
public static BooleanValue regex( TextValue text, Pattern pattern )
{
if ( lhs instanceof TextValue && rhs instanceof TextValue )
{
return ((TextValue) lhs).stringValue().endsWith( ((TextValue) rhs).stringValue() ) ? TRUE : FALSE;
}
else
{
return NO_VALUE;
}
}

public static Value contains( AnyValue lhs, AnyValue rhs )
{
if ( lhs instanceof TextValue && rhs instanceof TextValue )
{
return ((TextValue) lhs).stringValue().contains( ((TextValue) rhs).stringValue() ) ? TRUE : FALSE;
}
else
{
return NO_VALUE;
}
boolean matches = pattern.matcher( text.stringValue() ).matches();
return matches ? TRUE : FALSE;
}

public static Value lessThan( AnyValue lhs, AnyValue rhs )
Expand Down
Expand Up @@ -1162,14 +1162,19 @@ private static AnyValue mapAccess( MapValue container, AnyValue index )
return container.get( asString( index ) );
}

static String asString( AnyValue value )
public static TextValue asTextValue( AnyValue value )
{
if ( !(value instanceof TextValue) )
{
throw new CypherTypeException( format( "Expected %s to be a %s, but it was a %s", value,
TextValue.class.getName(), value.getClass().getName()), null );
TextValue.class.getName(), value.getClass().getName() ), null );
}
return ((TextValue) value).stringValue();
return (TextValue) value;
}

static String asString( AnyValue value )
{
return asTextValue( value ).stringValue();
}

private static NumberValue asNumberValue( AnyValue value )
Expand Down
Expand Up @@ -251,5 +251,16 @@ object CodeGeneration {

// (to) expressions
case Cast(to, expression) => Expression.cast(to, compileExpression(expression, block))

// expressions instance of t
case InstanceOf(typ, expression) => Expression.instanceOf(typ, compileExpression(expression, block))

case Not(test) => Expression.not(compileExpression(test, block))

case e@OneTime(inner) =>
if (!e.isUsed) {
e.use()
compileExpression(inner, block)
} else Expression.EMPTY
}
}

0 comments on commit 46f5378

Please sign in to comment.