@@ -40,6 +40,8 @@ import Type = NFType;
4040
4141protected
4242import Util ;
43+ import Absyn ;
44+ import List ;
4345
4446public
4547uniontype 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;
0 commit comments