diff --git a/java/ptrman/causalReasoningSystem/functional/language/ast/Type.java b/java/ptrman/causalReasoningSystem/functional/language/ast/Type.java index b469732..266e963 100644 --- a/java/ptrman/causalReasoningSystem/functional/language/ast/Type.java +++ b/java/ptrman/causalReasoningSystem/functional/language/ast/Type.java @@ -7,7 +7,7 @@ public class Type extends ptrman.causalReasoningSystem.functional.tree.Type { public enum EnumType { FUNCDAMENTAL_SEQUENCE, - COND, + //COND, is functioncall with method = cond FUNCTION_DEFINITION, FUNCTION_CALL, diff --git a/java/ptrman/causalReasoningSystem/functional/language/codegen/CodegenJava.java b/java/ptrman/causalReasoningSystem/functional/language/codegen/CodegenJava.java index aa4cfc3..0975834 100644 --- a/java/ptrman/causalReasoningSystem/functional/language/codegen/CodegenJava.java +++ b/java/ptrman/causalReasoningSystem/functional/language/codegen/CodegenJava.java @@ -1,7 +1,6 @@ package ptrman.causalReasoningSystem.functional.language.codegen; -import javassist.CannotCompileException; -import javassist.CtMethod; +import javassist.*; import ptrman.causalReasoningSystem.functional.language.ast.Element; import ptrman.causalReasoningSystem.functional.language.ast.Type; @@ -9,7 +8,6 @@ import java.util.List; import java.util.Map; -import javassist.CtClass; import ptrman.causalReasoningSystem.functional.language.runtime.ResultAndControlflowPropagationInfo; /** @@ -33,12 +31,26 @@ private enum EnumMathematicalAccumulatorInitialValue { private final CtClass ctClass; + private String variableContainerClassName; + private CtClass classForVariableContainer; - - public CodegenJava(CtClass classTemplate) { + public CodegenJava(ClassPool classPool, CtClass classTemplate) { this.ctClass = classTemplate; + + variableContainerClassName = "VariableContainerForFunctionalFunction0"; + classForVariableContainer = classPool.makeClass(variableContainerClassName); + } + + + + public void addVariableFieldsToVariableContainerForGeneratedFunctionalFunction(Map typeinfoOfVariables) throws CannotCompileException, NotFoundException { + for( String iterationVariablename : typeinfoOfVariables.keySet() ) { + classForVariableContainer.addField(new CtField(CtClass.booleanType, iterationVariablename, classForVariableContainer)); + } } + + public String generateFunctionByStaticTypes(Element astEntry, Map typeinfoOfVariables) { // TODO< uncomment, currently we are testing stuff > @@ -56,10 +68,11 @@ private InlinedResultCodeWithType generateFunctionByStaticTypesInternalFunctionW // public for testing public InlinedResultCodeWithType generateBodyForwardPass(Element astElement, Map typeinfoOfVariables) throws CannotCompileException { + /* if( astElement.type.isEqualWithType(new Type(Type.EnumType.COND)) ) { return generateBodyForwardPassForCondition(astElement, typeinfoOfVariables); - } - else if( astElement.type.isEqualWithType(new Type(Type.EnumType.FUNCTION_CALL))) { + }*/ + if( astElement.type.isEqualWithType(new Type(Type.EnumType.FUNCTION_CALL))) { if( astElement.identifier == null ) { if( astElement.callType == Element.EnumCallType.ADD ) { return generateBodyForwardPassForMathematicalAccumulatorOperation("+", EnumMathematicalAccumulatorInitialValue.ZERO, astElement, typeinfoOfVariables); @@ -78,17 +91,45 @@ else if( astElement.callType == Element.EnumCallType.DIV ) { } } else { + if( astElement.identifier.equals("cond") ) { + return generateBodyForwardPassForCondition(astElement, typeinfoOfVariables); + } + + throw new RuntimeException("TODO< generate code for call of function >"); } } else if( astElement.type.isEqualWithType(new Type(Type.EnumType.CONSTANT_INTEGER)) ) { return generateBodyForwardPassForIntegerConstant(astElement, typeinfoOfVariables); } + else if( astElement.type.isEqualWithType(new Type(Type.EnumType.VARIABLE)) ) { + return generateBodyForwardPassForVariable(astElement, typeinfoOfVariables); + } + else { throw new RuntimeException("Internal Error!"); } } + private InlinedResultCodeWithType generateBodyForwardPassForVariable(Element astElement, Map typeinfoOfVariables) throws CannotCompileException { + final String variableName = astElement.identifier; + + if( !typeinfoOfVariables.containsKey(variableName) ) { + throw new RuntimeException("Static compilation Error: Variable " + variableName + " was not found!"); + } + + final Typeinfo typeinfoOfVariable = typeinfoOfVariables.get(variableName); + final Typeinfo resultType = typeinfoOfVariable; + + StringBuilder resultCodeBuilder = new StringBuilder(); + resultCodeBuilder.append(String.format("result = %s.%s(variables.%s);\n", RESULT_AND_PROPAGATION_TYPENAME, ResultAndControlflowPropagationInfo.codegenGetJavaFunctionnameForCreationOfType(typeinfoOfVariable), variableName)); + + final String resultCode = resultCodeBuilder.toString(); + final GeneratedFunctionInfo generatedFunctionInfo = emitInternalFunction(resultType, resultCode); + + return new InlinedResultCodeWithType(resultType, generatedFunctionInfo); + } + private InlinedResultCodeWithType generateBodyForwardPassForMathematicalAccumulatorOperation(String operationString, EnumMathematicalAccumulatorInitialValue accumulatorInitialValue, Element astElement, Map typeinfoOfVariables) throws CannotCompileException { /*assert astElement.type.isEqualWithType(new Type(Type.EnumType.OPERATION_ADDITION)) || @@ -167,7 +208,7 @@ private List inlineResultCodeForAllArguments(Element } private InlinedResultCodeWithType generateBodyForwardPassForCondition(Element condition, Map typeinfoOfVariables) throws CannotCompileException { - assert condition.type.isEqualWithType(new Type(Type.EnumType.COND)); + //assert condition.type.isEqualWithType(new Type(Type.EnumType.COND)); Element conditionCheck = (Element)condition.children.get(0); Element conditionTrue = (Element) condition.children.get(1); @@ -218,8 +259,11 @@ private InlinedResultCodeWithType generateBodyForwardPassForIntegerConstant(Elem private String getJavaFunctioncallForFunction(GeneratedFunctionInfo functionInfo) { - // TODO< parameters > - return functionInfo.getInternalFunctionname() + "()"; + return functionInfo.getInternalFunctionname() + "(variables)"; + } + + private String getJavaClassnameOfVariableContainer() { + return variableContainerClassName; } @@ -234,7 +278,7 @@ private GeneratedFunctionInfo emitInternalFunction(Typeinfo typeinfo, String cod StringBuilder functionCodeBuilder = new StringBuilder(); // for now public for debugging, later private - functionCodeBuilder.append(String.format("public static %s %s() {\n", RESULT_AND_PROPAGATION_TYPENAME, generatedFunctionInfo.getInternalFunctionname())); + functionCodeBuilder.append(String.format("public static %s %s(%s variables) {\n", RESULT_AND_PROPAGATION_TYPENAME, generatedFunctionInfo.getInternalFunctionname(), getJavaClassnameOfVariableContainer())); functionCodeBuilder.append("// codegen: result allocation\n"); functionCodeBuilder.append(RESULT_AND_PROPAGATION_TYPENAME + " result;\n"); diff --git a/java/ptrman/causalReasoningSystem/functional/language/convertParseTreeToAst/ConvertParseTreeToAst.java b/java/ptrman/causalReasoningSystem/functional/language/convertParseTreeToAst/ConvertParseTreeToAst.java index 23bc6dd..daecbf5 100644 --- a/java/ptrman/causalReasoningSystem/functional/language/convertParseTreeToAst/ConvertParseTreeToAst.java +++ b/java/ptrman/causalReasoningSystem/functional/language/convertParseTreeToAst/ConvertParseTreeToAst.java @@ -26,7 +26,6 @@ else if( root instanceof BasicIntegerParseTreeElement ) { result.constantInteger = Integer.parseInt(convertedRoot.constant); return result; } - else if( root instanceof HasChildrenParseTreeElement ) { HasChildrenParseTreeElement convertedRoot = (HasChildrenParseTreeElement)root; @@ -82,6 +81,13 @@ else if( convertedRoot.type == HasChildrenParseTreeElement.EnumType.VARIABLE_OR_ // simplfy it return convert(convertedRoot.children[0]); } + else if( convertedRoot.type == HasChildrenParseTreeElement.EnumType.VARIABLE ) { + BasicIdentifierParseTreeElement identiferParseTreeElement = (BasicIdentifierParseTreeElement) convertedRoot.children[0]; + + Element result = new Element(new Type(Type.EnumType.VARIABLE)); + result.identifier = identiferParseTreeElement.identifier; + return result; + } else { throw new RuntimeException("Internal Error!"); } diff --git a/java/ptrman/causalReasoningSystem/functional/language/runtime/ResultAndControlflowPropagationInfo.java b/java/ptrman/causalReasoningSystem/functional/language/runtime/ResultAndControlflowPropagationInfo.java index 676a7c0..04b9eee 100644 --- a/java/ptrman/causalReasoningSystem/functional/language/runtime/ResultAndControlflowPropagationInfo.java +++ b/java/ptrman/causalReasoningSystem/functional/language/runtime/ResultAndControlflowPropagationInfo.java @@ -8,18 +8,30 @@ */ public class ResultAndControlflowPropagationInfo { int resultInteger; + boolean resultBoolean; // called from generated code - public static ResultAndControlflowPropagationInfo createResultForConstantInteger(final int value) { + public static ResultAndControlflowPropagationInfo createResultForInteger(final int value) { ResultAndControlflowPropagationInfo result = new ResultAndControlflowPropagationInfo(); result.resultInteger = value; return result; } + // called from generated code + public static ResultAndControlflowPropagationInfo createResultForBoolean(final boolean value) { + ResultAndControlflowPropagationInfo result = new ResultAndControlflowPropagationInfo(); + result.resultBoolean = value; + return result; + } + + // used by codegeneration public static String codegenGetJavaFunctionnameForCreationOfType(Typeinfo typeinfo) { if( typeinfo.type == Typeinfo.EnumType.INTEGER ) { - return "createResultForConstantInteger"; + return "createResultForInteger"; + } + else if( typeinfo.type == Typeinfo.EnumType.BOOLEAN ) { + return "createResultForBoolean"; } throw new RuntimeException("Internal Error: No function for Static Runtime found to create value of type!"); diff --git a/java/ptrman/causalReasoningSystem/interactiveTests/CodeGenTest.java b/java/ptrman/causalReasoningSystem/interactiveTests/CodeGenTest.java index ba1bcb0..9353c5b 100644 --- a/java/ptrman/causalReasoningSystem/interactiveTests/CodeGenTest.java +++ b/java/ptrman/causalReasoningSystem/interactiveTests/CodeGenTest.java @@ -3,13 +3,16 @@ import javassist.CannotCompileException; import javassist.ClassPool; import javassist.CtClass; +import javassist.NotFoundException; import ptrman.causalReasoningSystem.functional.language.ast.Element; import ptrman.causalReasoningSystem.functional.language.codegen.CodegenJava; +import ptrman.causalReasoningSystem.functional.language.codegen.Typeinfo; import ptrman.causalReasoningSystem.functional.language.convertParseTreeToAst.ConvertParseTreeToAst; import ptrman.causalReasoningSystem.functional.language.parseTree.ParseTreeElement; import ptrman.causalReasoningSystem.functional.language.parser.Parse; import java.util.HashMap; +import java.util.Map; /** * Created by r0b3 on 11.12.2015. @@ -23,13 +26,25 @@ public static void main(String[] args) { CtClass class0 = ctPool.makeClass("GeneratedCode"); - ParseTreeElement parseTree = Parse.parse("(+ 5 4)"); + ParseTreeElement parseTree = Parse.parse("(cond booleanX 5 6)"); Element ast = ConvertParseTreeToAst.convert(parseTree); - CodegenJava codegen = new CodegenJava(class0); + CodegenJava codegen = new CodegenJava(ctPool, class0); + + Map variableTypes = new HashMap<>(); + variableTypes.put("booleanX", new Typeinfo(Typeinfo.EnumType.BOOLEAN)); + + try { + codegen.addVariableFieldsToVariableContainerForGeneratedFunctionalFunction(variableTypes); + } catch (CannotCompileException e) { + e.printStackTrace(); + } catch (NotFoundException e) { + e.printStackTrace(); + } + try { - codegen.generateBodyForwardPass(ast, new HashMap<>()); + codegen.generateBodyForwardPass(ast, variableTypes); } catch (CannotCompileException e) { e.printStackTrace(); }