Ejemplo n.º 1
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if len(expr.arguments) != 1:
         raise Error.WrongArity(expr, 1)
     if not isinstance(expr.arguments[0].type, T.Array):
         raise Error.StaticTypeMismatch(expr, T.Array(None),
                                        expr.arguments[0].type)
     return T.Int()
Ejemplo n.º 2
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if len(expr.arguments) != 2:
         raise Error.WrongArity(expr, 2)
     expr.arguments[0].typecheck(T.String())
     expr.arguments[1].typecheck(T.Array(T.String()))
     return T.Array(
         T.String(),
         nonempty=(
             isinstance(expr.arguments[1].type, T.Array) and expr.arguments[1].type.nonempty
         ),
     )
Ejemplo n.º 3
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if len(expr.arguments) != 1:
         raise Error.WrongArity(expr, 1)
     if not isinstance(expr.arguments[0].type, T.Array):
         raise Error.StaticTypeMismatch(expr.arguments[0], T.Array(None),
                                        expr.arguments[0].type)
     if expr.arguments[0].type.item_type is None:
         # TODO: error for 'indeterminate type'
         raise Error.EmptyArray(expr.arguments[0])
     ty = expr.arguments[0].type.item_type
     assert isinstance(ty, T.Base)
     return T.Array(ty.copy(optional=False))
Ejemplo n.º 4
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if len(expr.arguments) != 1:
         raise Error.WrongArity(expr, 1)
     expr.arguments[0].typecheck(T.Int())
     nonempty = False
     arg0 = expr.arguments[0]
     if isinstance(arg0, E.Int) and arg0.value > 0:
         nonempty = True
     if isinstance(arg0, E.Apply) and arg0.function_name == "length":
         arg00ty = arg0.arguments[0].type
         if isinstance(arg00ty, T.Array) and arg00ty.nonempty:
             nonempty = True
     return T.Array(T.Int(), nonempty=nonempty)
Ejemplo n.º 5
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if len(expr.arguments) != 1:
         raise Error.WrongArity(expr, 1)
     expr.arguments[0].typecheck(T.Array(T.Any()))
     # TODO: won't handle implicit coercion from T to Array[T]
     assert isinstance(expr.arguments[0].type, T.Array)
     if expr.arguments[0].type.item_type is None:
         return T.Array(T.Any())
     if not isinstance(expr.arguments[0].type.item_type, T.Array):
         raise Error.StaticTypeMismatch(
             expr.arguments[0], T.Array(T.Array(T.Any())), expr.arguments[0].type
         )
     return expr.arguments[0].type
Ejemplo n.º 6
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if not expr.arguments:
         raise Error.WrongArity(expr, 1)
     if not expr.arguments[0].type.coerces(T.File(optional=True)):
         if isinstance(expr.arguments[0].type, T.Array):
             if expr.arguments[0].type.optional or not expr.arguments[0].type.item_type.coerces(
                 T.File(optional=True)
             ):
                 raise Error.StaticTypeMismatch(
                     expr.arguments[0], T.Array(T.File(optional=True)), expr.arguments[0].type
                 )
         else:
             raise Error.StaticTypeMismatch(
                 expr.arguments[0], T.File(optional=True), expr.arguments[0].type
             )
     if len(expr.arguments) == 2:
         if expr.arguments[1].type != T.String():
             raise Error.StaticTypeMismatch(
                 expr.arguments[1], T.String(), expr.arguments[1].type
             )
     elif len(expr.arguments) > 2:
         raise Error.WrongArity(expr, 2)
     return T.Float()
Ejemplo n.º 7
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if len(expr.arguments) != 2:
         raise Error.WrongArity(expr, 2)
     arg0ty: T.Base = expr.arguments[0].type
     if not isinstance(arg0ty, T.Array) or (expr._check_quant and arg0ty.optional):
         raise Error.StaticTypeMismatch(expr.arguments[0], T.Array(T.Any()), arg0ty)
     if isinstance(arg0ty.item_type, T.Any):
         # TODO: error for 'indeterminate type'
         raise Error.EmptyArray(expr.arguments[0])
     arg1ty: T.Base = expr.arguments[1].type
     if not isinstance(arg1ty, T.Array) or (expr._check_quant and arg1ty.optional):
         raise Error.StaticTypeMismatch(expr.arguments[1], T.Array(T.Any()), arg1ty)
     if isinstance(arg1ty.item_type, T.Any):
         # TODO: error for 'indeterminate type'
         raise Error.EmptyArray(expr.arguments[1])
     return T.Array(
         T.Pair(arg0ty.item_type, arg1ty.item_type),
         nonempty=(arg0ty.nonempty or arg1ty.nonempty),
     )
Ejemplo n.º 8
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     min_args = len(self.argument_types)
     for ty in reversed(self.argument_types):
         if ty.optional:
             min_args = min_args - 1
         else:
             break
     if len(expr.arguments) > len(self.argument_types) or len(expr.arguments) < min_args:
         raise Error.WrongArity(expr, len(self.argument_types))
     for i in range(len(expr.arguments)):
         try:
             expr.arguments[i].typecheck(self.argument_types[i])
         except Error.StaticTypeMismatch:
             raise Error.StaticTypeMismatch(
                 expr.arguments[i],
                 self.argument_types[i],
                 expr.arguments[i].type,
                 "for {} argument #{}".format(self.name, i + 1),
             ) from None
     return self.return_type
Ejemplo n.º 9
0
 def infer_type(self, expr: E.Apply) -> T.Base:
     if len(expr.arguments) != 1:
         raise Error.WrongArity(expr, 1)
     return T.Boolean()