OMCompiler/Compiler/BackEnd/BackendUtil.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated package BackendUtil | ||
| 37 | " file: BackendUtil.mo | ||
| 38 | package: BackendUtil | ||
| 39 | description: Miscellanous MetaModelica Compiler (MMC) utilities used by the backend." | ||
| 40 | |||
| 41 | protected | ||
| 42 | |||
| 43 | import List; | ||
| 44 | import System; | ||
| 45 | import DAE; | ||
| 46 | |||
| 47 | uniontype ReplacePattern | ||
| 48 | record REPLACEPATTERN | ||
| 49 | String from "from string (ie \".\"" ; | ||
| 50 | String to "to string (ie \"$p\") ))" ; | ||
| 51 | end REPLACEPATTERN; | ||
| 52 | end ReplacePattern; | ||
| 53 | |||
| 54 | constant list<ReplacePattern> replaceStringPatterns = | ||
| 55 | {REPLACEPATTERN(".",pointStr), | ||
| 56 | REPLACEPATTERN("[",leftBraketStr),REPLACEPATTERN("]",rightBraketStr), | ||
| 57 | REPLACEPATTERN("(",leftParStr),REPLACEPATTERN(")",rightParStr), | ||
| 58 | REPLACEPATTERN(",",commaStr), | ||
| 59 | REPLACEPATTERN("'",appostrophStr)}; | ||
| 60 | |||
| 61 | constant String pointStr = "$P"; | ||
| 62 | constant String leftBraketStr = "$lB"; | ||
| 63 | constant String rightBraketStr = "$rB"; | ||
| 64 | constant String leftParStr = "$lP"; | ||
| 65 | constant String rightParStr = "$rP"; | ||
| 66 | constant String commaStr = "$c"; | ||
| 67 | constant String appostrophStr = "$a"; | ||
| 68 | |||
| 69 | public | ||
| 70 | |||
| 71 | function modelicaStringToCStr " this replaces symbols that are illegal in C to legal symbols | ||
| 72 | see replaceStringPatterns to see the format. (example: \".\" becomes \"$P\") | ||
| 73 | author: x02lucpo | ||
| 74 | |||
| 75 | NOTE: This function should not be used in OMC, since the OMC backend no longer | ||
| 76 | uses stringified components. It is still used by MathCore though." | ||
| 77 | input String str; | ||
| 78 | input Boolean changeDerCall "if true, first change 'DER(v)' to $derivativev"; | ||
| 79 | output String res_str; | ||
| 80 | algorithm | ||
| 81 | res_str := match(str,changeDerCall) | ||
| 82 | local String s; | ||
| 83 | case(_,false) | ||
| 84 | algorithm | ||
| 85 | 4297 | res_str := "$"+ modelicaStringToCStr1(str, replaceStringPatterns); | |
| 86 | // debug_print("prefix$", res_str); | ||
| 87 | then res_str; | ||
| 88 | case(s,true) algorithm | ||
| 89 | ✗ | s := modelicaStringToCStr2(s); | |
| 90 | then s; | ||
| 91 | end match; | ||
| 92 | end modelicaStringToCStr; | ||
| 93 | |||
| 94 | protected | ||
| 95 | |||
| 96 | function modelicaStringToCStr1 "" | ||
| 97 | input String inString; | ||
| 98 | input list<ReplacePattern> inReplacePatternLst; | ||
| 99 | output String outString; | ||
| 100 | algorithm | ||
| 101 | outString:= | ||
| 102 | matchcontinue (inString,inReplacePatternLst) | ||
| 103 | local | ||
| 104 | String str,str_1,res_str,from,to; | ||
| 105 | list<ReplacePattern> res; | ||
| 106 | case (str,{}) then str; | ||
| 107 | case (str,(REPLACEPATTERN(from = from,to = to) :: res)) | ||
| 108 | algorithm | ||
| 109 | 30079 | str_1 := modelicaStringToCStr1(str, res); | |
| 110 | 30079 | res_str := System.stringReplace(str_1, from, to); | |
| 111 | then | ||
| 112 | res_str; | ||
| 113 | else | ||
| 114 | algorithm | ||
| 115 | ✗ | print(getInstanceName() + " failed for str:"+inString+"\n"); | |
| 116 | ✗ | then | |
| 117 | fail(); | ||
| 118 | end matchcontinue; | ||
| 119 | end modelicaStringToCStr1; | ||
| 120 | |||
| 121 | function modelicaStringToCStr2 "help function to modelicaStringToCStr, | ||
| 122 | first changes name 'der(v)' to $derivativev and 'pre(v)' to 'pre(v)' with applied rules for v" | ||
| 123 | input String inDerName; | ||
| 124 | output String outDerName; | ||
| 125 | algorithm | ||
| 126 | outDerName := matchcontinue inDerName | ||
| 127 | local | ||
| 128 | String name, derName; | ||
| 129 | list<String> names; | ||
| 130 | |||
| 131 | case derName algorithm | ||
| 132 | ✗ | 0 := System.strncmp(derName,"der(",4); | |
| 133 | // adrpo: 2009-09-08 | ||
| 134 | // the commented text: _::name::_ = listLast(System.strtok(derName,"()")); | ||
| 135 | // is wrong as der(der(x)) ends up beeing translated to $der$der instead | ||
| 136 | // of $der$der$x. Changed to the following 2 lines below! | ||
| 137 | ✗ | _::names := (System.strtok(derName,"()")); | |
| 138 | ✗ | names := List.map1(names, modelicaStringToCStr, false); | |
| 139 | ✗ | name := DAE.derivativeNamePrefix + stringAppendList(names); | |
| 140 | then name; | ||
| 141 | case derName algorithm | ||
| 142 | ✗ | 0 := System.strncmp(derName,"pre(",4); | |
| 143 | ✗ | _::name::_:= System.strtok(derName,"()"); | |
| 144 | ✗ | name := "pre(" + modelicaStringToCStr(name,false) + ")"; | |
| 145 | then name; | ||
| 146 | ✗ | case derName then modelicaStringToCStr(derName,false); | |
| 147 | end matchcontinue; | ||
| 148 | end modelicaStringToCStr2; | ||
| 149 | |||
| 150 | |||
| 151 | annotation(__OpenModelica_Interface="backend"); | ||
| 152 | end BackendUtil; | ||
| 153 |