@Override public DirectCompilerResult visit(FunctionDefNode n) { MethodCallExpr list = Expressions.list(); n.getFormalParameters() .stream() .map(fp -> fp.accept(this)) .map(DirectCompilerResult::getExpression) .forEach(list::addArgument); if (n.isExternal()) { List<String> paramNames = n.getFormalParameters().stream() .map(BaseNode::getText) .collect(Collectors.toList()); return Functions.declaration( n, list, Functions.external(paramNames, n.getBody())); } else { DirectCompilerResult body = n.getBody().accept(this); return Functions.declaration(n, list, body.getExpression()).withFD(body); } }
public static FunctionDefNode newFunctionDefinition(ParserRuleContext ctx, ListNode parameters, boolean external, BaseNode body) { return new FunctionDefNode( ctx, parameters, external, body ); }
public static Class<?> getType(String typeName) throws ClassNotFoundException { // first check if it is primitive Class<?> type = convertPrimitiveNameToType( typeName ); if( type == null ) { // if it is not, then try to load it type = Class.forName( typeName ); } return type; }
String[] mp = parseMethod( methodSignature ); if( mp != null ) { String methodName = mp[0]; String[] paramTypeNames = parseParams( mp[1] ); int numberOfParams = paramTypeNames.length; if( numberOfParams == params.size() ) { Class[] paramTypes = new Class[ numberOfParams ]; for( int i = 0; i < numberOfParams; i++ ) { paramTypes[i] = getType( paramTypeNames[i] ); ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.PARAMETER_COUNT_MISMATCH_ON_FUNCTION_DEFINITION, getText()) ) ); return null; ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.UNABLE_TO_FIND_EXTERNAL_FUNCTION_AS_DEFINED_BY, getText()) ) ); } catch( Exception e ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.ERROR_RESOLVING_EXTERNAL_FUNCTION_AS_DEFINED_BY, getText()), e) );
String[] mp = FunctionDefNode.parseMethod(methodSignature); try { String methodName = mp[0]; String[] paramTypeNames = FunctionDefNode.parseParams(mp[1]); ArrayList<Expression> paramExprs = new ArrayList<>(); if (paramTypeNames.length == params.size()) { Type paramTypeCanonicalName = JavaParser.parseType( FunctionDefNode.getType(paramTypeName).getCanonicalName());
public static DirectCompilerResult declaration(FunctionDefNode n, MethodCallExpr list, Expression fnBody) { LambdaExpr lambda = Expressions.lambda(fnBody); String fnName = Constants.functionName(n.getBody().getText()); DirectCompilerResult r = DirectCompilerResult.of( Functions.internal(list, new NameExpr(fnName)), BuiltInType.FUNCTION); r.addFieldDesclaration(Constants.function(fnName, lambda)); return r; }
@Test public void testFunctionDefinition() { String inputExpression = "{ is minor : function( person's age ) person's age < 21 }"; BaseNode ctxbase = parse( inputExpression ); assertThat( ctxbase, is( instanceOf( ContextNode.class ) ) ); assertThat( ctxbase.getText(), is( inputExpression ) ); ContextNode ctx = (ContextNode) ctxbase; assertThat( ctx.getEntries().size(), is( 1 ) ); ContextEntryNode entry = ctx.getEntries().get( 0 ); assertThat( entry.getName(), is( instanceOf( NameDefNode.class ) ) ); NameDefNode name = (NameDefNode) entry.getName(); assertThat( name.getText(), is("is minor") ); assertThat( entry.getValue(), is( instanceOf( FunctionDefNode.class ) ) ); assertThat( entry.getValue().getText(), is("function( person's age ) person's age < 21") ); FunctionDefNode isMinorFunc = (FunctionDefNode) entry.getValue(); assertThat( isMinorFunc.getFormalParameters().size(), is( 1 ) ); assertThat( isMinorFunc.getFormalParameters().get( 0 ).getText(), is( "person's age" ) ); assertThat( isMinorFunc.isExternal(), is( false ) ); assertThat( isMinorFunc.getBody(), is( instanceOf( InfixOpNode.class ) ) ); }
assertThat( cos.getFormalParameters().size(), is( 1 ) ); assertThat( cos.getFormalParameters().get( 0 ).getText(), is( "angle" ) ); assertThat( cos.isExternal(), is( true ) ); assertThat( cos.getBody(), is( instanceOf( ContextNode.class ) ) ); ContextNode body = (ContextNode) cos.getBody(); assertThat( body.getEntries().size(), is( 1 ) ); ContextEntryNode java = body.getEntries().get( 0 );