Skip to content

Commit

Permalink
a++ a-- --a ++a works just like C/C++ now
Browse files Browse the repository at this point in the history
  • Loading branch information
rex committed Jan 28, 2025
1 parent d1b9294 commit a286136
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 17 deletions.
78 changes: 75 additions & 3 deletions src/main/engine/sm_ast_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -3079,10 +3079,46 @@ inline void sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
RETURN_OBJ(((sm_object *)num));
sm_f64 *output = sm_new_f64(num->value + 1);
sm_cx_set(current_cx, sym, (sm_object *)output);
RETURN_OBJ(((sm_object *)output));
RETURN_OBJ(((sm_object *)num));
break;
}
case SM_DEC_EXPR: {
sm_symbol *sym = (sm_symbol *)sm_expr_get_arg(sme, 0);
if (sym->my_type != SM_SYMBOL_TYPE) {
sm_symbol *title = sm_new_symbol("cannotDecNonSymbol", 18);
sm_string *message =
sm_new_fstring_at(sms_heap, "Cannot apply -- to non-symbol. Object type is %s instead",
sm_type_name(sym->my_type));
RETURN_OBJ(((sm_object *)sm_new_error_from_expr(title, message, sme, NULL)));
}
eager_type_check(sme, 0, SM_F64_TYPE, current_cx, sf);
sm_f64 *num = (sm_f64 *)return_obj;
if (num->my_type != SM_F64_TYPE)
RETURN_OBJ(((sm_object *)num));
sm_f64 *output = sm_new_f64(num->value - 1);
sm_cx_set(current_cx, sym, (sm_object *)output);
RETURN_OBJ(((sm_object *)num));
break;
}
case SM_PREINC_EXPR: {
sm_symbol *sym = (sm_symbol *)sm_expr_get_arg(sme, 0);
if (sym->my_type != SM_SYMBOL_TYPE) {
sm_symbol *title = sm_new_symbol("cannotIncNonSymbol", 18);
sm_string *message =
sm_new_fstring_at(sms_heap, "Cannot apply ++ to non-symbol. Object type is %s instead",
sm_type_name(sym->my_type));
RETURN_OBJ(((sm_object *)sm_new_error_from_expr(title, message, sme, NULL)));
}
eager_type_check(sme, 0, SM_F64_TYPE, current_cx, sf);
sm_f64 *num = (sm_f64 *)return_obj;
if (num->my_type != SM_F64_TYPE)
RETURN_OBJ(((sm_object *)num));
sm_f64 *output = sm_new_f64(num->value + 1);
sm_cx_set(current_cx, sym, (sm_object *)output);
RETURN_OBJ(((sm_object *)output));
break;
}
case SM_PREDEC_EXPR: {
sm_symbol *sym = (sm_symbol *)sm_expr_get_arg(sme, 0);
if (sym->my_type != SM_SYMBOL_TYPE) {
sm_symbol *title = sm_new_symbol("cannotDecNonSymbol", 18);
Expand Down Expand Up @@ -3115,7 +3151,7 @@ inline void sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
RETURN_OBJ(((sm_object *)num));
sm_ui8 *output = sm_new_ui8(num->value + 1);
sm_cx_set(current_cx, sym, (sm_object *)output);
RETURN_OBJ(((sm_object *)output));
RETURN_OBJ((sm_object *)num);
break;
}
case SM_IDEC_EXPR: {
Expand All @@ -3133,7 +3169,43 @@ inline void sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
RETURN_OBJ(((sm_object *)num));
sm_ui8 *output = sm_new_ui8(num->value - 1);
sm_cx_set(current_cx, sym, (sm_object *)output);
RETURN_OBJ(((sm_object *)output));
RETURN_OBJ((sm_object *)output);
break;
}
case SM_PREIINC_EXPR: {
sm_symbol *sym = (sm_symbol *)sm_expr_get_arg(sme, 0);
if (sym->my_type != SM_SYMBOL_TYPE) {
sm_symbol *title = sm_new_symbol("cannotIncNonSymbol", 18);
sm_string *message =
sm_new_fstring_at(sms_heap, "Cannot apply ++ to non-symbol. Object type is %s instead",
sm_type_name(sym->my_type));
RETURN_OBJ(((sm_object *)sm_new_error_from_expr(title, message, sme, NULL)));
}
eager_type_check(sme, 0, SM_UI8_TYPE, current_cx, sf);
sm_ui8 *num = (sm_ui8 *)return_obj;
if (num->my_type != SM_UI8_TYPE)
RETURN_OBJ(((sm_object *)num));
sm_ui8 *output = sm_new_ui8(num->value + 1);
sm_cx_set(current_cx, sym, (sm_object *)output);
RETURN_OBJ(((sm_object *)num));
break;
}
case SM_PREIDEC_EXPR: {
sm_symbol *sym = (sm_symbol *)sm_expr_get_arg(sme, 0);
if (sym->my_type != SM_SYMBOL_TYPE) {
sm_symbol *title = sm_new_symbol("cannotDecNonSymbol", 18);
sm_string *message =
sm_new_fstring_at(sms_heap, "Cannot apply -- to non-symbol. Object type is %s instead",
sm_type_name(sym->my_type));
RETURN_OBJ(((sm_object *)sm_new_error_from_expr(title, message, sme, NULL)));
}
eager_type_check(sme, 0, SM_UI8_TYPE, current_cx, sf);
sm_ui8 *num = (sm_ui8 *)return_obj;
if (num->my_type != SM_UI8_TYPE)
RETURN_OBJ(((sm_object *)num));
sm_ui8 *output = sm_new_ui8(num->value - 1);
sm_cx_set(current_cx, sym, (sm_object *)output);
RETURN_OBJ(((sm_object *)num));
break;
}
case SM_IF_EXPR: {
Expand Down
4 changes: 4 additions & 0 deletions src/main/object/sm_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ enum SM_EXPR_TYPE {
SM_ABS_EXPR,
SM_INC_EXPR,
SM_DEC_EXPR,
SM_PREINC_EXPR,
SM_PREDEC_EXPR,
SM_IINC_EXPR,
SM_IDEC_EXPR,
SM_PREIINC_EXPR,
SM_PREIDEC_EXPR,
SM_DIFF_EXPR,
SM_SIMP_EXPR,
SM_INT_EXPR,
Expand Down
4 changes: 4 additions & 0 deletions src/main/parser/sms.y
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,12 @@ EXPR : SELF { $$ = (sm_expr*)sm_new_self(); }
| INTEGER { $$ = (sm_expr*)sm_new_f64($1);}
| SYM INC { $$ = sm_new_expr(SM_INC_EXPR,(sm_object*)$1,_note()); }
| SYM DEC { $$ = sm_new_expr(SM_DEC_EXPR,(sm_object*)$1,_note()); }
| INC SYM { $$ = sm_new_expr(SM_PREINC_EXPR,(sm_object*)$2,_note()); }
| DEC SYM { $$ = sm_new_expr(SM_PREDEC_EXPR,(sm_object*)$2,_note()); }
| SYM IINC { $$ = sm_new_expr(SM_IINC_EXPR,(sm_object*)$1,_note()); }
| SYM IDEC { $$ = sm_new_expr(SM_IDEC_EXPR,(sm_object*)$1,_note()); }
| IINC SYM { $$ = sm_new_expr(SM_PREIINC_EXPR,(sm_object*)$2,_note()); }
| IDEC SYM { $$ = sm_new_expr(SM_PREIDEC_EXPR,(sm_object*)$2,_note()); }
| MINUS EXPR {
switch (((sm_object *)$2)->my_type) {
case SM_F64_TYPE:
Expand Down
31 changes: 17 additions & 14 deletions src/main/sm_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,14 @@ char *sm_global_fn_name(uint32_t which) {
"exp", // SM_EXP_EXPR
"sqrt", // SM_SQRT_EXPR
"abs", // SM_ABS_EXPR
"inc", // SM_INC_EXPR
"dec", // SM_DEC_EXPR
"iinc", // SM_IINC_EXPR
"idec", // SM_IDEC_EXPR
"++", // SM_INC_EXPR
"--", // SM_DEC_EXPR
"++", // SM_PREINC_EXPR
"--", // SM_PREDEC_EXPR
"!++", // SM_IINC_EXPR
"!--", // SM_IDEC_EXPR
"!++", // SM_PREIINC_EXPR
"!--", // SM_PREIDEC_EXPR
"diff", // SM_DIFF_EXPR
"simp", // SM_SIMP_EXPR
"int", // SM_INT_EXPR
Expand Down Expand Up @@ -301,16 +305,15 @@ char *sm_global_fn_name(uint32_t which) {
// Corresponding string length of the string that would come from the sm_global_fn_name(which)
uint32_t sm_global_fn_name_len(uint32_t which) {
static uint16_t response_len[] = {
8, 4, 4, 5, 4, 2, 2, 3, 3, 3, 9, 3, 9, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 3, 1, 1, 1, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 3, 3,
3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 2, 3, 3,
4, 3, 3, 3, 4, 4, 4, 4, 3, 2, 2, 3, 6, 6, 5, 3, 5, 10, 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, 8, 6, 1};

if (which >= sm_global_num_fns())
return 1; // "?"
Expand Down

0 comments on commit a286136

Please sign in to comment.