From 92fc7033b874920acf1b2f6e21bde51dcd0197f1 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Mon, 24 Jul 2017 22:57:29 +0300 Subject: [PATCH] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D1=8B=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80?= =?utf8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- notes | 2 -- oberon.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- test.c | 1 + 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/notes b/notes index c1a8b0b..4133bad 100644 --- a/notes +++ b/notes @@ -1,5 +1,3 @@ -- нету комментариев - - нету тестовых процедур для ввода-вывода - нету процедуры NEW - нету открытых массивов diff --git a/oberon.c b/oberon.c index b56a458..e857b1d 100644 --- a/oberon.c +++ b/oberon.c @@ -228,8 +228,11 @@ oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, in static void oberon_get_char(oberon_context_t * ctx) { - ctx -> code_index += 1; - ctx -> c = ctx -> code[ctx -> code_index]; + if(ctx -> code[ctx -> code_index]) + { + ctx -> code_index += 1; + ctx -> c = ctx -> code[ctx -> code_index]; + } } static void @@ -379,6 +382,43 @@ oberon_skip_space(oberon_context_t * ctx) } } +static void +oberon_read_comment(oberon_context_t * ctx) +{ + int nesting = 1; + while(nesting >= 1) + { + if(ctx -> c == '(') + { + oberon_get_char(ctx); + if(ctx -> c == '*') + { + oberon_get_char(ctx); + nesting += 1; + } + } + else if(ctx -> c == '*') + { + oberon_get_char(ctx); + if(ctx -> c == ')') + { + oberon_get_char(ctx); + nesting -= 1; + } + } + else if(ctx -> c == 0) + { + oberon_error(ctx, "unterminated comment"); + } + else + { + oberon_get_char(ctx); + } + } +} + +static void oberon_read_token(oberon_context_t * ctx); + static void oberon_read_symbol(oberon_context_t * ctx) { @@ -408,6 +448,12 @@ oberon_read_symbol(oberon_context_t * ctx) case '(': ctx -> token = LPAREN; oberon_get_char(ctx); + if(ctx -> c == '*') + { + oberon_get_char(ctx); + oberon_read_comment(ctx); + oberon_read_token(ctx); + } break; case ')': ctx -> token = RPAREN; @@ -450,6 +496,11 @@ oberon_read_symbol(oberon_context_t * ctx) case '*': ctx -> token = STAR; oberon_get_char(ctx); + if(ctx -> c == ')') + { + oberon_get_char(ctx); + oberon_error(ctx, "unstarted comment"); + } break; case '/': ctx -> token = SLASH; @@ -480,7 +531,7 @@ oberon_read_symbol(oberon_context_t * ctx) oberon_get_char(ctx); break; default: - oberon_error(ctx, "invalid char"); + oberon_error(ctx, "invalid char %c", ctx -> c); break; } } diff --git a/test.c b/test.c index 541b3a5..f1b175c 100644 --- a/test.c +++ b/test.c @@ -5,6 +5,7 @@ #include static char source_test[] = + "(* Main module *)" "MODULE Test;" "IMPORT I := Imported;" "VAR" -- 2.29.2