DEADSOFTWARE

Добавлены комментарии
[dsw-obn.git] / oberon.c
index b56a458a6c1e979a743c9d4139b5df3044f1cdc3..e857b1d1ec6f93e454b5439cdcf81f96c150d167 100644 (file)
--- 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;
        }
 }