From e7c8a04078261b331b00490ffbffd9ff05d1e0d0 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 22 Jul 2020 23:49:52 +0200 Subject: Add variable to list value and map key --- src/bytecode.c | 2 +- src/parser.c | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/bytecode.c b/src/bytecode.c index b7c0364..9cf32a6 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -44,7 +44,7 @@ int tsl_bytecode_add_ins2(TslBytecode *self, TslOpcode opcode, double value) { TslInstructionType2 instruction; instruction.opcode = opcode; instruction.value = value; - fprintf(stderr, "%s %f\n", tsl_opcode_to_string(opcode), value); + fprintf(stderr, "%s %g\n", tsl_opcode_to_string(opcode), value); return tsl_buffer_append(&self->buffer, &instruction, sizeof(instruction)); } diff --git a/src/parser.c b/src/parser.c index e7fa99c..0c46f42 100644 --- a/src/parser.c +++ b/src/parser.c @@ -83,7 +83,17 @@ static TslParseResult tsl_parser_parse_map(TslParser *self, int *num_items) { *num_items = 0; for(;;) { TslToken token = tsl_tokenizer_next(&self->tokenizer); - if(token == TSL_TOKEN_NUM) { + if(token == TSL_TOKEN_STRING) { + ++*num_items; + return_if_error(tsl_bytecode_add_ins4(get_function_bytecode(self), TSL_OPCODE_LOADS, &self->tokenizer.string)); + parse_map_element_separator + } else if(token == TSL_TOKEN_IDENTIFIER) { + /* Use the identifier as a key for the map. This is not a variable, but a key (string) without spaces */ + /* This allows syntax like: variable = { key: "value" } */ + ++*num_items; + return_if_error(tsl_bytecode_add_ins4(get_function_bytecode(self), TSL_OPCODE_LOADS, &self->tokenizer.identifier)); + parse_map_element_separator + } else if(token == TSL_TOKEN_NUM) { ++*num_items; return_if_error(tsl_bytecode_add_ins2(get_function_bytecode(self), TSL_OPCODE_LOADN, self->tokenizer.number_value)); parse_map_element_separator @@ -95,10 +105,6 @@ static TslParseResult tsl_parser_parse_map(TslParser *self, int *num_items) { ++*num_items; return_if_error(tsl_bytecode_add_ins5(get_function_bytecode(self), TSL_OPCODE_LOADNULL)); parse_map_element_separator - } else if(token == TSL_TOKEN_STRING) { - ++*num_items; - return_if_error(tsl_bytecode_add_ins4(get_function_bytecode(self), TSL_OPCODE_LOADS, &self->tokenizer.string)); - parse_map_element_separator } else if(token == TSL_TOKEN_RBRACE) { /* '}' after trailing ',' or an empty map */ return TSL_PARSE_RESULT_OK; @@ -125,7 +131,11 @@ static TslParseResult tsl_parser_parse_list(TslParser *self, int *num_items) { for(;;) { /* TODO: Use tsl_parser_parse_rhs instead */ TslToken token = tsl_tokenizer_next(&self->tokenizer); - if(token == TSL_TOKEN_NUM) { + if(token == TSL_TOKEN_IDENTIFIER) { + ++*num_items; + return_if_error(tsl_bytecode_add_ins4(get_function_bytecode(self), TSL_OPCODE_LOADV, &self->tokenizer.identifier)); + parse_list_element_separator + } else if(token == TSL_TOKEN_NUM) { ++*num_items; return_if_error(tsl_bytecode_add_ins2(get_function_bytecode(self), TSL_OPCODE_LOADN, self->tokenizer.number_value)); parse_list_element_separator -- cgit v1.2.3