diff options
| author | Nakidai <plaza521@inbox.ru> | 2024-07-05 00:04:56 +0300 |
|---|---|---|
| committer | Nakidai <plaza521@inbox.ru> | 2024-07-05 00:04:56 +0300 |
| commit | a9c159f5f7bf3479c3236735960597b4bc36a204 (patch) | |
| tree | f325e249f11a4bb8518f01151114b1239207a33b /src | |
| download | 3cl-a9c159f5f7bf3479c3236735960597b4bc36a204.tar.gz 3cl-a9c159f5f7bf3479c3236735960597b4bc36a204.zip | |
Add code
Diffstat (limited to 'src')
| -rw-r--r-- | src/cccl.c | 80 | ||||
| -rw-r--r-- | src/main.c | 56 | ||||
| -rw-r--r-- | src/utils.c | 23 |
3 files changed, 159 insertions, 0 deletions
diff --git a/src/cccl.c b/src/cccl.c new file mode 100644 index 0000000..1803fcf --- /dev/null +++ b/src/cccl.c @@ -0,0 +1,80 @@ +#include "cccl.h" +#include "types.h" + +#include <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +#include "cvector.h" +#include "utils.h" + + +static cccl pc = {0}; + +void cccl_init(s8 *filename) +{ + memset(&pc, 0, sizeof(pc)); + + cvector_init(pc.stack, 1, NULL); + cvector_init(pc.variables, 1, NULL); + cvector_init(pc.procedures, 1, NULL); + cvector_init(pc.ep_stack, 1, NULL); + if (errno) die(1, strerror(errno)); + + pc.filename = filename; +} + +void cccl_free() +{ + cvector_free(pc.stack); + cvector_free(pc.variables); + cvector_free(pc.procedures); + cvector_free(pc.ep_stack); + free(pc.code); +} + +void cccl_read(void) +{ + s8 chr; + FILE *file = fopen(pc.filename, "r"); + if (errno) die(1, strerror(errno)); + fseek(file, 0, SEEK_END); + pc.size = ftell(file); + fseek(file, 0, SEEK_SET); + pc.code = calloc(pc.size + 1, sizeof(s8)); + + for (u32 i = 0; i < pc.size; ++i) + { + if ((chr = fgetc(file)) == EOF) + { + if (errno) + { + fclose(file); + die(1, strerror(errno)); + } + else + { + break; + } + } + pc.code[i] = chr; + } + fclose(file); +} + +void cccl_run(void) +{ + bool is_comment; + for (pc.ep = 0; pc.ep < pc.size; ++pc.ep) + { + if (is_comment && pc.code[pc.ep] == '\n') + is_comment = false; + switch (pc.code[pc.ep]) + { + case '/': + is_comment = true; + break; + } + } +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..36cdf5b --- /dev/null +++ b/src/main.c @@ -0,0 +1,56 @@ +#include "main.h" +#include "types.h" + +#include <getopt.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdnoreturn.h> + + +const s8 *program_name; + +static const s8 *const usage_message = + "usage: %s [-h] file\n"; +static const s8 *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(i32 argc, s8 **argv) +{ + program_name = argv[0]; + + i32 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); + + return 0; +} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..ee37110 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,23 @@ +#include "utils.h" +#include "types.h" + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdnoreturn.h> + +#include "main.h" + + +noreturn void die(i32 code, s8 *fmt, ...) +{ + va_list args; + + fprintf(stderr, "%s: ", program_name); + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + exit(code); +} |