2024-10-31 07:18:54 +00:00
|
|
|
fun getBeginCell() {
|
|
|
|
return beginCell;
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getBeginParse() {
|
|
|
|
return beginParse;
|
|
|
|
}
|
|
|
|
|
|
|
|
@method_id(101)
|
|
|
|
fun testVarApply1() {
|
|
|
|
var (_, f_end_cell) = (0, endCell);
|
|
|
|
var b: builder = (getBeginCell())().storeInt(1, 32);
|
|
|
|
b.storeInt(2, 32);
|
|
|
|
var s = (getBeginParse())(f_end_cell(b));
|
|
|
|
return (s.loadInt(32), s.loadInt(32));
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
@inline
|
|
|
|
fun my_throw_always() {
|
|
|
|
throw 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
|
|
|
fun get_raiser() {
|
|
|
|
return my_throw_always;
|
|
|
|
}
|
|
|
|
|
|
|
|
@method_id(102)
|
|
|
|
fun testVarApplyWithoutSavingResult() {
|
|
|
|
try {
|
|
|
|
var raiser = get_raiser();
|
|
|
|
raiser(); // `some_var()` is always impure, the compiler has no considerations about its runtime value
|
|
|
|
return 0;
|
|
|
|
} catch (code) {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
|
|
|
fun sum(a: int, b: int) {
|
|
|
|
assert(a + b < 24, 1000);
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
@inline
|
|
|
|
fun mul(a: int, b: int) {
|
|
|
|
assert(a * b < 24, 1001);
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
fun demo_handler(op: int, query_id: int, a: int, b: int): int {
|
|
|
|
if (op == 0xF2) {
|
|
|
|
val func = query_id % 2 == 0 ? sum : mul;
|
|
|
|
val result = func(a, b);
|
|
|
|
return 0; // result not used, we test that func is nevertheless called
|
|
|
|
}
|
|
|
|
if (op == 0xF4) {
|
|
|
|
val func = query_id % 2 == 0 ? sum : mul;
|
|
|
|
val result = func(a, b);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@method_id(103)
|
|
|
|
fun testVarApplyInTernary() {
|
|
|
|
var t: tuple = createEmptyTuple();
|
|
|
|
try {
|
|
|
|
t.tuplePush(demo_handler(0xF2, 122, 100, 200));
|
|
|
|
} catch(code) {
|
|
|
|
t.tuplePush(code);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
t.tuplePush(demo_handler(0xF4, 122, 100, 200));
|
|
|
|
} catch(code) {
|
|
|
|
t.tuplePush(code);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
t.tuplePush(demo_handler(0xF2, 122, 10, 10));
|
|
|
|
} catch(code) {
|
|
|
|
t.tuplePush(code);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
t.tuplePush(demo_handler(0xF2, 123, 10, 10));
|
|
|
|
} catch(code) {
|
|
|
|
t.tuplePush(code);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
fun always_throw2(x: int) {
|
|
|
|
throw 239 + x;
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
global global_f: int -> void;
|
[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)
|
|
|
|
fun testGlobalVarApply() {
|
|
|
|
try {
|
|
|
|
global_f = always_throw2;
|
|
|
|
global_f(1);
|
|
|
|
return 0;
|
|
|
|
} catch (code) {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
@method_id(105)
|
|
|
|
fun testVarApply2() {
|
|
|
|
var creator = createEmptyTuple;
|
|
|
|
var t = creator();
|
|
|
|
t.tuplePush(1);
|
|
|
|
var sizer = t.tupleSize;
|
|
|
|
return sizer(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getTupleLastGetter<X>(): (tuple) -> X {
|
|
|
|
return tupleLast<X>;
|
|
|
|
}
|
|
|
|
|
|
|
|
@method_id(106)
|
|
|
|
fun testVarApply3() {
|
|
|
|
var t = createEmptyTuple();
|
|
|
|
t.tuplePush(1);
|
|
|
|
t.tuplePush([2]);
|
|
|
|
var getIntAt = t.tupleAt<int>;
|
|
|
|
var getTupleFirstInt = createEmptyTuple().tupleFirst<int>;
|
|
|
|
var getTupleLastTuple = getTupleLastGetter<tuple>();
|
|
|
|
return (getIntAt(t, 0), getTupleFirstInt(t), getTupleLastTuple(t), getTupleLastGetter<tuple>()(t));
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:18:54 +00:00
|
|
|
fun main() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@testcase | 101 | | 1 2
|
[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
|
|
|
@testcase | 102 | | 1000
|
|
|
|
@testcase | 103 | | [ 1000 1000 0 1001 ]
|
|
|
|
@testcase | 104 | | 240
|
[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
|
|
|
@testcase | 105 | | 1
|
|
|
|
@testcase | 106 | | 1 1 [ 2 ] [ 2 ]
|
2024-10-31 07:18:54 +00:00
|
|
|
*/
|