Skip to content

Commit

Permalink
added strRepeat
Browse files Browse the repository at this point in the history
  • Loading branch information
reginaldford committed Jul 17, 2024
1 parent adb0f8d commit 89e5f1b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
11 changes: 4 additions & 7 deletions sms_src/examples/tgaMaker.sms
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@
let tga_data = header;

# Manually initialize pixel data with zeros
# TODO: create strRepeat("..", number)
for (let i = 0; i < pixel_count * 3; i++) {
tga_data = tga_data str+ "\x00";
};
tga_data = strRepeat("\x00",pixel_count*3);

# Build the pixel data
let offset = 18;
for (let y = 0; y <= height - 1; y++) {
for (let x = 0; x <= width - 1; x++) {
let color = (x * 255) / (width - 1);
tga_data = strMut(tga_data, offset, char(color));
tga_data = strMut(tga_data, offset + 1, char(color));
tga_data = strMut(tga_data, offset + 2, char(color));
strMut(tga_data, offset, char(color));
strMut(tga_data, offset + 1, char(color));
strMut(tga_data, offset + 2, char(color));
offset = offset + 3;
};
};
Expand Down
18 changes: 18 additions & 0 deletions src/main/engine/sm_ast_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,24 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
return (sm_object *)sm_new_double(str0->size);
break;
}
case SM_STR_REPEAT_EXPR: {
sm_string *str = (sm_string *)eager_type_check(sme, 0, SM_STRING_TYPE, current_cx, sf);
if (str->my_type == SM_ERR_TYPE)
return (sm_object *)str;
sm_double *reps = (sm_double *)eager_type_check(sme, 1, SM_DOUBLE_TYPE, current_cx, sf);
if (reps->my_type == SM_ERR_TYPE)
return (sm_object *)reps;
double repetitions = reps->value;
int original_size = str->size;
int new_size = (int)(original_size * repetitions);
sm_string *new_str = sm_new_string_manual(new_size);
char *content = &(new_str->content);
for (int i = 0; i < new_size; i += original_size) {
sm_strncpy(content + i, &(str->content), original_size);
}
return (sm_object *)new_str;
break;
}
case SM_ZEROS_EXPR: {
sm_double *num0 = (sm_double *)eager_type_check(sme, 0, SM_DOUBLE_TYPE, current_cx, sf);
if (num0->my_type != SM_DOUBLE_TYPE)
Expand Down
2 changes: 1 addition & 1 deletion src/main/parser/sms.l
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ symName {return SYM_NAME;}

newStr {return NEW_STR;}
"str\+" {return STR_CAT;}
strRpt {return STR_REPEAT;}
strRepeat {return STR_REPEAT;}
strEscape {return STR_ESCAPE;}
strUnescape {return STR_UNESCAPE;}
strSize {return STR_SIZE;}
Expand Down
3 changes: 2 additions & 1 deletion src/main/parser/sms.y
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ int parsing_fpath_len;
%token <expr> STR_SIZE
%token <expr> STR_MUT
%token <expr> STR_CAT
%token <expr> STR_REPEAT
%token <expr> STR_GET
%token <expr> STR_SET
%token <expr> STR_MAP
Expand All @@ -160,7 +161,6 @@ int parsing_fpath_len;
%token <expr> STR_GETCHAR
%token <expr> STR_TONUMS
%token <expr> STR_CMP
%token <expr> STR_REPEAT
%token <expr> STR_TOMEM
%token <expr> NEW_STR
%token <expr> TO_STRFMT
Expand Down Expand Up @@ -458,6 +458,7 @@ EXPR : SELF { $$ = (sm_expr*)sm_new_self(); }
| STR_FIND '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_STR_FIND_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| STR_CAT '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_STR_CAT_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| EXPR STR_CAT EXPR {$$ = sm_new_expr_2(SM_STR_CAT_EXPR,(sm_object*)$1,(sm_object*)$3, _note());}
| STR_REPEAT '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_STR_REPEAT_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| STR_SPLIT '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_STR_SPLIT_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| STR_PART '(' EXPR ',' EXPR ',' EXPR ')' {$$ = sm_new_expr_3(SM_STR_PART_EXPR,(sm_object*)$3,(sm_object*)$5,(sm_object*)$7, _note());}
| DATE_STR '(' ')' { $$ = sm_new_expr_n(SM_DATE_STR_EXPR,0,0, _note());}
Expand Down

0 comments on commit 89e5f1b

Please sign in to comment.