if exprssion
This commit is contained in:
parent
b296e67ad1
commit
5d679d88b5
@ -38,6 +38,8 @@ def parse_expr(ctx: Context,
|
||||
return parse_call(ctx, sym_table, body)
|
||||
if isinstance(body, ast.Subscript):
|
||||
return parse_subscript(ctx, sym_table, body)
|
||||
if isinstance(body, ast.IfExp):
|
||||
return parse_if_expr(ctx, sym_table, body)
|
||||
raise CustomError(f'{body} is not yet supported')
|
||||
|
||||
|
||||
@ -65,10 +67,10 @@ def parse_constant(ctx: Context,
|
||||
sym_table: dict[str, Type],
|
||||
node):
|
||||
v = node.value
|
||||
if isinstance(v, int):
|
||||
return ctx.types['int32']
|
||||
elif isinstance(v, bool):
|
||||
if isinstance(v, bool):
|
||||
return ctx.types['bool']
|
||||
elif isinstance(v, int):
|
||||
return ctx.types['int32']
|
||||
else:
|
||||
raise CustomError(f'unknown constant {v}')
|
||||
|
||||
@ -189,3 +191,16 @@ def parse_subscript(ctx: Context,
|
||||
else:
|
||||
raise CustomError(f'index of type {s} is not supported')
|
||||
|
||||
def parse_if_expr(ctx: Context,
|
||||
sym_table: dict[str, Type],
|
||||
node):
|
||||
b = ctx.types['bool']
|
||||
t = parse_expr(ctx, sym_table, node.test)
|
||||
if t != b:
|
||||
raise CustomError(f'type of conditional must be bool instead of {t}')
|
||||
ty1 = parse_expr(ctx, sym_table, node.body)
|
||||
ty2 = parse_expr(ctx, sym_table, node.orelse)
|
||||
if ty1 != ty2:
|
||||
raise CustomError(f'divergent type for if expression: {ty1} != {ty2}')
|
||||
return ty1
|
||||
|
||||
|
@ -76,6 +76,9 @@ test_expr('[1,2,3][:a:2]', {'a': I})
|
||||
test_expr('[1,2,3][:a:2]', {'a': i32})
|
||||
test_expr('a == a == a', {'a': I})
|
||||
test_expr('a == a and 1 == 2', {'a': I})
|
||||
test_expr('1 if a == b else 0', {'a': I, 'b': I})
|
||||
test_expr('a if a == b else 1', {'a': I, 'b': I})
|
||||
test_expr('a if a == b else b', {'a': I, 'b': I})
|
||||
|
||||
test_classes = """
|
||||
class Foo:
|
||||
|
Loading…
Reference in New Issue
Block a user