summaryrefslogtreecommitdiffhomepage
path: root/compiler.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-04-12 23:03:32 +0200
committerJo-Philipp Wich <jo@mein.io>2022-04-13 15:55:22 +0200
commite14b0993b101839d2d40b5c4f184e6b0c2083b65 (patch)
treec23ec0351ac1f08917604bfa824fa83d0ab5668c /compiler.c
parent23ddf91d6380da392c8eea7b7fe12c2cd687b6de (diff)
syntax: implement support for ES6 template literals
Implement support for ECMAScript 6 template literals which allow simple interpolation of variable values into strings without resorting to `sprintf()` or manual string concatenation. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'compiler.c')
-rw-r--r--compiler.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/compiler.c b/compiler.c
index 80b873d..d4725b1 100644
--- a/compiler.c
+++ b/compiler.c
@@ -33,6 +33,7 @@ static void uc_compiler_compile_paren(uc_compiler_t *compiler);
static void uc_compiler_compile_call(uc_compiler_t *compiler);
static void uc_compiler_compile_post_inc(uc_compiler_t *compiler);
static void uc_compiler_compile_constant(uc_compiler_t *compiler);
+static void uc_compiler_compile_template(uc_compiler_t *compiler);
static void uc_compiler_compile_comma(uc_compiler_t *compiler);
static void uc_compiler_compile_labelexpr(uc_compiler_t *compiler);
static void uc_compiler_compile_function(uc_compiler_t *compiler);
@@ -72,6 +73,7 @@ uc_compiler_parse_rules[TK_ERROR + 1] = {
[TK_NULL] = { uc_compiler_compile_constant, NULL, P_NONE },
[TK_THIS] = { uc_compiler_compile_constant, NULL, P_NONE },
[TK_REGEXP] = { uc_compiler_compile_constant, NULL, P_NONE },
+ [TK_TEMPLATE] = { uc_compiler_compile_template, NULL, P_NONE },
[TK_COMMA] = { NULL, uc_compiler_compile_comma, P_COMMA },
[TK_LABEL] = { uc_compiler_compile_labelexpr, NULL, P_NONE },
[TK_FUNC] = { uc_compiler_compile_function, NULL, P_NONE },
@@ -1484,6 +1486,27 @@ uc_compiler_compile_constant(uc_compiler_t *compiler)
}
static void
+uc_compiler_compile_template(uc_compiler_t *compiler)
+{
+ uc_compiler_emit_constant(compiler, compiler->parser->prev.pos, compiler->parser->prev.uv);
+
+ while (true) {
+ if (uc_compiler_parse_match(compiler, TK_TEMPLATE)) {
+ uc_compiler_emit_constant(compiler, compiler->parser->prev.pos, compiler->parser->prev.uv);
+ uc_compiler_emit_insn(compiler, 0, I_ADD);
+ }
+ else if (uc_compiler_parse_match(compiler, TK_PLACEH)) {
+ uc_compiler_compile_expression(compiler);
+ uc_compiler_emit_insn(compiler, 0, I_ADD);
+ uc_compiler_parse_consume(compiler, TK_RBRACE);
+ }
+ else {
+ break;
+ }
+ }
+}
+
+static void
uc_compiler_compile_comma(uc_compiler_t *compiler)
{
uc_compiler_emit_insn(compiler, 0, I_POP);