2
0
Эх сурвалжийг харах

ternary operator returns STRING if one of its operands is string literal

Vladislav Folts 9 жил өмнө
parent
commit
b1416b1ed3

BIN
bin/compiled.zip


+ 10 - 4
src/eberon/EberonContextExpression.ob

@@ -101,7 +101,7 @@ BEGIN
     RETURN result;
 END;
 
-PROCEDURE findCommonBase(t1, t2: Record.PType): Record.PType;
+PROCEDURE findCommonBaseRecord(t1, t2: Record.PType): Record.PType;
 VAR
     result: Record.PType;
 BEGIN
@@ -123,6 +123,12 @@ BEGIN
     RETURN commonBase1;
 END;
 
+PROCEDURE findCommonBase(t1, t2: Types.PType): Types.PType;
+    RETURN (t1 IS Types.PString) OR (t2 IS Types.PString) 
+           ? EberonString.string 
+           : t1;
+END;
+
 PROCEDURE parentTerm(VAR maybeFactor: ContextHierarchy.Node): PTermList;
     RETURN maybeFactor IS ContextExpression.Factor 
         ? maybeFactor.factor^(ETFactor).termList
@@ -189,9 +195,9 @@ BEGIN
         firstType <- SELF.first.type();
         secondType <- SELF.second.type();
         IF (firstType IS Record.PType) & (secondType IS Record.PType) THEN
-            resultType := findCommonBase(firstType, secondType);
+            resultType := findCommonBaseRecord(firstType, secondType);
         ELSIF (firstType IS Record.PPointer) & (secondType IS Record.PPointer) THEN
-            resultType := EberonContextDesignator.makePointer(findCommonBase(Record.pointerBase(firstType^), Record.pointerBase(secondType^)));
+            resultType := EberonContextDesignator.makePointer(findCommonBaseRecord(Record.pointerBase(firstType^), Record.pointerBase(secondType^)));
         ELSIF (firstType = Types.nil) & (secondType IS Record.PPointer) THEN
             resultType := secondType;
         ELSIF (secondType = Types.nil) & (firstType IS Record.PPointer) THEN
@@ -203,7 +209,7 @@ BEGIN
                 Errors.raise("incompatible types in ternary operator: '" 
                              + firstType.description() + "' and '" + secondType.description() + "'");
             END;
-            resultType := firstType;
+            resultType := findCommonBase(firstType, secondType);
         END;
 
         checkResultType <- resultType;

+ 3 - 0
test/test_unit_eberon.js

@@ -1533,6 +1533,9 @@ exports.suite = {
              "FLT(b ? i1 : i2)",
              "b ? i1 : byte",
              "b ? byte : i1",
+             "b ? \"abc\" : \"de\"",
+             "b ? s : \"de\"",
+             "b ? \"abc\" : s",
              "b ? pb : pd",
              "b ? pd : pb",
              "b ? pb : NIL",