From e9b64944925eadc5022edfee26281a4814fd9124 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Thu, 3 Aug 2017 13:28:52 +0300 Subject: [PATCH] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=86?= =?utf8?q?=D0=B8=D1=8F=20ELSIF-THEN?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/oberon.c | 26 +++++++++++++-- src/test.c | 89 +++++++--------------------------------------------- 2 files changed, 35 insertions(+), 80 deletions(-) diff --git a/src/oberon.c b/src/oberon.c index f2c9014..b428ecf 100644 --- a/src/oberon.c +++ b/src/oberon.c @@ -64,7 +64,8 @@ enum { STRING, IF, THEN, - ELSE + ELSE, + ELSIF }; // ======================================================================= @@ -395,6 +396,10 @@ oberon_read_ident(oberon_context_t * ctx) { ctx -> token = ELSE; } + else if(strcmp(ident, "ELSIF") == 0) + { + ctx -> token = ELSIF; + } } static void @@ -2918,8 +2923,25 @@ oberon_statement(oberon_context_t * ctx) oberon_generate_branch(ctx, cond, false, els); oberon_statement_seq(ctx); oberon_generate_goto(ctx, end); - oberon_generate_label(ctx, els); + + while(ctx -> token == ELSIF) + { + els = oberon_generator_reserve_label(ctx); + + oberon_assert_token(ctx, ELSIF); + cond = oberon_expr(ctx); + if(cond -> result -> class != OBERON_TYPE_BOOLEAN) + { + oberon_error(ctx, "condition must be boolean"); + } + oberon_assert_token(ctx, THEN); + oberon_generate_branch(ctx, cond, false, els); + oberon_statement_seq(ctx); + oberon_generate_goto(ctx, end); + oberon_generate_label(ctx, els); + } + if(ctx -> token == ELSE) { oberon_assert_token(ctx, ELSE); diff --git a/src/test.c b/src/test.c index d250161..bcc8e86 100644 --- a/src/test.c +++ b/src/test.c @@ -4,94 +4,27 @@ #include "../include/oberon.h" -/* static char source_test[] = "(* Main module *)" "MODULE Test;" "IMPORT Out;" "" - "TYPE" - " R = LONGINT;" - "" - "PROCEDURE Factorial(n : R) : R;" - "BEGIN" - " IF n <= 1 THEN" - " RETURN 1;" - " ELSE" - " RETURN n * Factorial(n - 1);" - " END;" - " RETURN 0; (* Детектор ретурнов - дерьмо *)" - "END Factorial;" + "VAR" + " i : INTEGER;" "" "BEGIN" + " i := 4;" " Out.Open();" - " Out.Int(Factorial(0), 0); Out.Ln;" - " Out.Int(Factorial(1), 0); Out.Ln;" - " Out.Int(Factorial(2), 0); Out.Ln;" - " Out.Int(Factorial(3), 0); Out.Ln;" - " Out.Int(Factorial(4), 0); Out.Ln;" - " Out.Int(Factorial(5), 0); Out.Ln;" - " Out.Int(Factorial(6), 0); Out.Ln;" - " Out.Int(Factorial(7), 0); Out.Ln;" - " Out.Int(Factorial(8), 0); Out.Ln;" - " Out.Int(Factorial(9), 0); Out.Ln;" - " Out.Int(Factorial(10), 0); Out.Ln;" - " Out.Int(Factorial(11), 0); Out.Ln;" - " Out.Int(Factorial(12), 0); Out.Ln;" - " Out.Int(Factorial(13), 0); Out.Ln;" - " Out.Int(Factorial(14), 0); Out.Ln;" - " Out.Int(Factorial(15), 0); Out.Ln;" - " Out.Int(Factorial(16), 0); Out.Ln;" - " Out.Int(Factorial(17), 0); Out.Ln;" - " Out.Int(Factorial(18), 0); Out.Ln;" - " Out.Int(Factorial(19), 0); Out.Ln;" - " Out.Int(Factorial(20), 0); Out.Ln;" - "END Test." -; -*/ - -static char source_test[] = - "(* Main module *)" - "MODULE Test;" - "IMPORT Out;" - "" - "TYPE" - " R = LONGREAL;" - "" - "PROCEDURE Factorial(n : R) : R;" - "BEGIN" - " IF n <= 1 THEN" - " RETURN 1;" + " 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" - " RETURN n * Factorial(n - 1);" + " Out.String('Branch else'); Out.Ln;" " END;" - " RETURN 0; (* Детектор ретурнов - дерьмо *)" - "END Factorial;" - "" - "BEGIN" - " Out.Open();" - " Out.LongReal(Factorial(0), 0); Out.Ln;" - " Out.LongReal(Factorial(1), 0); Out.Ln;" - " Out.LongReal(Factorial(2), 0); Out.Ln;" - " Out.LongReal(Factorial(3), 0); Out.Ln;" - " Out.LongReal(Factorial(4), 0); Out.Ln;" - " Out.LongReal(Factorial(5), 0); Out.Ln;" - " Out.LongReal(Factorial(6), 0); Out.Ln;" - " Out.LongReal(Factorial(7), 0); Out.Ln;" - " Out.LongReal(Factorial(8), 0); Out.Ln;" - " Out.LongReal(Factorial(9), 0); Out.Ln;" - " Out.LongReal(Factorial(10), 0); Out.Ln;" - " Out.LongReal(Factorial(11), 0); Out.Ln;" - " Out.LongReal(Factorial(12), 0); Out.Ln;" - " Out.LongReal(Factorial(13), 0); Out.Ln;" - " Out.LongReal(Factorial(14), 0); Out.Ln;" - " Out.LongReal(Factorial(15), 0); Out.Ln;" - " Out.LongReal(Factorial(16), 0); Out.Ln;" - " Out.LongReal(Factorial(17), 0); Out.Ln;" - " Out.LongReal(Factorial(18), 0); Out.Ln;" - " Out.LongReal(Factorial(19), 0); Out.Ln;" - " Out.LongReal(Factorial(20), 0); Out.Ln;" - " Out.LongReal(Factorial(170), 0); Out.Ln;" + " Out.String('end'); Out.Ln;" "END Test." ; -- 2.29.2