@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); } }
@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 ) ) );