Changeset b78044c in OpenModelica


Ignore:
Timestamp:
2020-05-04T16:54:57+02:00 (4 years ago)
Author:
johti <johti17@…>
Children:
ab920ce4
Parents:
79775bb0
git-author:
johti <johti17@…> (04/07/20 11:04:09)
git-committer:
johti <johti17@…> (05/04/20 16:54:57)
Message:

Added missing files

Location:
OMCompiler/Compiler
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/Compiler/Template/AbsynToJulia.tpl

    ra4f0ef3 rb78044c  
    11package AbsynToJulia
    22"
    3  Translates Absyn to Julia.
     3 Translates Absyn IR  to Julia.
    44 @Authors: John Tinnerholm & Martin Sjoelund
     5
     6 46 is used as the seed for enumerations.
    57"
    6 /* TODOS:
    7 
    8 TODO: Public/Private semantics : (
    9 TODO: Julia does string concatination with * instead of +
    10 
     8
     9/*
     10TODOS:
     11  TODO: Module renamning and Julia adaptation of uniontypes
     12     <In progress>
     13     TODO: Make sure renamning of imports work as they should
    1114*/
    1215
     
    1619template dumpProgram(Absyn.Program program)
    1720::=
     21/* Reset the tick before code generation! */
    1822match program
    1923  case PROGRAM(classes = {}) then ""
     
    2832template dumpSCodeElements(list<SCode.Element> elements)
    2933"
    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"
    3237::= dumpSCodeElements2(filterElements(elements, defaultOptions))
    3338end dumpSCodeElements;
     
    3540template dumpSCodeElements2(list<SCode.Element> elements)
    3641::=
    37   let str = elements |> el hasindex i1 fromindex 1 =>
     42  let str = List.unique(elements) |> el hasindex i1 fromindex 1 =>
    3843  (
    3944    match el
     
    4146        '@UniontypeDecl <%name%> <%\n%>'
    4247      case CLASS(classDef = parts as SCode.PARTS(__), partialPrefix = SCode.NOT_PARTIAL(), restriction=SCode.R_FUNCTION(__)) then
    43       dumpSCodeElements2(parts.elementLst)
     48        dumpSCodeElements2(parts.elementLst)
    4449      case CLASS(partialPrefix = SCode.PARTIAL(), restriction=SCode.R_FUNCTION(__)) then
    4550       '<%name%> = Function<%\n%>'
     
    5257
    5358template 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 :) */
    5560::= dumpClassElement(cls, options, noContext)
    5661end dumpClass;
     
    8287    let header = dumpClassHeader(parts, restriction)
    8388    let functionBodyStr = dumpClassDef(parts, makeFunctionContext(return_str), options)
     89    let cmt2 = if commentStr then '"""<%commentStr%>"""' else ""
    8490    /*
    8591      Input output variables are treated as parameters
     
    8793    */
    8894    <<
    89     <%commentStr%>
     95    <%cmt2%>
    9096    function <%name%>(<%if header then typevar_inputs else inputs_str%>) <%if header then "" else returnType%> <%header%>
    9197      <%functionBodyStr%>
     
    96102    let enc_str = if encapsulatedPrefix then "" /*Should we use a macro here?*/ else ""
    97103    let partial_str = if partialPrefix then "#=TODO: Originally partial =# " else ""
     104    let commentStr = dumpCommentStrOpt(parts.comment)
    98105    let class_type_str = dumpClassType(restriction)
    99106    let cdef_str1 = match restriction
     
    111118      case R_PACKAGE(__) then
    112119        <<
    113         <%\n%>
    114120        using MetaModelica
    115         #= ExportAll is not good practice but it makes it so that we do not have to write export after each function :( =#
    116121        using ExportAll
    117122        <%inform%>
     
    120125        <%cdef_str1%>
    121126        <%\n%>
    122         #= So that we can use wildcard imports and named imports when they do occur. Not good Julia practice =#
    123127        @exportAll()
    124128        >>
    125129      else
    126       <<
    127         <%cdef_str1%>
    128       >>
     130        <<
     131          <%cdef_str1%>
     132        >>
    129133   let begin_str = match restriction
    130134     case R_RECORD(__) then  'begin'
    131135     else ''
    132 
    133136    let cdef_str = cdef_str2
    134137    let cmt_str = dumpCommentStrOpt(parts.comment)
     
    153156    <<
    154157    <%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)()
    155171    >>
    156172  /*
     
    172188        @ExtendedFunction <%name_of_new_function%> <%spec%>(<%args%>)
    173189      >>
    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? */
    175194end dumpClassElement;
    176195
     
    264283    AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassDef: CLASS_EXETENDS not yet supported.")
    265284  case ENUMERATION(__) then
    266     AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassDef: CLASS_ENUMERATION not yet supported.")
     285    let enum_str = dumpEnumDef(enumLiterals)
     286    ' =  @enum(<%enum_str%>)'
    267287  else "TODO Unkown class definition"
    268288end dumpClassDef;
    269289
     290template dumpEnumDef(Absyn.EnumDef enum_def)
     291::=
     292match enum_def
     293  case ENUMLITERALS(__) then
     294    (enumLiterals |> lit => dumpEnumLiteral(lit) ;separator=";")
     295  case ENUM_COLON() then ":"
     296end dumpEnumDef;
     297
     298template dumpEnumLiteral(Absyn.EnumLiteral lit)
     299::=
     300match lit
     301  case ENUMLITERAL(__) then
     302    '<%literal%>'
     303end dumpEnumLiteral;
     304
    270305template dumpClassType(Absyn.Restriction restriction)
    271306::=
    272307match restriction
    273   case R_PACKAGE(__) then "module"
     308  case R_PACKAGE(__) then 'module'
    274309  case R_METARECORD(__) then "struct"
    275310  case R_RECORD(__) then '@Record' //Only handles Metamodelica records(!)
     
    277312  case R_TYPE(__) then '' // Should be const iff we are in a global scope (Julia 1.0 (Packages are not))
    278313  case R_FUNCTION(__) then "function"
     314  case R_CLASS(__) then "module"
     315  case R_MODEL(__) then "module #= Should be a model here =#"
    279316  /* TODO: The other ones are probably not relevant for now */
    280317  else AbsynDumpTpl.errorMsg("AbsynToJulia.dumpClassType: Unknown restriction for class." + AbsynDumpTpl.dumpRestriction(restriction))
     
    313350    match externalDecl
    314351      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.")
    316353end dumpClassPart;
    317354
     
    509546    let mod_str = if args_str then '(<%args_str%>)'
    510547    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%>'
    512549  case COMPONENTS(__) then
    513550    let attr_str = dumpElementAttr(attributes)
     
    523560                      (components |> comp =>
    524561                        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%>'
    526564                      ;separator="\n")
    527565                    else ''
     
    659697end dumpGroupImport;
    660698
    661 template dumpEquation(Absyn.Equation eq)
    662 ::= "No equations allowed. Translate them to algorithms"
    663 end dumpEquation;
    664 
    665699template dumpAlgorithmItems(list<Absyn.AlgorithmItem> algs, Context context)
    666700::= (algs |> alg => dumpAlgorithmItem(alg, context) ;separator="\n")
     
    686720    if AbsynUtil.complexIsCref(assignComponent) then
    687721      match assignComponent
    688       case CONS(__) then
    689         '@match <%lhs_str%> = <%rhs_str%>'
    690       else
    691         '<%lhs_str%> = <%rhs_str%>'
    692     else
    693       '@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%>'
    694728  case ALG_IF(__) then
    695729    let if_str = dumpAlgorithmBranch(ifExp, trueBranch, "if", context)
     
    769803 Needed since certain keywords will have a sligthly different meaning in Julia"
    770804::=
    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.")
     805match 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.")
    792850end dumpPathJL;
    793851
     
    808866"
    809867Dumps 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
    814870"
    815871::=
     
    840896       '<%path_str%>{<%ty_str%>}<%arraydim_str%>'
    841897end 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 then
    852     let sub_str = (subscripts |> s => 'Array' ;separator=", ")
    853     'Array{<%sub_str%>}'
    854 end dumpSubscriptsTypeSpec;
    855898
    856899template dumpArrayDimOpt(Option<Absyn.ArrayDim> arraydim, Context context)
     
    9641007  case LIST(__) then
    9651008    let list_str = (exps |> e => dumpExp(e, context) ;separator=", ")
    966     'list(<%list_str%>)'
     1009    '@list(<%list_str%>)'
    9671010  case DOT(__) then
    9681011    '<%dumpExp(exp, context)%>.<%dumpExp(index, context)%>'
  • OMCompiler/Compiler/boot/LoadCompilerSources.mos

    r98d8681d rb78044c  
    116116    "../Script/Interactive.mo",
    117117    "../Script/NFApi.mo",
     118    "../Script/MMToJuliaHT.mo",
    118119    "../Script/MMToJuliaUtil.mo",
    119120    "../Script/StaticScript.mo",
  • OMCompiler/Compiler/susan_codegen/TplCodegen.tpl

    r83be5f2b rb78044c  
    101101    case ( <%mexps |> it => mmMatchingExp(it) ;separator=",\n"; anchor%> )
    102102      <%if statements then <<
    103       equation
     103      algorithm
    104104        <%statements |> it => '<%mmExp(it, "=")%>;' ;separator="\n"%>
    105105      >>%>
Note: See TracChangeset for help on using the changeset viewer.