From 4e58c0e61815196bcf87405ab9d070631bc72f90 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Sat, 19 Aug 2017 12:27:16 +0300 Subject: [PATCH] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=20=D1=81=D1=87=D1=91=D1=82=20=D1=81=D1=82=D1=80=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Test.obn | 20 ---------- notes | 4 +- src/backends/jvm/generator-jvm-abi.c | 1 - src/backends/jvm/generator-jvm-asm.c | 1 - src/backends/jvm/generator-jvm-basic.c | 1 - src/backends/jvm/generator-jvm.c | 1 - src/main.c | 43 ++++++++++++++------- src/oberon-common.c | 7 +--- src/oberon-internals.h | 28 +++++++++++++- src/oberon-type-compat.c | 4 +- src/oberon.c | 52 ++++++++++++++++++++------ {include => src}/oberon.h | 0 12 files changed, 101 insertions(+), 61 deletions(-) rename {include => src}/oberon.h (100%) diff --git a/Test.obn b/Test.obn index f47edbb..6fae529 100644 --- a/Test.obn +++ b/Test.obn @@ -1,23 +1,3 @@ MODULE Test; -IMPORT Files; - -TYPE - String = POINTER TO ARRAY OF CHAR; - R* = RECORD ch : CHAR END; - -VAR - f : Files.File; - r : Files.Rider; - m : R; - s : String; - -BEGIN - f := Files.Old("Test.obn"); - ASSERT(f # NIL); - Files.Set(r, f, 0); - - Files.Read(r, m.ch); - - ASSERT(s = NIL); END Test. diff --git a/notes b/notes index 0fa3ba8..71fcaf3 100644 --- a/notes +++ b/notes @@ -1,13 +1,15 @@ +- Нужно передавать информацию о файле и строках в кодогенератор. - Нет процедур привязанных к типм (10.2) - Не полная реализация модуля Files * Не реализована запись в файл +- Нужно передлать кодогенератор что бы складировать код в структурах а не напрямую срать в файлы + - Не полная реализация модуля Strings * Не реализованы процедуры Insert Append Delete Replace Extract Pos - Сделать проверку повторов в CASE. - Сделать нормальную проверку наличия RETURN. -- Нет счёта строк / столбцов (хрен с ними - у меня есть утилита tail!) - Нужны средства создания биндингов. (oakwood 3.5) - Любая ошибка фатальна diff --git a/src/backends/jvm/generator-jvm-abi.c b/src/backends/jvm/generator-jvm-abi.c index 2889ae3..d1e87f1 100644 --- a/src/backends/jvm/generator-jvm-abi.c +++ b/src/backends/jvm/generator-jvm-abi.c @@ -8,7 +8,6 @@ #include -#include "../../../include/oberon.h" #include "../../oberon-internals.h" #include "generator-jvm.h" #include "generator-jvm-abi.h" diff --git a/src/backends/jvm/generator-jvm-asm.c b/src/backends/jvm/generator-jvm-asm.c index 1d1220d..bd1812c 100644 --- a/src/backends/jvm/generator-jvm-asm.c +++ b/src/backends/jvm/generator-jvm-asm.c @@ -8,7 +8,6 @@ #include -#include "../../../include/oberon.h" #include "../../oberon-internals.h" #include "generator-jvm.h" #include "generator-jvm-abi.h" diff --git a/src/backends/jvm/generator-jvm-basic.c b/src/backends/jvm/generator-jvm-basic.c index 2baed65..e4c6c15 100644 --- a/src/backends/jvm/generator-jvm-basic.c +++ b/src/backends/jvm/generator-jvm-basic.c @@ -7,7 +7,6 @@ #include -#include "../../../include/oberon.h" #include "../../oberon-internals.h" #include "generator-jvm.h" #include "generator-jvm-basic.h" diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index eb44591..e4ea383 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -8,7 +8,6 @@ #include -#include "../../../include/oberon.h" #include "../../oberon-internals.h" #include "generator-jvm.h" #include "generator-jvm-abi.h" diff --git a/src/main.c b/src/main.c index 754d90a..9d471e6 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,8 @@ #include #include -#include "../include/oberon.h" +#include "oberon-internals.h" +#include "generator.h" struct string_stack { @@ -27,47 +28,61 @@ add_to_stack(struct string_stack ** stack, char * string) } static FILE * -open_file(struct string_stack * path, const char * ext, const char * name, const char * mode) +open_file(struct string_stack * path, const char * ext, const char * name, const char * mode, oberon_scanner_t * res) { + char fname[256]; + memset(fname, 0, 256); + FILE * fp = NULL; while(fp == NULL && path != NULL) { - char fname[256]; snprintf(fname, 256, "%s/%s.%s", path -> string, name, ext); fp = fopen(fname, mode); path = path -> next; } + + if(fp == NULL) + { + snprintf(fname, 256, "%s.%s", name, ext); + fp = fopen(fname, mode); + } + + size_t len = strlen(fname); + res -> source = malloc(len + 1); + memcpy(res -> source, fname, len); + return fp; } -static const char * +static oberon_scanner_t * import_module(const char * name) { + oberon_scanner_t * res = malloc(sizeof *res); + memset(res, 0, sizeof *res); + FILE * fp; - fp = open_file(path_list, "obn", name, "r"); + fp = open_file(path_list, "obn", name, "r", res); if(fp == NULL) { return NULL; } - char * source; size_t len; fseek(fp, 0, SEEK_END); len = ftell(fp); fseek(fp, 0, SEEK_SET); - source = calloc(1, len + 1); - fread(source, len, 1, fp); + res -> code = calloc(1, len + 1); + fread(res -> code, len, 1, fp); - return source; + return res; } static void init(int argc, char ** argv) { int i = 1; - add_to_stack(&path_list, "."); out_path = "."; while(argc > 2) @@ -102,12 +117,12 @@ init(int argc, char ** argv) int main(int argc, char ** argv) { - const char * code; + oberon_scanner_t * s; init(argc, argv); - code = import_module(module); - if(code == NULL) + s = import_module(module); + if(s == NULL) { printf("can't open module source %s\n", module); return 0; @@ -116,7 +131,7 @@ main(int argc, char ** argv) ctx = oberon_create_context(import_module); oberon_set_out_directory(ctx, out_path); - mod = oberon_compile_module(ctx, code); + mod = oberon_compile_module(ctx, s); oberon_destroy_context(ctx); diff --git a/src/oberon-common.c b/src/oberon-common.c index 01f2c9e..997b7d2 100644 --- a/src/oberon-common.c +++ b/src/oberon-common.c @@ -2,8 +2,6 @@ #include #include -#include "../include/oberon.h" - #include "oberon-internals.h" void @@ -11,12 +9,9 @@ oberon_error(oberon_context_t * ctx, const char * fmt, ...) { va_list ptr; va_start(ptr, fmt); - printf("error: "); + printf("%s: (%i:%i) ", ctx -> loc.source, ctx -> loc.line, ctx -> loc.col); vprintf(fmt, ptr); printf("\n"); - printf(" code_index = %i\n", ctx -> code_index); - printf(" c = %c\n", ctx -> c); - printf(" token = %i\n", ctx -> token); va_end(ptr); exit(1); } diff --git a/src/oberon-internals.h b/src/oberon-internals.h index ad2ecc6..d4f3cf2 100644 --- a/src/oberon-internals.h +++ b/src/oberon-internals.h @@ -17,6 +17,8 @@ typedef struct oberon_module_t oberon_module_t; typedef struct oberon_context_t oberon_context_t; typedef struct oberon_scope_t oberon_scope_t; +typedef struct oberon_location_t oberon_location_t; +typedef struct oberon_scanner_t oberon_scanner_t; typedef struct oberon_item_t oberon_item_t; typedef struct oberon_oper_t oberon_oper_t; typedef union oberon_expr_t oberon_expr_t; @@ -130,13 +132,28 @@ struct oberon_module_t gen_module_t * gen_mod; }; -typedef const char * (*ModuleImportCallback)(const char * name); +struct oberon_location_t +{ + const char * source; + int line; + int col; +}; + +struct oberon_scanner_t +{ + char * source; + char * code; +}; + +typedef oberon_scanner_t * (*ModuleImportCallback)(const char * name); struct oberon_context_t { /*** SCANER DATA ***/ const char * code; int code_index; + oberon_location_t loc; + oberon_location_t xloc; char c; int token; @@ -278,4 +295,13 @@ union oberon_expr_t oberon_oper_t oper; }; +extern oberon_context_t * +oberon_create_context(ModuleImportCallback import_module); + +extern void +oberon_destroy_context(oberon_context_t * ctx); + +extern oberon_module_t * +oberon_compile_module(oberon_context_t * ctx, oberon_scanner_t * s); + #endif // OBERON_INTERNALS_H diff --git a/src/oberon-type-compat.c b/src/oberon-type-compat.c index a308499..964f0a0 100644 --- a/src/oberon-type-compat.c +++ b/src/oberon-type-compat.c @@ -7,11 +7,9 @@ #include #include -#include "../include/oberon.h" - -#include "oberon-common.h" #include "oberon-internals.h" #include "oberon-type-compat.h" +#include "oberon-common.h" bool oberon_is_array_type(oberon_type_t * t) diff --git a/src/oberon.c b/src/oberon.c index 29c40cb..e684e65 100644 --- a/src/oberon.c +++ b/src/oberon.c @@ -10,8 +10,6 @@ #include -#include "../include/oberon.h" - #include "oberon-internals.h" #include "oberon-type-compat.h" #include "oberon-common.h" @@ -362,15 +360,20 @@ oberon_get_char(oberon_context_t * ctx) if(ctx -> code[ctx -> code_index]) { ctx -> code_index += 1; + ctx -> xloc.col += 1; ctx -> c = ctx -> code[ctx -> code_index]; } } static void -oberon_init_scaner(oberon_context_t * ctx, const char * code) +oberon_init_scaner(oberon_context_t * ctx, oberon_scanner_t * s) { - ctx -> code = code; + ctx -> code = s -> code; ctx -> code_index = 0; + ctx -> xloc.source = s -> source; + ctx -> xloc.line = 1; + ctx -> xloc.col = 1; + ctx -> loc = ctx -> xloc; ctx -> c = ctx -> code[ctx -> code_index]; } @@ -694,7 +697,26 @@ oberon_skip_space(oberon_context_t * ctx) { while(isspace(ctx -> c)) { - oberon_get_char(ctx); + if(ctx -> c == 0xD) + { + oberon_get_char(ctx); + if(ctx -> c == 0xA) + { + oberon_get_char(ctx); + } + ctx -> xloc.line += 1; + ctx -> xloc.col = 1; + } + else if(ctx -> c == 0xA) + { + oberon_get_char(ctx); + ctx -> xloc.line += 1; + ctx -> xloc.col = 1; + } + else + { + oberon_get_char(ctx); + } } } @@ -910,6 +932,8 @@ oberon_read_token(oberon_context_t * ctx) { oberon_skip_space(ctx); + ctx -> loc = ctx -> xloc; + int c = ctx -> c; if(isalpha(c) || c == '_') { @@ -3567,14 +3591,14 @@ oberon_import_module(oberon_context_t * ctx, char * alias, char * name) if(m == NULL) { - const char * code; - code = ctx -> import_module(name); - if(code == NULL) + oberon_scanner_t * s; + s = ctx -> import_module(name); + if(s == NULL) { - oberon_error(ctx, "no such module"); + oberon_error(ctx, "no such module %s", name); } - m = oberon_compile_module(ctx, code); + m = oberon_compile_module(ctx, s); assert(m); } @@ -4878,10 +4902,12 @@ oberon_destroy_context(oberon_context_t * ctx) } oberon_module_t * -oberon_compile_module(oberon_context_t * ctx, const char * newcode) +oberon_compile_module(oberon_context_t * ctx, oberon_scanner_t * s) { const char * code = ctx -> code; int code_index = ctx -> code_index; + oberon_location_t loc = ctx -> loc; + oberon_location_t xloc = ctx -> xloc; char c = ctx -> c; int token = ctx -> token; char * string = ctx -> string; @@ -4903,13 +4929,15 @@ oberon_compile_module(oberon_context_t * ctx, const char * newcode) ctx -> mod = module; ctx -> module_list = module; - oberon_init_scaner(ctx, newcode); + oberon_init_scaner(ctx, s); oberon_parse_module(ctx); module -> ready = 1; ctx -> code = code; ctx -> code_index = code_index; + ctx -> loc = loc; + ctx -> xloc = xloc; ctx -> c = c; ctx -> token = token; ctx -> string = string; diff --git a/include/oberon.h b/src/oberon.h similarity index 100% rename from include/oberon.h rename to src/oberon.h -- 2.29.2