diff --git a/lib/pinc_compiler/compiler.ml b/lib/pinc_compiler/compiler.ml index eb43857..de96ced 100644 --- a/lib/pinc_compiler/compiler.ml +++ b/lib/pinc_compiler/compiler.ml @@ -165,25 +165,28 @@ let emit_set_symbol t symbol = ;; let compile_string_template t s = - s - |> List.fold_left - (fun (t, index) template -> - let t = - match template.Pinc_Types.Ast.string_template_desc with - | StringInterpolation (Lowercase_Id (name, loc)) -> - let symbol = get_symbol t ~loc name in - emit_get_symbol t symbol - | StringText s -> emit_constant t (Pinc_Bytecode.Value.String s) - in - let t = - if index > 0 then - emit t @@ Pinc_Bytecode.Instruction.I_Concat - else - t - in - (t, succ index)) - (t, 0) - |> fst + match s with + | [] -> emit_constant t (Pinc_Bytecode.Value.String "") + | s -> + fst + @@ List.fold_left + (fun (t, index) template -> + let t = + match template.Pinc_Types.Ast.string_template_desc with + | StringInterpolation (Lowercase_Id (name, loc)) -> + let symbol = get_symbol t ~loc name in + emit_get_symbol t symbol + | StringText s -> emit_constant t (Pinc_Bytecode.Value.String s) + in + let t = + if index > 0 then + emit t @@ Pinc_Bytecode.Instruction.I_Concat + else + t + in + (t, succ index)) + (t, 0) + s ;; let rec compile_expr t (expr : Pinc_Types.Ast.expression) = diff --git a/test/vm/strings.pi b/test/vm/strings.pi index 70af512..32e3d00 100644 --- a/test/vm/strings.pi +++ b/test/vm/strings.pi @@ -10,3 +10,7 @@ component StringInterpolation { let subject = "World"; "Hello $(subject)!" } + +component EmptyString { + "" +} diff --git a/test/vm/strings.t b/test/vm/strings.t index 02fe376..8d2db05 100644 --- a/test/vm/strings.t +++ b/test/vm/strings.t @@ -6,3 +6,6 @@ $ NO_COLOR="1" print_vm . StringInterpolation Hello World! + + $ NO_COLOR="1" print_vm . EmptyString + diff --git a/test/vm/strings_instructions.t b/test/vm/strings_instructions.t index fbc9fd7..fca4099 100644 --- a/test/vm/strings_instructions.t +++ b/test/vm/strings_instructions.t @@ -37,3 +37,11 @@ 0023 I_Constant 0x00000002 (00000002) 0028 I_Concat 0029 I_Pop + + $ NO_COLOR="1" print_instructions . EmptyString + [CONSTANTS] + 0x00000000 (00000000) : "" + + [INSTRUCTIONS] + 0000 I_Constant 0x00000000 (00000000) + 0005 I_Pop