From 5db2598eeeff9b6d11595bd7a4f3a001200b7ee6 Mon Sep 17 00:00:00 2001 From: jgjanes Date: Thu, 28 Apr 2022 20:07:33 -0700 Subject: [PATCH] Added for loop final implementation Co-authored-by: Rithvik Arun Co-authored-by: Joseph Hale Co-authored-by: Sai Nishanth Vaka --- data/rangeCount.md | 9 ++++----- src/runtime/codeable.pl | 23 ++++++++++++++--------- src/runtime/codeable_tests.pl | 4 ++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/data/rangeCount.md b/data/rangeCount.md index e2bed52..7b47c24 100644 --- a/data/rangeCount.md +++ b/data/rangeCount.md @@ -1,11 +1,10 @@ +fyi < rangeCount > +fyi < example usage of a for loop > + i stores 0 a stores 5 fyi < uses i as an iterator for a > -for i in a - +for i from 0 to a by 1 show i - repeat - - diff --git a/src/runtime/codeable.pl b/src/runtime/codeable.pl index 8eb187f..5bf4aed 100644 --- a/src/runtime/codeable.pl +++ b/src/runtime/codeable.pl @@ -51,8 +51,8 @@ selection(if(B, C1, C2)) --> [if], boolean(B), command(C1), [otherwise], command selection_inline(ternary(B, T, F)) --> expr(T), [if], boolean(B), [otherwise], expr(F). loop(while(B, C)) --> [while], boolean(B), command(C), [repeat]. -loop(for(I, E, C)) --> [for], expr(I), [in], expr(E), command(C), [repeat]. - % loop(for(I1, I2, C)) --> [for], identifier(I1), [from], identifier(I2), command(C), [repeat]. +loop(for(I, Start, Stop, 1, C)) --> [for], identifier(I), [from], expr(Start), [to], expr(Stop), command(C), [repeat]. +loop(for(I, Start, Stop, Step, C)) --> [for], identifier(I), [from], expr(Start), [to], expr(Stop), [by], expr(Step), command(C), [repeat]. show(show_string(S)) --> [show], strings(S). show(show_numeric(D)) --> [show], numeric(D). @@ -125,14 +125,19 @@ eval(while(B, C), EnvIn, EnvOut, ValueOut) :- eval(while(B, C), EnvTemp2, EnvOut, ValueOut). eval(while(B, _), EnvIn, EnvOut, _ValueOut) :- eval(B, EnvIn, EnvOut, false). -eval(for(I, E, C), EnvIn, EnvOut, ValueOut) :- - eval(is_less_than(I, E), EnvIn, EnvTemp, true), - update(I, I + 1, EnvTemp, EnvTemp2), - eval(C, EnvTemp2, EnvTemp3, _ValueOut), - eval(for(I, E, C), EnvTemp3, EnvOut, ValueOut). -eval(for(I, E, _), EnvIn, EnvOut, _ValueOut) :- - eval(is_less_than(I, E), EnvIn, EnvOut, false). +eval(for(I, Start, Stop, Step, C), EnvIn, EnvOut, ValueOut) :- + eval(is_less_than(Start, Stop), EnvIn, EnvTemp, true), + eval(Start, EnvTemp, EnvTemp1, ValueStart), + update(I, ValueStart, EnvTemp1, EnvTemp2), + eval(C, EnvTemp2, EnvTemp3, _ValueOut), + eval(Step, EnvTemp3, EnvTemp4, ValueStep), + NextStart is ValueStart + ValueStep, + eval(for(I, expr_term(term_factor(factor_numeric(NextStart))), Stop, Step, C), EnvTemp4, EnvOut, ValueOut). +eval(for(I, Start, Stop, _, _), EnvIn, EnvOut, _ValueOut) :- + eval(Start, EnvIn, EnvTemp, ValueStart), + update(I, ValueStart, EnvTemp, EnvTemp2), + eval(is_less_than(Start, Stop), EnvTemp2, EnvOut, false). eval(expr_plus(Term, Expression), EnvIn, EnvOut, ValueOut) :- eval(Term, EnvIn, EnvTemp, ValTerm), diff --git a/src/runtime/codeable_tests.pl b/src/runtime/codeable_tests.pl index 09e4f97..a9c0936 100644 --- a/src/runtime/codeable_tests.pl +++ b/src/runtime/codeable_tests.pl @@ -22,4 +22,8 @@ test("exponentiation parses correctly") :- program(ParseTree, [a, stores, 2, raised-to, 3], []), ParseTree = prog(assign(id(a), expr_term(term_exponent(factor_numeric(2), factor_numeric(3))))). +test("for loop parses correctly") :- + program(ParseTree, [for, i, from, 0, to, 10, by, 1, show, i, repeat], []), + ParseTree = + :- end_tests(codeable). -- 2.51.2