Skip to content

Commit 5b44e01

Browse files
committed
[Tolk] Allow cell and slice be valid identifiers
They are not keywords anymore. > var cell = ...; > var cell: cell = ...; Motivation: in the future, when structures are implemented, this obviously should be valid: > struct a { ... } > var a = ...; Struct fields will also be allowed to have names int/slice/cell.
1 parent 7a1602f commit 5b44e01

File tree

9 files changed

+47
-69
lines changed

9 files changed

+47
-69
lines changed

tolk-tester/tests/assignment-tests.tolk

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ fun autoInferIntNull(x: int) {
1414
return x;
1515
}
1616

17+
fun typesAsIdentifiers(builder: builder) {
18+
var int = 1;
19+
var cell = builder.endCell();
20+
var slice = cell.beginParse();
21+
{
22+
var cell: cell = cell;
23+
var tuple: tuple = createEmptyTuple();
24+
var bool: bool = tuple.tupleAt<int>(0) > 0;
25+
}
26+
return int;
27+
}
28+
1729
fun main(value: int) {
1830
var (x: int, y) = (autoInferIntNull(value), autoInferIntNull(value * 2));
1931
if (x == null && y == null) { return null; }
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
fun main() {
22
try {
33

4-
} catch(int, arg) {}
4+
} catch(if, arg) {}
55
return 0;
66
}
77

88
/**
99
@compilation_should_fail
10-
@stderr expected identifier, got `int`
11-
@stderr catch(int
10+
@stderr expected identifier, got `if`
11+
@stderr catch(if
1212
*/

tolk-tester/tests/invalid-declaration-2.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ fun main(int): int {
44

55
/**
66
@compilation_should_fail
7-
@stderr expected parameter name, got `int`
7+
@stderr expected `: <parameter_type>`, got `)`
88
*/

tolk-tester/tests/invalid-declaration-4.tolk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ fun main() {
44

55
/**
66
@compilation_should_fail
7-
@stderr probably, you use FunC-like declarations; valid syntax is `var x: int = ...`
7+
@stderr expected `;`, got `x`
88
*/

tolk-tester/tests/method_id.tolk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ fun foo1(): int { return 111; }
44
fun foo2(): int { return 222; }
55
@method_id(10)
66
fun foo3(): int { return 333; }
7+
@method_id(11)
8+
fun slice(slice: slice): slice { return slice; }
79
fun main(): int { return 999; }
810

911
/**

tolk/ast-from-tokens.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,6 @@ static void diagnose_addition_in_bitshift(SrcLocation loc, std::string_view bits
111111
}
112112
}
113113

114-
// fire an error for FunC-style variable declaration, like "int i"
115-
GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD
116-
static void fire_error_FunC_style_var_declaration(Lexer& lex) {
117-
SrcLocation loc = lex.cur_location();
118-
std::string type_str = static_cast<std::string>(lex.cur_str()); // int / slice / etc.
119-
lex.next();
120-
std::string var_name = lex.tok() == tok_identifier ? static_cast<std::string>(lex.cur_str()) : "name";
121-
throw ParseError(loc, "can't parse; probably, you use FunC-like declarations; valid syntax is `var " + var_name + ": " + type_str + " = ...`");
122-
}
123-
124114
// replace (a == null) and similar to isNull(a) (call of a built-in function)
125115
static AnyExprV maybe_replace_eq_null_with_isNull_call(V<ast_binary_operator> v) {
126116
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) {
377367
}
378368
return createV<ast_reference>(loc, v_ident, v_instantiationTs);
379369
}
380-
default: {
381-
// show a proper error for `int i` (FunC-style declarations)
382-
TokenType t = lex.tok();
383-
if (t == tok_int || t == tok_cell || t == tok_slice || t == tok_builder || t == tok_tuple) {
384-
fire_error_FunC_style_var_declaration(lex);
385-
}
370+
default:
386371
lex.unexpected("<expression>");
387-
}
388372
}
389373
}
390374

tolk/lexer.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase {
331331
if (str == "as") return tok_as;
332332
break;
333333
case 3:
334-
if (str == "int") return tok_int;
335334
if (str == "var") return tok_var;
336335
if (str == "fun") return tok_fun;
337336
if (str == "asm") return tok_asm;
@@ -342,18 +341,13 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase {
342341
case 4:
343342
if (str == "else") return tok_else;
344343
if (str == "true") return tok_true;
345-
if (str == "cell") return tok_cell;
346344
if (str == "null") return tok_null;
347-
if (str == "void") return tok_void;
348-
if (str == "bool") return tok_bool;
349345
if (str == "self") return tok_self;
350346
if (str == "tolk") return tok_tolk;
351347
if (str == "type") return tok_type;
352348
if (str == "enum") return tok_enum;
353349
break;
354350
case 5:
355-
if (str == "slice") return tok_slice;
356-
if (str == "tuple") return tok_tuple;
357351
if (str == "const") return tok_const;
358352
if (str == "false") return tok_false;
359353
if (str == "redef") return tok_redef;
@@ -374,16 +368,12 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase {
374368
if (str == "export") return tok_export;
375369
break;
376370
case 7:
377-
if (str == "builder") return tok_builder;
378371
if (str == "builtin") return tok_builtin;
379372
break;
380373
case 8:
381374
if (str == "continue") return tok_continue;
382375
if (str == "operator") return tok_operator;
383376
break;
384-
case 12:
385-
if (str == "continuation") return tok_continuation;
386-
break;
387377
default:
388378
break;
389379
}

tolk/lexer.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ enum TokenType {
118118
tok_if,
119119
tok_else,
120120

121-
tok_int,
122-
tok_cell,
123-
tok_bool,
124-
tok_slice,
125-
tok_builder,
126-
tok_continuation,
127-
tok_tuple,
128-
tok_void,
129121
tok_arrow,
130122
tok_as,
131123

tolk/type-system.cpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -581,40 +581,38 @@ std::vector<TypePtr> parse_nested_type_list_in_parenthesis(Lexer& lex) {
581581

582582
static TypePtr parse_simple_type(Lexer& lex) {
583583
switch (lex.tok()) {
584-
case tok_int:
585-
lex.next();
586-
return TypeDataInt::create();
587-
case tok_bool:
588-
lex.next();
589-
return TypeDataBool::create();
590-
case tok_cell:
591-
lex.next();
592-
return TypeDataCell::create();
593-
case tok_builder:
594-
lex.next();
595-
return TypeDataBuilder::create();
596-
case tok_slice:
597-
lex.next();
598-
return TypeDataSlice::create();
599-
case tok_tuple:
600-
lex.next();
601-
return TypeDataTuple::create();
602-
case tok_continuation:
603-
lex.next();
604-
return TypeDataContinuation::create();
605-
case tok_null:
606-
lex.next();
607-
return TypeDataNullLiteral::create();
608-
case tok_void:
609-
lex.next();
610-
return TypeDataVoid::create();
611584
case tok_self:
612585
case tok_identifier: {
613586
SrcLocation loc = lex.cur_location();
614-
std::string text = static_cast<std::string>(lex.cur_str());
587+
std::string_view str = lex.cur_str();
615588
lex.next();
616-
return TypeDataUnresolved::create(std::move(text), loc);
589+
switch (str.size()) {
590+
case 3:
591+
if (str == "int") return TypeDataInt::create();
592+
break;
593+
case 4:
594+
if (str == "cell") return TypeDataCell::create();
595+
if (str == "void") return TypeDataVoid::create();
596+
if (str == "bool") return TypeDataBool::create();
597+
break;
598+
case 5:
599+
if (str == "slice") return TypeDataSlice::create();
600+
if (str == "tuple") return TypeDataTuple::create();
601+
break;
602+
case 7:
603+
if (str == "builder") return TypeDataBuilder::create();
604+
break;
605+
case 12:
606+
if (str == "continuation") return TypeDataContinuation::create();
607+
break;
608+
default:
609+
break;
610+
}
611+
return TypeDataUnresolved::create(std::string(str), loc);
617612
}
613+
case tok_null:
614+
lex.next();
615+
return TypeDataNullLiteral::create();
618616
case tok_oppar: {
619617
std::vector<TypePtr> items = parse_nested_type_list_in_parenthesis(lex);
620618
if (items.size() == 1) {

0 commit comments

Comments
 (0)