@@ -160,6 +160,22 @@ static bool parse_raw_address(const std::string& acc_string, int& workchain, ton
160160 return true ;
161161}
162162
163+ static Expr* create_expr_apply (SrcLocation loc, SymDef* sym, std::vector<Expr*>&& args) {
164+ Expr* apply = new Expr (Expr::_Apply, sym, std::move (args));
165+ apply->here = loc;
166+ apply->flags = Expr::_IsRvalue;
167+ apply->deduce_type ();
168+ return apply;
169+ }
170+
171+ static Expr* create_expr_int_const (SrcLocation loc, int int_val) {
172+ Expr* int_const = new Expr (Expr::_Const, loc);
173+ int_const->intval = td::make_refint (int_val);
174+ int_const->flags = Expr::_IsRvalue;
175+ int_const->e_type = TypeExpr::new_atomic (TypeExpr::_Int);
176+ return int_const;
177+ }
178+
163179namespace blk_fl {
164180enum { end = 1 , ret = 2 , empty = 4 };
165181typedef int val;
@@ -238,13 +254,10 @@ static Expr* process_expr(V<ast_binary_operator> v, CodeBlob& code) {
238254 if (x->is_immutable ()) {
239255 x->fire_error_modifying_immutable (" left side of assignment" );
240256 }
241- sym_idx_t name = G. symbols . lookup_add ( " ^_" + operator_name + " _" );
257+ SymDef* sym = lookup_symbol ( calc_sym_idx ( " ^_" + operator_name + " _" ) );
242258 Expr* y = process_expr (v->get_rhs (), code);
243259 y->chk_rvalue ();
244- Expr* z = new Expr{Expr::_Apply, name, {x, y}};
245- z->here = v->loc ;
246- z->flags = Expr::_IsRvalue;
247- z->deduce_type ();
260+ Expr* z = create_expr_apply (v->loc , sym, {x, y});
248261 Expr* res = new Expr{Expr::_Letop, {x->copy (), z}};
249262 res->here = v->loc ;
250263 res->flags = x->flags | Expr::_IsRvalue;
@@ -276,25 +289,35 @@ static Expr* process_expr(V<ast_binary_operator> v, CodeBlob& code) {
276289 t == tok_mul || t == tok_div || t == tok_mod || t == tok_divC || t == tok_divR) {
277290 Expr* res = process_expr (v->get_lhs (), code);
278291 res->chk_rvalue ();
279- sym_idx_t name = G. symbols . lookup_add ( " _" + operator_name + " _" );
292+ SymDef* sym = lookup_symbol ( calc_sym_idx ( " _" + operator_name + " _" ) );
280293 Expr* x = process_expr (v->get_rhs (), code);
281294 x->chk_rvalue ();
282- res = new Expr{Expr::_Apply, name, {res, x}};
283- res->here = v->loc ;
284- res->flags = Expr::_IsRvalue;
285- res->deduce_type ();
295+ res = create_expr_apply (v->loc , sym, {res, x});
286296 return res;
287297 }
288298 if (t == tok_logical_and || t == tok_logical_or) {
289- v->error (" logical operators are not supported yet" );
299+ // do the following transformations:
300+ // a && b -> a ? (b != 0) : 0
301+ // a || b -> a ? 1 : (b != 0)
302+ SymDef* sym_neq = lookup_symbol (calc_sym_idx (" _!=_" ));
303+ Expr* lhs = process_expr (v->get_lhs (), code);
304+ Expr* rhs = process_expr (v->get_rhs (), code);
305+ Expr* e_neq0 = create_expr_apply (v->loc , sym_neq, {rhs, create_expr_int_const (v->loc , 0 )});
306+ Expr* e_when_true = t == tok_logical_and ? e_neq0 : create_expr_int_const (v->loc , -1 );
307+ Expr* e_when_false = t == tok_logical_and ? create_expr_int_const (v->loc , 0 ) : e_neq0;
308+ Expr* e_ternary = new Expr (Expr::_CondExpr, {lhs, e_when_true, e_when_false});
309+ e_ternary->here = v->loc ;
310+ e_ternary->flags = Expr::_IsRvalue;
311+ e_ternary->deduce_type ();
312+ return e_ternary;
290313 }
291314
292315 v->error (" unsupported binary operator" );
293316}
294317
295318static Expr* process_expr (V<ast_unary_operator> v, CodeBlob& code) {
296319 TokenType t = v->tok ;
297- sym_idx_t name = G. symbols . lookup_add ( static_cast <std::string>(v->operator_name ) + " _" );
320+ SymDef* sym = lookup_symbol ( calc_sym_idx ( static_cast <std::string>(v->operator_name ) + " _" ) );
298321 Expr* x = process_expr (v->get_rhs (), code);
299322 x->chk_rvalue ();
300323
@@ -316,11 +339,7 @@ static Expr* process_expr(V<ast_unary_operator> v, CodeBlob& code) {
316339 return x;
317340 }
318341
319- auto res = new Expr{Expr::_Apply, name, {x}};
320- res->here = v->loc ;
321- res->flags = Expr::_IsRvalue;
322- res->deduce_type ();
323- return res;
342+ return create_expr_apply (v->loc , sym, {x});
324343}
325344
326345static Expr* process_expr (V<ast_ternary_operator> v, CodeBlob& code) {
@@ -683,19 +702,12 @@ static Expr* process_expr(V<ast_string_const> v) {
683702
684703static Expr* process_expr (V<ast_bool_const> v) {
685704 SymDef* builtin_sym = lookup_symbol (calc_sym_idx (v->bool_val ? " __true" : " __false" ));
686- Expr* res = new Expr{Expr::_Apply, builtin_sym, {}};
687- res->flags = Expr::_IsRvalue;
688- res->deduce_type ();
689- return res;
705+ return create_expr_apply (v->loc , builtin_sym, {});
690706}
691707
692708static Expr* process_expr (V<ast_null_keyword> v) {
693709 SymDef* builtin_sym = lookup_symbol (calc_sym_idx (" __null" ));
694- Expr* res = new Expr{Expr::_Apply, builtin_sym, {}};
695- res->here = v->loc ;
696- res->flags = Expr::_IsRvalue;
697- res->deduce_type ();
698- return res;
710+ return create_expr_apply (v->loc , builtin_sym, {});
699711}
700712
701713static Expr* process_expr (V<ast_self_keyword> v, CodeBlob& code) {
@@ -1116,11 +1128,9 @@ static blk_fl::val process_vertex(V<ast_throw_statement> v, CodeBlob& code) {
11161128 args.push_back (process_expr (v->get_thrown_code (), code));
11171129 }
11181130
1119- Expr* expr = new Expr{Expr::_Apply, builtin_sym, std::move (args)};
1120- expr->here = v->loc ;
1121- expr->flags = Expr::_IsRvalue | Expr::_IsImpure;
1122- expr->deduce_type ();
1123- expr->pre_compile (code);
1131+ Expr* apply = create_expr_apply (v->loc , builtin_sym, std::move (args));
1132+ apply->flags |= Expr::_IsImpure;
1133+ apply->pre_compile (code);
11241134 return blk_fl::end;
11251135}
11261136
@@ -1137,11 +1147,9 @@ static blk_fl::val process_vertex(V<ast_assert_statement> v, CodeBlob& code) {
11371147 }
11381148
11391149 SymDef* builtin_sym = lookup_symbol (calc_sym_idx (" __throw_if_unless" ));
1140- Expr* expr = new Expr{Expr::_Apply, builtin_sym, std::move (args)};
1141- expr->here = v->loc ;
1142- expr->flags = Expr::_IsRvalue | Expr::_IsImpure;
1143- expr->deduce_type ();
1144- expr->pre_compile (code);
1150+ Expr* apply = create_expr_apply (v->loc , builtin_sym, std::move (args));
1151+ apply->flags |= Expr::_IsImpure;
1152+ apply->pre_compile (code);
11451153 return blk_fl::end;
11461154}
11471155
0 commit comments