diff options
| author | Nakidai <nakidai@disroot.org> | 2025-03-23 21:11:13 +0300 |
|---|---|---|
| committer | Nakidai <nakidai@disroot.org> | 2025-03-23 21:11:13 +0300 |
| commit | ad9d6a199db7c28f8b20f131dfb55a26e0e251de (patch) | |
| tree | e5bb112cd902d9c09f5ad79ce98ba8a315d20bbb /src | |
| parent | 159666bae6cc185a5abac154b85c49406f32f30c (diff) | |
| download | 3cl-ad9d6a199db7c28f8b20f131dfb55a26e0e251de.tar.gz 3cl-ad9d6a199db7c28f8b20f131dfb55a26e0e251de.zip | |
Again...
Diffstat (limited to 'src')
| -rw-r--r-- | src/3cl.c | 80 | ||||
| -rw-r--r-- | src/instruction.c | 40 | ||||
| -rw-r--r-- | src/instruction/add.c | 13 | ||||
| -rw-r--r-- | src/instruction/assign.c | 32 | ||||
| -rw-r--r-- | src/instruction/decrement.c | 11 | ||||
| -rw-r--r-- | src/instruction/delete.c | 32 | ||||
| -rw-r--r-- | src/instruction/increment.c | 11 | ||||
| -rw-r--r-- | src/instruction/invalid.c | 8 | ||||
| -rw-r--r-- | src/instruction/nop.c | 6 | ||||
| -rw-r--r-- | src/instruction/pushzero.c | 9 | ||||
| -rw-r--r-- | src/instruction/reverse.c | 51 | ||||
| -rw-r--r-- | src/instruction/subtract.c | 12 | ||||
| -rw-r--r-- | src/main.c | 66 | ||||
| -rw-r--r-- | src/platform/getch.c | 53 | ||||
| -rw-r--r-- | src/readchar.c | 59 | ||||
| -rw-r--r-- | src/readfile.c | 33 | ||||
| -rw-r--r-- | src/stack.c | 22 | ||||
| -rw-r--r-- | src/utils.c | 20 | ||||
| -rw-r--r-- | src/variable.c | 58 |
19 files changed, 0 insertions, 616 deletions
diff --git a/src/3cl.c b/src/3cl.c deleted file mode 100644 index bcadf4d..0000000 --- a/src/3cl.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "3cl.h" - -#include <errno.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> - -#include "instruction.h" - - -int ccl_init(struct CCL *ccl, const char *code, int (*in)(), void (*out)(int)) -{ - *ccl = (struct CCL) - { - .code = code, - .stopped = false, - .in = in, - .out = out, - .rootframe = (struct CCLFrame) - { - .prev = NULL, .next = NULL, - .type = CCL_ROOT, - .ep = 0, - .vars = (struct CCLVariable) - { - .prev = NULL, - .next = NULL, - .name = '_', - .value = 0 - }, - }, - .stack = (struct CCLStack) - { - .length = CCL_STACKSIZE, - .stack = (CCLNum *)malloc(CCL_STACKSIZE) - }, - }; - - return errno; -} - -void ccl_free(struct CCL *ccl) -{ - free(ccl->stack.stack); - if (ccl->rootframe.next != NULL) - { - for (struct CCLFrame *frame = ccl->rootframe.next, *new;;) - { - if (frame->vars.next != NULL) - { - for (struct CCLVariable *var = frame->vars.next, *new;;) - { - if (var->next == NULL) - break; - new = var->next; - free(var); - var = new; - } - } - - if (frame->next == NULL) - break; - new = frame->next; - free(frame); - frame = new; - } - } -} - -void ccl_exec(struct CCL *ccl) -{ - struct CCLFrame *curframe = &ccl->rootframe; - - for (;;) - { - curframe = ccl_instruction(ccl, curframe); - if (ccl->stopped) - break; - } -} diff --git a/src/instruction.c b/src/instruction.c deleted file mode 100644 index 4189dd1..0000000 --- a/src/instruction.c +++ /dev/null @@ -1,40 +0,0 @@ -#include "instruction.h" -#include "instructions.h" - -#include <stdbool.h> -#include <stdio.h> - -#include "readchar.h" -#include "3cl.h" - - -struct CCLFrame *ccl_instruction(struct CCL *ccl, struct CCLFrame *frame) -{ - CCLInstruction instruction; - char chr = ccl_readchar(ccl, frame, CCL_RC_CCL_INSTR); - - if (chr == '\0') - { - ccl->stopped = true; - return INST(nop)(ccl, frame); - } - -#define ISSW(NAME) instruction = INST(NAME); break - switch (chr) - { - case '\n': /* FALLTHROUGH */ - case ' ' : /* FALLTHROUGH */ - case '\t': ISSW(nop); - case '^' : ISSW(pushzero); - case '+' : ISSW(increment); - case '-' : ISSW(decrement); - case '*' : ISSW(add); - case '~' : ISSW(subtract); - case '%' : ISSW(reverse); - case '=' : ISSW(assign); - default : ISSW(invalid); - } -#undef INSW - return instruction(ccl, frame); - puts("aboba"); -} diff --git a/src/instruction/add.c b/src/instruction/add.c deleted file mode 100644 index 23d4f8c..0000000 --- a/src/instruction/add.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "3cl.h" - -#include "stack.h" -#include "utils.h" - -struct CCLFrame *ccl_instruction_add(struct CCL *ccl, struct CCLFrame *frame) -{ - if (ccl->stack.cur < 2) - die(1, "stack size is %d (%d required)", ccl->stack.cur, 2); - ccl_stack_push(&ccl->stack, ccl_stack_pop(&ccl->stack) + ccl_stack_pop(&ccl->stack)); - - return frame; -} diff --git a/src/instruction/assign.c b/src/instruction/assign.c deleted file mode 100644 index 81e70f9..0000000 --- a/src/instruction/assign.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "3cl.h" - -#include <stdio.h> - -#include "readchar.h" -#include "stack.h" -#include "utils.h" -#include "variable.h" - - -struct CCLFrame *ccl_instruction_assign(struct CCL *ccl, struct CCLFrame *frame) -{ - if (ccl->stack.cur < 1) - die(1, "stack size is %d (%d required)", ccl->stack.cur, 1); - - CCLNum value = ccl_stack_pop(&ccl->stack); - - char name = ccl_readchar(ccl, frame, CCL_RC_CCL_VARUS); - if (name == '\0') - die(1, "Unexpected EOF"); - - if (name == '_') - return frame; - - struct CCLVariable *var = ccl_variable_getany(ccl, frame, name); - if (var == NULL) - ccl_variable_set(&ccl->rootframe.vars, name, value); - else - var->value = value; - - return frame; -} diff --git a/src/instruction/decrement.c b/src/instruction/decrement.c deleted file mode 100644 index ce3af87..0000000 --- a/src/instruction/decrement.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "3cl.h" - -#include "utils.h" - -struct CCLFrame *ccl_instruction_decrement(struct CCL *ccl, struct CCLFrame *frame) -{ - if (ccl->stack.cur < 1) - die(1, "stack size is %d (%d required)", ccl->stack.cur, 1); - --ccl->stack.stack[ccl->stack.cur]; - return frame; -} diff --git a/src/instruction/delete.c b/src/instruction/delete.c deleted file mode 100644 index 81e70f9..0000000 --- a/src/instruction/delete.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "3cl.h" - -#include <stdio.h> - -#include "readchar.h" -#include "stack.h" -#include "utils.h" -#include "variable.h" - - -struct CCLFrame *ccl_instruction_assign(struct CCL *ccl, struct CCLFrame *frame) -{ - if (ccl->stack.cur < 1) - die(1, "stack size is %d (%d required)", ccl->stack.cur, 1); - - CCLNum value = ccl_stack_pop(&ccl->stack); - - char name = ccl_readchar(ccl, frame, CCL_RC_CCL_VARUS); - if (name == '\0') - die(1, "Unexpected EOF"); - - if (name == '_') - return frame; - - struct CCLVariable *var = ccl_variable_getany(ccl, frame, name); - if (var == NULL) - ccl_variable_set(&ccl->rootframe.vars, name, value); - else - var->value = value; - - return frame; -} diff --git a/src/instruction/increment.c b/src/instruction/increment.c deleted file mode 100644 index d2d51a5..0000000 --- a/src/instruction/increment.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "3cl.h" - -#include "utils.h" - -struct CCLFrame *ccl_instruction_increment(struct CCL *ccl, struct CCLFrame *frame) -{ - if (ccl->stack.cur < 1) - die(1, "stack size is %d (%d required)", ccl->stack.cur, 1); - ++ccl->stack.stack[ccl->stack.cur]; - return frame; -} diff --git a/src/instruction/invalid.c b/src/instruction/invalid.c deleted file mode 100644 index 7110bb9..0000000 --- a/src/instruction/invalid.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "3cl.h" - -#include "utils.h" - -struct CCLFrame *ccl_instruction_invalid(struct CCL *ccl, struct CCLFrame *frame) -{ - die(1, "Invalid instruction at %d", frame->ep - 1); -} diff --git a/src/instruction/nop.c b/src/instruction/nop.c deleted file mode 100644 index 778b8ed..0000000 --- a/src/instruction/nop.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "3cl.h" - -struct CCLFrame *ccl_instruction_nop(struct CCL *ccl, struct CCLFrame *frame) -{ - return frame; -} diff --git a/src/instruction/pushzero.c b/src/instruction/pushzero.c deleted file mode 100644 index cd0a023..0000000 --- a/src/instruction/pushzero.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "3cl.h" - -#include "stack.h" - -struct CCLFrame *ccl_instruction_pushzero(struct CCL *ccl, struct CCLFrame *frame) -{ - ccl_stack_push(&ccl->stack, 0); - return frame; -} diff --git a/src/instruction/reverse.c b/src/instruction/reverse.c deleted file mode 100644 index bd863ae..0000000 --- a/src/instruction/reverse.c +++ /dev/null @@ -1,51 +0,0 @@ -#include "3cl.h" - -#include <errno.h> -#include <stddef.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "readchar.h" -#include "utils.h" -#include "variable.h" - - -struct CCLFrame *ccl_instruction_reverse(struct CCL *ccl, struct CCLFrame *frame) -{ - char varname; - size_t toreverse; - - varname = ccl_readchar(ccl, frame, CCL_RC_CCL_VARUS); - if (varname == '\0') - { - die(1, "Unexpected EOF"); - } else if (varname == '_') - { - toreverse = ccl->stack.cur; - } else - { - struct CCLVariable *var = ccl_variable_getany(ccl, frame, varname); - if (var == NULL) - die(1, "Unknown variable '%c' at %d", varname, frame->ep); - toreverse = var->value; - } - - if (ccl->stack.cur < toreverse) - die(1, "stack size is %d (%d required)", ccl->stack.cur, toreverse); - - CCLNum *temp = malloc(sizeof(*temp) * toreverse); - if (temp == NULL) - die(1, "malloc(): %s", strerror(errno)); - - size_t start; - for (start = 0; start < toreverse; ++start) - temp[start] = ccl->stack.stack[ccl->stack.cur - start]; - start = ccl->stack.cur - start; - for (int i = 0; i < toreverse; ++i) - ccl->stack.stack[start + i] = temp[i]; - - free(temp); - - return frame; -} diff --git a/src/instruction/subtract.c b/src/instruction/subtract.c deleted file mode 100644 index f140cfb..0000000 --- a/src/instruction/subtract.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "3cl.h" - -#include "stack.h" -#include "utils.h" - -struct CCLFrame *ccl_instruction_subtract(struct CCL *ccl, struct CCLFrame *frame) -{ - if (ccl->stack.cur < 2) - die(1, "stack size is %d (%d required)", ccl->stack.cur, 2); - ccl_stack_push(&ccl->stack, -ccl_stack_pop(&ccl->stack) + ccl_stack_pop(&ccl->stack)); - return frame; -} diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 3ab2608..0000000 --- a/src/main.c +++ /dev/null @@ -1,66 +0,0 @@ -#include "main.h" - -#include <getopt.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> -#include <stdnoreturn.h> - -#include "platform/getch.h" -#include "3cl.h" -#include "readfile.h" - - -const char *program_name; - -static const char *const usage_message = - "usage: %s [-h] file\n"; -static const char *const usage_description = - "ccl language interpreter\n" - "Arguments:\n" - " file file to execute\n" - "Options\n" - " -h, --help show this help message and quit\n"; - -static struct option long_options[] = -{ - {"help", no_argument, NULL, 'b'}, - {0} -}; - -noreturn void usage(bool full) -{ - printf(usage_message, program_name); - if (full) printf(usage_description); - exit(full ? 0 : 1); -} - -int main(int argc, char **argv) -{ - program_name = argv[0]; - getch_init(); - - int ch; - while ((ch = getopt_long(argc, argv, "h", long_options, NULL)) != EOF) - { - switch (ch) - { - case 'h': - usage(true); - break; - default: - usage(false); - break; - } - } - if (argv[optind] == NULL) - usage(false); - - char *code = readfile(argv[optind]); - struct CCL ccl; - ccl_init(&ccl, code, getch, (void(*)(int))putchar); - ccl_exec(&ccl); - free(code); - ccl_free(&ccl); - return 0; -} diff --git a/src/platform/getch.c b/src/platform/getch.c deleted file mode 100644 index 48c846e..0000000 --- a/src/platform/getch.c +++ /dev/null @@ -1,53 +0,0 @@ -#ifdef _WIN32 -void getch_init(void) {} -#else -#include <signal.h> -#include <stdio.h> -#include <stdlib.h> -#include <stdnoreturn.h> -#include <termios.h> -#include <unistd.h> - -static struct termios default_state = {0}; - -int getch(void) -{ - int ch = 0; - struct termios old = {0}; - fflush(stdout); - - tcgetattr(0, &old); - old.c_lflag &= ~ICANON; - - tcsetattr(0, TCSANOW, &old); - ch = getchar(); - tcsetattr(0, TCSADRAIN, &default_state); - - return ch; -} - -static void get_reset_terminal_state(void) -{ - tcsetattr(0, TCSANOW, &default_state); -} - -static noreturn void get_reset_terminal_state_handler(int sig) -{ - get_reset_terminal_state(); - _Exit(sig); -} - -void getch_init(void) -{ - tcgetattr(0, &default_state); - - atexit(get_reset_terminal_state); - - signal(SIGINT, get_reset_terminal_state_handler); - signal(SIGABRT, get_reset_terminal_state_handler); - signal(SIGFPE, get_reset_terminal_state_handler); - signal(SIGILL, get_reset_terminal_state_handler); - signal(SIGSEGV, get_reset_terminal_state_handler); - signal(SIGTERM, get_reset_terminal_state_handler); -} -#endif /* _WIN32 */ diff --git a/src/readchar.c b/src/readchar.c deleted file mode 100644 index 428b8fd..0000000 --- a/src/readchar.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "3cl.h" -#include "readchar.h" - -#include <stdbool.h> -#include <string.h> - -#include "utils.h" - - -static const char *const space = " \n\t"; -static const char *const brackets = "{[(?;)]}"; -static const char *const instr = "^+-*~%=!$&<>@#:"; -static const char *const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - -char ccl_readchar(struct CCL *ccl, struct CCLFrame *frame, enum CCLRCFlags flags) -{ - bool iscomment; - - char chr; - while ((chr = ccl->code[frame->ep++]) != '\0') - { - if (iscomment && chr == '\n') - iscomment = false; - if (iscomment) - continue; - if (chr == '/') - iscomment = true; - - if (strchr(space, chr)) - continue; - - if (strchr(brackets, chr)) - if (!(flags & CCL_RC_BRACK) && flags & CCL_RC_DIE) - goto invalid; - else - goto ok; - else if (strchr(instr, chr)) - if (!(flags & CCL_RC_IS) && flags & CCL_RC_DIE) - goto invalid; - else - goto ok; - else if (strchr(alphabet, chr)) - if (!(flags & CCL_RC_ALP) && flags & CCL_RC_DIE) - goto invalid; - else - goto ok; - else if (chr == '_') - if (!(flags & CCL_RC_US) && flags & CCL_RC_DIE) - goto invalid; - else - goto ok; - else - goto invalid; - } -ok: - return chr; -invalid: - die(1, "Invalid symbol at %d", frame->ep); -} diff --git a/src/readfile.c b/src/readfile.c deleted file mode 100644 index fb756de..0000000 --- a/src/readfile.c +++ /dev/null @@ -1,33 +0,0 @@ -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "utils.h" - - -char *readfile(const char *name) -{ - char *code, chr; - FILE *file = fopen(name, "r"); - if (errno) die(1, strerror(errno)); - fseek(file, 0, SEEK_END); - size_t size = ftell(file); - fseek(file, 0, SEEK_SET); - code = calloc(size + 1, sizeof(*code)); - - for (int i = 0; i < size; ++i) - { - if ((chr = fgetc(file)) == EOF) - { - if (errno) - die(1, strerror(errno)); - else - break; - } - code[i] = chr; - } - fclose(file); - - return code; -} diff --git a/src/stack.c b/src/stack.c deleted file mode 100644 index 3a1f697..0000000 --- a/src/stack.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "3cl.h" - -#include <stdlib.h> - - -static inline void resize(struct CCLStack *stack) -{ - stack->length *= 2; - stack->stack = realloc(stack->stack, stack->length * sizeof(*stack->stack)); -} - -void ccl_stack_push(struct CCLStack *stack, CCLNum num) -{ - if (stack->cur + 1 == stack->length) - resize(stack); - stack->stack[++stack->cur] = num; -} - -CCLNum ccl_stack_pop(struct CCLStack *stack, CCLNum num) -{ - return stack->stack[stack->cur--]; -} diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index ba5638f..0000000 --- a/src/utils.c +++ /dev/null @@ -1,20 +0,0 @@ -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <stdnoreturn.h> - -#include "main.h" - - -noreturn void die(int code, char *fmt, ...) -{ - va_list args; - - fprintf(stderr, "%s: ", program_name); - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - putc('\n', stderr); - - exit(code); -} diff --git a/src/variable.c b/src/variable.c deleted file mode 100644 index b764c28..0000000 --- a/src/variable.c +++ /dev/null @@ -1,58 +0,0 @@ -#include "3cl.h" -#include "variable.h" - -#include <stdbool.h> -#include <stdlib.h> - - -struct CCLVariable *ccl_variable_get(struct CCLVariable *vars, char name) -{ - for (struct CCLVariable *var = vars;;var = var->next) - if (var->name == name) - return var; - else if (var->name == '_' || var->next == NULL) - return NULL; -} - -struct CCLVariable *ccl_variable_getany(struct CCL *ccl, struct CCLFrame *frame, char name) -{ - bool root = false; - struct CCLFrame *curframe; - struct CCLVariable *found; - - for (curframe = frame; curframe->type != CCL_PROC && curframe->type != CCL_ROOT; curframe = curframe->prev); - if (curframe->type == CCL_ROOT) - root = true; - - found = ccl_variable_get(&curframe->vars, name); - if (!found && !root) - return ccl_variable_get(&ccl->rootframe.vars, name); - - return NULL; -} - -struct CCLVariable *ccl_variable_set(struct CCLVariable *vars, char name, CCLNum value) -{ - struct CCLVariable *toset; - struct CCLVariable var = (struct CCLVariable) - { - .next = NULL, - .name = name, - .value = value, - }; - - if (vars->name == '_') - { - toset = vars; - var.prev = NULL; - } else - { - for (toset = vars; toset->next != NULL; toset = toset->next); - toset->next = (struct CCLVariable *)malloc(sizeof(*toset->next)); - var.prev = toset; - toset = toset->next; - } - - *toset = var; - return toset; -} |