diff --git a/examples/input_and_compute.ltor b/examples/input_and_compute.ltor index ba62740..8fcaad8 100644 --- a/examples/input_and_compute.ltor +++ b/examples/input_and_compute.ltor @@ -3,4 +3,4 @@ set a to input_number() set b to input_number() set res to a+b -eval print_int(c) +print_int(res) diff --git a/examples/print_42.ltor b/examples/print_42.ltor index 308a9ce..b05e0cc 100644 --- a/examples/print_42.ltor +++ b/examples/print_42.ltor @@ -1,2 +1,3 @@ # just print 42 -eval print_number(42) +print_number(42) + diff --git a/examples/sequence.ltor b/examples/sequence.ltor new file mode 100644 index 0000000..b70df13 --- /dev/null +++ b/examples/sequence.ltor @@ -0,0 +1,5 @@ +set i to 0 +while !(i = 10) do + print_number(i) + set i to i+1 +end diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..ab3e764 --- /dev/null +++ b/src/config.h @@ -0,0 +1,6 @@ +#ifndef G_CONFIG_H_ +#define G_CONFIG_H_ + +#define G_DEBUG_LEVEL 0 + +#endif \ No newline at end of file diff --git a/src/evaluator.c b/src/evaluator.c index 007b9d1..85375c3 100644 --- a/src/evaluator.c +++ b/src/evaluator.c @@ -539,7 +539,7 @@ int evaluate(struct StateContainer* state, char* inputStr, int* resultPtr, unsig // identify token // first try a variable then a func name // not a float, check if this is a common function name - if (EVALUATOR_DEBUG_LEVEL >= 2) printf("now going to identify token '%s' \n"); + if (EVALUATOR_DEBUG_LEVEL >= 2) printf("now going to identify token '%s' \n", buff); if (EVALUATOR_DEBUG_LEVEL >= 2) var_store_print(state->varStore); int varKey = (int) var_store_get_key(state->varStore, buff); diff --git a/src/funcs.c b/src/funcs.c index ac64897..67f0d52 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -2,6 +2,7 @@ #include #include #include +#include "./config.h" #include "./types.h" #include "./utils.h" @@ -246,7 +247,7 @@ int execute_func(short funcID, short argsLen, unsigned char* argsTypes, int* arg return 1; } char* name = intros[funcID].name; - printf("Executing func '%s' \n", name); + if (G_DEBUG_LEVEL >= 2) printf("Executing func '%s' \n", name); if (argsLen < intros[funcID].nbArgs) { printf("ERR: Too few arguments for func call '%s' \n", name); return 1; @@ -260,7 +261,7 @@ int execute_func(short funcID, short argsLen, unsigned char* argsTypes, int* arg // first cast the function ptr impl(resPtr, resTypePtr, argsTypes, argsValues); - printf("Got %s \n", get_repr(*resTypePtr, resPtr)); + if (G_DEBUG_LEVEL >= 2) printf("Got %s \n", get_repr(*resTypePtr, resPtr)); return 0; } diff --git a/src/line_processing.c b/src/line_processing.c index 60baa43..16cc9f3 100644 --- a/src/line_processing.c +++ b/src/line_processing.c @@ -432,7 +432,7 @@ int process_line(struct StateContainer* state, char* str) for (int z = 0; z < nameLen; z++) { name[z] = str[setStatementParsing.name_start + z]; } - name[len] = '\0'; + name[nameLen] = '\0'; int res; byte resType; diff --git a/src/line_processing.h b/src/line_processing.h index 5fedb35..96e3e7b 100644 --- a/src/line_processing.h +++ b/src/line_processing.h @@ -2,7 +2,7 @@ #ifndef LINE_PROCESSING_H_ #define LINE_PROCESSING_H_ -#define LINE_PROCESSING_DEBUG_LEVEL 1 +#define LINE_PROCESSING_DEBUG_LEVEL 0 #define BLOCK_IF 2 #define BLOCK_WHILE 3 diff --git a/src/main.c b/src/main.c index 98c5510..38a06e9 100644 --- a/src/main.c +++ b/src/main.c @@ -1,45 +1,114 @@ #include #include #include +#include "./config.h" +#include "./types.h" +#include "./utils.h" #include "./stack.h" #include "./list.h" #include "./number_parsing.h" #include "./evaluator.h" -#include "./utils.h" -#include "./types.h" +#include "./state.h" +#include "./var_store.h" #include "./line_processing.h" -byte load_file(char* filename, char** resultPtr) { - FILE* f = fopen(filename, "rb"); +#define HELP_FLAG 1 +#define VERSION_FLAG 4 +#define INTERACTIVE_FLAG 8 +#define STDIN_EXP_FLAG 16 + +int help_mode() { + char* helpStr = "Usage: langatator [options] [file]\n" + " -h --help print this usage and exit\n" + " -v --version print version information and exit\n" + " -i --interactive force interactive mode\n" + " -e --stdin-expression evaluate expression from stdin\n"; + printf(helpStr); + return 0; +} + +int version_mode() { + char* helpStr = "Langatator 0.0.1\n"; + printf(helpStr); + return 0; +} + +int interactive_mode() { + printf("Interactive not implemented yet ¯\\_(ツ)_/¯ \n"); + return 1; +} + +int stdin_expression_mode() { + printf("This mode is not implemented yet ¯\\_(ツ)_/¯ \n"); + return 1; +} + +int file_mode(char* fileName) { + FILE* f = fopen(fileName, "rb"); if (!f) { - return 0; + printf(stderr, "Cannot load file '%s' status: %d\n", fileName, f); + return 1; } fseek(f, 0, SEEK_END); long length = ftell(f); - fseek (f, 0, SEEK_SET); - char* buffer = (char*) malloc(length); - if (!buffer) { - return 0; + fseek(f, 0, SEEK_SET); + + char* buff = (char*) malloc(length); + if (!buff) { + printf(stderr, "Could not allocate buffer to process file content\n"); + return 1; } - if (buffer) { - fread(buffer, 1, length, f); + if (buff) { + fread(buff, 1, length, f); } fclose(f); - *resultPtr = buffer; + struct StateContainer* state = state_init(); + process_script(state, buff); - return 1; + return 0; } int main (int argc, char** argv) { - // if (argc == 1) { - // printf("One arg '%s'", argv[0]); + int flags = INTERACTIVE_FLAG; - // return 0; - // } + int nonFlagArgumentCount = 0; + int nonFlagArgumentPos = 0; - // printf("Invalid usage use --help to document your self\n"); - printf("CLI not implemented yet ¯\\_(ツ)_/¯ \n"); + char* cmdName = argv[0]; + for (int i = 1; i < argc; i++) { + if (str_starts_with("--help", argv[i]) || str_starts_with("-h", argv[i])) { + flags = HELP_FLAG; + break; + } + if (str_starts_with("--version", argv[i]) || str_starts_with("-v", argv[i])) { + flags = VERSION_FLAG; + break; + } + if (str_starts_with("--interactive", argv[i]) || str_starts_with("-i", argv[i])) { + flags = INTERACTIVE_FLAG; + break; + } + if (str_starts_with("--stdin-expression", argv[i]) || str_starts_with("-e", argv[i])) { + flags = STDIN_EXP_FLAG; + break; + } + // no flag match + if (str_starts_with("--", argv[i])) { + printf(stderr, "Invalid flag '%s'\n", argv[i]); + return 1; + } + nonFlagArgumentCount++; + nonFlagArgumentPos = i; + } + if (nonFlagArgumentCount == 1) return file_mode(argv[nonFlagArgumentPos]); + + if (flags & HELP_FLAG) return help_mode(); + if (flags & VERSION_FLAG) return version_mode(); + if (flags & INTERACTIVE_FLAG) return interactive_mode(); + if (flags & STDIN_EXP_FLAG) return stdin_expression_mode(); + + help_mode(); return 1; } diff --git a/src/utils.h b/src/utils.h index 5d4320c..e8134ee 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,3 +1,5 @@ +#include "./config.h" + #ifndef UTILS_H_ #define UTILS_H_ diff --git a/src/var_store.h b/src/var_store.h index ee18725..9140390 100644 --- a/src/var_store.h +++ b/src/var_store.h @@ -61,4 +61,6 @@ byte var_store_set(struct VariableStore* store, char* varName, byte type, void* byte var_store_delete(struct VariableStore* store, char* varName); +void var_store_print(struct VariableStore* store); + #endif