Skip to content

Commit 95744f2

Browse files
committed
Added string concat
"He" + "llo" "He" + 77
1 parent 81bb339 commit 95744f2

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

src/compiler.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,15 +1011,19 @@ void term() {
10111011
}
10121012

10131013
void additive_expr() {
1014-
term();
1014+
term(); // Parses multiplication/division first
10151015
while (curr.type == TK_PLUS || curr.type == TK_MINUS) {
10161016
MyloTokenType op = curr.type;
10171017
next_token();
10181018
term();
10191019
switch (op) {
1020-
case TK_PLUS: emit(OP_ADD);
1020+
case TK_PLUS:
1021+
// The VM's exec_math_op already checks if the
1022+
// operands are T_STR and performs concatenation.
1023+
emit(OP_ADD);
10211024
break;
1022-
case TK_MINUS: emit(OP_SUB);
1025+
case TK_MINUS:
1026+
emit(OP_SUB);
10231027
break;
10241028
default: break;
10251029
}
@@ -1166,11 +1170,8 @@ static void parse_region() {
11661170
static void parse_print() {
11671171
match(TK_PRINT);
11681172
match(TK_LPAREN);
1169-
if (curr.type == TK_STR) {
1170-
int id = make_string(compiling_vm, curr.text);
1171-
emit(OP_PSH_STR); emit(id);
1172-
match(TK_STR);
1173-
} else expression();
1173+
expression();
1174+
11741175
match(TK_RPAREN);
11751176
emit(OP_PRN);
11761177
}

src/vm.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,27 @@ static void exec_math_op(VM* vm, int op) {
10891089
broadcast_math(vm, op, arrVal, scalVal, scalType, objIsLhs);
10901090
return;
10911091
}
1092+
1093+
if (op == OP_ADD && (ta == T_STR || tb == T_STR)) {
1094+
char s1[MAX_STRING_LENGTH], s2[MAX_STRING_LENGTH];
1095+
1096+
// Convert Left Side
1097+
if (ta == T_STR) strncpy(s1, vm->string_pool[(int)a], MAX_STRING_LENGTH-1);
1098+
else snprintf(s1, MAX_STRING_LENGTH, "%g", a);
1099+
s1[MAX_STRING_LENGTH-1] = '\0';
1100+
1101+
// Convert Right Side
1102+
if (tb == T_STR) strncpy(s2, vm->string_pool[(int)b], MAX_STRING_LENGTH-1);
1103+
else snprintf(s2, MAX_STRING_LENGTH, "%g", b);
1104+
s2[MAX_STRING_LENGTH-1] = '\0';
1105+
1106+
char res[MAX_STRING_LENGTH * 2];
1107+
snprintf(res, sizeof(res), "%s%s", s1, s2);
1108+
int id = make_string(vm, res);
1109+
vm->stack[vm->sp] = (double)id;
1110+
vm->stack_types[vm->sp] = T_STR;
1111+
return;
1112+
}
10921113
RUNTIME_ERROR("Invalid types for math operation");
10931114
}
10941115

tests/test_generate_list.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,19 @@ inline TestOutput test_list_concat() {
462462
return run_source_test(src, expected);
463463
}
464464

465+
inline TestOutput test_string_concat() {
466+
467+
std::string src = """"
468+
"print(\"He\" + \"llo\")\n"
469+
"print(\"He\" + 77)\n";
470+
471+
std::string expected = """"
472+
"Hello\nHe77\n";
473+
474+
return run_source_test(src, expected);
475+
}
476+
477+
465478
inline TestOutput test_list_slices() {
466479

467480
std::string src = """"
@@ -1328,6 +1341,7 @@ inline void test_generate_list() {
13281341
ADD_TEST("Test Struct", test_struct);
13291342
ADD_TEST("Test Type Struct", test_typed_struct);
13301343
ADD_TEST("Test String Interpolation", test_string_interp);
1344+
ADD_TEST("Test String Concat", test_string_concat);
13311345
ADD_TEST("Test Scope", test_scope);
13321346
ADD_TEST("Test Variable Access", test_var_access);
13331347
ADD_TEST("Test Reverse Loop", test_literal_reverse);

0 commit comments

Comments
 (0)