aboutsummaryrefslogtreecommitdiff
path: root/src/program.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/program.c')
-rw-r--r--src/program.c118
1 files changed, 61 insertions, 57 deletions
diff --git a/src/program.c b/src/program.c
index 71b310e..030d5aa 100644
--- a/src/program.c
+++ b/src/program.c
@@ -225,11 +225,6 @@ static TslProgramResult tsl_value_create_from_stack_value(TslProgram *self, TslV
dst->type = TSL_TYPE_NULL;
break;
}
- case TSL_STACK_VALUE_TYPE_COMMAND_ARG: {
- assert(0 && "This is a bug in the compiler. Unable to convert command arg to tsl value");
- abort();
- break;
- }
}
return TSL_PROGRAM_RESULT_OK;
}
@@ -378,31 +373,41 @@ static TslProgramResult tsl_program_run_function(TslProgram *self, int function_
}
case TSL_OPCODE_CALLF: {
size_t args_size = 1 + instruction_type1->value;
- TslStackValue *args = (TslStackValue*)tsl_buffer_end(&self->stack_values) - args_size;
+ TslStackValue *args;
assert(self->stack_values.size >= args_size);
{
- /* Resolved values pushed to stack and replace them in the stack */
- size_t prev_stack_size = self->stack_values.size;
+ /* Resolve args (which may have more than one stack value each) and then readd them onto the stack */
+
+ TslValue resolved_args[255];
+ size_t resolved_arg_index = 0;
TslStackValue *arg = (TslStackValue*)tsl_buffer_end(&self->stack_values) - 1;
- TslStackValue *args_begin = args - 1;
+ TslStackValue *args_begin = (TslStackValue*)tsl_buffer_end(&self->stack_values) - args_size - 1;
+ /* TODO: Support more than 255 args */
+ assert(self->stack_values.size / sizeof(TslStackValue) <= 255);
while(arg != args_begin) {
- TslValue new_value;
- cleanup_if_error(tsl_value_create_from_stack_value(self, &new_value));
- arg->data.variable = new_value;
- arg->type = TSL_STACK_VALUE_TYPE_VARIABLE;
+ cleanup_if_error(tsl_value_create_from_stack_value(self, &resolved_args[resolved_arg_index]));
+ ++resolved_arg_index;
--arg;
}
- /*
- Reset the stack size here because @tsl_value_create_from_stack_value which is called above pops the stack (without reallocation).
- TODO: Modify @tsl_value_create_from_stack_value to not pop stack and instead operate with stack pointer.
- */
- self->stack_values.size = prev_stack_size;
+
+ {
+ size_t i = 0;
+ for(; i < resolved_arg_index; ++i) {
+ TslStackValue stack_value;
+ stack_value.data.variable = resolved_args[i];
+ stack_value.type = TSL_STACK_VALUE_TYPE_VARIABLE;
+ tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value));
+ }
+ }
}
+
+ args = (TslStackValue*)tsl_buffer_end(&self->stack_values) - args_size;
assert(args[0].type == TSL_STACK_VALUE_TYPE_VARIABLE);
if(args[0].data.variable.type != TSL_TYPE_FUNCTION) {
fprintf(stderr, "Error: Unable to call a non-function type\n");
+ result = TSL_PROGRAM_RESULT_ERR;
goto cleanup;
}
@@ -453,53 +458,52 @@ static TslProgramResult tsl_program_run_function(TslProgram *self, int function_
instruction += sizeof(TslInstructionType5);
break;
}
- case TSL_OPCODE_LOADCA: {
- TslStackValue stack_value;
- stack_value.data.str = instruction_type4->value;
- stack_value.type = TSL_STACK_VALUE_TYPE_COMMAND_ARG;
- cleanup_if_error(tsl_buffer_append(&self->stack_values, &stack_value, sizeof(stack_value)));
- /*printf("loadca \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data);*/
- instruction += sizeof(TslInstructionType4);
- break;
- }
case TSL_OPCODE_CALLC: {
- TslStackValue *args_begin = tsl_buffer_pop(&self->stack_values, sizeof(TslStackValue) * instruction_type1->value);
- TslStackValue *args_end = args_begin + instruction_type1->value;
- TslStackValue *arg = args_begin;
char *command_args[255];
+ size_t command_arg_sizes[255];
char temp_null_save[255];
- size_t arg_index = 0;
- assert(instruction_type1->value > 0);
+ int arg_index = 254;
+
+
{
- /* TODO: Support infinite amount of args */
- assert(args_end - args_begin < 255);
- /*printf("Running command: ");*/
- while(arg != args_end) {
- assert(arg->type == TSL_STACK_VALUE_TYPE_COMMAND_ARG);
- temp_null_save[arg_index] = arg->data.str.data[arg->data.str.size];
- arg->data.str.data[arg->data.str.size] = '\0';
-
- command_args[arg_index] = arg->data.str.data;
- /*printf("%s ", command_args[arg_index]);*/
- ++arg_index;
- ++arg;
+ /* Resolve args (which may have more than one stack value each) and then readd them onto the stack */
+ int args_size = instruction_type1->value;
+ assert(args_size > 0);
+ /* TODO: Support more than 255 args */
+ assert(self->stack_values.size / sizeof(TslStackValue) <= 255);
+ for(; args_size > 0; --args_size) {
+ TslValue resolved_arg;
+ cleanup_if_error(tsl_value_create_from_stack_value(self, &resolved_arg));
+
+ switch(resolved_arg.type) {
+ case TSL_TYPE_STRING: {
+ --arg_index;
+ command_args[arg_index] = resolved_arg.data.string->data;
+ command_arg_sizes[arg_index] = resolved_arg.data.string->size;
+ break;
+ }
+ case TSL_TYPE_STRING_REF: {
+ --arg_index;
+ temp_null_save[arg_index] = resolved_arg.data.string_ref.data[resolved_arg.data.string_ref.size];
+ resolved_arg.data.string_ref.data[resolved_arg.data.string_ref.size] = '\0';
+ command_args[arg_index] = resolved_arg.data.string_ref.data;
+ command_arg_sizes[arg_index] = resolved_arg.data.string_ref.size;
+ break;
+ }
+ default:
+ fprintf(stderr, "Error: Unable to convert %s to a string in a command\n", "TODO");
+ result = TSL_PROGRAM_RESULT_ERR;
+ goto cleanup;
+ }
}
- command_args[arg_index] = NULL;
- /*printf("\n");*/
+ command_args[254] = NULL;
}
- tsl_command_exec(command_args, program_output_temp, NULL);
+ tsl_command_exec(command_args + arg_index, program_output_temp, NULL);
- {
- arg = args_begin;
- arg_index = 0;
- /* TODO: Support infinite amount of args */
- while(arg != args_end) {
- arg->data.str.data[arg->data.str.size] = temp_null_save[arg_index];
- ++arg_index;
- ++arg;
- }
- command_args[arg_index] = NULL;
+ /* Restore null terminate character switched. TODO: Only do this once at program launch, when tsl owns the source code buffer */
+ for(; arg_index < 254; ++arg_index) {
+ command_args[arg_index][command_arg_sizes[arg_index]] = temp_null_save[arg_index];
}
{