Skip to content

Commit 81e29fb

Browse files
author
Daniel Hedberg
committed
Added a few utility functions:
- crefPrefixOf - isNone - isSome git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5162 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 7442792 commit 81e29fb

File tree

2 files changed

+121
-14
lines changed

2 files changed

+121
-14
lines changed

Compiler/Absyn.mo

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,23 +2065,51 @@ algorithm outPaths := matchcontinue(path)
20652065
end matchcontinue;
20662066
end pathToStringList;
20672067

2068-
public function pathPrefixOf "returns true if prefix_path is a prefix of path"
2069-
input Path prefix_path;
2068+
public function pathPrefixOf
2069+
"
2070+
funcion: pathPrefixOf
2071+
Alternative names: isPrefixOf, pathIsPrefixOf, prefixOf
2072+
2073+
Returns true if prefixPath is a prefix of path, false otherwise.
2074+
"
2075+
input Path prefixPath;
20702076
input Path path;
2071-
output Boolean res;
2077+
output Boolean out;
20722078
algorithm
2073-
res := matchcontinue(prefix_path,path)
2074-
local Path p;
2075-
case(prefix_path,path)
2079+
out := matchcontinue(prefixPath, path)
2080+
case(prefixPath, path)
20762081
equation
2077-
true = ModUtil.pathEqual(prefix_path,path);
2082+
true = ModUtil.pathEqual(prefixPath, path);
20782083
then true;
2079-
case(prefix_path,path)
2080-
then pathPrefixOf(prefix_path,stripLast(path));
2081-
case(_,_) then false;
2084+
case(prefixPath, path)
2085+
then pathPrefixOf(prefixPath, stripLast(path));
2086+
case(_, _) then false;
20822087
end matchcontinue;
20832088
end pathPrefixOf;
20842089

2090+
public function crefPrefixOf
2091+
"
2092+
function: crefPrefixOf
2093+
Alternative names: crefIsPrefixOf, isPrefixOf, prefixOf
2094+
Author: DH 2010-03
2095+
2096+
Returns true if prefixCr is a prefix of cr, i.e., false otherwise.
2097+
Subscripts are NOT checked.
2098+
"
2099+
input ComponentRef prefixCr;
2100+
input ComponentRef cr;
2101+
output Boolean out;
2102+
algorithm
2103+
out := matchcontinue(prefixCr, cr)
2104+
case(prefixCr, cr)
2105+
equation
2106+
true = crefEqualNoSubs(prefixCr, cr);
2107+
then true;
2108+
case(prefixCr, cr)
2109+
then crefPrefixOf(prefixCr, crefStripLast(cr));
2110+
case(_, _) then false;
2111+
end matchcontinue;
2112+
end crefPrefixOf;
20852113

20862114
public function removePrefix "removes the prefix_path from path, and returns the rest of path"
20872115
input Path prefix_path;
@@ -2101,6 +2129,38 @@ algorithm
21012129
end matchcontinue;
21022130
end removePrefix;
21032131

2132+
public function crefRemovePrefix
2133+
"
2134+
function: crefRemovePrefix
2135+
Alternative names: removePrefix
2136+
Author: DH 2010-03
2137+
2138+
If prefixCr is a prefix of cr, removes prefixCr from cr and returns the remaining reference,
2139+
otherwise fails. Subscripts are NOT checked.
2140+
"
2141+
input ComponentRef prefixCr;
2142+
input ComponentRef cr;
2143+
output ComponentRef out;
2144+
algorithm
2145+
out := matchcontinue(prefixCr, cr)
2146+
local
2147+
Ident prefixIdent, ident;
2148+
ComponentRef prefixRestCr, restCr;
2149+
case(CREF_QUAL(name = prefixIdent, componentRef = prefixRestCr), CREF_QUAL(name = ident, componentRef = restCr))
2150+
equation
2151+
equality(prefixIdent = ident);
2152+
then crefRemovePrefix(prefixRestCr, restCr);
2153+
case(CREF_IDENT(name = prefixIdent), CREF_QUAL(name = ident, componentRef = restCr))
2154+
equation
2155+
equality(prefixIdent = ident);
2156+
then restCr;
2157+
case(CREF_IDENT(name = prefixIdent), CREF_IDENT(name = ident))
2158+
equation
2159+
equality(prefixIdent = ident);
2160+
then CREF_IDENT("", {});
2161+
end matchcontinue;
2162+
end crefRemovePrefix;
2163+
21042164
public function pathContains "
21052165
Author BZ,
21062166
checks if one Absyn.IDENT(..) is contained in path.

Compiler/Util.mo

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* This file is part of OpenModelica.
33
*
4-
* Copyright (c) 1998-CurrentYear, Linköping University,
4+
* Copyright (c) 1998-CurrentYear, Linköping University,
55
* Department of Computer and Information Science,
6-
* SE-58183 Linköping, Sweden.
6+
* SE-58183 Linköping, Sweden.
77
*
88
* All rights reserved.
99
*
@@ -14,7 +14,7 @@
1414
*
1515
* The OpenModelica software and the Open Source Modelica
1616
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17-
* from Linköping University, either from the above address,
17+
* from Linköping University, either from the above address,
1818
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
1919
* http://www.openmodelica.org, and in the OpenModelica distribution.
2020
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
@@ -255,7 +255,24 @@ algorithm isequal := matchcontinue(input1,input2,equalLength)
255255
end matchcontinue;
256256
end isListEqual;
257257

258-
public function isListNotEmpty
258+
public function isListEmpty
259+
"
260+
function: isListEmpty
261+
Author: DH 2010-03
262+
263+
Returns true if the given list is empty, false otherwise.
264+
"
265+
input list<Type_a> inList;
266+
output Boolean out;
267+
replaceable type Type_a subtypeof Any;
268+
algorithm
269+
out := matchcontinue(inList)
270+
case({}) then true;
271+
case(_) then false;
272+
end matchcontinue;
273+
end isListEmpty;
274+
275+
public function isListNotEmpty
259276
input list<Type_a> input1;
260277
output Boolean isempty;
261278
replaceable type Type_a subtypeof Any;
@@ -4986,6 +5003,36 @@ algorithm unOption := matchcontinue (inOption)
49865003
end matchcontinue;
49875004
end genericOption;
49885005

5006+
public function isNone
5007+
"
5008+
function: isNone
5009+
Author: DH, 2010-03
5010+
"
5011+
input Option<Type_a> inOption;
5012+
output Boolean out;
5013+
replaceable type Type_a subtypeof Any;
5014+
algorithm out := matchcontinue (inOption)
5015+
local Type_a item;
5016+
case (NONE) then true;
5017+
case (_) then false;
5018+
end matchcontinue;
5019+
end isNone;
5020+
5021+
public function isSome
5022+
"
5023+
function: isSome
5024+
Author: DH, 2010-03
5025+
"
5026+
input Option<Type_a> inOption;
5027+
output Boolean out;
5028+
replaceable type Type_a subtypeof Any;
5029+
algorithm out := matchcontinue (inOption)
5030+
local Type_a item;
5031+
case (NONE) then false;
5032+
case (_) then true;
5033+
end matchcontinue;
5034+
end isSome;
5035+
49895036
public function makeOptIfNonEmptyList "function: stringOption
49905037
author: BZ
49915038
Construct a Option<Type_a> if the list contains one and only one element. If more, error. On empty=>NONE"

0 commit comments

Comments
 (0)