Skip to content

Commit b175341

Browse files
committed
[Tolk] Fix parsing sequential as operator
1 parent 0fb8c1c commit b175341

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

tolk-tester/tests/match-by-expr-tests.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fun test7(x: int8) {
8181

8282
@method_id(108)
8383
fun test8(x: int) {
84-
var r1 = match (x is int) { false => 0, else => 10 };
84+
var r1 = match (x is int && x is int is bool) { false => 0, else => 10 };
8585
match (x) { }
8686
match (x * 0) { 1 => {} else => { r1 += 20; } }
8787
return r1;

tolk-tester/tests/parse-address.tolk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ fun main() {
142142
(cc1 as slice).bitsEqual(((cc2 as slice) as address) as slice),
143143
createAddressNone() == cc1,
144144
createAddressNone() == address.createNone(),
145-
check0(address("0:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFfffffffffffffffffffffffffffff"))
145+
check0(address("0:FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFfffffffffffffffffffffffffffff")),
146+
cc1 == cc1 as bits267 as address,
147+
cc1 == cc1 as slice as bits999 as address,
146148
);
147149
}
148150

149151
/**
150-
@testcase | 0 | | -1 0 -1 0 -1
152+
@testcase | 0 | | -1 0 -1 0 -1 -1 -1
151153

152154
@fif_codegen
153155
"""

tolk/ast-from-tokens.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -951,23 +951,24 @@ static AnyExprV parse_expr75(Lexer& lex) {
951951
return parse_expr80(lex);
952952
}
953953

954-
// parse E as / is / !is <type>
954+
// parse E as / is / !is <type> (left-to-right)
955955
static AnyExprV parse_expr40(Lexer& lex) {
956956
AnyExprV lhs = parse_expr75(lex);
957-
if (lex.tok() == tok_as) {
958-
SrcLocation loc = lex.cur_location();
959-
lex.next();
960-
AnyTypeV cast_to_type = parse_type_from_tokens(lex);
961-
lhs = createV<ast_cast_as_operator>(loc, lhs, cast_to_type);
962-
} else if (lex.tok() == tok_is) {
957+
TokenType t = lex.tok();
958+
while (t == tok_as || t == tok_is) {
963959
SrcLocation loc = lex.cur_location();
964960
lex.next();
965961
AnyTypeV rhs_type = parse_type_from_tokens(lex);
966-
bool is_negated = lhs->kind == ast_not_null_operator; // `a !is ...`, now lhs = `a!`
967-
if (is_negated) {
968-
lhs = lhs->as<ast_not_null_operator>()->get_expr();
962+
if (t == tok_as) {
963+
lhs = createV<ast_cast_as_operator>(loc, lhs, rhs_type);
964+
} else {
965+
bool is_negated = lhs->kind == ast_not_null_operator; // `a !is ...`, now lhs = `a!`
966+
if (is_negated) {
967+
lhs = lhs->as<ast_not_null_operator>()->get_expr();
968+
}
969+
lhs = createV<ast_is_type_operator>(loc, lhs, rhs_type, is_negated);
969970
}
970-
lhs = createV<ast_is_type_operator>(loc, lhs, rhs_type, is_negated);
971+
t = lex.tok();
971972
}
972973
return lhs;
973974
}

tolk/pipe-ast-to-legacy.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,16 @@ static std::vector<var_idx_t> transition_expr_to_runtime_type_impl(std::vector<v
10091009
return rvect;
10101010
}
10111011

1012+
// pass `bits267` to `address`
1013+
if (target_type == TypeDataAddress::create() && original_type->try_as<TypeDataBitsN>()) {
1014+
return rvect;
1015+
}
1016+
1017+
// pass `address` to `bits267`
1018+
if (original_type == TypeDataAddress::create() && target_type->try_as<TypeDataBitsN>()) {
1019+
return rvect;
1020+
}
1021+
10121022
// pass a typed tuple `[int, int]` to an untyped (via `as` operator)
10131023
if (original_type->try_as<TypeDataBrackets>() && target_type->try_as<TypeDataTuple>()) {
10141024
return rvect;

tolk/type-system.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ bool TypeDataContinuation::can_be_casted_with_as_operator(TypePtr cast_to) const
955955
}
956956

957957
bool TypeDataAddress::can_be_casted_with_as_operator(TypePtr cast_to) const {
958-
if (cast_to == TypeDataSlice::create()) {
958+
if (cast_to == TypeDataSlice::create() || cast_to->try_as<TypeDataBitsN>()) {
959959
return true;
960960
}
961961
if (const TypeDataUnion* to_union = cast_to->try_as<TypeDataUnion>()) {

0 commit comments

Comments
 (0)