diff --git a/tolk-tester/tests/assignment-tests.tolk b/tolk-tester/tests/assignment-tests.tolk index 89de8cf4..40761939 100644 --- a/tolk-tester/tests/assignment-tests.tolk +++ b/tolk-tester/tests/assignment-tests.tolk @@ -14,6 +14,18 @@ fun autoInferIntNull(x: int) { return x; } +fun typesAsIdentifiers(builder: builder) { + var int = 1; + var cell = builder.endCell(); + var slice = cell.beginParse(); + { + var cell: cell = cell; + var tuple: tuple = createEmptyTuple(); + var bool: bool = tuple.tupleAt(0) > 0; + } + return int; +} + fun main(value: int) { var (x: int, y) = (autoInferIntNull(value), autoInferIntNull(value * 2)); if (x == null && y == null) { return null; } diff --git a/tolk-tester/tests/invalid-catch-1.tolk b/tolk-tester/tests/invalid-catch-1.tolk index 756722bb..54f6e182 100644 --- a/tolk-tester/tests/invalid-catch-1.tolk +++ b/tolk-tester/tests/invalid-catch-1.tolk @@ -1,12 +1,12 @@ fun main() { try { - } catch(int, arg) {} + } catch(if, arg) {} return 0; } /** @compilation_should_fail -@stderr expected identifier, got `int` -@stderr catch(int +@stderr expected identifier, got `if` +@stderr catch(if */ diff --git a/tolk-tester/tests/invalid-declaration-2.tolk b/tolk-tester/tests/invalid-declaration-2.tolk index 70063251..07ffb683 100644 --- a/tolk-tester/tests/invalid-declaration-2.tolk +++ b/tolk-tester/tests/invalid-declaration-2.tolk @@ -4,5 +4,5 @@ fun main(int): int { /** @compilation_should_fail -@stderr expected parameter name, got `int` +@stderr expected `: `, got `)` */ diff --git a/tolk-tester/tests/invalid-declaration-4.tolk b/tolk-tester/tests/invalid-declaration-4.tolk index 183dda96..62fd6c56 100644 --- a/tolk-tester/tests/invalid-declaration-4.tolk +++ b/tolk-tester/tests/invalid-declaration-4.tolk @@ -4,5 +4,5 @@ fun main() { /** @compilation_should_fail -@stderr probably, you use FunC-like declarations; valid syntax is `var x: int = ...` +@stderr expected `;`, got `x` */ diff --git a/tolk-tester/tests/method_id.tolk b/tolk-tester/tests/method_id.tolk index c2d0b9aa..e7e70d24 100644 --- a/tolk-tester/tests/method_id.tolk +++ b/tolk-tester/tests/method_id.tolk @@ -4,6 +4,8 @@ fun foo1(): int { return 111; } fun foo2(): int { return 222; } @method_id(10) fun foo3(): int { return 333; } +@method_id(11) +fun slice(slice: slice): slice { return slice; } fun main(): int { return 999; } /** diff --git a/tolk/ast-from-tokens.cpp b/tolk/ast-from-tokens.cpp index 3eb4385a..f5855bc1 100644 --- a/tolk/ast-from-tokens.cpp +++ b/tolk/ast-from-tokens.cpp @@ -111,16 +111,6 @@ static void diagnose_addition_in_bitshift(SrcLocation loc, std::string_view bits } } -// fire an error for FunC-style variable declaration, like "int i" -GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD -static void fire_error_FunC_style_var_declaration(Lexer& lex) { - SrcLocation loc = lex.cur_location(); - std::string type_str = static_cast(lex.cur_str()); // int / slice / etc. - lex.next(); - std::string var_name = lex.tok() == tok_identifier ? static_cast(lex.cur_str()) : "name"; - throw ParseError(loc, "can't parse; probably, you use FunC-like declarations; valid syntax is `var " + var_name + ": " + type_str + " = ...`"); -} - // replace (a == null) and similar to isNull(a) (call of a built-in function) static AnyExprV maybe_replace_eq_null_with_isNull_call(V v) { bool has_null = v->get_lhs()->type == ast_null_keyword || v->get_rhs()->type == ast_null_keyword; @@ -377,14 +367,8 @@ static AnyExprV parse_expr100(Lexer& lex) { } return createV(loc, v_ident, v_instantiationTs); } - default: { - // show a proper error for `int i` (FunC-style declarations) - TokenType t = lex.tok(); - if (t == tok_int || t == tok_cell || t == tok_slice || t == tok_builder || t == tok_tuple) { - fire_error_FunC_style_var_declaration(lex); - } + default: lex.unexpected(""); - } } } diff --git a/tolk/lexer.cpp b/tolk/lexer.cpp index 78ec991e..06913a5f 100644 --- a/tolk/lexer.cpp +++ b/tolk/lexer.cpp @@ -331,7 +331,6 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase { if (str == "as") return tok_as; break; case 3: - if (str == "int") return tok_int; if (str == "var") return tok_var; if (str == "fun") return tok_fun; if (str == "asm") return tok_asm; @@ -342,18 +341,13 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase { case 4: if (str == "else") return tok_else; if (str == "true") return tok_true; - if (str == "cell") return tok_cell; if (str == "null") return tok_null; - if (str == "void") return tok_void; - if (str == "bool") return tok_bool; if (str == "self") return tok_self; if (str == "tolk") return tok_tolk; if (str == "type") return tok_type; if (str == "enum") return tok_enum; break; case 5: - if (str == "slice") return tok_slice; - if (str == "tuple") return tok_tuple; if (str == "const") return tok_const; if (str == "false") return tok_false; if (str == "redef") return tok_redef; @@ -374,16 +368,12 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase { if (str == "export") return tok_export; break; case 7: - if (str == "builder") return tok_builder; if (str == "builtin") return tok_builtin; break; case 8: if (str == "continue") return tok_continue; if (str == "operator") return tok_operator; break; - case 12: - if (str == "continuation") return tok_continuation; - break; default: break; } diff --git a/tolk/lexer.h b/tolk/lexer.h index 9dbfe3b6..58bc3640 100644 --- a/tolk/lexer.h +++ b/tolk/lexer.h @@ -118,14 +118,6 @@ enum TokenType { tok_if, tok_else, - tok_int, - tok_cell, - tok_bool, - tok_slice, - tok_builder, - tok_continuation, - tok_tuple, - tok_void, tok_arrow, tok_as, diff --git a/tolk/type-system.cpp b/tolk/type-system.cpp index 401c72af..c7122e10 100644 --- a/tolk/type-system.cpp +++ b/tolk/type-system.cpp @@ -581,40 +581,38 @@ std::vector parse_nested_type_list_in_parenthesis(Lexer& lex) { static TypePtr parse_simple_type(Lexer& lex) { switch (lex.tok()) { - case tok_int: - lex.next(); - return TypeDataInt::create(); - case tok_bool: - lex.next(); - return TypeDataBool::create(); - case tok_cell: - lex.next(); - return TypeDataCell::create(); - case tok_builder: - lex.next(); - return TypeDataBuilder::create(); - case tok_slice: - lex.next(); - return TypeDataSlice::create(); - case tok_tuple: - lex.next(); - return TypeDataTuple::create(); - case tok_continuation: - lex.next(); - return TypeDataContinuation::create(); - case tok_null: - lex.next(); - return TypeDataNullLiteral::create(); - case tok_void: - lex.next(); - return TypeDataVoid::create(); case tok_self: case tok_identifier: { SrcLocation loc = lex.cur_location(); - std::string text = static_cast(lex.cur_str()); + std::string_view str = lex.cur_str(); lex.next(); - return TypeDataUnresolved::create(std::move(text), loc); + switch (str.size()) { + case 3: + if (str == "int") return TypeDataInt::create(); + break; + case 4: + if (str == "cell") return TypeDataCell::create(); + if (str == "void") return TypeDataVoid::create(); + if (str == "bool") return TypeDataBool::create(); + break; + case 5: + if (str == "slice") return TypeDataSlice::create(); + if (str == "tuple") return TypeDataTuple::create(); + break; + case 7: + if (str == "builder") return TypeDataBuilder::create(); + break; + case 12: + if (str == "continuation") return TypeDataContinuation::create(); + break; + default: + break; + } + return TypeDataUnresolved::create(std::string(str), loc); } + case tok_null: + lex.next(); + return TypeDataNullLiteral::create(); case tok_oppar: { std::vector items = parse_nested_type_list_in_parenthesis(lex); if (items.size() == 1) {