Changeset b78044c in OpenModelica
- Timestamp:
- 2020-05-04T16:54:57+02:00 (4 years ago)
- Children:
- ab920ce4
- Parents:
- 79775bb0
- git-author:
- johti <johti17@…> (04/07/20 11:04:09)
- git-committer:
- johti <johti17@…> (05/04/20 16:54:57)
- Location:
- OMCompiler/Compiler
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
OMCompiler/Compiler/Template/AbsynToJulia.tpl
ra4f0ef3 rb78044c 1 1 package AbsynToJulia 2 2 " 3 Translates Absyn to Julia.3 Translates Absyn IR to Julia. 4 4 @Authors: John Tinnerholm & Martin Sjoelund 5 6 46 is used as the seed for enumerations. 5 7 " 6 /* TODOS: 7 8 TODO: Public/Private semantics : ( 9 TODO: Julia does string concatination with * instead of + 10 8 9 /* 10 TODOS: 11 TODO: Module renamning and Julia adaptation of uniontypes 12 <In progress> 13 TODO: Make sure renamning of imports work as they should 11 14 */ 12 15 … … 16 19 template dumpProgram(Absyn.Program program) 17 20 ::= 21 /* Reset the tick before code generation! */ 18 22 match program 19 23 case PROGRAM(classes = {}) then "" … … 28 32 template dumpSCodeElements(list<SCode.Element> elements) 29 33 " 30 Dumps forward declaration of uniontypes and partial functions unless elements is empty. 31 Recursion needed to find all partial functions!. This should be call on a per module basis" 34 Dumps forward declaration of uniontypes and partial functions unless elements is empty. 35 This should be called once and only once per module basis 36 " 32 37 ::= dumpSCodeElements2(filterElements(elements, defaultOptions)) 33 38 end dumpSCodeElements; … … 35 40 template dumpSCodeElements2(list<SCode.Element> elements) 36 41 ::= 37 let str = elements|> el hasindex i1 fromindex 1 =>42 let str = List.unique(elements) |> el hasindex i1 fromindex 1 => 38 43 ( 39 44 match el … … 41 46 '@UniontypeDecl <%name%> <%\n%>' 42 47 case CLASS(classDef = parts as SCode.PARTS(__), partialPrefix = SCode.NOT_PARTIAL(), restriction=SCode.R_FUNCTION(__)) then 43 dumpSCodeElements2(parts.elementLst)48 dumpSCodeElements2(parts.elementLst) 44 49 case CLASS(partialPrefix = SCode.PARTIAL(), restriction=SCode.R_FUNCTION(__)) then 45 50 '<%name%> = Function<%\n%>' … … 52 57 53 58 template dumpClass(Absyn.Class cls, DumpOptions options) 54 /*We do not yet know our context in Absyn*/59 /*We do not yet know our context in Absyn :) */ 55 60 ::= dumpClassElement(cls, options, noContext) 56 61 end dumpClass; … … 82 87 let header = dumpClassHeader(parts, restriction) 83 88 let functionBodyStr = dumpClassDef(parts, makeFunctionContext(return_str), options) 89 let cmt2 = if commentStr then '"""<%commentStr%>"""' else "" 84 90 /* 85 91 Input output variables are treated as parameters … … 87 93 */ 88 94 << 89 <%c ommentStr%>95 <%cmt2%> 90 96 function <%name%>(<%if header then typevar_inputs else inputs_str%>) <%if header then "" else returnType%> <%header%> 91 97 <%functionBodyStr%> … … 96 102 let enc_str = if encapsulatedPrefix then "" /*Should we use a macro here?*/ else "" 97 103 let partial_str = if partialPrefix then "#=TODO: Originally partial =# " else "" 104 let commentStr = dumpCommentStrOpt(parts.comment) 98 105 let class_type_str = dumpClassType(restriction) 99 106 let cdef_str1 = match restriction … … 111 118 case R_PACKAGE(__) then 112 119 << 113 <%\n%>114 120 using MetaModelica 115 #= ExportAll is not good practice but it makes it so that we do not have to write export after each function :( =#116 121 using ExportAll 117 122 <%inform%> … … 120 125 <%cdef_str1%> 121 126 <%\n%> 122 #= So that we can use wildcard imports and named imports when they do occur. Not good Julia practice =#123 127 @exportAll() 124 128 >> 125 129 else 126 <<127 <%cdef_str1%>128 >>130 << 131 <%cdef_str1%> 132 >> 129 133 let begin_str = match restriction 130 134 case R_RECORD(__) then 'begin' 131 135 else '' 132 133 136 let cdef_str = cdef_str2 134 137 let cmt_str = dumpCommentStrOpt(parts.comment) … … 153 156 << 154 157 <%name%> = <%spec%> <%attr%><%comment%> 158 >> 159 case CLASS(body=parts as ENUMERATION(__), restriction=R_TYPE(__)) then 160 let _ = System.tmpTickReset(1) 161 let comment = dumpCommentOpt(parts.comment, context) 162 let enumArgs = dumpEnumDef(parts.enumLiterals) 163 << 164 <%name%> = #= Enumeration =# (() -> begin 165 <%match parts.enumLiterals 166 case ENUMLITERALS(__) then 167 (enumLiterals |> lit => '<%dumpEnumLiteral(lit)%> = <%intString(System.tmpTick())%>' ;separator="\n") 168 %> 169 ()->(<%enumArgs%>) 170 end)() 155 171 >> 156 172 /* … … 172 188 @ExtendedFunction <%name_of_new_function%> <%spec%>(<%args%>) 173 189 >> 174 /*PDER. Should not occur. Derived Enumeration and Overload might?*/ 190 //TODO: case CLASS(restriction=R_ENUMERATION(__)) then 191 //let cdef_str = dumpClassDef(parts, packageContext, options) 192 //AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassHeader:") 193 /* PDER. Should not occur. Derived, and Overload might? */ 175 194 end dumpClassElement; 176 195 … … 264 283 AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassDef: CLASS_EXETENDS not yet supported.") 265 284 case ENUMERATION(__) then 266 AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassDef: CLASS_ENUMERATION not yet supported.") 285 let enum_str = dumpEnumDef(enumLiterals) 286 ' = @enum(<%enum_str%>)' 267 287 else "TODO Unkown class definition" 268 288 end dumpClassDef; 269 289 290 template dumpEnumDef(Absyn.EnumDef enum_def) 291 ::= 292 match enum_def 293 case ENUMLITERALS(__) then 294 (enumLiterals |> lit => dumpEnumLiteral(lit) ;separator=";") 295 case ENUM_COLON() then ":" 296 end dumpEnumDef; 297 298 template dumpEnumLiteral(Absyn.EnumLiteral lit) 299 ::= 300 match lit 301 case ENUMLITERAL(__) then 302 '<%literal%>' 303 end dumpEnumLiteral; 304 270 305 template dumpClassType(Absyn.Restriction restriction) 271 306 ::= 272 307 match restriction 273 case R_PACKAGE(__) then "module"308 case R_PACKAGE(__) then 'module' 274 309 case R_METARECORD(__) then "struct" 275 310 case R_RECORD(__) then '@Record' //Only handles Metamodelica records(!) … … 277 312 case R_TYPE(__) then '' // Should be const iff we are in a global scope (Julia 1.0 (Packages are not)) 278 313 case R_FUNCTION(__) then "function" 314 case R_CLASS(__) then "module" 315 case R_MODEL(__) then "module #= Should be a model here =#" 279 316 /* TODO: The other ones are probably not relevant for now */ 280 317 else AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassType: Unknown restriction for class." + AbsynDumpTpl.dumpRestriction(restriction)) … … 313 350 match externalDecl 314 351 case EXTERNALDECL(__) then //Turned of temporary to translate builtin 315 " #= TODO: Defined in the runtime =#" //AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassPart: EXTERNALDECL(__) not supported.")352 "@error \"TODO: Defined in the runtime\"" //AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassPart: EXTERNALDECL(__) not supported.") 316 353 end dumpClassPart; 317 354 … … 509 546 let mod_str = if args_str then '(<%args_str%>)' 510 547 let ann_str = dumpAnnotationOptSpace(annotationOpt, context) 511 ' extends<%bc_str%><%mod_str%><%ann_str%>'548 'using #= Modelica extend clause =# <%bc_str%><%mod_str%><%ann_str%>' 512 549 case COMPONENTS(__) then 513 550 let attr_str = dumpElementAttr(attributes) … … 523 560 (components |> comp => 524 561 let comp_str = dumpComponentItem(comp, noContext) 525 ' <%match context case PACKAGE(__) then "const "%><%comp_str%>::<%ty_str%>' 562 '<%match context 563 case PACKAGE(__) then "const "%><%comp_str%>::<%ty_str%>' 526 564 ;separator="\n") 527 565 else '' … … 659 697 end dumpGroupImport; 660 698 661 template dumpEquation(Absyn.Equation eq)662 ::= "No equations allowed. Translate them to algorithms"663 end dumpEquation;664 665 699 template dumpAlgorithmItems(list<Absyn.AlgorithmItem> algs, Context context) 666 700 ::= (algs |> alg => dumpAlgorithmItem(alg, context) ;separator="\n") … … 686 720 if AbsynUtil.complexIsCref(assignComponent) then 687 721 match assignComponent 688 case CONS(__) then689 '@match <%lhs_str%> = <%rhs_str%>'690 else691 '<%lhs_str%> = <%rhs_str%>'692 else693 '@match <%lhs_str%> = <%rhs_str%>'722 case CONS(__) then 723 '@match <%lhs_str%> = <%rhs_str%>' 724 else /* MetaModelica assignenment via the assign operator */ 725 '@assign <%lhs_str%> = <%rhs_str%>' 726 else 727 '@match <%lhs_str%> = <%rhs_str%>' 694 728 case ALG_IF(__) then 695 729 let if_str = dumpAlgorithmBranch(ifExp, trueBranch, "if", context) … … 769 803 Needed since certain keywords will have a sligthly different meaning in Julia" 770 804 ::= 771 match path 772 case FULLYQUALIFIED(__) then 773 '.<%AbsynDumpTpl.dumpPath(path)%>' 774 case QUALIFIED(__) then 775 if (Flags.getConfigBool(Flags.MODELICA_OUTPUT)) then 776 '<%name%>__<%AbsynDumpTpl.dumpPath(path)%>' 777 else 778 '<%name%>.<%AbsynDumpTpl.dumpPath(path)%>' 779 case IDENT(__) then 780 match name 781 case "Real" then 'Float' 782 case "Integer" then 'Integer' 783 case "Boolean" then 'Bool' 784 case "list" then 'List' 785 case "array" then 'Array' 786 case "tuple" then 'Tuple' 787 case "polymorphic" then 'Any' 788 case "Mutable" then 'MutableType' 789 else '<%name%>' 790 else 791 AbsynDumpTpl.errorMsg("AbsynToJulia.dumpPathJL: Unknown path.") 805 match MMToJuliaHT.componentInPathIsInHT(path) 806 case false then 807 match path 808 case FULLYQUALIFIED(__) then 809 '.<%AbsynDumpTpl.dumpPath(path)%>' 810 case QUALIFIED(__) then 811 '<%name%>.<%AbsynDumpTpl.dumpPath(path)%>' 812 case IDENT(__) then 813 match name 814 case "Real" then 'AbstractFloat' 815 case "Integer" then 'Integer' 816 case "Boolean" then 'Bool' 817 case "list" then 'List' 818 case "array" then 'Array' 819 case "tuple" then 'Tuple' 820 case "polymorphic" then 'Any' 821 case "Mutable" then 'MutableType' 822 else '<%name%>' 823 else 824 AbsynDumpTpl.errorMsg("AbsynToJulia.dumpPathJL: Unknown path:" + AbsynDumpTpl.dumpPath(path)) 825 else /* The path we try to access have been refactored and is present in the Hash table. */ 826 match path 827 case FULLYQUALIFIED(__) then 828 '.<%AbsynDumpTpl.dumpPath(path)%>' 829 case QUALIFIED(__) then 830 '<%name%>.<%AbsynDumpTpl.dumpPath(path)%>' 831 case IDENT(__) then 832 let tmpName = name 833 match name 834 case "Real" then 'AbstractFloat' 835 case "Integer" then 'Integer' 836 case "Boolean" then 'Bool' 837 case "list" then 'List' 838 case "array" then 'Array' 839 case "tuple" then 'Tuple' 840 case "polymorphic" then 'Any' 841 case "Mutable" then 'MutableType' 842 else 843 match MMToJuliaHT.get(name) 844 case SOME(CLASS_INFO(originalClass = CLASS(name = name1), wrapperClass = CLASS(name = name2))) then 845 'test <%name2%>' 846 else 847 AbsynDumpTpl.errorMsg("AbsynToJulia.dumpPathJL: Unknown path:" + AbsynDumpTpl.dumpPath(path)) 848 else 849 AbsynDumpTpl.errorMsg("AbsynToJulia.dumpPathJL: Unknown path.") 792 850 end dumpPathJL; 793 851 … … 808 866 " 809 867 Dumps the type specification: 810 811 TODO add several <: for the different types. Not important at the present time. 812 813 TODO: Any types should not have the <: syntax 868 TODO add several <: for the different types. Not important at the present time. 869 TODO: Any types should not have the <: syntax 814 870 " 815 871 ::= … … 840 896 '<%path_str%>{<%ty_str%>}<%arraydim_str%>' 841 897 end dumpTypeSpec; 842 843 template dumpArrayDimOptTypeSpec(Option<Absyn.ArrayDim> arraydim, Context context)844 "Not in use"845 ::= match arraydim case SOME(ad) then dumpSubscriptsTypeSpec(ad, context)846 end dumpArrayDimOptTypeSpec;847 848 template dumpSubscriptsTypeSpec(list<Subscript> subscripts, Context context)849 "Not in use"850 ::=851 if subscripts then852 let sub_str = (subscripts |> s => 'Array' ;separator=", ")853 'Array{<%sub_str%>}'854 end dumpSubscriptsTypeSpec;855 898 856 899 template dumpArrayDimOpt(Option<Absyn.ArrayDim> arraydim, Context context) … … 964 1007 case LIST(__) then 965 1008 let list_str = (exps |> e => dumpExp(e, context) ;separator=", ") 966 ' list(<%list_str%>)'1009 '@list(<%list_str%>)' 967 1010 case DOT(__) then 968 1011 '<%dumpExp(exp, context)%>.<%dumpExp(index, context)%>' -
OMCompiler/Compiler/boot/LoadCompilerSources.mos
r98d8681d rb78044c 116 116 "../Script/Interactive.mo", 117 117 "../Script/NFApi.mo", 118 "../Script/MMToJuliaHT.mo", 118 119 "../Script/MMToJuliaUtil.mo", 119 120 "../Script/StaticScript.mo", -
OMCompiler/Compiler/susan_codegen/TplCodegen.tpl
r83be5f2b rb78044c 101 101 case ( <%mexps |> it => mmMatchingExp(it) ;separator=",\n"; anchor%> ) 102 102 <%if statements then << 103 equation103 algorithm 104 104 <%statements |> it => '<%mmExp(it, "=")%>;' ;separator="\n"%> 105 105 >>%>
Note: See TracChangeset
for help on using the changeset viewer.