From: DeaDDooMER Date: Wed, 26 Jul 2017 19:57:29 +0000 (+0300) Subject: JVM: Добавлены логические OR и AND X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=ad7420eb4c7aba592f4d52ecea715b309fd82bf7;p=dsw-obn.git JVM: Добавлены логические OR и AND --- diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index b0c54ff..eb0f3dc 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -920,6 +920,44 @@ jvm_generate_operator(oberon_context_t * ctx, FILE *fp, char prefix, int op) } } +static void +jvm_generate_logical_or(oberon_context_t * ctx, FILE * fp, oberon_expr_t * p, oberon_expr_t * q) +{ + int label_calc_q = jvm_new_label_id(ctx); + int label_done = jvm_new_label_id(ctx); + char * label_name_calc_q = jvm_get_label_name(label_calc_q); + char * label_name_done = jvm_get_label_name(label_done); + + /* p OR q -- если p, то TRUE, иначе q */ + + push_expr(ctx, fp, p); + fprintf(fp, "ifne %s\n", label_name_calc_q); + fprintf(fp, "iconst_1\n"); + fprintf(fp, "goto %s\n", label_name_done); + jvm_generate_label(fp, label_calc_q); + push_expr(ctx, fp, q); + jvm_generate_label(fp, label_done); +} + +static void +jvm_generate_logical_and(oberon_context_t * ctx, FILE * fp, oberon_expr_t * p, oberon_expr_t * q) +{ + int label_false = jvm_new_label_id(ctx); + int label_done = jvm_new_label_id(ctx); + char * label_name_false = jvm_get_label_name(label_false); + char * label_name_done = jvm_get_label_name(label_done); + + /* p AND q -- если p, то q, иначе FALSE */ + + push_expr(ctx, fp, p); + fprintf(fp, "ifne %s\n", label_name_false); + push_expr(ctx, fp, q); + fprintf(fp, "goto %s\n", label_name_done); + jvm_generate_label(fp, label_false); + fprintf(fp, "iconst_0\n"); + jvm_generate_label(fp, label_done); +} + static void push_operator(oberon_context_t * ctx, FILE * fp, oberon_oper_t * oper) { @@ -955,6 +993,12 @@ push_operator(oberon_context_t * ctx, FILE * fp, oberon_oper_t * oper) jvm_generate_operator(ctx, fp, prefix, op); break; + case OP_LOGIC_OR: + jvm_generate_logical_or(ctx, fp, oper -> left, oper -> right); + break; + case OP_LOGIC_AND: + jvm_generate_logical_and(ctx, fp, oper -> left, oper -> right); + break; default: oberon_error(ctx, "push_oper: unk op %i", op); break; diff --git a/src/test.c b/src/test.c index 96e160a..6543c47 100644 --- a/src/test.c +++ b/src/test.c @@ -22,7 +22,7 @@ static char source_test[] = "BEGIN;" " x := ABS(-666);" " x := Tier(x);" - " b := ~TRUE;" + " b := TRUE OR FALSE;" "END Test." ;