DEADSOFTWARE

Добавлена конструкция WHILE-DO
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Thu, 3 Aug 2017 10:38:25 +0000 (13:38 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Thu, 3 Aug 2017 10:38:25 +0000 (13:38 +0300)
src/oberon.c
src/test.c

index b428ecf73f6bda15c85365a8d49a5dd6aafb45ad..8a84e1359dcd48f368f41cd945a76bb92a11b599 100644 (file)
@@ -65,7 +65,9 @@ enum {
        IF,
        THEN,
        ELSE,
-       ELSIF
+       ELSIF,
+       WHILE,
+       DO
 };
 
 // =======================================================================
@@ -400,6 +402,14 @@ oberon_read_ident(oberon_context_t * ctx)
        {
                ctx -> token = ELSIF;
        }
+       else if(strcmp(ident, "WHILE") == 0)
+       {
+               ctx -> token = WHILE;
+       }
+       else if(strcmp(ident, "DO") == 0)
+       {
+               ctx -> token = DO;
+       }
 }
 
 static void
@@ -2951,6 +2961,31 @@ oberon_statement(oberon_context_t * ctx)
                oberon_generate_label(ctx, end);
                oberon_assert_token(ctx, END);          
        }
+       else if(ctx -> token == WHILE)
+       {
+               gen_label_t * begin;
+               gen_label_t * end;
+               oberon_expr_t * cond;
+
+               begin = oberon_generator_reserve_label(ctx);
+               end = oberon_generator_reserve_label(ctx);
+
+               oberon_assert_token(ctx, WHILE);
+               oberon_generate_label(ctx, begin);
+               cond = oberon_expr(ctx);
+               if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
+               {
+                       oberon_error(ctx, "condition must be boolean");
+               }
+               oberon_generate_branch(ctx, cond, false, end);
+
+               oberon_assert_token(ctx, DO);
+               oberon_statement_seq(ctx);
+               oberon_generate_goto(ctx, begin);
+
+               oberon_assert_token(ctx, END);
+               oberon_generate_label(ctx, end);
+       }
        else if(ctx -> token == RETURN)
        {
                oberon_assert_token(ctx, RETURN);
index bcc8e86c8fe1c3a607d1d235ef29bd3d92c62820..549e5eaa69e890f6c70b2c053474152028f2f571 100644 (file)
@@ -13,16 +13,11 @@ static char source_test[] =
        "  i : INTEGER;"
        ""
        "BEGIN"
-       "  i := 4;"
+       "  i := 0;"
        "  Out.Open();"
-       "  IF i = 0 THEN"
-       "    Out.String('Branch 0'); Out.Ln;"
-       "  ELSIF i = 1 THEN"
-       "    Out.String('Branch 1'); Out.Ln;"
-       "  ELSIF i = 2 THEN"
-       "    Out.String('Branch 2'); Out.Ln;"
-       "  ELSE"
-       "    Out.String('Branch else'); Out.Ln;"
+       "  WHILE i < 4 DO"
+       "    Out.String('Count '); Out.Int(i, 0); Out.Ln;"
+       "    i := i + 1;"
        "  END;"
        "  Out.String('end'); Out.Ln;"
        "END Test."