Skip to content

Commit

Permalink
Add Lambda example
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Sep 13, 2023
1 parent 121e2e6 commit af38c16
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Phase2Lambda",
"request": "launch",
"mainClass": "ortus.boxlang.runtime.testing.Phase2Lambda",
"projectName": "runtime"
},
{
"type": "java",
"name": "Phase2Closure",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* [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.runtime.testing;

import ortus.boxlang.runtime.context.FunctionBoxContext;
import ortus.boxlang.runtime.operators.Concat;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.scopes.LocalScope;
import ortus.boxlang.runtime.types.Lambda;

/**
* Phase 2 BoxLang
* Example of UDF delcaration and execution
*/
public class Phase2Lambda$greet extends Lambda {

private static Phase2Lambda$greet instance;

private Phase2Lambda$greet() {
super(
new Argument[] {
new Argument( true, "String", Key.of( "name" ), "Brad", "" )
}
);
}

public static synchronized Phase2Lambda$greet getInstance() {
if ( instance == null ) {
instance = new Phase2Lambda$greet();
}
return instance;
}

/**
* <pre>
( required string name='Brad' ) -> {
var greeting = "Hello " & name;
return greeting;
}
* </pre>
*/
@Override
public Object invoke( FunctionBoxContext context ) {

context.getScopeNearby( LocalScope.name ).assign(
Key.of( "Greeting" ),
Concat.invoke(
"Hello ",
context.scopeFindNearby( Key.of( "name" ), null ).value()
)
);

return context.scopeFindNearby( Key.of( "greeting" ), null ).value();
}

}
142 changes: 142 additions & 0 deletions runtime/src/main/java/ortus/boxlang/runtime/testing/Phase2Lambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* [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.runtime.testing;

import java.util.List;
import java.util.Map;

import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.context.IBoxContext;
// BoxLang Auto Imports
import ortus.boxlang.runtime.dynamic.BaseTemplate;
import ortus.boxlang.runtime.dynamic.Referencer;
import ortus.boxlang.runtime.loader.ClassLocator;
import ortus.boxlang.runtime.loader.ImportDefinition;
import ortus.boxlang.runtime.scopes.IScope;
import ortus.boxlang.runtime.scopes.Key;

/**
* Phase 2 BoxLang
* Example of UDF delcaration and execution
*/
public class Phase2Lambda extends BaseTemplate {

private static Phase2Lambda instance;

private final static List<ImportDefinition> imports = List.of();

private Phase2Lambda() {
}

public static synchronized Phase2Lambda getInstance() {
if ( instance == null ) {
instance = new Phase2Lambda();
}
return instance;
}

/**
* <pre>
<cfscript>
variables.greet = ( required string name='Brad' ) -> {
var greeting = "Hello " & name;
return greeting;
}
variables.out = (create java.lang.System).out;
// Positional args
variables.out.println( greet( 'John' ) );
// named args
variables.out.println( greet( name='John' ) );
</cfscript>
* </pre>
*/

@Override
public void _invoke( IBoxContext context ) throws Throwable {
ClassLocator classLocator = ClassLocator.getInstance();
IScope variablesScope = context.getScopeNearby( Key.of( "variables" ) );

// Create instance of Lambda and set in the variables scope
variablesScope.put(
Key.of( "greet" ),
Phase2Lambda$greet.getInstance()
);

variablesScope.put(
Key.of( "out" ),
Referencer.get(
classLocator.load( context, "java:java.lang.System", imports ),
Key.of( "out" ),
false )
);

// Positional args
Referencer.getAndInvoke(
context,
// Object
variablesScope.get( Key.of( "out" ) ),
// Method
Key.of( "println" ),
// Arguments
new Object[] {
context.invokeFunction( Key.of( "greet" ), new Object[] { "John" } )
},
false
);

// named args
Referencer.getAndInvoke(
context,
// Object
variablesScope.get( Key.of( "out" ) ),
// Method
Key.of( "println" ),
// Arguments
new Object[] {
context.invokeFunction( Key.of( "greet" ), Map.of( Key.of( "name" ), "Bob" ) )
},
false
);

}

/**
* Main method
*
* @param args
*/
public static void main( String[] args ) {
// This is the main method, it will be invoked when the template is executed
// You can use this
// Get a runtime going
BoxRuntime boxRuntime = BoxRuntime.getInstance( true );

try {
boxRuntime.executeTemplate( Phase2Lambda.getInstance() );
} catch ( Throwable e ) {
e.printStackTrace();
System.exit( 1 );
}

// Bye bye! Ciao Bella!
boxRuntime.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ public void testItCanExecutePhase2Closure() throws Throwable {
} );
}

@DisplayName( "It can execute Phase2 Lambda example" )
@Test
public void testItCanExecutePhase2Lambda() throws Throwable {
Phase2Lambda.main( new String[] {
} );
}

}

0 comments on commit af38c16

Please sign in to comment.