Allow generic if statements

Previously, only comparisons between strings/variable references
and other strings/variable references or certain function calls
were allowed. After this cl, any two starlark expressions can
be compared. If they have different types, they will be converted
to strings first.

Fixes: 205995738
Bug: 201700692
Test: go test
Change-Id: Ib463de7b24d3abcd032dbdcba7c3f1351c314d01
This commit is contained in:
Cole Faust
2021-11-10 15:05:07 -08:00
parent 92a89ed1c5
commit f83202143a
3 changed files with 204 additions and 124 deletions

View File

@@ -197,6 +197,51 @@ func (v *variableRefExpr) emitListVarCopy(gctx *generationContext) {
}
}
type toStringExpr struct {
expr starlarkExpr
}
func (s *toStringExpr) eval(valueMap map[string]starlarkExpr) (res starlarkExpr, same bool) {
if x, same := s.expr.eval(valueMap); same {
res = s
} else {
res = &toStringExpr{expr: x}
}
return
}
func (s *toStringExpr) emit(ctx *generationContext) {
switch s.expr.typ() {
case starlarkTypeString, starlarkTypeUnknown:
// Assume unknown types are strings already.
s.expr.emit(ctx)
case starlarkTypeList:
ctx.write(`" ".join(`)
s.expr.emit(ctx)
ctx.write(")")
case starlarkTypeInt:
ctx.write(`("%d" % (`)
s.expr.emit(ctx)
ctx.write("))")
case starlarkTypeBool:
ctx.write("((")
s.expr.emit(ctx)
ctx.write(`) ? "true" : "")`)
case starlarkTypeVoid:
ctx.write(`""`)
default:
panic("Unknown starlark type!")
}
}
func (s *toStringExpr) typ() starlarkType {
return starlarkTypeString
}
func (s *toStringExpr) emitListVarCopy(gctx *generationContext) {
s.emit(gctx)
}
type notExpr struct {
expr starlarkExpr
}
@@ -255,6 +300,12 @@ func (eq *eqExpr) emit(gctx *generationContext) {
return
}
if eq.left.typ() != eq.right.typ() {
eq.left = &toStringExpr{expr: eq.left}
eq.right = &toStringExpr{expr: eq.right}
}
// General case
eq.left.emit(gctx)
if eq.isEq {