diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-07-22 23:49:52 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-22 23:49:52 +0200 |
commit | e7c8a04078261b331b00490ffbffd9ff05d1e0d0 (patch) | |
tree | 4df3cb197f6b3baab3bb0272acbf1e38d27eb68b /src | |
parent | e9e41c02d4309cd50b5584a8dfc0d7e01cb27e10 (diff) |
Add variable to list value and map key
Diffstat (limited to 'src')
-rw-r--r-- | src/bytecode.c | 2 | ||||
-rw-r--r-- | src/parser.c | 22 |
2 files changed, 17 insertions, 7 deletions
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 |