mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] bool
type (-1/0 int under the hood)
Comparison operators `== / >= /...` return `bool`. Logical operators `&& ||` return bool. Constants `true` and `false` have the `bool` type. Lots of stdlib functions return `bool`, not `int`. Operator `!x` supports both `int` and `bool`. Condition of `if` accepts both `int` and `bool`. Arithmetic operators are restricted to integers. Logical `&&` and `||` accept both `bool` and `int`. No arithmetic operations with bools allowed (only bitwise and logical).
This commit is contained in:
parent
799e2d1265
commit
974d76c5f6
33 changed files with 764 additions and 212 deletions
|
@ -23,7 +23,8 @@
|
|||
* This pipe is supposed to do constant folding, like replacing `2 + 3` with `5`.
|
||||
* It happens after type inferring and validity checks, one of the last ones.
|
||||
*
|
||||
* Currently, it just replaces `-1` (ast_unary_operator ast_int_const) with a number -1.
|
||||
* Currently, it just replaces `-1` (ast_unary_operator ast_int_const) with a number -1
|
||||
* and `!true` with false.
|
||||
* More rich constant folding should be done some day, but even without this, IR optimizations
|
||||
* (operating low-level stack variables) pretty manage to do all related optimizations.
|
||||
* Constant folding in the future, done at AST level, just would slightly reduce amount of work for optimizer.
|
||||
|
@ -39,6 +40,13 @@ class ConstantFoldingReplacer final : public ASTReplacerInFunctionBody {
|
|||
return v_int;
|
||||
}
|
||||
|
||||
static V<ast_bool_const> create_bool_const(SrcLocation loc, bool bool_val) {
|
||||
auto v_bool = createV<ast_bool_const>(loc, bool_val);
|
||||
v_bool->assign_inferred_type(TypeDataBool::create());
|
||||
v_bool->assign_rvalue_true();
|
||||
return v_bool;
|
||||
}
|
||||
|
||||
AnyExprV replace(V<ast_unary_operator> v) override {
|
||||
parent::replace(v);
|
||||
|
||||
|
@ -58,6 +66,15 @@ class ConstantFoldingReplacer final : public ASTReplacerInFunctionBody {
|
|||
return v->get_rhs();
|
||||
}
|
||||
|
||||
// `!true` / `!false`
|
||||
if (t == tok_logical_not && v->get_rhs()->type == ast_bool_const) {
|
||||
return create_bool_const(v->loc, !v->get_rhs()->as<ast_bool_const>()->bool_val);
|
||||
}
|
||||
// `!0`
|
||||
if (t == tok_logical_not && v->get_rhs()->type == ast_int_const) {
|
||||
return create_bool_const(v->loc, v->get_rhs()->as<ast_int_const>()->intval == 0);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue