DEADSOFTWARE

Добавлена конструкция CASE
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 4 Aug 2017 19:22:19 +0000 (22:22 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 4 Aug 2017 19:22:19 +0000 (22:22 +0300)
jvm_test.sh
notes
src/backends/jvm/generator-jvm.c
src/oberon.c
src/test.c

index 0ec37e70c47df3ee58ea7b335045217189b1f12c..6519656ef1f13317b42551fb8e3b63bf9a35f1cc 100755 (executable)
@@ -16,7 +16,7 @@ jasmin -d classes *.j
 javac -d classes rtl/*.java
 
 # -a -- for asm as comments
-#jad -o -b -noinner classes/*
+jad -o -b -noinner classes/*
 
 proguard -injars classes \
        -libraryjars /usr/lib/jvm/java-8-openjdk/jre/lib/rt.jar \
diff --git a/notes b/notes
index 705055ee658ce8321a65f8f3b71c7126f4d291b0..2c9c569342c3efc26308c142cb758ebfdba52ccd 100644 (file)
--- a/notes
+++ b/notes
@@ -1,4 +1,3 @@
-- Нет конструкции CASE
 - Нет конструкции WITH
 
 - Нет модуля SYSTEM
index 03b29f8ae7fed461f5fa3f452b0bdd986b5a5b93..a107efc119eca8a22afc620cbc912e8e284f86fa 100644 (file)
@@ -1690,7 +1690,7 @@ jvm_generate_logical_or(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
        /* a OR b -- если a, то TRUE, иначе b */
 
        push_expr(p, a);
-       jvm_generate(p, 1, 0, "ifne %s", label_name_calc_b);
+       jvm_generate(p, 1, 0, "ifeq %s", label_name_calc_b);
        jvm_generate(p, 0, 1, "iconst_1");
        jvm_generate(p, 0, 0, "goto %s", label_name_done);
        jvm_generate_label(p, label_calc_b);
@@ -1709,7 +1709,7 @@ jvm_generate_logical_and(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
        /* a AND b -- если a, то b, иначе FALSE */
 
        push_expr(p, a);
-       jvm_generate(p, 1, 0, "ifne %s", label_name_false);
+       jvm_generate(p, 1, 0, "ifeq %s", label_name_false);
        push_expr(p, b);
        jvm_generate(p, 0, 0, "goto %s", label_name_done);
        jvm_generate_label(p, label_false);
index 38e24a8da8b963669a406220cef798290e3b43ef..3b4a3747b904d0dbef91c4bb3f2ee11960e19679 100644 (file)
@@ -77,7 +77,9 @@ enum {
        EXIT,
        LBRACE,
        RBRACE,
-       DOTDOT
+       DOTDOT,
+       CASE,
+       BAR
 };
 
 // =======================================================================
@@ -461,6 +463,10 @@ oberon_read_ident(oberon_context_t * ctx)
        {
                ctx -> token = EXIT;
        }
+       else if(strcmp(ident, "CASE") == 0)
+       {
+               ctx -> token = CASE;
+       }
 }
 
 static void
@@ -822,6 +828,10 @@ oberon_read_symbol(oberon_context_t * ctx)
                        ctx -> token = RBRACE;
                        oberon_get_char(ctx);
                        break;
+               case '|':
+                       ctx -> token = BAR;
+                       oberon_get_char(ctx);
+                       break;
                default:
                        oberon_error(ctx, "invalid char %c", ctx -> c);
                        break;
@@ -3171,6 +3181,101 @@ oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
        oberon_generate_assign(ctx, src, dst);
 }
 
+static oberon_expr_t *
+oberon_case_labels(oberon_context_t * ctx, oberon_expr_t * val)
+{
+       oberon_expr_t * e1;
+       oberon_expr_t * e2;
+       oberon_expr_t * cond;
+       oberon_expr_t * cond2;
+
+       e1 = (oberon_expr_t *) oberon_const_expr(ctx);
+       oberon_autocast_to(ctx, e1, val -> result);
+       
+       e2 = NULL;
+       if(ctx -> token == DOTDOT)
+       {
+               oberon_assert_token(ctx, DOTDOT);
+               e2 = (oberon_expr_t *) oberon_const_expr(ctx);
+               oberon_autocast_to(ctx, e2, val -> result);
+       }
+
+       if(e2 == NULL)
+       {
+               /* val == e1 */
+               cond = oberon_make_bin_op(ctx, EQUAL, val, e1);
+       }
+       else
+       {
+               /* val >= e1 && val <= e2 */
+               cond = oberon_make_bin_op(ctx, GEQ, val, e1);
+               cond2 = oberon_make_bin_op(ctx, LEQ, val, e2);
+               cond = oberon_make_bin_op(ctx, AND, cond, cond2);
+       }
+
+       return cond;
+}
+
+static void
+oberon_case(oberon_context_t * ctx, oberon_expr_t * val, gen_label_t * end)
+{
+       oberon_expr_t * cond;
+       oberon_expr_t * cond2;
+       gen_label_t * this_end;
+
+       if(ISEXPR(ctx -> token))
+       {
+               this_end = oberon_generator_reserve_label(ctx);
+
+               cond = oberon_case_labels(ctx, val);
+               while(ctx -> token == COMMA)
+               {
+                       oberon_assert_token(ctx, COMMA);
+                       /* cond || cond2 */
+                       cond2 = oberon_case_labels(ctx, val);
+                       cond = oberon_make_bin_op(ctx, OR, cond, cond2);
+               }
+               oberon_assert_token(ctx, COLON);
+
+               oberon_generate_branch(ctx, cond, false, this_end);
+               oberon_statement_seq(ctx);
+               oberon_generate_goto(ctx, end);
+
+               oberon_generate_label(ctx, this_end);
+       }
+}
+
+static void
+oberon_case_statement(oberon_context_t * ctx)
+{
+       oberon_expr_t * val;
+       oberon_expr_t * expr;
+       gen_label_t * end;
+
+       end = oberon_generator_reserve_label(ctx);
+
+       oberon_assert_token(ctx, CASE);
+       expr = oberon_expr(ctx);
+       val = oberon_make_temp_var_item(ctx, expr -> result);
+       oberon_assign(ctx, expr, val);
+       oberon_assert_token(ctx, OF);
+       oberon_case(ctx, val, end);
+       while(ctx -> token == BAR)
+       {
+               oberon_assert_token(ctx, BAR);
+               oberon_case(ctx, val, end);
+       }
+
+       if(ctx -> token == ELSE)
+       {
+               oberon_assert_token(ctx, ELSE);
+               oberon_statement_seq(ctx);
+       }
+
+       oberon_generate_label(ctx, end);
+       oberon_assert_token(ctx, END);
+}
+
 static void
 oberon_statement(oberon_context_t * ctx)
 {
@@ -3377,6 +3482,10 @@ oberon_statement(oberon_context_t * ctx)
                }
                oberon_generate_goto(ctx, ctx -> decl -> exit_label);
        }
+       else if(ctx -> token == CASE)
+       {
+               oberon_case_statement(ctx);
+       }
        else if(ctx -> token == RETURN)
        {
                oberon_assert_token(ctx, RETURN);
index 25497b0d22502b0bb7c806ed817a873e36836781..0eb787ca9b45e4df2b49ec325567a651fd57636c 100644 (file)
@@ -9,13 +9,18 @@ static char source_test[] =
        "MODULE Test;"
        "IMPORT Out;"
        ""
+       "VAR"
+       "  i : INTEGER;"
+       ""
        "BEGIN"
        "  Out.Open;"
-       "  IF 8 IN { 2, 3, 7..10 } THEN"
-       "    Out.String('Yes'); Out.Ln;"
+       "  CASE 666 OF"
+       "    2, 3, 4, 5: Out.String('Zero');"
+       "  | 10..20, 24: Out.String('Holy shit');"
        "  ELSE"
-       "    Out.String('No'); Out.Ln;"
+       "    Out.String('Something else');"
        "  END;"
+       "  Out.Ln;"
        "END Test."
 ;