Skip to content

Commit

Permalink
BL-194 and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Jun 1, 2024
1 parent 1dbafa1 commit 651ce31
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 52 deletions.
32 changes: 19 additions & 13 deletions src/main/antlr/BoxScriptGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -557,17 +557,13 @@ notTernaryExpression:
) notTernaryExpression
| notTernaryExpression XOR notTernaryExpression
| notTernaryExpression instanceOf notTernaryExpression
| notTernaryExpression (AMPERSAND notTernaryExpression)+
| left = notTernaryExpression AMPERSAND right = notTernaryExpression
| notTernaryExpression (
eq
| (
gte
| GREATER THAN OR EQ TO
| GREATER THAN OR EQUAL TO
)
| (gt | GREATER THAN)
| (lte | LESS THAN OR EQ TO | LESS THAN OR EQUAL TO)
| (lt | LESS THAN)
| gte
| gt
| lte
| lt
| neq
| EQV
| IMP
Expand Down Expand Up @@ -618,13 +614,23 @@ and: AND | AMPAMP;

eq: EQ | EQUAL | EQEQ;

gt: GT | GTSIGN;
gt: GT | GTSIGN | GREATER THAN;

gte: GTE | GE | GTESIGN;
gte:
GTE
| GE
| GTESIGN
| GREATER THAN OR EQ TO
| GREATER THAN OR EQUAL TO;

lt: LT | LTSIGN;
lt: LT | LTSIGN | LESS THAN;

lte: LTE | LE | LTESIGN;
lte:
LTE
| LE
| LTESIGN
| LESS THAN OR EQ TO
| LESS THAN OR EQUAL TO;

neq: NEQ | IS NOT | BANGEQUAL | LESSTHANGREATERTHAN;

Expand Down
32 changes: 19 additions & 13 deletions src/main/antlr/CFScriptGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -528,24 +528,20 @@ notTernaryExpression:
) notTernaryExpression
| notTernaryExpression (PLUS | MINUS) notTernaryExpression
| notTernaryExpression XOR notTernaryExpression
| left = notTernaryExpression AMPERSAND right = notTernaryExpression
| notTernaryExpression (
eq
| (
gte
| GREATER THAN OR EQ TO
| GREATER THAN OR EQUAL TO
)
| (gt | GREATER THAN)
| (lte | LESS THAN OR EQ TO | LESS THAN OR EQUAL TO)
| (lt | LESS THAN)
| gte
| gt
| lte
| lt
| neq
| EQV
| IMP
| CONTAINS
| NOT CONTAINS
| TEQ
) notTernaryExpression // Comparision
| notTernaryExpression (AMPERSAND notTernaryExpression)+
| notTernaryExpression ELVIS notTernaryExpression // Elvis operator
| notTernaryExpression IS notTernaryExpression // IS operator
| notTernaryExpression DOES NOT CONTAIN notTernaryExpression
Expand All @@ -558,13 +554,23 @@ and: AND | AMPAMP;

eq: EQ | EQUAL | EQEQ;

gt: GT | GTSIGN;
gt: GT | GTSIGN | GREATER THAN;

gte: GTE | GE | GTESIGN;
gte:
GTE
| GE
| GTESIGN
| GREATER THAN OR EQ TO
| GREATER THAN OR EQUAL TO;

lt: LT | LTSIGN;
lt: LT | LTSIGN | LESS THAN;

lte: LTE | LE | LTESIGN;
lte:
LTE
| LE
| LTESIGN
| LESS THAN OR EQ TO
| LESS THAN OR EQUAL TO;

neq: NEQ | IS NOT | BANGEQUAL | LESSTHANGREATERTHAN;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ortus/boxlang/compiler/Boxpiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public Boxpiler() {
if ( BoxRuntime.getInstance().inDebugMode() && Files.exists( this.classGenerationDirectory ) ) {
try {
logger.debug( "Running in debugmode, first startup cleaning out class generation directory: " + classGenerationDirectory );
// if ( false )
FileUtils.cleanDirectory( classGenerationDirectory.toFile() );
if ( false )
FileUtils.cleanDirectory( classGenerationDirectory.toFile() );
} catch ( IOException e ) {
throw new BoxRuntimeException( "Error cleaning out class generation directory on first run", e );
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/ortus/boxlang/compiler/parser/BoxScriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ private BoxExpression toAst( File file, NotTernaryExpressionContext expression )
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxBinaryOperation( left, BoxBinaryOperator.And, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.or() != null && ( expression.THAN() == null ) ) {
} else if ( expression.or() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxBinaryOperation( left, BoxBinaryOperator.Or, right, getPosition( expression ), getSourceText( expression ) );
Expand Down Expand Up @@ -1559,34 +1559,35 @@ private BoxExpression toAst( File file, NotTernaryExpressionContext expression )
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.NotEqual, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.gt() != null || ( expression.GREATER() != null && expression.THAN() != null ) && expression.OR() == null ) {
} else if ( expression.gt() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.GreaterThan, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.gte() != null || ( expression.GREATER() != null && expression.THAN() != null ) && expression.OR() != null ) {
} else if ( expression.gte() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.GreaterThanEquals, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.lt() != null || ( expression.LESS() != null && expression.THAN() != null && expression.OR() == null ) ) {
} else if ( expression.lt() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.LessThan, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.lte() != null || ( expression.LESS() != null && expression.THAN() != null && expression.OR() != null ) ) {
} else if ( expression.lte() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.LesslThanEqual, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.eq() != null || expression.IS() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.Equal, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.AMPERSAND().size() > 0 ) {
} else if ( expression.AMPERSAND() != null ) {
List<BoxExpression> parts = new ArrayList<>();
BoxScriptGrammar.NotTernaryExpressionContext current = expression;
// unwrap nested foo & bar & baz & bum into a single concat node
do {
parts.add( toAst( file, ( BoxScriptGrammar.NotTernaryExpressionContext ) current.notTernaryExpression().get( 0 ) ) );
current = current.notTernaryExpression().get( 1 );
} while ( current.AMPERSAND() != null && current.AMPERSAND().size() > 0 );
parts.add( toAst( file, ( BoxScriptGrammar.NotTernaryExpressionContext ) current ) );
parts.add( 0, toAst( file, current.right ) );
current = current.left;
} while ( current.AMPERSAND() != null );
parts.add( 0, toAst( file, current ) );

return new BoxStringConcat( parts, getPosition( expression ), getSourceText( expression ) );

Expand Down
21 changes: 11 additions & 10 deletions src/main/java/ortus/boxlang/compiler/parser/CFScriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ private BoxExpression toAst( File file, NotTernaryExpressionContext expression )
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxBinaryOperation( left, BoxBinaryOperator.And, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.or() != null && ( expression.THAN() == null ) ) {
} else if ( expression.or() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxBinaryOperation( left, BoxBinaryOperator.Or, right, getPosition( expression ), getSourceText( expression ) );
Expand Down Expand Up @@ -1463,34 +1463,35 @@ private BoxExpression toAst( File file, NotTernaryExpressionContext expression )
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.NotEqual, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.gt() != null || ( expression.GREATER() != null && expression.THAN() != null ) && expression.OR() == null ) {
} else if ( expression.gt() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.GreaterThan, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.gte() != null || ( expression.GREATER() != null && expression.THAN() != null ) && expression.OR() != null ) {
} else if ( expression.gte() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.GreaterThanEquals, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.lt() != null || ( expression.LESS() != null && expression.THAN() != null && expression.OR() == null ) ) {
} else if ( expression.lt() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.LessThan, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.lte() != null || ( expression.LESS() != null && expression.THAN() != null && expression.OR() != null ) ) {
} else if ( expression.lte() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.LesslThanEqual, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.eq() != null || expression.IS() != null ) {
BoxExpression left = toAst( file, expression.notTernaryExpression( 0 ) );
BoxExpression right = toAst( file, expression.notTernaryExpression( 1 ) );
return new BoxComparisonOperation( left, BoxComparisonOperator.Equal, right, getPosition( expression ), getSourceText( expression ) );
} else if ( expression.AMPERSAND().size() > 0 ) {
} else if ( expression.AMPERSAND() != null ) {
List<BoxExpression> parts = new ArrayList<>();
CFScriptGrammar.NotTernaryExpressionContext current = expression;
// unwrap nested foo & bar & baz & bum into a single concat node
do {
parts.add( toAst( file, ( CFScriptGrammar.NotTernaryExpressionContext ) current.notTernaryExpression().get( 0 ) ) );
current = current.notTernaryExpression().get( 1 );
} while ( current.AMPERSAND() != null && current.AMPERSAND().size() > 0 );
parts.add( toAst( file, ( CFScriptGrammar.NotTernaryExpressionContext ) current ) );
parts.add( 0, toAst( file, current.right ) );
current = current.left;
} while ( current.AMPERSAND() != null );
parts.add( 0, toAst( file, current ) );

return new BoxStringConcat( parts, getPosition( expression ), getSourceText( expression ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public GetBaseTemplatePath() {
*
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
return context.findBaseTemplate().toString();
return context.findBaseTemplate().absolutePath().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -274,7 +275,8 @@ private Optional<ClassLocation> findByRelativeLocation(
// System.out.println( "packageName: " + packageName );
// System.out.println( "classname: " + className );
// System.out.println( "name: " + name );
ResolvedFilePath newResolvedFilePath = resolvedFilePath.newFromRelative( targetPath.toString() );
ResolvedFilePath newResolvedFilePath = resolvedFilePath
.newFromRelative( parentPath.relativize( Paths.get( targetPath.toString() ) ).toString() );
return Optional.of( new ClassLocation(
FilenameUtils.getBaseName( newResolvedFilePath.absolutePath().toString() ),
targetPath.toAbsolutePath().toString(),
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/ortus/boxlang/runtime/scopes/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,12 @@ public static Key of( Object obj ) {
if ( obj instanceof Double d ) {
return Key.of( d );
}
if ( obj instanceof Integer i ) {
return Key.of( i );
}
if ( obj instanceof Long l ) {
return Key.of( l );
}
// TODO: TODO: also check this higher up so we can tell the user more about what
// was null.
if ( obj == null ) {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/ortus/boxlang/runtime/util/ResolvedFilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,23 @@ public FQN getPackage( String prefix ) {
* @return A new ResolvedFilePath instance.
*/
public ResolvedFilePath newFromRelative( String relativePath ) {
String newRelativePath;
Path absoluteParent = absolutePath().getParent();
Path newAbsolutePath = absoluteParent.resolve( relativePath );

if ( absolutePath().toString().equals( relativePath() ) ) {
newRelativePath = newAbsolutePath.toString();
} else {
String tmpAbsolutePath = Paths.get( absolutePath().toString() ).toAbsolutePath().normalize().toString();
String tmpRelativePath = Paths.get( relativePath() ).normalize().toString();
Path absoluteRoot = Paths.get( tmpAbsolutePath.replace( tmpRelativePath, "" ) );
newRelativePath = absoluteRoot.relativize( newAbsolutePath ).toString();
}
return ResolvedFilePath.of(
mappingName,
mappingPath,
Paths.get( relativePath() ).resolveSibling( relativePath ).toString(),
absolutePath.getParent().resolve( relativePath )
newRelativePath,
newAbsolutePath
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/TestCases/phase1/OperatorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public void testMutliStringConcat() {
assertThat( result ).isEqualTo( "foobarbazbum" );
}

@DisplayName( "concat and eq" )
@Test
public void testConcatAndEQ() {
Object result = instance.executeStatement( "'$' & 'foo' == '$foo'", context );
assertThat( result ).isEqualTo( true );
}

@DisplayName( "string contains" )
@Test
public void testStringContains() {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/TestCases/phase3/ClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1111,4 +1111,14 @@ public void testDotExtends() {
assertThat( variables.get( result ) ).isEqualTo( "childUDFparent" );
}

@Test
public void testRelativeInstantiation() {
instance.executeSource(
"""
clazz = new src.test.java.TestCases.phase3.RelativeInstantiation();
result = clazz.findSibling()
""", context );
assertThat( variables.get( result ) ).isEqualTo( "bar" );
}

}
5 changes: 5 additions & 0 deletions src/test/java/TestCases/phase3/FindMe.bx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class {
function foo() {
return "bar";
}
}
5 changes: 5 additions & 0 deletions src/test/java/TestCases/phase3/RelativeInstantiation.bx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class {
function findSibling() {
return new findMe().foo();
}
}

0 comments on commit 651ce31

Please sign in to comment.