From ef263d9cc436a16c2865bcd656fd47cae25bfebb Mon Sep 17 00:00:00 2001 From: zongor Date: Sat, 4 Mar 2023 13:20:59 -0500 Subject: [PATCH] add & float, init tok map --- .clang-format | 2 +- .vscode/launch.json | 16 + Makefile | 4 +- compile_test.c | 145 +++++++++ compiler.c | 606 ++++++++++++++++++++--------------- main.c | 42 ++- mkfile | 19 ++ tokenizer.c | 766 ++++++++++++++++++++++---------------------- 8 files changed, 924 insertions(+), 676 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 compile_test.c create mode 100644 mkfile diff --git a/.clang-format b/.clang-format index 6a844b4..a6689aa 100644 --- a/.clang-format +++ b/.clang-format @@ -1 +1 @@ -BasedOnStyle: GNU \ No newline at end of file +BasedOnStyle: Mozilla \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d750cf2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/varaq", + "args": ["tests/add.vq", "public/module.wasm"], + "cwd": "${workspaceFolder}" + } + ] +} diff --git a/Makefile b/Makefile index a277148..a212db6 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,7 @@ CC = gcc CCFLAGS= -g -all: varaq - -varaq: +all: $(CC) $(CCFLAGS) -o varaq main.c compiler.c tokenizer.c clean: diff --git a/compile_test.c b/compile_test.c new file mode 100644 index 0000000..2ab9019 --- /dev/null +++ b/compile_test.c @@ -0,0 +1,145 @@ +#include "compiler.h" + +#include "common.h" +#include "tokenizer.h" + +Code * +demo_function_compile () +{ + Code *add_args_code = (Code *)calloc (1, sizeof (Code)); + append_byte (add_args_code, f32); + append_byte (add_args_code, f32); + + Code *add_return_code = (Code *)calloc (1, sizeof (Code)); + append_byte (add_return_code, f32); + + Code *add_function_type = (Code *)calloc (1, sizeof (Code)); + append_byte (add_function_type, FUNCTION); + + append (add_function_type, encodeVector (add_args_code)); + append (add_function_type, encodeVector (add_return_code)); + add_function_type->override = true; + add_function_type->override_count = 1; + + Code *type_section = createSection (TYPE, encodeVector (add_function_type)); + + Code *return_type_code = (Code *)calloc (1, sizeof (Code)); + append_byte (return_type_code, 0x00); + return_type_code->override = true; + return_type_code->override_count = 1; + + Code *func_section = createSection (FUNC, encodeVector (return_type_code)); + + Code *exp = encodeString ("main"); + append_byte (exp, EXPORT_FUNC); + append_byte (exp, 0x00); + exp->override = true; + exp->override_count = 1; + Code *export_section = createSection (EXPORT, encodeVector (exp)); + + Code *code = (Code *)calloc (1, sizeof (Code)); + append_byte (code, LOCAL_GET); + append (code, unsignedLEB128 (0)); + append_byte (code, LOCAL_GET); + append (code, unsignedLEB128 (1)); + append_byte (code, F32_ADD); + + Code *body = (Code *)calloc (1, sizeof (Code)); + append_byte (body, EMPTY_ARRAY); + append (body, code); + append_byte (body, END); + + Code *function_body = (Code *)calloc (1, sizeof (Code)); + append (function_body, encodeVector (body)); + function_body->override = true; + function_body->override_count = 1; + Code *code_section = createSection (CODE, encodeVector (function_body)); + + Code *tape = (Code *)malloc (sizeof (Code)); + tape->cells = calloc (8, sizeof (uint8_t)); + tape->cells[0] = 0; + tape->cells[1] = 'a'; + tape->cells[2] = 's'; + tape->cells[3] = 'm'; + tape->cells[4] = 1; + tape->cells[5] = 0; + tape->cells[6] = 0; + tape->cells[7] = 0; + tape->count = 8; + + append (tape, type_section); + append (tape, func_section); + append (tape, export_section); + append (tape, code_section); + + return tape; +} + +Code * +demo_add_compile () +{ + Code *add_args_code = (Code *)calloc (1, sizeof (Code)); + Code *add_return_code = (Code *)calloc (1, sizeof (Code)); + append_byte (add_return_code, f64); + + Code *add_function_type = (Code *)calloc (1, sizeof (Code)); + append_byte (add_function_type, FUNCTION); + + append (add_function_type, encodeVector (add_args_code)); + append (add_function_type, encodeVector (add_return_code)); + add_function_type->override = true; + add_function_type->override_count = 1; + + Code *type_section = createSection (TYPE, encodeVector (add_function_type)); + + Code *return_type_code = (Code *)calloc (1, sizeof (Code)); + append_byte (return_type_code, 0x00); + return_type_code->override = true; + return_type_code->override_count = 1; + + Code *func_section = createSection (FUNC, encodeVector (return_type_code)); + + Code *exp = encodeString ("main"); + append_byte (exp, EXPORT_FUNC); + append_byte (exp, 0x00); + exp->override = true; + exp->override_count = 1; + Code *export_section = createSection (EXPORT, encodeVector (exp)); + + Code *code = (Code *)calloc (1, sizeof (Code)); + append_byte (code, F64_CONST); + append_f64 (code, 6.7); + append_byte (code, F64_CONST); + append_f64 (code, 8.5); + append_byte (code, F64_ADD); + + Code *body = (Code *)calloc (1, sizeof (Code)); + append_byte (body, EMPTY_ARRAY); + append (body, code); + append_byte (body, END); + + Code *function_body = (Code *)calloc (1, sizeof (Code)); + append (function_body, encodeVector (body)); + function_body->override = true; + function_body->override_count = 1; + Code *code_section = createSection (CODE, encodeVector (function_body)); + + Code *tape = (Code *)malloc (sizeof (Code)); + tape->cells = calloc (8, sizeof (uint8_t)); + tape->cells[0] = 0; + tape->cells[1] = 'a'; + tape->cells[2] = 's'; + tape->cells[3] = 'm'; + tape->cells[4] = 1; + tape->cells[5] = 0; + tape->cells[6] = 0; + tape->cells[7] = 0; + tape->count = 8; + + append (tape, type_section); + append (tape, func_section); + append (tape, export_section); + append (tape, code_section); + + return tape; +} \ No newline at end of file diff --git a/compiler.c b/compiler.c index 20f627b..6434932 100644 --- a/compiler.c +++ b/compiler.c @@ -3,338 +3,416 @@ #include "common.h" #include "tokenizer.h" -Code * -signedLEB128 (size_t num) +Code* +signedLEB128(size_t num) { bool more = true; - Code *buffer = (Code *)malloc (sizeof (Code)); + Code* buffer = (Code*)malloc(sizeof(Code)); buffer->count = 0; int n = (int)num; - while (more) - { - uint8_t byte = n & 0x7f; - n >>= 7; - if ((n == 0 && (byte & 0x40) == 0) || (n == -1 && (byte & 0x40) != 0)) - { - more = false; - } - else - { - byte |= 0x80; - } - - size_t old_count = buffer->count; - - uint8_t *tmp = (uint8_t *)calloc ( - (old_count + 1), - sizeof (uint8_t)); // really slow and bad refactor later - memcpy (tmp, buffer->cells, buffer->count * sizeof (uint8_t)); - if (tmp) - { - buffer->cells = tmp; - } - buffer->cells[old_count] = byte; - buffer->count += 1; + while (more) { + uint8_t byte = n & 0x7f; + n >>= 7; + if ((n == 0 && (byte & 0x40) == 0) || (n == -1 && (byte & 0x40) != 0)) { + more = false; + } else { + byte |= 0x80; } + size_t old_count = buffer->count; + + uint8_t* tmp = + (uint8_t*)calloc((old_count + 1), + sizeof(uint8_t)); // really slow and bad refactor later + memcpy(tmp, buffer->cells, buffer->count * sizeof(uint8_t)); + if (tmp) { + buffer->cells = tmp; + } + buffer->cells[old_count] = byte; + buffer->count += 1; + } + return buffer; } -Code * -unsignedLEB128 (size_t num) +Code* +unsignedLEB128(size_t num) { - Code *buffer = (Code *)malloc (sizeof (Code)); + Code* buffer = (Code*)malloc(sizeof(Code)); buffer->count = 0; int n = (int)num; - do - { - uint8_t byte = n & 0x7f; - n >>= 7; - if (n != 0) - { - byte |= 0x80; - } - - size_t old_count = buffer->count; - - uint8_t *tmp = (uint8_t *)calloc ( - (old_count + 1), - sizeof (uint8_t)); // really slow and bad refactor later - memcpy (tmp, buffer->cells, buffer->count * sizeof (uint8_t)); - if (tmp) - { - buffer->cells = tmp; - } - buffer->cells[old_count] = byte; - buffer->count += 1; + do { + uint8_t byte = n & 0x7f; + n >>= 7; + if (n != 0) { + byte |= 0x80; } - while (n != 0); + + size_t old_count = buffer->count; + + uint8_t* tmp = + (uint8_t*)calloc((old_count + 1), + sizeof(uint8_t)); // really slow and bad refactor later + memcpy(tmp, buffer->cells, buffer->count * sizeof(uint8_t)); + if (tmp) { + buffer->cells = tmp; + } + buffer->cells[old_count] = byte; + buffer->count += 1; + } while (n != 0); return buffer; } -Code * -append_byte (Code *tape, uint8_t data) +Code* +generic_append(Code* tape, void* data, size_t data_size) { size_t old_count = tape->count; - uint8_t *tmp = (uint8_t *)calloc ((old_count + 1), sizeof (uint8_t)); - if (old_count > 0) - { - memcpy (tmp, tape->cells, tape->count * sizeof (uint8_t)); - } - if (tmp) - { - tape->cells = tmp; - } - tape->cells[old_count] = data; - tape->count += 1; - return tape; -} -Code * -append_f64 (Code *tape, double data) -{ - size_t old_count = tape->count; + uint8_t* tmp = (uint8_t*)calloc((old_count + data_size), sizeof(uint8_t)); + if (old_count > 0) { + memcpy(tmp, tape->cells, tape->count * sizeof(uint8_t)); + } + if (tmp) { + tape->cells = tmp; + } - uint8_t *tmp - = (uint8_t *)calloc ((old_count + sizeof (data)), sizeof (uint8_t)); - if (old_count > 0) - { - memcpy (tmp, tape->cells, tape->count * sizeof (uint8_t)); - } - if (tmp) - { - tape->cells = tmp; - } - - memcpy ((tape->cells + old_count), (unsigned char *)&data, sizeof (data)); - tape->count += sizeof (data); + memcpy((tape->cells + old_count), (unsigned char*)data, data_size); + tape->count += data_size; return tape; } -Code * -append (Code *tape, Code *data) +Code* +append_byte(Code* tape, uint8_t data) +{ + return generic_append(tape, &data, sizeof(uint8_t)); +} + +Code* +append_f64(Code* tape, double data) +{ + return generic_append(tape, &data, sizeof(double)); +} + +Code* +append(Code* tape, Code* data) { size_t old_count = tape->count; - uint8_t *tmp - = (uint8_t *)calloc ((old_count + data->count), sizeof (uint8_t)); - memcpy (tmp, tape->cells, tape->count * sizeof (uint8_t)); - if (tmp) - { - tape->cells = tmp; - } + uint8_t* tmp = (uint8_t*)calloc((old_count + data->count), sizeof(uint8_t)); + memcpy(tmp, tape->cells, tape->count * sizeof(uint8_t)); + if (tmp) { + tape->cells = tmp; + } - memcpy ((tape->cells + old_count), data->cells, - data->count * sizeof (uint8_t)); + memcpy((tape->cells + old_count), data->cells, data->count * sizeof(uint8_t)); tape->count += data->count; return tape; } -Code * -encodeString (char *string) +Code* +encodeString(char* string) { - Code *buffer = (Code *)malloc (sizeof (Code)); - buffer->cells = (uint8_t *)malloc (sizeof (uint8_t)); - buffer->cells[0] = strlen (string); + Code* buffer = (Code*)malloc(sizeof(Code)); + buffer->cells = (uint8_t*)malloc(sizeof(uint8_t)); + buffer->cells[0] = strlen(string); buffer->count = 1; - uint8_t *tmp = (uint8_t *)malloc ((1 + strlen (string)) * sizeof (char)); - memcpy (tmp, buffer->cells, buffer->count * sizeof (uint8_t)); - if (tmp) - { - buffer->cells = tmp; - } + uint8_t* tmp = (uint8_t*)malloc((1 + strlen(string)) * sizeof(char)); + memcpy(tmp, buffer->cells, buffer->count * sizeof(uint8_t)); + if (tmp) { + buffer->cells = tmp; + } - memcpy ((buffer->cells + 1), string, strlen (string) * sizeof (char)); - buffer->count += strlen (string); + memcpy((buffer->cells + 1), string, strlen(string) * sizeof(char)); + buffer->count += strlen(string); return buffer; } -Code * -encodeVector (Code *data) +Code* +encodeVector(Code* data) { size_t count = data->override ? data->override_count : data->count; - Code *buffer = unsignedLEB128 (count); - append (buffer, data); + Code* buffer = unsignedLEB128(count); + append(buffer, data); return buffer; } -Code * -createSection (uint8_t section, Code *data) +Code* +createSection(uint8_t section, Code* data) { - Code *buffer = (Code *)malloc (sizeof (Code)); - buffer->cells = (uint8_t *)malloc (sizeof (uint8_t)); + Code* buffer = (Code*)malloc(sizeof(Code)); + buffer->cells = (uint8_t*)malloc(sizeof(uint8_t)); buffer->cells[0] = section; buffer->count = 1; - return append (buffer, encodeVector (data)); + return append(buffer, encodeVector(data)); } -Code * -demo_function_compile () +Code* +compile(char* buffer) { - Code *add_args_code = (Code *)calloc (1, sizeof (Code)); - append_byte (add_args_code, f32); - append_byte (add_args_code, f32); + char number[100]; - Code *add_return_code = (Code *)calloc (1, sizeof (Code)); - append_byte (add_return_code, f32); + Code* add_args_code = (Code*)calloc(1, sizeof(Code)); + Code* add_return_code = (Code*)calloc(1, sizeof(Code)); + append_byte(add_return_code, f64); - Code *add_function_type = (Code *)calloc (1, sizeof (Code)); - append_byte (add_function_type, FUNCTION); + Code* add_function_type = (Code*)calloc(1, sizeof(Code)); + append_byte(add_function_type, FUNCTION); - append (add_function_type, encodeVector (add_args_code)); - append (add_function_type, encodeVector (add_return_code)); + append(add_function_type, encodeVector(add_args_code)); + append(add_function_type, encodeVector(add_return_code)); add_function_type->override = true; add_function_type->override_count = 1; - Code *type_section = createSection (TYPE, encodeVector (add_function_type)); + Code* type_section = createSection(TYPE, encodeVector(add_function_type)); - Code *return_type_code = (Code *)calloc (1, sizeof (Code)); - append_byte (return_type_code, 0x00); + Code* return_type_code = (Code*)calloc(1, sizeof(Code)); + append_byte(return_type_code, 0x00); return_type_code->override = true; return_type_code->override_count = 1; - Code *func_section = createSection (FUNC, encodeVector (return_type_code)); + Code* func_section = createSection(FUNC, encodeVector(return_type_code)); - Code *exp = encodeString ("main"); - append_byte (exp, EXPORT_FUNC); - append_byte (exp, 0x00); + Code* exp = encodeString("main"); + append_byte(exp, EXPORT_FUNC); + append_byte(exp, 0x00); exp->override = true; exp->override_count = 1; - Code *export_section = createSection (EXPORT, encodeVector (exp)); + Code* export_section = createSection(EXPORT, encodeVector(exp)); - Code *code = (Code *)calloc (1, sizeof (Code)); - append_byte (code, LOCAL_GET); - append (code, unsignedLEB128 (0)); - append_byte (code, LOCAL_GET); - append (code, unsignedLEB128 (1)); - append_byte (code, F32_ADD); + // Code* code = (Code*)calloc(1, sizeof(Code)); + // append_byte(code, F64_CONST); + // append_f64(code, 1.0); + // append_byte(code, F64_CONST); + // append_f64(code, 2.0); + // append_byte(code, F64_ADD); - Code *body = (Code *)calloc (1, sizeof (Code)); - append_byte (body, EMPTY_ARRAY); - append (body, code); - append_byte (body, END); - - Code *function_body = (Code *)calloc (1, sizeof (Code)); - append (function_body, encodeVector (body)); - function_body->override = true; - function_body->override_count = 1; - Code *code_section = createSection (CODE, encodeVector (function_body)); - - Code *tape = (Code *)malloc (sizeof (Code)); - tape->cells = calloc (8, sizeof (uint8_t)); - tape->cells[0] = 0; - tape->cells[1] = 'a'; - tape->cells[2] = 's'; - tape->cells[3] = 'm'; - tape->cells[4] = 1; - tape->cells[5] = 0; - tape->cells[6] = 0; - tape->cells[7] = 0; - tape->count = 8; - - append (tape, type_section); - append (tape, func_section); - append (tape, export_section); - append (tape, code_section); - - return tape; -} - -Code * -demo_add_compile () -{ - Code *add_args_code = (Code *)calloc (1, sizeof (Code)); - Code *add_return_code = (Code *)calloc (1, sizeof (Code)); - append_byte (add_return_code, f64); - - Code *add_function_type = (Code *)calloc (1, sizeof (Code)); - append_byte (add_function_type, FUNCTION); - - append (add_function_type, encodeVector (add_args_code)); - append (add_function_type, encodeVector (add_return_code)); - add_function_type->override = true; - add_function_type->override_count = 1; - - Code *type_section = createSection (TYPE, encodeVector (add_function_type)); - - Code *return_type_code = (Code *)calloc (1, sizeof (Code)); - append_byte (return_type_code, 0x00); - return_type_code->override = true; - return_type_code->override_count = 1; - - Code *func_section = createSection (FUNC, encodeVector (return_type_code)); - - Code *exp = encodeString ("main"); - append_byte (exp, EXPORT_FUNC); - append_byte (exp, 0x00); - exp->override = true; - exp->override_count = 1; - Code *export_section = createSection (EXPORT, encodeVector (exp)); - - Code *code = (Code *)calloc (1, sizeof (Code)); - append_byte (code, F64_CONST); - append_f64 (code, 6.7); - append_byte (code, F64_CONST); - append_f64 (code, 8.5); - append_byte (code, F64_ADD); - - Code *body = (Code *)calloc (1, sizeof (Code)); - append_byte (body, EMPTY_ARRAY); - append (body, code); - append_byte (body, END); - - Code *function_body = (Code *)calloc (1, sizeof (Code)); - append (function_body, encodeVector (body)); - function_body->override = true; - function_body->override_count = 1; - Code *code_section = createSection (CODE, encodeVector (function_body)); - - Code *tape = (Code *)malloc (sizeof (Code)); - tape->cells = calloc (8, sizeof (uint8_t)); - tape->cells[0] = 0; - tape->cells[1] = 'a'; - tape->cells[2] = 's'; - tape->cells[3] = 'm'; - tape->cells[4] = 1; - tape->cells[5] = 0; - tape->cells[6] = 0; - tape->cells[7] = 0; - tape->count = 8; - - append (tape, type_section); - append (tape, func_section); - append (tape, export_section); - append (tape, code_section); - - return tape; -} - -Code * -compile (char *buffer) -{ - Code *tape = (Code *)malloc (sizeof (Code)); - tape->cells = calloc (8, sizeof (uint8_t)); - tape->cells[0] = 0; - tape->cells[1] = 'a'; - tape->cells[2] = 's'; - tape->cells[3] = 'm'; - tape->cells[4] = 1; - tape->cells[5] = 0; - tape->cells[6] = 0; - tape->cells[7] = 0; - tape->count = 8; - - initTokenizer (buffer); - Token t = nextToken (); - while (t.type != TOKEN_EOF) - { - debug_printToken (t); - t = nextToken (); + Code* code = (Code*)calloc(1, sizeof(Code)); + initTokenizer(buffer); + Token t = nextToken(); + do { + // debug_printToken(t); + switch (t.type) { + case TOKEN_LEFT_PAREN: + break; + case TOKEN_RIGHT_PAREN: + break; + case TOKEN_LEFT_BRACE: + break; + case TOKEN_RIGHT_BRACE: + break; + case TOKEN_TILDE: + break; + case TOKEN_SLASH: + break; + case TOKEN_MINUS: + break; + case TOKEN_IDENTIFIER: + break; + case TOKEN_STRING: + break; + case TOKEN_FLOAT: + strncpy(number, t.start, t.length); + number[t.length + 1] = '\0'; + append_byte(code, F64_CONST); + append_f64(code, strtod(number, NULL)); + break; + case TOKEN_LIST: + break; + case TOKEN_ERROR: + break; + case TOKEN_FALSE: + break; + case TOKEN_TRUE: + break; + case TOKEN_PI: + break; + case TOKEN_E: + break; + case TOKEN_EOF: + break; + case TOKEN_POP: + break; + case TOKEN_DUP: + break; + case TOKEN_EXCH: + break; + case TOKEN_CLEAR: + break; + case TOKEN_REMEMBER: + break; + case TOKEN_FORGET: + break; + case TOKEN_DUMP: + break; + case TOKEN_NAME: + break; + case TOKEN_SET: + break; + case TOKEN_IFYES: + break; + case TOKEN_IFNO: + break; + case TOKEN_CHOOSE: + break; + case TOKEN_EVAL: + break; + case TOKEN_ESCAPE: + break; + case TOKEN_REPEAT: + break; + case TOKEN_SPLIT: + break; + case TOKEN_CONS: + break; + case TOKEN_SHATTER: + break; + case TOKEN_EMPTY: + break; + case TOKEN_COMPOSE: + break; + case TOKEN_STREQ: + break; + case TOKEN_STRCUT: + break; + case TOKEN_STRMEASURE: + break; + case TOKEN_STRTIE: + break; + case TOKEN_EXPLODE: + break; + case TOKEN_ADD: + append_byte(code, F64_ADD); + break; + case TOKEN_SUB: + break; + case TOKEN_MUL: + break; + case TOKEN_DIV: + break; + case TOKEN_IDIV: + break; + case TOKEN_MOD: + break; + case TOKEN_POW: + break; + case TOKEN_SQRT: + break; + case TOKEN_ADD1: + break; + case TOKEN_SUB1: + break; + case TOKEN_SIN: + break; + case TOKEN_COS: + break; + case TOKEN_TAN: + break; + case TOKEN_ATAN: + break; + case TOKEN_LN: + break; + case TOKEN_LOG: + break; + case TOKEN_LOG3: + break; + case TOKEN_CLIP: + break; + case TOKEN_SMOOTH: + break; + case TOKEN_HOWMUCH: + break; + case TOKEN_SETRAND: + break; + case TOKEN_RAND: + break; + case TOKEN_INT: + break; + case TOKEN_NUMBERIZE: + break; + case TOKEN_ISOLATE: + break; + case TOKEN_MIX: + break; + case TOKEN_CONTRADICT: + break; + case TOKEN_COMPL: + break; + case TOKEN_SHIFTRIGHT: + break; + case TOKEN_SHIFTLEFT: + break; + case TOKEN_GT: + break; + case TOKEN_LT: + break; + case TOKEN_EQ: + break; + case TOKEN_GE: + break; + case TOKEN_LE: + break; + case TOKEN_NE: + break; + case TOKEN_NULL: + break; + case TOKEN_NEGATIVE: + break; + case TOKEN_ISNULL: + break; + case TOKEN_ISINT: + break; + case TOKEN_ISNUMBER: + break; + case TOKEN_AND: + break; + case TOKEN_OR: + break; + case TOKEN_XOR: + break; + case TOKEN_DISP: + break; + case TOKEN_LISTEN: + break; + case TOKEN_COMPLAIN: + break; + case TOKEN_TIME: + break; + case TOKEN_GARBAGE_COLLECT: + break; } + t = nextToken(); + } while (t.type != TOKEN_EOF); + Code* body = (Code*)calloc(1, sizeof(Code)); + append_byte(body, EMPTY_ARRAY); + append(body, code); + append_byte(body, END); + + Code* function_body = (Code*)calloc(1, sizeof(Code)); + append(function_body, encodeVector(body)); + function_body->override = true; + function_body->override_count = 1; + Code* code_section = createSection(CODE, encodeVector(function_body)); + + Code* tape = (Code*)malloc(sizeof(Code)); + tape->cells = calloc(8, sizeof(uint8_t)); + tape->cells[0] = 0; + tape->cells[1] = 'a'; + tape->cells[2] = 's'; + tape->cells[3] = 'm'; + tape->cells[4] = 1; + tape->cells[5] = 0; + tape->cells[6] = 0; + tape->cells[7] = 0; + tape->count = 8; + + append(tape, type_section); + append(tape, func_section); + append(tape, export_section); + append(tape, code_section); return tape; } diff --git a/main.c b/main.c index abe5fa0..bf818cf 100644 --- a/main.c +++ b/main.c @@ -3,35 +3,33 @@ #include "tokenizer.h" int -main (int argc, char **argv) +main(int argc, char** argv) { - if (argc != 3) - { - printf ("usage: varaq input.vq output.wasm"); - return EXIT_FAILURE; - } + if (argc != 3) { + printf("usage: varaq input.vq output.wasm"); + return EXIT_FAILURE; + } - char *buffer; + char* buffer; int length = 0; - int sp = open (argv[1], O_RDONLY); + int sp = open(argv[1], O_RDONLY); - if (sp) - { - length = lseek (sp, (size_t)0, SEEK_END); - lseek (sp, (size_t)0, SEEK_SET); - buffer = malloc (length); - if (buffer) - { - read (sp, buffer, length * sizeof (char)); - } - close (sp); + if (sp) { + length = lseek(sp, (size_t)0, SEEK_END); + lseek(sp, (size_t)0, SEEK_SET); + buffer = malloc(length); + if (buffer) { + read(sp, buffer, length * sizeof(char)); } + close(sp); + } - Code *prog = compile (buffer); + initMap(); + Code* prog = compile(buffer); - int tp = open (argv[2], O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH); - write (tp, prog->cells, prog->count); - close (tp); + int tp = open(argv[2], O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH); + write(tp, prog->cells, prog->count); + close(tp); return EXIT_SUCCESS; } diff --git a/mkfile b/mkfile new file mode 100644 index 0000000..b052e98 --- /dev/null +++ b/mkfile @@ -0,0 +1,19 @@ +next) - if (strcmp (s, np->keyword) == 0) + Map* np; + for (np = hashtab[hash(s)]; np != NULL; np = np->next) + if (strcmp(s, np->keyword) == 0) return np; return NULL; } TokenType -get (char *s) +get(char* s) { - Map *np; - for (np = hashtab[hash (s)]; np != NULL; np = np->next) - if (strcmp (s, np->keyword) == 0) + Map* np; + for (np = hashtab[hash(s)]; np != NULL; np = np->next) + if (strcmp(s, np->keyword) == 0) return np->token; return TOKEN_IDENTIFIER; } -Map * -put (char *keyword, TokenType token) +Map* +put(char* keyword, TokenType token) { - Map *np; + Map* np; unsigned int hashval; - if ((np = lookup (keyword)) == NULL) - { - np = (Map *)malloc (sizeof (*np)); - if (np == NULL || (np->keyword = strdup (keyword)) == NULL) - return NULL; - hashval = hash (keyword); - np->next = hashtab[hashval]; - hashtab[hashval] = np; - } + if ((np = lookup(keyword)) == NULL) { + np = (Map*)malloc(sizeof(*np)); + if (np == NULL || (np->keyword = strdup(keyword)) == NULL) + return NULL; + hashval = hash(keyword); + np->next = hashtab[hashval]; + hashtab[hashval] = np; + } np->token = token; return np; } void -initMap () +initMap() { - put ("and", TOKEN_AND); - put ("atan", TOKEN_ATAN); - put ("add", TOKEN_ADD); - put ("bep", TOKEN_COMPLAIN); - put ("complain", TOKEN_COMPLAIN); - put ("compl", TOKEN_COMPL); - put ("compose", TOKEN_COMPOSE); - put ("contradict", TOKEN_CONTRADICT); - put ("cons", TOKEN_CONS); - put ("cos", TOKEN_COS); - put ("chImmoH", TOKEN_CLEAR); - put ("chIm'a'", TOKEN_EMPTY); - put ("cher", TOKEN_SET); - put ("boq", TOKEN_ADD); - put ("choose", TOKEN_CHOOSE); - put ("chov", TOKEN_EVAL); - put ("chuv", TOKEN_MOD); - put ("cha'", TOKEN_DISP); - put ("clear", TOKEN_CLEAR); - put ("dup", TOKEN_DUP); - put ("dump", TOKEN_DUMP); - put ("disp", TOKEN_DISP); - put ("div", TOKEN_DIV); - put ("DuD", TOKEN_MIX); - put ("e", TOKEN_E); - put ("exch", TOKEN_EXCH); - put ("eval", TOKEN_EVAL); - put ("escape", TOKEN_ESCAPE); - put ("empty?", TOKEN_EMPTY); - put ("explode", TOKEN_EXPLODE); - put ("eq?", TOKEN_EQ); - put ("forget", TOKEN_FORGET); - put ("gt?", TOKEN_GT); - put ("ge?", TOKEN_GE); - put ("ghap", TOKEN_XOR); - put ("ghurmI'", TOKEN_E); - put ("ghurtaH", TOKEN_LN); - put ("ghorqu'", TOKEN_SHATTER); - put ("ghobe'chugh", TOKEN_IFNO); - put ("HIja'chugh", TOKEN_IFYES); - put ("Hotlh", TOKEN_DUMP); - put ("HeHmI'", TOKEN_PI); - put ("Habwav", TOKEN_IDIV); - put ("HabmI''a'", TOKEN_INT); - put ("idiv", TOKEN_IDIV); - put ("int?", TOKEN_INT); - put ("isolate", TOKEN_ISOLATE); - put ("ifyes", TOKEN_IFYES); - put ("ifno", TOKEN_IFNO); - put ("je", TOKEN_AND); - put ("jor", TOKEN_EXPLODE); - put ("joq", TOKEN_OR); - put ("ln", TOKEN_LN); - put ("lt?", TOKEN_LT); - put ("le?", TOKEN_LE); - put ("listen", TOKEN_LISTEN); - put ("loS'ar", TOKEN_SQRT); - put ("log", TOKEN_LOG); - put ("log3", TOKEN_LOG3); - put ("latlh", TOKEN_DUP); - put ("law'moH", TOKEN_MUL); - put ("law'qa'moH", TOKEN_POW); - put ("law''a'", TOKEN_GT); - put ("law'rap'a'", TOKEN_GE); - put ("maHghurtaH", TOKEN_LOG); - put ("mix", TOKEN_MIX); - put ("mi'moH", TOKEN_NUMBERIZE); - put ("muv", TOKEN_CONS); - put ("mul", TOKEN_MUL); - put ("mod", TOKEN_MOD); - put ("mobmoH", TOKEN_ISOLATE); - put ("mIScher", TOKEN_SETRAND); - put ("mIS", TOKEN_RAND); - put ("mI''a'", TOKEN_FLOAT); - put ("nIHghoS", TOKEN_SHIFTRIGHT); - put ("ne?", TOKEN_NE); - put ("negative?", TOKEN_NEGATIVE); - put ("name", TOKEN_NAME); - put ("nargh", TOKEN_ESCAPE); - put ("naQmoH", TOKEN_COMPOSE); - put ("number?", TOKEN_ISNUMBER); - put ("numberize", TOKEN_NUMBERIZE); - put ("null?", TOKEN_NULL); - put ("or", TOKEN_OR); - put ("pi", TOKEN_PI); - put ("pagh'a'", TOKEN_NULL); - put ("pop", TOKEN_POP); - put ("pong", TOKEN_NAME); - put ("pow", TOKEN_POW); - put ("poSghoS", TOKEN_SHIFTLEFT); - put ("puS'a'", TOKEN_LT); - put ("puSrap'a'", TOKEN_LE); - put ("qaw", TOKEN_REMEMBER); - put ("qawHa'", TOKEN_FORGET); - put ("qojmI'", TOKEN_TAN); - put ("qojHa'", TOKEN_ATAN); - put ("Qo'moH", TOKEN_COMPL); - put ("remember", TOKEN_REMEMBER); - put ("repeat", TOKEN_REPEAT); - put ("rand", TOKEN_RAND); - put ("rap'a'", TOKEN_EQ); - put ("rapbe'a'", TOKEN_NE); - put ("set", TOKEN_SET); - put ("split", TOKEN_SPLIT); - put ("shatter", TOKEN_SHATTER); - put ("strcut", TOKEN_STRCUT); - put ("streq?", TOKEN_STREQ); - put ("strmeasure", TOKEN_STRMEASURE); - put ("strtie", TOKEN_STRTIE); - put ("tlheghrar", TOKEN_STRTIE); - put ("sub", TOKEN_SUB); - put ("sub1", TOKEN_SUB1); - put ("sqrt", TOKEN_SQRT); - put ("sin", TOKEN_SIN); - put ("clip", TOKEN_CLIP); - put ("poD", TOKEN_CLIP); - put ("smooth", TOKEN_SMOOTH); - put ("Hab", TOKEN_SMOOTH); - put ("howmuch", TOKEN_HOWMUCH); - put ("'ar", TOKEN_HOWMUCH); - put ("setrand", TOKEN_SETRAND); - put ("shift right", TOKEN_SHIFTRIGHT); - put ("shift left", TOKEN_SHIFTLEFT); - put ("SIj", TOKEN_SPLIT); - put ("boqHa'", TOKEN_SUB); - put ("tam", TOKEN_EXCH); - put ("tan", TOKEN_TAN); - put ("taH'a'", TOKEN_NEGATIVE); - put ("tlhoch", TOKEN_CONTRADICT); - put ("tlheghpe'", TOKEN_STRCUT); - put ("tlheghjuv", TOKEN_STRMEASURE); - put ("tlheghrap'a'", TOKEN_STREQ); - put ("vangqa'", TOKEN_REPEAT); - put ("wIv", TOKEN_CHOOSE); - put ("woD", TOKEN_POP); - put ("wav", TOKEN_DIV); - put ("wa'teq", TOKEN_SUB1); - put ("wa'chel", TOKEN_ADD1); - put ("wejghurtaH", TOKEN_LOG3); - put ("xor", TOKEN_XOR); - put ("\'Ij", TOKEN_LISTEN); - put ("time", TOKEN_TIME); - put ("poH", TOKEN_TIME); + put("and", TOKEN_AND); + put("atan", TOKEN_ATAN); + put("add", TOKEN_ADD); + put("bep", TOKEN_COMPLAIN); + put("complain", TOKEN_COMPLAIN); + put("compl", TOKEN_COMPL); + put("compose", TOKEN_COMPOSE); + put("contradict", TOKEN_CONTRADICT); + put("cons", TOKEN_CONS); + put("cos", TOKEN_COS); + put("chImmoH", TOKEN_CLEAR); + put("chIm'a'", TOKEN_EMPTY); + put("cher", TOKEN_SET); + put("boq", TOKEN_ADD); + put("choose", TOKEN_CHOOSE); + put("chov", TOKEN_EVAL); + put("chuv", TOKEN_MOD); + put("cha'", TOKEN_DISP); + put("clear", TOKEN_CLEAR); + put("dup", TOKEN_DUP); + put("dump", TOKEN_DUMP); + put("disp", TOKEN_DISP); + put("div", TOKEN_DIV); + put("DuD", TOKEN_MIX); + put("e", TOKEN_E); + put("exch", TOKEN_EXCH); + put("eval", TOKEN_EVAL); + put("escape", TOKEN_ESCAPE); + put("empty?", TOKEN_EMPTY); + put("explode", TOKEN_EXPLODE); + put("eq?", TOKEN_EQ); + put("forget", TOKEN_FORGET); + put("gt?", TOKEN_GT); + put("ge?", TOKEN_GE); + put("ghap", TOKEN_XOR); + put("ghurmI'", TOKEN_E); + put("ghurtaH", TOKEN_LN); + put("ghorqu'", TOKEN_SHATTER); + put("ghobe'chugh", TOKEN_IFNO); + put("HIja'chugh", TOKEN_IFYES); + put("Hotlh", TOKEN_DUMP); + put("HeHmI'", TOKEN_PI); + put("Habwav", TOKEN_IDIV); + put("HabmI''a'", TOKEN_INT); + put("idiv", TOKEN_IDIV); + put("int?", TOKEN_INT); + put("isolate", TOKEN_ISOLATE); + put("ifyes", TOKEN_IFYES); + put("ifno", TOKEN_IFNO); + put("je", TOKEN_AND); + put("jor", TOKEN_EXPLODE); + put("joq", TOKEN_OR); + put("ln", TOKEN_LN); + put("lt?", TOKEN_LT); + put("le?", TOKEN_LE); + put("listen", TOKEN_LISTEN); + put("loS'ar", TOKEN_SQRT); + put("log", TOKEN_LOG); + put("log3", TOKEN_LOG3); + put("latlh", TOKEN_DUP); + put("law'moH", TOKEN_MUL); + put("law'qa'moH", TOKEN_POW); + put("law''a'", TOKEN_GT); + put("law'rap'a'", TOKEN_GE); + put("maHghurtaH", TOKEN_LOG); + put("mix", TOKEN_MIX); + put("mi'moH", TOKEN_NUMBERIZE); + put("muv", TOKEN_CONS); + put("mul", TOKEN_MUL); + put("mod", TOKEN_MOD); + put("mobmoH", TOKEN_ISOLATE); + put("mIScher", TOKEN_SETRAND); + put("mIS", TOKEN_RAND); + put("mI''a'", TOKEN_FLOAT); + put("nIHghoS", TOKEN_SHIFTRIGHT); + put("ne?", TOKEN_NE); + put("negative?", TOKEN_NEGATIVE); + put("name", TOKEN_NAME); + put("nargh", TOKEN_ESCAPE); + put("naQmoH", TOKEN_COMPOSE); + put("number?", TOKEN_ISNUMBER); + put("numberize", TOKEN_NUMBERIZE); + put("null?", TOKEN_NULL); + put("or", TOKEN_OR); + put("pi", TOKEN_PI); + put("pagh'a'", TOKEN_NULL); + put("pop", TOKEN_POP); + put("pong", TOKEN_NAME); + put("pow", TOKEN_POW); + put("poSghoS", TOKEN_SHIFTLEFT); + put("puS'a'", TOKEN_LT); + put("puSrap'a'", TOKEN_LE); + put("qaw", TOKEN_REMEMBER); + put("qawHa'", TOKEN_FORGET); + put("qojmI'", TOKEN_TAN); + put("qojHa'", TOKEN_ATAN); + put("Qo'moH", TOKEN_COMPL); + put("remember", TOKEN_REMEMBER); + put("repeat", TOKEN_REPEAT); + put("rand", TOKEN_RAND); + put("rap'a'", TOKEN_EQ); + put("rapbe'a'", TOKEN_NE); + put("set", TOKEN_SET); + put("split", TOKEN_SPLIT); + put("shatter", TOKEN_SHATTER); + put("strcut", TOKEN_STRCUT); + put("streq?", TOKEN_STREQ); + put("strmeasure", TOKEN_STRMEASURE); + put("strtie", TOKEN_STRTIE); + put("tlheghrar", TOKEN_STRTIE); + put("sub", TOKEN_SUB); + put("sub1", TOKEN_SUB1); + put("sqrt", TOKEN_SQRT); + put("sin", TOKEN_SIN); + put("clip", TOKEN_CLIP); + put("poD", TOKEN_CLIP); + put("smooth", TOKEN_SMOOTH); + put("Hab", TOKEN_SMOOTH); + put("howmuch", TOKEN_HOWMUCH); + put("'ar", TOKEN_HOWMUCH); + put("setrand", TOKEN_SETRAND); + put("shift right", TOKEN_SHIFTRIGHT); + put("shift left", TOKEN_SHIFTLEFT); + put("SIj", TOKEN_SPLIT); + put("boqHa'", TOKEN_SUB); + put("tam", TOKEN_EXCH); + put("tan", TOKEN_TAN); + put("taH'a'", TOKEN_NEGATIVE); + put("tlhoch", TOKEN_CONTRADICT); + put("tlheghpe'", TOKEN_STRCUT); + put("tlheghjuv", TOKEN_STRMEASURE); + put("tlheghrap'a'", TOKEN_STREQ); + put("vangqa'", TOKEN_REPEAT); + put("wIv", TOKEN_CHOOSE); + put("woD", TOKEN_POP); + put("wav", TOKEN_DIV); + put("wa'teq", TOKEN_SUB1); + put("wa'chel", TOKEN_ADD1); + put("wejghurtaH", TOKEN_LOG3); + put("xor", TOKEN_XOR); + put("\'Ij", TOKEN_LISTEN); + put("time", TOKEN_TIME); + put("poH", TOKEN_TIME); // Wrong word in original spec, old one meant "waving hands or flapping" // Also fixes the conflicting joq issue meaning sin or 'or' - put ("yu'eghHa'", TOKEN_COS); - put ("yu'egh", TOKEN_SIN); + put("yu'eghHa'", TOKEN_COS); + put("yu'egh", TOKEN_SIN); // This one has a special case too as it is the same as the '~' operator - put ("lI'moH", TOKEN_TILDE); - put ("woDHa'", TOKEN_GARBAGE_COLLECT); - put ("gc", TOKEN_GARBAGE_COLLECT); + put("lI'moH", TOKEN_TILDE); + put("woDHa'", TOKEN_GARBAGE_COLLECT); + put("gc", TOKEN_GARBAGE_COLLECT); } typedef struct Tokenizer Tokenizer; struct Tokenizer { - char *start; - char *current; + char* start; + char* current; int32_t line; }; Tokenizer tokenizer; void -initTokenizer (char *src) +initTokenizer(char* src) { tokenizer.start = src; tokenizer.current = src; @@ -238,26 +237,26 @@ initTokenizer (char *src) } static bool -isAlpha (char c) +isAlpha(char c) { - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' - || c == '\'' || c == '?'; + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || + c == '\'' || c == '?'; } static bool -isDigit (char c) +isDigit(char c) { return (c >= '0' && c <= '9') || c == '-'; } static bool -isAtEnd () +isAtEnd() { return *tokenizer.current == '\0'; } static Token -makeToken (TokenType type) +makeToken(TokenType type) { Token token; token.type = type; @@ -268,41 +267,41 @@ makeToken (TokenType type) } static Token -errorToken (char *msg) +errorToken(char* msg) { Token token; token.type = TOKEN_ERROR; token.start = msg; - token.length = (int32_t)strlen (msg); + token.length = (int32_t)strlen(msg); token.line = tokenizer.line; return token; } static char -advance () +advance() { tokenizer.current++; return tokenizer.current[-1]; } static char -peek () +peek() { return *tokenizer.current; } static char -peekNext () +peekNext() { - if (isAtEnd ()) + if (isAtEnd()) return '\0'; return tokenizer.current[1]; } static bool -match (char expected) +match(char expected) { - if (isAtEnd ()) + if (isAtEnd()) return false; if (*tokenizer.current != expected) return false; @@ -311,437 +310,432 @@ match (char expected) } static void -skipWhitespace () +skipWhitespace() { - for (;;) - { - char c = peek (); - switch (c) - { - case ' ': - case '\r': - case '\t': - advance (); - break; - case '\n': - tokenizer.line++; - advance (); - break; - case '/': - if (peekNext () == '/') - { - // Ignore the preprocessor import until end of the line. - while (peek () != '\n' && !isAtEnd ()) - advance (); - } - else - { - return; - } - break; - case '(': - if (peekNext () == '*') - { - advance (); // consume ( - advance (); // consume * - while (!isAtEnd () && peek () != '*' && peekNext () != ')') - advance (); // Consume contents - advance (); // consume * - advance (); // consume ) - } - break; - default: + for (;;) { + char c = peek(); + switch (c) { + case ' ': + case '\r': + case '\t': + advance(); + break; + case '\n': + tokenizer.line++; + advance(); + break; + case '/': + if (peekNext() == '/') { + // Ignore the preprocessor import until end of the line. + while (peek() != '\n' && !isAtEnd()) + advance(); + } else { return; } + break; + case '(': + if (peekNext() == '*') { + advance(); // consume ( + advance(); // consume * + while (!isAtEnd() && peek() != '*' && peekNext() != ')') + advance(); // Consume contents + advance(); // consume * + advance(); // consume ) + } + break; + default: + return; } + } } static TokenType -checkKeyword (int start, int length, char *rest, TokenType type) +checkKeyword(int start, int length, char* rest, TokenType type) { - if (tokenizer.current - tokenizer.start == start + length - && memcmp (tokenizer.start + start, rest, length) == 0) - { - return type; - } + if (tokenizer.current - tokenizer.start == start + length && + memcmp(tokenizer.start + start, rest, length) == 0) { + return type; + } return TOKEN_IDENTIFIER; } static TokenType -identifierType () +identifierType() { - char *check; + char* check; int32_t size = tokenizer.current - tokenizer.start; - check = (char *)malloc (sizeof (size)); - strncpy (check, tokenizer.start, size); + check = (char*)malloc(sizeof(size)); + strncpy(check, tokenizer.start, size); check[size] = '\0'; - TokenType t = get (check); - free (check); + TokenType t = get(check); + free(check); return t; } static Token -identifier () +identifier() { - while (isAlpha (peek ()) || isDigit (peek ())) - advance (); - return makeToken (identifierType ()); + while (isAlpha(peek()) || isDigit(peek())) + advance(); + return makeToken(identifierType()); } static Token -number () +number() { bool is_float = false; - while (isDigit (peek ())) - advance (); + while (isDigit(peek())) + advance(); // Look for a fractional part. - if (peek () == '.' && isDigit (peekNext ())) - { - is_float = true; - // Consume the ".". - advance (); + if (peek() == '.' && isDigit(peekNext())) { + is_float = true; + // Consume the ".". + advance(); - while (isDigit (peek ())) - advance (); - } + while (isDigit(peek())) + advance(); + } - return makeToken (is_float ? TOKEN_FLOAT - : TOKEN_INT); // or measure if ends in postscript + return makeToken(TOKEN_FLOAT); // or measure if ends in postscript } static Token -string () +string() { - while (peek () != '"' && !isAtEnd ()) - { - if (peek () == '\n') - tokenizer.line++; - advance (); - } + while (peek() != '"' && !isAtEnd()) { + if (peek() == '\n') + tokenizer.line++; + advance(); + } - if (isAtEnd ()) - return errorToken ("Unterminated string."); + if (isAtEnd()) + return errorToken("Unterminated string."); // The closing quote. - advance (); - return makeToken (TOKEN_STRING); + advance(); + return makeToken(TOKEN_STRING); } Token -nextToken () +nextToken() { - skipWhitespace (); + skipWhitespace(); tokenizer.start = tokenizer.current; - if (isAtEnd ()) - return makeToken (TOKEN_EOF); + if (isAtEnd()) + return makeToken(TOKEN_EOF); - char c = advance (); - if (isAlpha (c)) - return identifier (); - if (isDigit (c)) - return number (); - switch (c) - { + char c = advance(); + if (isAlpha(c)) + return identifier(); + if (isDigit(c)) + return number(); + switch (c) { case '(': - return makeToken (TOKEN_LEFT_PAREN); + return makeToken(TOKEN_LEFT_PAREN); case ')': - return makeToken (TOKEN_RIGHT_PAREN); + return makeToken(TOKEN_RIGHT_PAREN); case '{': - return makeToken (TOKEN_LEFT_BRACE); + return makeToken(TOKEN_LEFT_BRACE); case '}': - return makeToken (TOKEN_RIGHT_BRACE); + return makeToken(TOKEN_RIGHT_BRACE); case '-': - return makeToken (TOKEN_NEGATIVE); + return makeToken(TOKEN_NEGATIVE); case '~': - return makeToken (TOKEN_TILDE); + return makeToken(TOKEN_TILDE); case '/': - return makeToken (TOKEN_SLASH); + return makeToken(TOKEN_SLASH); case '"': - return string (); - } + return string(); + } - return errorToken ("Unexpected character."); + return errorToken("Unexpected character."); } void -debug_printToken (Token t) +debug_printToken(Token t) { - switch (t.type) - { + char* str; + int32_t size = tokenizer.current - tokenizer.start; + str = (char*)malloc(sizeof(size)); + strncpy(str, tokenizer.start, size); + str[size] = '\0'; + + switch (t.type) { case TOKEN_LEFT_PAREN: - printf ("TOKEN_LEFT_PAREN line_no=%d\n", t.line); + printf("TOKEN_LEFT_PAREN %s line_no=%d\n", str, t.line); break; case TOKEN_RIGHT_PAREN: - printf ("TOKEN_RIGHT_PAREN line_no=%d\n", t.line); + printf("TOKEN_RIGHT_PAREN %s line_no=%d\n", str, t.line); break; case TOKEN_LEFT_BRACE: - printf ("TOKEN_LEFT_BRACE line_no=%d\n", t.line); + printf("TOKEN_LEFT_BRACE %s line_no=%d\n", str, t.line); break; case TOKEN_RIGHT_BRACE: - printf ("TOKEN_RIGHT_BRACE line_no=%d\n", t.line); + printf("TOKEN_RIGHT_BRACE %s line_no=%d\n", str, t.line); break; case TOKEN_TILDE: - printf ("TOKEN_TILDE line_no=%d\n", t.line); + printf("TOKEN_TILDE %s line_no=%d\n", str, t.line); break; case TOKEN_SLASH: - printf ("TOKEN_SLASH line_no=%d\n", t.line); + printf("TOKEN_SLASH %s line_no=%d\n", str, t.line); break; case TOKEN_MINUS: - printf ("TOKEN_MINUS line_no=%d\n", t.line); + printf("TOKEN_MINUS %s line_no=%d\n", str, t.line); break; case TOKEN_IDENTIFIER: - printf ("TOKEN_IDENTIFIER line_no=%d\n", t.line); + printf("TOKEN_IDENTIFIER %s line_no=%d\n", str, t.line); break; case TOKEN_STRING: - printf ("TOKEN_STRING line_no=%d\n", t.line); + printf("TOKEN_STRING %s line_no=%d\n", str, t.line); break; case TOKEN_FLOAT: - printf ("TOKEN_FLOAT line_no=%d\n", t.line); + printf("TOKEN_FLOAT %s line_no=%d\n", str, t.line); break; case TOKEN_LIST: - printf ("TOKEN_LIST line_no=%d\n", t.line); + printf("TOKEN_LIST %s line_no=%d\n", str, t.line); break; case TOKEN_ERROR: - printf ("TOKEN_ERROR line_no=%d\n", t.line); + printf("TOKEN_ERROR %s line_no=%d\n", str, t.line); break; case TOKEN_FALSE: - printf ("TOKEN_FALSE line_no=%d\n", t.line); + printf("TOKEN_FALSE %s line_no=%d\n", str, t.line); break; case TOKEN_TRUE: - printf ("TOKEN_TRUE line_no=%d\n", t.line); + printf("TOKEN_TRUE %s line_no=%d\n", str, t.line); break; case TOKEN_PI: - printf ("TOKEN_PI line_no=%d\n", t.line); + printf("TOKEN_PI %s line_no=%d\n", str, t.line); break; case TOKEN_E: - printf ("TOKEN_E line_no=%d\n", t.line); + printf("TOKEN_E %s line_no=%d\n", str, t.line); break; case TOKEN_EOF: - printf ("TOKEN_EOF line_no=%d\n", t.line); + printf("TOKEN_EOF %s line_no=%d\n", str, t.line); break; case TOKEN_POP: - printf ("TOKEN_POP line_no=%d\n", t.line); + printf("TOKEN_POP %s line_no=%d\n", str, t.line); break; case TOKEN_DUP: - printf ("TOKEN_DUP line_no=%d\n", t.line); + printf("TOKEN_DUP %s line_no=%d\n", str, t.line); break; case TOKEN_EXCH: - printf ("TOKEN_EXCH line_no=%d\n", t.line); + printf("TOKEN_EXCH %s line_no=%d\n", str, t.line); break; case TOKEN_CLEAR: - printf ("TOKEN_CLEAR line_no=%d\n", t.line); + printf("TOKEN_CLEAR %s line_no=%d\n", str, t.line); break; case TOKEN_REMEMBER: - printf ("TOKEN_REMEMBER line_no=%d\n", t.line); + printf("TOKEN_REMEMBER %s line_no=%d\n", str, t.line); break; case TOKEN_FORGET: - printf ("TOKEN_FORGET line_no=%d\n", t.line); + printf("TOKEN_FORGET %s line_no=%d\n", str, t.line); break; case TOKEN_DUMP: - printf ("TOKEN_DUMP line_no=%d\n", t.line); + printf("TOKEN_DUMP %s line_no=%d\n", str, t.line); break; case TOKEN_NAME: - printf ("TOKEN_NAME line_no=%d\n", t.line); + printf("TOKEN_NAME %s line_no=%d\n", str, t.line); break; case TOKEN_SET: - printf ("TOKEN_SET line_no=%d\n", t.line); + printf("TOKEN_SET %s line_no=%d\n", str, t.line); break; case TOKEN_IFYES: - printf ("TOKEN_IFYES line_no=%d\n", t.line); + printf("TOKEN_IFYES %s line_no=%d\n", str, t.line); break; case TOKEN_IFNO: - printf ("TOKEN_IFNO line_no=%d\n", t.line); + printf("TOKEN_IFNO %s line_no=%d\n", str, t.line); break; case TOKEN_CHOOSE: - printf ("TOKEN_CHOOSE line_no=%d\n", t.line); + printf("TOKEN_CHOOSE %s line_no=%d\n", str, t.line); break; case TOKEN_EVAL: - printf ("TOKEN_EVAL line_no=%d\n", t.line); + printf("TOKEN_EVAL %s line_no=%d\n", str, t.line); break; case TOKEN_ESCAPE: - printf ("TOKEN_ESCAPE line_no=%d\n", t.line); + printf("TOKEN_ESCAPE %s line_no=%d\n", str, t.line); break; case TOKEN_REPEAT: - printf ("TOKEN_REPEAT line_no=%d\n", t.line); + printf("TOKEN_REPEAT %s line_no=%d\n", str, t.line); break; case TOKEN_SPLIT: - printf ("TOKEN_SPLIT line_no=%d\n", t.line); + printf("TOKEN_SPLIT %s line_no=%d\n", str, t.line); break; case TOKEN_CONS: - printf ("TOKEN_CONS line_no=%d\n", t.line); + printf("TOKEN_CONS %s line_no=%d\n", str, t.line); break; case TOKEN_SHATTER: - printf ("TOKEN_SHATTER line_no=%d\n", t.line); + printf("TOKEN_SHATTER %s line_no=%d\n", str, t.line); break; case TOKEN_EMPTY: - printf ("TOKEN_EMPTY line_no=%d\n", t.line); + printf("TOKEN_EMPTY %s line_no=%d\n", str, t.line); break; case TOKEN_COMPOSE: - printf ("TOKEN_COMPOSE line_no=%d\n", t.line); + printf("TOKEN_COMPOSE %s line_no=%d\n", str, t.line); break; case TOKEN_STREQ: - printf ("TOKEN_STREQ line_no=%d\n", t.line); + printf("TOKEN_STREQ %s line_no=%d\n", str, t.line); break; case TOKEN_STRCUT: - printf ("TOKEN_STRCUT line_no=%d\n", t.line); + printf("TOKEN_STRCUT %s line_no=%d\n", str, t.line); break; case TOKEN_STRMEASURE: - printf ("TOKEN_STRMEASURE line_no=%d\n", t.line); + printf("TOKEN_STRMEASURE %s line_no=%d\n", str, t.line); break; case TOKEN_STRTIE: - printf ("TOKEN_STRTIE line_no=%d\n", t.line); + printf("TOKEN_STRTIE %s line_no=%d\n", str, t.line); break; case TOKEN_EXPLODE: - printf ("TOKEN_EXPLODE line_no=%d\n", t.line); + printf("TOKEN_EXPLODE %s line_no=%d\n", str, t.line); break; case TOKEN_ADD: - printf ("TOKEN_ADD line_no=%d\n", t.line); + printf("TOKEN_ADD %s line_no=%d\n", str, t.line); break; case TOKEN_SUB: - printf ("TOKEN_SUB line_no=%d\n", t.line); + printf("TOKEN_SUB %s line_no=%d\n", str, t.line); break; case TOKEN_MUL: - printf ("TOKEN_MUL line_no=%d\n", t.line); + printf("TOKEN_MUL %s line_no=%d\n", str, t.line); break; case TOKEN_DIV: - printf ("TOKEN_DIV line_no=%d\n", t.line); + printf("TOKEN_DIV %s line_no=%d\n", str, t.line); break; case TOKEN_IDIV: - printf ("TOKEN_IDIV line_no=%d\n", t.line); + printf("TOKEN_IDIV %s line_no=%d\n", str, t.line); break; case TOKEN_MOD: - printf ("TOKEN_MOD line_no=%d\n", t.line); + printf("TOKEN_MOD %s line_no=%d\n", str, t.line); break; case TOKEN_POW: - printf ("TOKEN_POW line_no=%d\n", t.line); + printf("TOKEN_POW %s line_no=%d\n", str, t.line); break; case TOKEN_SQRT: - printf ("TOKEN_SQRT line_no=%d\n", t.line); + printf("TOKEN_SQRT %s line_no=%d\n", str, t.line); break; case TOKEN_ADD1: - printf ("TOKEN_ADD1 line_no=%d\n", t.line); + printf("TOKEN_ADD1 %s line_no=%d\n", str, t.line); break; case TOKEN_SUB1: - printf ("TOKEN_SUB1 line_no=%d\n", t.line); + printf("TOKEN_SUB1 %s line_no=%d\n", str, t.line); break; case TOKEN_SIN: - printf ("TOKEN_SIN line_no=%d\n", t.line); + printf("TOKEN_SIN %s line_no=%d\n", str, t.line); break; case TOKEN_COS: - printf ("TOKEN_COS line_no=%d\n", t.line); + printf("TOKEN_COS %s line_no=%d\n", str, t.line); break; case TOKEN_TAN: - printf ("TOKEN_TAN line_no=%d\n", t.line); + printf("TOKEN_TAN %s line_no=%d\n", str, t.line); break; case TOKEN_ATAN: - printf ("TOKEN_ATAN line_no=%d\n", t.line); + printf("TOKEN_ATAN %s line_no=%d\n", str, t.line); break; case TOKEN_LN: - printf ("TOKEN_LN line_no=%d\n", t.line); + printf("TOKEN_LN %s line_no=%d\n", str, t.line); break; case TOKEN_LOG: - printf ("TOKEN_LOG line_no=%d\n", t.line); + printf("TOKEN_LOG %s line_no=%d\n", str, t.line); break; case TOKEN_LOG3: - printf ("TOKEN_LOG3 line_no=%d\n", t.line); + printf("TOKEN_LOG3 %s line_no=%d\n", str, t.line); break; case TOKEN_CLIP: - printf ("TOKEN_CLIP line_no=%d\n", t.line); + printf("TOKEN_CLIP %s line_no=%d\n", str, t.line); break; case TOKEN_SMOOTH: - printf ("TOKEN_SMOOTH line_no=%d\n", t.line); + printf("TOKEN_SMOOTH %s line_no=%d\n", str, t.line); break; case TOKEN_HOWMUCH: - printf ("TOKEN_HOWMUCH line_no=%d\n", t.line); + printf("TOKEN_HOWMUCH %s line_no=%d\n", str, t.line); break; case TOKEN_SETRAND: - printf ("TOKEN_SETRAND line_no=%d\n", t.line); + printf("TOKEN_SETRAND %s line_no=%d\n", str, t.line); break; case TOKEN_RAND: - printf ("TOKEN_RAND line_no=%d\n", t.line); + printf("TOKEN_RAND %s line_no=%d\n", str, t.line); break; case TOKEN_INT: - printf ("TOKEN_INT line_no=%d\n", t.line); + printf("TOKEN_INT %s line_no=%d\n", str, t.line); break; case TOKEN_NUMBERIZE: - printf ("TOKEN_NUMBERIZE line_no=%d\n", t.line); + printf("TOKEN_NUMBERIZE %s line_no=%d\n", str, t.line); break; case TOKEN_ISOLATE: - printf ("TOKEN_ISOLATE line_no=%d\n", t.line); + printf("TOKEN_ISOLATE %s line_no=%d\n", str, t.line); break; case TOKEN_MIX: - printf ("TOKEN_MIX line_no=%d\n", t.line); + printf("TOKEN_MIX %s line_no=%d\n", str, t.line); break; case TOKEN_CONTRADICT: - printf ("TOKEN_CONTRADICT line_no=%d\n", t.line); + printf("TOKEN_CONTRADICT %s line_no=%d\n", str, t.line); break; case TOKEN_COMPL: - printf ("TOKEN_COMPL line_no=%d\n", t.line); + printf("TOKEN_COMPL %s line_no=%d\n", str, t.line); break; case TOKEN_SHIFTRIGHT: - printf ("TOKEN_SHIFTRIGHT line_no=%d\n", t.line); + printf("TOKEN_SHIFTRIGHT %s line_no=%d\n", str, t.line); break; case TOKEN_SHIFTLEFT: - printf ("TOKEN_SHIFTLEFT line_no=%d\n", t.line); + printf("TOKEN_SHIFTLEFT %s line_no=%d\n", str, t.line); break; case TOKEN_GT: - printf ("TOKEN_GT line_no=%d\n", t.line); + printf("TOKEN_GT %s line_no=%d\n", str, t.line); break; case TOKEN_LT: - printf ("TOKEN_LT line_no=%d\n", t.line); + printf("TOKEN_LT %s line_no=%d\n", str, t.line); break; case TOKEN_EQ: - printf ("TOKEN_EQ line_no=%d\n", t.line); + printf("TOKEN_EQ %s line_no=%d\n", str, t.line); break; case TOKEN_GE: - printf ("TOKEN_GE line_no=%d\n", t.line); + printf("TOKEN_GE %s line_no=%d\n", str, t.line); break; case TOKEN_LE: - printf ("TOKEN_LE line_no=%d\n", t.line); + printf("TOKEN_LE %s line_no=%d\n", str, t.line); break; case TOKEN_NE: - printf ("TOKEN_NE line_no=%d\n", t.line); + printf("TOKEN_NE %s line_no=%d\n", str, t.line); break; case TOKEN_NULL: - printf ("TOKEN_NULL line_no=%d\n", t.line); + printf("TOKEN_NULL %s line_no=%d\n", str, t.line); break; case TOKEN_NEGATIVE: - printf ("TOKEN_NEGATIVE line_no=%d\n", t.line); + printf("TOKEN_NEGATIVE %s line_no=%d\n", str, t.line); break; case TOKEN_ISNULL: - printf ("TOKEN_ISNULL line_no=%d\n", t.line); + printf("TOKEN_ISNULL %s line_no=%d\n", str, t.line); break; case TOKEN_ISINT: - printf ("TOKEN_ISINT line_no=%d\n", t.line); + printf("TOKEN_ISINT %s line_no=%d\n", str, t.line); break; case TOKEN_ISNUMBER: - printf ("TOKEN_ISNUMBER line_no=%d\n", t.line); + printf("TOKEN_ISNUMBER %s line_no=%d\n", str, t.line); break; case TOKEN_AND: - printf ("TOKEN_AND line_no=%d\n", t.line); + printf("TOKEN_AND %s line_no=%d\n", str, t.line); break; case TOKEN_OR: - printf ("TOKEN_OR line_no=%d\n", t.line); + printf("TOKEN_OR %s line_no=%d\n", str, t.line); break; case TOKEN_XOR: - printf ("TOKEN_XOR line_no=%d\n", t.line); + printf("TOKEN_XOR %s line_no=%d\n", str, t.line); break; case TOKEN_DISP: - printf ("TOKEN_DISP line_no=%d\n", t.line); + printf("TOKEN_DISP %s line_no=%d\n", str, t.line); break; case TOKEN_LISTEN: - printf ("TOKEN_LISTEN line_no=%d\n", t.line); + printf("TOKEN_LISTEN %s line_no=%d\n", str, t.line); break; case TOKEN_COMPLAIN: - printf ("TOKEN_COMPLAIN line_no=%d\n", t.line); + printf("TOKEN_COMPLAIN %s line_no=%d\n", str, t.line); break; case TOKEN_TIME: - printf ("TOKEN_TIME line_no=%d\n", t.line); + printf("TOKEN_TIME %s line_no=%d\n", str, t.line); break; case TOKEN_GARBAGE_COLLECT: - printf ("TOKEN_GARBAGE_COLLECT line_no=%d\n", t.line); + printf("TOKEN_GARBAGE_COLLECT %s line_no=%d\n", str, t.line); break; - } + } + free(str); } \ No newline at end of file