From ffb4ecea4a579b74deb022e43829ff66fd9fa6f0 Mon Sep 17 00:00:00 2001 From: jgjanes Date: Thu, 28 Apr 2022 18:09:21 -0700 Subject: [PATCH] Added range test and basic for loop, in infinite loop --- data/rangeCount.md | 11 +++++++++++ src/runtime/codeable.pl | 11 ++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 data/rangeCount.md diff --git a/data/rangeCount.md b/data/rangeCount.md new file mode 100644 index 0000000..e2bed52 --- /dev/null +++ b/data/rangeCount.md @@ -0,0 +1,11 @@ +i stores 0 +a stores 5 + +fyi < uses i as an iterator for a > +for i in a + + show i + +repeat + + diff --git a/src/runtime/codeable.pl b/src/runtime/codeable.pl index 2bac28c..8eb187f 100644 --- a/src/runtime/codeable.pl +++ b/src/runtime/codeable.pl @@ -51,7 +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(I1, I2, C)) --> [for], identifier(I1), [from], identifier(I2), 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]. show(show_string(S)) --> [show], strings(S). show(show_numeric(D)) --> [show], numeric(D). @@ -124,6 +125,14 @@ 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(expr_plus(Term, Expression), EnvIn, EnvOut, ValueOut) :- eval(Term, EnvIn, EnvTemp, ValTerm), -- knotmirror 0.0.1