From 1aaf0c83b9cdd5855a3a2cbff7538ad36f55cc24 Mon Sep 17 00:00:00 2001 From: sijanec Date: Tue, 19 Jan 2021 13:43:26 +0100 Subject: added while loops, simple intager math --- src/bvrcommands.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 2 deletions(-) (limited to 'src/bvrcommands.c') diff --git a/src/bvrcommands.c b/src/bvrcommands.c index 9484e49..a3c5dba 100644 --- a/src/bvrcommands.c +++ b/src/bvrcommands.c @@ -272,11 +272,19 @@ int bvr_handle_equals(FILE * input, FILE * output) { int return_value = 0; char * string1 = bvr_var_get(item); char * string2 = bvr_var_get(value); + uuid_t binuuid; + uuid_generate_random(binuuid); + char uuid[37]; + char outputst[2]; + outputst[1] = '\0'; + uuid_unparse(binuuid, uuid); if(strcmp(string1, string2) == 0) { - fprintf(output, "1"); + outputst[0] = '1'; } else { - fprintf(output, "0"); + outputst[0] = '0'; } + return_value = bvr_var_set(uuid, outputst); + fprintf(output, "%s", uuid); free(item); free(value); item = NULL; @@ -338,6 +346,142 @@ int bvr_handle_if(FILE * input, FILE * output) { // ?f 1 <@this is all executed@ } return return_value; } +int bvr_handle_while(FILE * input, FILE * output) { + char chars_to_break_value[69] = " "; + strlcat(chars_to_break_value, BVR_CHARS_TO_BREAK_VALUE, sizeof(chars_to_break_value)); + char * item = bvr_commands_get_value(input, chars_to_break_value); + int return_value = 0; + char input_char = fgetc(input); + char previous_char = 'a'; + char * string1 = bvr_var_get(item); + FILE * codestream; + int depth = -1; // to increase to 0 after first <@ + int we_re_in_a_comment = 0; + char * code = malloc(sizeof(char)*2); + size_t code_index = 0; + size_t read_index = 0; + int in_string = 0; + while(1) { + if((input_char == BVR_CLOSING_COMMAND_TAG_CHAR_2 && previous_char == BVR_CLOSING_COMMAND_TAG_CHAR_1 && depth == 0 && + we_re_in_a_comment == 0 && in_string == 0)) { + break; + } + if(previous_char == BVR_OPENING_COMMAND_TAG_CHAR_1 && input_char == BVR_OPENING_COMMAND_TAG_CHAR_2) { + depth++; + } // this šubidubi doesn't account for <@ and @> in strings. + if(previous_char == BVR_CLOSING_COMMAND_TAG_CHAR_1 && input_char== BVR_CLOSING_COMMAND_TAG_CHAR_2) { + depth--; + } + if(previous_char == LINE_COMMAND_CHAR && input_char == BVR_BREAK_STRING_CHAR) { + in_string = 1; + } + if (previous_char != BVR_ESCAPE_CHAR && input_char == BVR_BREAK_STRING_CHAR && in_string == 1) { + in_string = 0; + } + if(previous_char == BVR_NEWLINE_CHAR && input_char == LINE_COMMENT_CHAR) { + we_re_in_a_comment = 1; + } + if(we_re_in_a_comment && input_char == BVR_NEWLINE_CHAR) { + we_re_in_a_comment = 0; + } + previous_char = input_char; + input_char = fgetc(input); + read_index++; + if (read_index >= 2) { + code[code_index++] = input_char; + code[code_index] = '\0'; + } + code = realloc(code, sizeof(char)*(2+code_index)); + if (code == NULL) { + fprintf(stderr, "segmentation fault caught (?) in bvr_handle_while, expect killing of the process soon. there was also a memory leak of around %lu bytes.\n", code_index+2); + return_value = FAILURE; + goto criticalfuckery; + } + } + code[strlen(code)-2] = '\0'; + code_index -= 2; + codestream = fmemopen(code, code_index, "r"); + while (atoi(string1) > 0) { /* basically the whole logic */ + rewind(codestream); + // fprintf(stderr, "debug: %s\n", code); + return_value = return_value != SUCCESS ? return_value : bvr_compose_stream(codestream, output); + string1 = bvr_var_get(item); + } + fclose(codestream); + criticalfuckery: + free(item); + item = NULL; + fflush(output); + return return_value; +} +int bvr_handle_math(FILE * input, FILE * output) { + char operation = fgetc(input); + int return_value = SUCCESS; + char chars_to_break_value[69] = " "; + strlcat(chars_to_break_value, BVR_CHARS_TO_BREAK_VALUE, sizeof(chars_to_break_value)); + char * item = bvr_commands_get_value(input, chars_to_break_value); + char * value = bvr_commands_get_value(input, chars_to_break_value); + long long int stvar1 = strtoll(bvr_var_get(item), NULL, 10); + long long int stvar2 = strtoll(bvr_var_get(value), NULL, 10); + char numberplaceholder[69]; + switch (operation) { + case '+': + stvar1 += stvar2; + break; + case '-': + stvar1 -= stvar2; + break; + case '*': + stvar1 *= stvar2; + break; + case '/': + if (stvar2 == 0) { + return_value = FAILURE; + fprintf(stderr, "division with zero is illegal, prevented, but raised an error. setting to highest value possible.\n"); + stvar1 = LLONG_MAX; + } else { + stvar1 /= stvar2; + } + break; + default: + return_value = FAILURE; + break; + } + snprintf(numberplaceholder, 69-1, "%lld", stvar1); + return_value = bvr_var_set(item, numberplaceholder); + free(item); + free(value); + item = NULL; + value = NULL; + return return_value; +} +int bvr_handle_explode(FILE * input, FILE * output) { + char chars_to_break_value[69] = " "; + strlcat(chars_to_break_value, BVR_CHARS_TO_BREAK_VALUE, sizeof(chars_to_break_value)); + char * item = bvr_commands_get_value(input, chars_to_break_value); + char * value = bvr_commands_get_value(input, chars_to_break_value); + int return_value = SUCCESS; + char * string1 = bvr_var_get(item); + char * string2 = bvr_var_get(value); + char * token; + char * rest = string1; + char * charpointer; + int index = strlen(item); + item[index++] = '['; + item[index] = '\0'; + index = 0; + while ((token = strtok_r(rest, string2, &rest))) { + charpointer = strrchr(item, BVR_ARRAY_INDEX_CHAR); + snprintf(charpointer+1, (BVR_MAX_VARIABLE_SIZE-(charpointer-string1))-4, "%d" BVR_ARRAY_AFTER_INDEX, index++); + return_value = return_value != SUCCESS ? return_value : bvr_var_set(charpointer, token); // če je bila prej napaka pač ne poskušamo več! + } + free(item); + free(value); + item = NULL; + value = NULL; + fflush(output); + return return_value; +} int bvr_handle_info(FILE * input, FILE * output) { // fprintf(stderr, "[bvrcommands.c] bvr_handle_info: bvr bVerbose HTPCMS %d.%d.%d\n", BVR_VER_MAJOR, BVR_VER_MINOR, BVR_VER_PATCH); fprintf(output, "\nbvr bVerbose HTPCMS %d.%d.%d\n", BVR_VER_MAJOR, BVR_VER_MINOR, BVR_VER_PATCH); -- cgit v1.2.3