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

Commit 67fd997

Browse files
petfrOpenModelica-Hudson
authored andcommitted
NF typing and typechecking of unary operators
- Changes to NFTyping.mo and NFTypeCheck.mo for typing and typechecking unary operators +, -, not. - Changes to Absyn.mo - better comment text for unary operators
1 parent f25b1b5 commit 67fd997

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

Compiler/FrontEnd/Absyn.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,9 @@ uniontype Exp "The Exp uniontype is the container of a Modelica expression.
720720
Exp exp2;
721721
end BINARY;
722722

723-
record UNARY "Unary operations, e.g. -(x)"
723+
record UNARY "Unary operations, e.g. -(x), +(x)"
724724
Operator op "op" ;
725-
Exp exp "exp Logical binary operations: and, or" ;
725+
Exp exp "exp - any arithmetic expression" ;
726726
end UNARY;
727727

728728
record LBINARY
@@ -733,7 +733,7 @@ uniontype Exp "The Exp uniontype is the container of a Modelica expression.
733733

734734
record LUNARY "Logical unary operations: not"
735735
Operator op "op" ;
736-
Exp exp "exp Relations, e.g. a >= 0" ;
736+
Exp exp "exp - any logical or relation expression" ;
737737
end LUNARY;
738738

739739
record RELATION

Compiler/NFFrontEnd/NFTypeCheck.mo

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,42 @@ algorithm
480480
end try;
481481
end checkLogicalBinaryOperation;
482482

483+
public function checkLogicalUnaryOperation
484+
"petfr:
485+
Typechecks logical unary operations, i.e. the not operator"
486+
input DAE.Exp exp1;
487+
input DAE.Type type1;
488+
input DAE.Operator operator;
489+
output DAE.Exp exp;
490+
output DAE.Type ty;
491+
protected
492+
DAE.Exp e1;
493+
DAE.Operator op;
494+
DAE.TypeSource ty_src;
495+
String e1_str, ty1_str, msg_str, op_str, s1;
496+
algorithm
497+
try
498+
true := Types.isBoolean(type1);
499+
// Logical unary operations here are allowed only on Booleans.
500+
ty := type1;
501+
op := Expression.setOpType(operator, ty);
502+
exp := DAE.LUNARY(op, exp1);
503+
504+
else
505+
e1_str := ExpressionDump.printExpStr(exp1);
506+
ty1_str := Types.unparseTypeNoAttr(type1);
507+
op_str := DAEDump.dumpOperatorString(operator);
508+
509+
// Just for proper error messages.
510+
msg_str := if not (Types.isBoolean(type1)) then
511+
"\n: Logical operations involving non-Boolean types are not valid in Modelica." else ty1_str;
512+
513+
s1 := "' " + e1_str + op_str + " '";
514+
515+
Error.addSourceMessage(Error.UNRESOLVABLE_TYPE, {s1, msg_str}, Absyn.dummyInfo);
516+
end try;
517+
end checkLogicalUnaryOperation;
518+
483519
public function checkRelationOperation
484520
"mahge:
485521
Type checks relational operations. Relations on scalars are handled
@@ -531,7 +567,7 @@ public function checkBinaryOperation
531567
"mahge:
532568
Type checks binary operations. operations on scalars are handled
533569
simply by using Types.matchType(). This way conversions from Integer to Real
534-
are handled internaly.
570+
are handled internally.
535571
Operations involving arrays and Complex types are handled differently."
536572
input DAE.Exp exp1;
537573
input DAE.Type type1;
@@ -610,6 +646,33 @@ algorithm
610646
end checkBinaryOperation;
611647

612648

649+
public function checkUnaryOperation
650+
"petfr:
651+
Type checks arithmetic unary operations. Both for simple scalar types and
652+
operations involving array types. Builds DAE unary node."
653+
input DAE.Exp exp1;
654+
input DAE.Type type1;
655+
input DAE.Operator operator;
656+
output DAE.Exp unaryExp;
657+
output DAE.Type unaryType;
658+
protected
659+
DAE.Operator op;
660+
DAE.TypeSource ty_src;
661+
String e1_str, ty1_str, s1;
662+
algorithm
663+
try
664+
unaryType := type1; // ?? correct?, since no type change for unary, see matchType?
665+
op := Expression.setOpType(operator, unaryType);
666+
unaryExp := DAE.UNARY(op, exp1);
667+
else
668+
e1_str := ExpressionDump.printExpStr(exp1); // ??Remove possible Error message since Unary?
669+
ty1_str := Types.unparseTypeNoAttr(type1);
670+
s1 := "' " + e1_str + DAEDump.dumpOperatorSymbol(operator) + " '";
671+
Error.addSourceMessage(Error.UNRESOLVABLE_TYPE, {s1, ty1_str}, Absyn.dummyInfo);
672+
fail();
673+
end try;
674+
end checkUnaryOperation;
675+
613676
public function checkBinaryOperationArrays
614677
"mahge:
615678
Type checks binary operations involving arrays. This involves more checks than

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,14 @@ algorithm
701701
then
702702
(typedExp, ty, Types.constAnd(var1, var2));
703703

704+
case Absyn.Exp.UNARY() // Unary +, -
705+
algorithm
706+
(e1, ty1, var1) := typeExp(untypedExp.exp, scope, component, info);
707+
op := translateOperator(untypedExp.op);
708+
(typedExp, ty) := TypeCheck.checkUnaryOperation(e1, ty1, op);
709+
then
710+
(typedExp, ty, var1);
711+
704712
case Absyn.Exp.LBINARY()
705713
algorithm
706714
(e1, ty1, var1) := typeExp(untypedExp.exp1, scope, component, info);
@@ -711,6 +719,14 @@ algorithm
711719
then
712720
(typedExp, ty, Types.constAnd(var1, var2));
713721

722+
case Absyn.Exp.LUNARY() // Unary not
723+
algorithm
724+
(e1, ty1, var1) := typeExp(untypedExp.exp, scope, component, info);
725+
op := translateOperator(untypedExp.op);
726+
(typedExp, ty) := TypeCheck.checkLogicalUnaryOperation(e1, ty1, op);
727+
then
728+
(typedExp, ty, var1);
729+
714730
case Absyn.Exp.RELATION()
715731
algorithm
716732
(e1, ty1, var1) := typeExp(untypedExp.exp1, scope, component, info);

Compiler/boot/Makefile.common

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ make-bootstrap-tarball: clean
103103
xz --best --force bootstrap-sources.tar
104104

105105
templates:
106-
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory -C $(TOP_DIR)/Compiler/Template
106+
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory -C $(TOP_DIR)/Compiler/Template OMBUILDDIR=$(OMBUILDDIR)
107107

108108
scripting:
109-
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory -C $(TOP_DIR)/Compiler/Script
109+
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory -C $(TOP_DIR)/Compiler/Script OMBUILDDIR=$(OMBUILDDIR)
110110

111111
clean:
112112
rm -rf $(GEN_DIR)

0 commit comments

Comments
 (0)