Skip to content

Commit

Permalink
Implemented SymbolSolver for Named Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mctaverna committed Jan 17, 2024
1 parent ad34117 commit 956d980
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ortus/boxlang/ast/expression/BoxClosure.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BoxClosure extends BoxExpr {
public BoxClosure( List<BoxArgumentDeclaration> args, List<BoxAnnotation> annotations, List<BoxStatement> body, Position position,
String sourceText ) {
super( position, sourceText );
this.args = Collections.unmodifiableList( args );
this.args = args;
this.args.forEach( arg -> arg.setParent( this ) );
this.annotations = annotations;
this.annotations.forEach( arg -> arg.setParent( this ) );
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ortus/boxlang/ast/expression/BoxLambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BoxLambda extends BoxExpr {
public BoxLambda( List<BoxArgumentDeclaration> args, List<BoxAnnotation> annotations, List<BoxStatement> body, Position position,
String sourceText ) {
super( position, sourceText );
this.args = Collections.unmodifiableList( args );
this.args = args;
this.args.forEach( arg -> arg.setParent( this ) );
this.annotations = annotations;
this.annotations.forEach( arg -> arg.setParent( this ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import java.util.Map;

import ortus.boxlang.ast.BoxStatement;
import ortus.boxlang.ast.Named;
import ortus.boxlang.ast.Position;

/**
* AST Node representing a function definition
*/
public class BoxFunctionDeclaration extends BoxStatement {
public class BoxFunctionDeclaration extends BoxStatement implements Named {

private final BoxAccessModifier accessModifier;
private final String name;
Expand Down Expand Up @@ -67,7 +68,7 @@ public BoxFunctionDeclaration( BoxAccessModifier accessModifier, String name, Bo
this.annotations.forEach( arg -> arg.setParent( this ) );
this.documentation = documentation;
this.documentation.forEach( arg -> arg.setParent( this ) );
this.args = Collections.unmodifiableList( args );
this.args = args;
this.args.forEach( arg -> arg.setParent( this ) );
this.body = Collections.unmodifiableList( body );
this.body.forEach( stmt -> stmt.setParent( this ) );
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/ortus/boxlang/parser/BoxCFParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
import ortus.boxlang.parser.antlr.CFParser.ComponentContext;
import ortus.boxlang.parser.antlr.CFParser.PreannotationContext;

import static ortus.boxlang.runtime.config.util.PlaceholderHelper.resolve;

/**
* Parser for CF scripts
*/
Expand Down Expand Up @@ -155,7 +157,9 @@ public ParsingResult parse( String code ) throws IOException {

CFParser.ScriptContext parseTree = ( CFParser.ScriptContext ) parserFirstStage( inputStream );
if ( issues.isEmpty() ) {
BoxNode ast = parseTreeToAst( file, parseTree );
BoxNode ast = parseTreeToAst( file, parseTree );
SymbolSolver solver = new BoxSymbolSolver();
solver.resolve( ast );
return new ParsingResult( ast, issues );
}
return new ParsingResult( null, issues );
Expand Down Expand Up @@ -1369,12 +1373,15 @@ private List<BoxArgument> toAst( File file, CFParser.ArgumentListContext node )
*/
private BoxArgument toAst( File file, CFParser.ArgumentContext node ) {

if ( node.EQUAL() != null || node.COLON() != null ) {
if ( /* node.EQUAL() != null || */ node.COLON() != null ) {
BoxExpr value = toAst( file, node.expression().get( 1 ) );
BoxExpr name = toAst( file, node.expression().get( 0 ) );
return new BoxArgument( name, value, getPosition( node ), getSourceText( node ) );
} else {
BoxExpr value = toAst( file, node.expression().get( 0 ) );
if ( value instanceof BoxAssignment arg ) {
return new BoxArgument( arg.getLeft(), arg.getRight(), getPosition( node ), getSourceText( node ) );
}
return new BoxArgument( value, getPosition( node ), getSourceText( node ) );
}
}
Expand Down
140 changes: 140 additions & 0 deletions src/main/java/ortus/boxlang/parser/BoxSymbolSolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ortus.boxlang.parser;

import ortus.boxlang.ast.BoxNode;
import ortus.boxlang.ast.Named;
import ortus.boxlang.ast.Node;
import ortus.boxlang.ast.ReferenceByName;
import ortus.boxlang.ast.expression.*;
import ortus.boxlang.ast.statement.BoxArgumentDeclaration;
import ortus.boxlang.ast.statement.BoxFunctionDeclaration;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class BoxSymbolSolver extends SymbolSolver {

@Override
void resolve( BoxNode result ) {
result.walk().forEach( node -> {
if ( node instanceof BoxFunctionInvocation invocation ) {
ReferenceByName name = invocation.getName();
List<Node> ancestors = node.walkAncestors();
ortus.boxlang.ast.Node root = ancestors.get( ancestors.size() - 1 );
root.walk().forEach( it -> {
if ( it instanceof Named symbol ) {
if ( symbol.getName().equalsIgnoreCase( name.getName() ) ) {
if ( it.getParent() instanceof BoxAssignment assignment ) {
if ( assignment.getRight() instanceof BoxClosure closure ) {
// invocation.getName().setReference( assignment.getRight() );
invocation.getArguments().forEach( iarg -> {
AtomicBoolean exists = new AtomicBoolean( false );
closure.getArgs().forEach( carg -> {
if ( iarg.getName() != null ) {
String argName = ( ( BoxIdentifier ) iarg.getName() ).getName();
if ( argName.equalsIgnoreCase( carg.getName() ) ) {
exists.set( true );
}
}
} );
if ( !exists.get() ) {
if ( iarg.getName() != null ) {
String aname = ( ( BoxIdentifier ) iarg.getName() ).getName();
closure.getArgs().add( new BoxArgumentDeclaration(
false,
"Any",
aname,
null,
List.of(),
List.of(),
iarg.getPosition(),
iarg.getSourceText()
) );
}
}
} );
}
if ( assignment.getRight() instanceof BoxLambda lambda ) {
// invocation.getName().setReference( assignment.getRight() );
invocation.getArguments().forEach( iarg -> {
AtomicBoolean exists = new AtomicBoolean( false );
lambda.getArgs().forEach( carg -> {
if ( iarg.getName() != null ) {
String argName = ( ( BoxIdentifier ) iarg.getName() ).getName();
if ( argName.equalsIgnoreCase( carg.getName() ) ) {
exists.set( true );
}
}
} );
if ( !exists.get() ) {
if ( iarg.getName() != null ) {
String aname = ( ( BoxIdentifier ) iarg.getName() ).getName();
lambda.getArgs().add( new BoxArgumentDeclaration(
false,
"Any",
aname,
null,
List.of(),
List.of(),
iarg.getPosition(),
iarg.getSourceText()
) );
}
}
} );
}
}
if ( it instanceof BoxFunctionDeclaration function ) {
// invocation.getName().setReference( assignment.getRight() );
invocation.getArguments().forEach( iarg -> {
AtomicBoolean exists = new AtomicBoolean( false );
function.getArgs().forEach( carg -> {
if ( iarg.getName() != null ) {
String argName = ( ( BoxIdentifier ) iarg.getName() ).getName();
if ( argName.equalsIgnoreCase( carg.getName() ) ) {
exists.set( true );
}
}
} );
if ( !exists.get() ) {
if ( iarg.getName() != null ) {
String aname = ( ( BoxIdentifier ) iarg.getName() ).getName();
function.getArgs().add( new BoxArgumentDeclaration(
false,
"Any",
aname,
null,
List.of(),
List.of(),
iarg.getPosition(),
iarg.getSourceText()
) );
}
}
} );

}
}
}
} );
}
} );
}
}
25 changes: 25 additions & 0 deletions src/main/java/ortus/boxlang/parser/SymbolSolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* [BoxLang]
*
* Copyright [2023] [Ortus Solutions, Corp]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ortus.boxlang.parser;

import ortus.boxlang.ast.BoxNode;

public abstract class SymbolSolver {

abstract void resolve( BoxNode result );
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
package ortus.boxlang.transpiler.transformer.expression;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.Expression;

import ortus.boxlang.ast.BoxNode;
import ortus.boxlang.ast.expression.BoxArgument;
import ortus.boxlang.ast.Named;
import ortus.boxlang.ast.expression.*;
import ortus.boxlang.ast.statement.BoxArgumentDeclaration;
import ortus.boxlang.transpiler.JavaTranspiler;
import ortus.boxlang.transpiler.transformer.AbstractTransformer;
import ortus.boxlang.transpiler.transformer.TransformerContext;
Expand Down Expand Up @@ -56,6 +60,8 @@ public Node transform( BoxNode node, TransformerContext context ) throws Illegal
String side = context == TransformerContext.NONE ? "" : "(" + context.toString() + ") ";
Expression expr = ( Expression ) transpiler.transform( arg.getValue(), context );
// TODO handle named parameters
// Symbol resolution

Map<String, String> values = new HashMap<>() {

{
Expand Down

0 comments on commit 956d980

Please sign in to comment.