scripts: csv.py: Enforced matching types in ternary branches

So in:

  $ ./scripts/csv.py input.csv -fa='b?c:d'

c and d must have matching types or else an error is raised.

This requires an explicit definition for the ternary operator since it's
a special case in that the type of b does not matter.

Compare to a 3-arg max call:

  $ ./scripts/csv.py input.csv -fa='int(b)?float(c):float(d)'      # ok
  $ ./scripts/csv.py input.csv -fa='max(int(b),float(c),float(d))' # error
This commit is contained in:
Christopher Haster
2024-11-14 00:09:20 -06:00
parent 9e7e79390a
commit 47f28946f6
+5 -1
View File
@@ -989,7 +989,11 @@ class RExpr:
class IfElse(Expr):
"""b if a is non-zero, otherwise c"""
def type(self, types={}):
return self.b.type(types)
t = self.b.type(types)
u = self.c.type(types)
if t != u:
raise RExpr.Error("mismatched types? %r" % self)
return t
def fold(self, types={}):
return self.b.fold(types)