2024-10-31 07:11:41 +00:00
|
|
|
fun foo(x: int): int {
|
|
|
|
try {
|
|
|
|
if (x == 7) {
|
|
|
|
throw 44;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
} catch {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
|
|
|
fun foo_inline(x: int): int {
|
|
|
|
try {
|
|
|
|
assert(!(x == 7)) throw 44;
|
|
|
|
return x;
|
|
|
|
} catch {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@inline_ref
|
|
|
|
fun foo_inlineref(x: int): int {
|
|
|
|
try {
|
|
|
|
if (x == 7) { throw (44, 2); }
|
|
|
|
return x;
|
|
|
|
} catch (_, arg) {
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
return arg as int;
|
2024-10-31 07:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
@method_id(101)
|
2024-10-31 07:11:41 +00:00
|
|
|
fun test(x: int, y: int, z: int): int {
|
|
|
|
y = foo(y);
|
|
|
|
return x * 100 + y * 10 + z;
|
|
|
|
}
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
@method_id(102)
|
2024-10-31 07:11:41 +00:00
|
|
|
fun test_inline(x: int, y: int, z: int): int {
|
|
|
|
y = foo_inline(y);
|
|
|
|
return x * 100 + y * 10 + z;
|
|
|
|
}
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
@method_id(103)
|
2024-10-31 07:11:41 +00:00
|
|
|
fun test_inlineref(x: int, y: int, z: int): int {
|
|
|
|
y = foo_inlineref(y);
|
|
|
|
return x * 100 + y * 10 + z;
|
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
|
|
|
fun foo_inline_big(
|
|
|
|
x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int, x8: int, x9: int, x10: int,
|
|
|
|
x11: int, x12: int, x13: int, x14: int, x15: int, x16: int, x17: int, x18: int, x19: int, x20: int
|
|
|
|
): int {
|
|
|
|
try {
|
|
|
|
if (x1 == 7) {
|
|
|
|
throw 44;
|
|
|
|
}
|
|
|
|
return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20;
|
|
|
|
} catch {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
@method_id(104)
|
2024-10-31 07:11:41 +00:00
|
|
|
fun test_inline_big(x: int, y: int, z: int): int {
|
|
|
|
y = foo_inline_big(
|
|
|
|
y, y + 1, y + 2, y + 3, y + 4, y + 5, y + 6, y + 7, y + 8, y + 9,
|
|
|
|
y + 10, y + 11, y + 12, y + 13, y + 14, y + 15, y + 16, y + 17, y + 18, y + 19);
|
|
|
|
return x * 1000000 + y * 1000 + z;
|
|
|
|
}
|
|
|
|
|
|
|
|
fun foo_big(
|
|
|
|
x1: int, x2: int, x3: int, x4: int, x5: int, x6: int, x7: int, x8: int, x9: int, x10: int,
|
|
|
|
x11: int, x12: int, x13: int, x14: int, x15: int, x16: int, x17: int, x18: int, x19: int, x20: int
|
|
|
|
): int {
|
|
|
|
try {
|
|
|
|
if (x1 == 7) {
|
|
|
|
throw (44, 1);
|
|
|
|
}
|
|
|
|
return x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20;
|
|
|
|
} catch (code, arg) {
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
return arg as int;
|
2024-10-31 07:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
@method_id(105)
|
2024-10-31 07:11:41 +00:00
|
|
|
fun test_big(x: int, y: int, z: int): int {
|
|
|
|
y = foo_big(
|
|
|
|
y, y + 1, y + 2, y + 3, y + 4, y + 5, y + 6, y + 7, y + 8, y + 9,
|
|
|
|
y + 10, y + 11, y + 12, y + 13, y + 14, y + 15, y + 16, y + 17, y + 18, y + 19);
|
|
|
|
return x * 1000000 + y * 1000 + z;
|
|
|
|
}
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
@method_id(106)
|
2024-10-31 07:11:41 +00:00
|
|
|
fun test_catch_into_same(x: int): int {
|
|
|
|
var code = x;
|
|
|
|
try {
|
|
|
|
assert(x <= 10, 44);
|
|
|
|
} catch(code) {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
@method_id(107)
|
2024-10-31 07:11:41 +00:00
|
|
|
fun test_catch_into_same_2(x: int): int {
|
|
|
|
var code = x;
|
|
|
|
try {
|
|
|
|
if (x > 10) {
|
|
|
|
throw 44;
|
|
|
|
}
|
|
|
|
} catch(code) {
|
|
|
|
}
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
global after046: int;
|
|
|
|
|
|
|
|
// this bug existed in FunC and is fixed in v0.4.6
|
|
|
|
fun bug_046_internal(op: int) {
|
|
|
|
if (op == 1) {
|
|
|
|
return;
|
|
|
|
} else if (op == 2) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun bug_046_called() {
|
|
|
|
after046 = 0;
|
|
|
|
try {
|
|
|
|
bug_046_internal(1337);
|
|
|
|
after046 = 1; // shouldn't be called
|
|
|
|
} catch(n) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@method_id(108)
|
|
|
|
fun bug_046_entrypoint() {
|
|
|
|
bug_046_called();
|
|
|
|
return after046;
|
|
|
|
}
|
|
|
|
|
|
|
|
global g_reg: int;
|
|
|
|
|
|
|
|
@method_id(109)
|
|
|
|
fun test109(): (int, int) {
|
|
|
|
var l_reg = 10;
|
|
|
|
g_reg = 10;
|
|
|
|
try {
|
|
|
|
// note, that regardless of assignment, an exception RESTORES them to previous (to 10)
|
|
|
|
// it's very unexpected, but is considered to be a TVM feature, not a bug
|
|
|
|
g_reg = 999;
|
|
|
|
l_reg = 999;
|
|
|
|
bug_046_internal(999); // throws
|
|
|
|
} catch {
|
|
|
|
}
|
|
|
|
// returns (10,10) because of an exception, see a comment above
|
|
|
|
return (g_reg, l_reg);
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:11:41 +00:00
|
|
|
fun main() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
method_id | in | out
|
|
|
|
@testcase | 101 | 1 2 3 | 123
|
|
|
|
@testcase | 101 | 3 8 9 | 389
|
|
|
|
@testcase | 101 | 3 7 9 | 329
|
|
|
|
@testcase | 102 | 1 2 3 | 123
|
|
|
|
@testcase | 102 | 3 8 9 | 389
|
|
|
|
@testcase | 102 | 3 7 9 | 329
|
|
|
|
@testcase | 103 | 1 2 3 | 123
|
|
|
|
@testcase | 103 | 3 8 9 | 389
|
|
|
|
@testcase | 103 | 3 7 9 | 329
|
|
|
|
@testcase | 104 | 4 8 9 | 4350009
|
|
|
|
@testcase | 104 | 4 7 9 | 4001009
|
|
|
|
@testcase | 105 | 4 8 9 | 4350009
|
|
|
|
@testcase | 105 | 4 7 9 | 4001009
|
|
|
|
@testcase | 106 | 5 | 5
|
|
|
|
@testcase | 106 | 20 | 44
|
|
|
|
@testcase | 107 | 5 | 5
|
|
|
|
@testcase | 107 | 20 | 20
|
|
|
|
@testcase | 108 | | 0
|
|
|
|
|
|
|
|
@code_hash 39307974281105539319288356721945232226028429128341177951717392648324358675585
|
2024-10-31 07:11:41 +00:00
|
|
|
*/
|