Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 376bb8f

Browse files
adrpoOpenModelica-Hudson
authored andcommitted
updates to NFFrontEnd
- implement toString for expressions - implement toString for types - add CAST expression - handle Type.FUNCTION better
1 parent 507cdb3 commit 376bb8f

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import Type = NFType;
4040

4141
protected
4242
import Util;
43+
import Absyn;
44+
import List;
4345

4446
public
4547
uniontype CallAttributes
@@ -147,6 +149,11 @@ uniontype Expression
147149
Expression falseBranch;
148150
end IF;
149151

152+
record CAST
153+
Type ty;
154+
Expression exp;
155+
end CAST;
156+
150157
record UNBOX "MetaModelica value unboxing (similar to a cast)"
151158
Expression exp;
152159
Type ty;
@@ -343,6 +350,15 @@ uniontype Expression
343350
then
344351
compare(exp1.exp, e1);
345352

353+
case CAST()
354+
algorithm
355+
e1 := match exp2
356+
case CAST(exp = e1) then e1;
357+
case e1 then e1;
358+
end match;
359+
then
360+
compare(exp1.exp, e1);
361+
346362
else
347363
algorithm
348364
assert(false, getInstanceName() + " got unknown expression.");
@@ -407,6 +423,10 @@ uniontype Expression
407423
case REAL() then Type.REAL();
408424
case STRING() then Type.STRING();
409425
case BOOLEAN() then Type.BOOLEAN();
426+
case CAST(ty, _)
427+
algorithm
428+
ty := if listMember(ty, {Type.INTEGER(), Type.REAL(), Type.STRING(), Type.BOOLEAN()}) then ty else Type.UNKNOWN();
429+
then ty;
410430
else Type.UNKNOWN();
411431
end match;
412432
end typeOf;
@@ -432,6 +452,10 @@ uniontype Expression
432452
then
433453
ARRAY(t, el);
434454

455+
case (_, Type.REAL())
456+
then
457+
CAST(Type.REAL(), exp);
458+
435459
end match;
436460
end typeCastElements;
437461

@@ -501,12 +525,46 @@ uniontype Expression
501525
function toString
502526
input Expression exp;
503527
output String str;
528+
protected
529+
Expression e;
530+
Type t;
504531
algorithm
505532
str := match exp
506533
case INTEGER() then String(exp.value);
507534
case REAL() then String(exp.value);
508535
case STRING() then exp.value;
509536
case BOOLEAN() then String(exp.value);
537+
538+
case ENUM() then Absyn.pathString(exp.name);
539+
case CREF() then Prefix.toString(exp.prefix);
540+
case ARRAY() then "{" + stringDelimitList(List.map(exp.elements, toString), ", ") + "}";
541+
542+
case RANGE() then toString(exp.start) +
543+
(
544+
if isSome(exp.step)
545+
then ":" + toString(Util.getOption(exp.step))
546+
else ""
547+
) + ":" + toString(exp.stop);
548+
case CALL() then Absyn.pathString(exp.path) + "(" + stringDelimitList(List.map(exp.arguments, toString), ", ") + ")";
549+
case SIZE() then "size(" + toString(exp.exp) +
550+
(
551+
if isSome(exp.dimIndex)
552+
then ", " + toString(Util.getOption(exp.dimIndex))
553+
else ""
554+
) + ")";
555+
case BINARY() then "(" + toString(exp.exp1) + Operator.symbol(exp.operator) + toString(exp.exp2) + ")";
556+
case UNARY() then "(" + Operator.symbol(exp.operator) + " " + toString(exp.exp) + ")";
557+
case LBINARY() then "(" + toString(exp.exp1) + Operator.symbol(exp.operator) + toString(exp.exp2) + ")";
558+
case LUNARY() then "(" + Operator.symbol(exp.operator) + " " + toString(exp.exp) + ")";
559+
560+
case RELATION() then "(" + toString(exp.exp1) + Operator.symbol(exp.operator) + toString(exp.exp2) + ")";
561+
case IF() then "if" + toString(exp.condition) + " then " + toString(exp.trueBranch) + " else " + toString(exp.falseBranch);
562+
563+
case UNBOX() then "UNBOX(" + toString(exp.exp) + ")";
564+
565+
case CAST() then "CAST(" + Type.toString(exp.ty) + ", " + toString(exp.exp) + ")";
566+
567+
510568
else "NFExpression.toString: IMPLEMENT ME";
511569
end match;
512570
end toString;
@@ -520,6 +578,7 @@ uniontype Expression
520578
case REAL() then DAE.RCONST(exp.value);
521579
case STRING() then DAE.SCONST(exp.value);
522580
case BOOLEAN() then DAE.BCONST(exp.value);
581+
case ENUM() then DAE.ENUM_LITERAL(exp.name, exp.index);
523582

524583
case CREF()
525584
then DAE.CREF(Prefix.toCref(exp.prefix), DAE.T_UNKNOWN_DEFAULT);
@@ -528,12 +587,47 @@ uniontype Expression
528587
then DAE.ARRAY(Type.toDAE(exp.ty), Type.isScalarArray(exp.ty),
529588
list(toDAE(e) for e in exp.elements));
530589

590+
case RANGE()
591+
then DAE.RANGE(
592+
Type.toDAE(exp.ty),
593+
toDAE(exp.start),
594+
if isSome(exp.step)
595+
then SOME(toDAE(Util.getOption(exp.step)))
596+
else NONE(),
597+
toDAE(exp.stop));
598+
599+
case CALL()
600+
then DAE.CALL(exp.path, List.map(exp.arguments, toDAE), toDAECallAtributes(exp.attr));
601+
602+
case SIZE()
603+
then DAE.SIZE(toDAE(exp.exp),
604+
if isSome(exp.dimIndex)
605+
then SOME(toDAE(Util.getOption(exp.dimIndex)))
606+
else NONE());
607+
531608
case BINARY()
532609
then DAE.BINARY(toDAE(exp.exp1), Operator.toDAE(exp.operator), toDAE(exp.exp2));
533610

534611
case UNARY()
535612
then DAE.UNARY(Operator.toDAE(exp.operator), toDAE(exp.exp));
536613

614+
case LBINARY()
615+
then DAE.LBINARY(toDAE(exp.exp1), Operator.toDAE(exp.operator), toDAE(exp.exp2));
616+
617+
case LUNARY()
618+
then DAE.LUNARY(Operator.toDAE(exp.operator), toDAE(exp.exp));
619+
620+
case RELATION()
621+
then DAE.RELATION(toDAE(exp.exp1), Operator.toDAE(exp.operator), toDAE(exp.exp2), 0, NONE());
622+
623+
case IF()
624+
then DAE.IFEXP(toDAE(exp.condition), toDAE(exp.trueBranch), toDAE(exp.falseBranch));
625+
626+
case CAST() then DAE.CAST(Type.toDAE(exp.ty), toDAE(exp.exp));
627+
628+
case UNBOX()
629+
then DAE.UNBOX(toDAE(exp.exp), Type.toDAE(exp.ty));
630+
537631
else
538632
algorithm
539633
assert(false, getInstanceName() + " got unknown expression");
@@ -543,6 +637,22 @@ uniontype Expression
543637
end match;
544638
end toDAE;
545639

640+
function toDAECallAtributes
641+
input CallAttributes attr;
642+
output DAE.CallAttributes fattr;
643+
protected
644+
Type ty "The type of the return value, if several return values this is undefined";
645+
Boolean tuple_ "tuple";
646+
Boolean builtin "builtin Function call";
647+
Boolean isImpure "if the function has prefix *impure* is true, else false";
648+
Boolean isFunctionPointerCall;
649+
DAE.InlineType inlineType;
650+
DAE.TailCall tailCall "Input variables of the function if the call is tail-recursive";
651+
algorithm
652+
CALL_ATTR(ty, tuple_, builtin, isImpure, isFunctionPointerCall, inlineType, tailCall) := attr;
653+
fattr := DAE.CALL_ATTR(Type.toDAE(ty), tuple_, builtin, isImpure, isFunctionPointerCall, inlineType, tailCall);
654+
end toDAECallAtributes;
655+
546656
function makeBuiltinCall
547657
"Create a CALL with the given data for a call to a builtin function."
548658
input String name;

Compiler/NFFrontEnd/NFOperator.mo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,11 @@ public
375375
symbol := spacing + symbol + spacing;
376376
end symbol;
377377

378+
function toString
379+
input Operator op;
380+
algorithm
381+
symbol(op);
382+
end toString;
383+
378384
annotation(__OpenModelica_Interface="frontend");
379385
end NFOperator;

Compiler/NFFrontEnd/NFType.mo

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
encapsulated uniontype NFType
3333
protected
3434
import Type = NFType;
35+
import List;
3536

3637
public
3738
import Dimension = NFDimension;
@@ -192,6 +193,7 @@ public
192193
isNumeric := match ty
193194
case REAL() then true;
194195
case INTEGER() then true;
196+
case FUNCTION() then isBasicNumeric(ty.resultType);
195197
else false;
196198
end match;
197199
end isBasicNumeric;
@@ -308,7 +310,25 @@ public
308310
input Type ty;
309311
output String str;
310312
algorithm
311-
str := "IMPLEMENT ME";
313+
str := match ty
314+
case Type.INTEGER() then "Integer";
315+
case Type.REAL() then "Real";
316+
case Type.STRING() then "String";
317+
case Type.BOOLEAN() then "Boolean";
318+
case Type.ENUMERATION() then "enumeration()";
319+
case Type.CLOCK() then "Clock";
320+
case Type.ARRAY() then toString(ty.elementType) + "[" + stringDelimitList(List.map(ty.dimensions, Dimension.toString), ", ") + "]";
321+
case Type.TUPLE() then "tuple(" + stringDelimitList(List.map(ty.types, toString), ", ") + ")";
322+
case Type.FUNCTION() then "function( output " + toString(ty.resultType) + " )";
323+
case Type.NORETCALL() then "noretcall()";
324+
case Type.UNKNOWN() then "unknown()";
325+
case Type.COMPLEX() then "complex()";
326+
else
327+
algorithm
328+
assert(false, getInstanceName() + " got unknown type: " + anyString(ty));
329+
then
330+
fail();
331+
end match;
312332
end toString;
313333

314334
function toDAE
@@ -321,9 +341,14 @@ public
321341
case Type.STRING() then DAE.T_STRING_DEFAULT;
322342
case Type.BOOLEAN() then DAE.T_BOOL_DEFAULT;
323343
case Type.CLOCK() then DAE.T_CLOCK_DEFAULT;
344+
case Type.ENUMERATION() then DAE.T_ENUMERATION_DEFAULT;
324345
case Type.ARRAY()
325346
then DAE.T_ARRAY(toDAE(ty.elementType),
326347
list(Dimension.toDAE(d) for d in ty.dimensions));
348+
case Type.TUPLE()
349+
then DAE.T_TUPLE(list(toDAE(t) for t in ty.types), ty.names);
350+
case Type.FUNCTION()
351+
then DAE.T_FUNCTION({}, toDAE(ty.resultType), ty.attributes, DAE.emptyTypeSource);
327352
case Type.NORETCALL() then DAE.T_NORETCALL_DEFAULT;
328353
case Type.UNKNOWN() then DAE.T_UNKNOWN_DEFAULT;
329354
case Type.COMPLEX() then DAE.T_COMPLEX_DEFAULT;

Compiler/NFFrontEnd/NFTypeCheck.mo

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ function matchTypes
14201420
algorithm
14211421
// Return true if the references are the same.
14221422
if referenceEq(actualType, expectedType) then
1423-
compatibleType := actualType;
1423+
compatibleType := actualType;
14241424
return;
14251425
end if;
14261426

@@ -1429,7 +1429,15 @@ algorithm
14291429
// If the types are not of the same kind we might need to type cast the
14301430
// expression to make it compatible.
14311431
(expression, compatibleType, compatible) :=
1432-
matchTypes_cast(actualType, expectedType, expression, allowUnknown);
1432+
match (actualType, expectedType)
1433+
case (Type.FUNCTION(), Type.FUNCTION())
1434+
then matchTypes(actualType.resultType, expectedType.resultType, expression, allowUnknown);
1435+
case (Type.FUNCTION(), _)
1436+
then matchTypes(actualType.resultType, expectedType, expression, allowUnknown);
1437+
case (_, Type.FUNCTION())
1438+
then matchTypes(actualType, expectedType.resultType, expression, allowUnknown);
1439+
else matchTypes_cast(actualType, expectedType, expression, allowUnknown);
1440+
end match;
14331441
return;
14341442
end if;
14351443

@@ -1476,6 +1484,13 @@ algorithm
14761484
then
14771485
compatibleType;
14781486

1487+
case Type.FUNCTION()
1488+
algorithm
1489+
(expression, compatibleType, compatible) :=
1490+
matchTypes_cast(actualType.resultType, expectedType, expression, allowUnknown);
1491+
then
1492+
compatibleType;
1493+
14791494
else
14801495
algorithm
14811496
assert(false, getInstanceName() + " got unknown type.");

0 commit comments

Comments
 (0)