From 323e774380995c04ae705e29ae0e51d62246333d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 9 Nov 2017 17:06:35 +0200 Subject: Add support for for-loop The semantics is similar to the C++11 range-based for: list = 1 2 3 for i: $list print $i Note that there is no scoping of any kind for the loop variable ('i' in the above example). See tests/loop/for.test for some examples/ideas. In the future the plan is to also support more general while-loop as well as break and continue. --- tests/loop/buildfile | 5 +++ tests/loop/for.test | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 tests/loop/buildfile create mode 100644 tests/loop/for.test (limited to 'tests') diff --git a/tests/loop/buildfile b/tests/loop/buildfile new file mode 100644 index 0000000..7517da5 --- /dev/null +++ b/tests/loop/buildfile @@ -0,0 +1,5 @@ +# file : tests/loop/buildfile +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +./: test{*} $b diff --git a/tests/loop/for.test b/tests/loop/for.test new file mode 100644 index 0000000..746f1bb --- /dev/null +++ b/tests/loop/for.test @@ -0,0 +1,112 @@ +# file : tests/loop/for.test +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +# Test for-loop. + +.include ../common.test + +: line +: +$* <>EOO +for i: 1 2 3 + print $i +EOI +1 +2 +3 +EOO + +: block +: +$* <>EOO +for i: 1 2 3 +{ + # This is a block if you haven't noticed. + j = $i + print $j +} +EOI +1 +2 +3 +EOO + +: empty +: +$* <>EOO +for i: 1 2 3 +{ + for j: + - + { + print $j$i + } +} +EOI ++1 +-1 ++2 +-2 ++3 +-3 +EOO + +: diag-line +: +$* <>EOE != 0 +for i: true false +{ + assert $i +} +EOI +:3:3: error: assertion failed +EOE + +: var-attribute +: +$* <>EOO +for [uint64] i: 0 1 2 +{ + i += 1 + print $i +} +EOI +1 +2 +3 +EOO + +: val-attribute +: +$* <>EOO +for i: [uint64s] 0 1 2 +{ + i += 1 + print $i +} +EOI +1 +2 +3 +EOO + +: pairs +: +$* <>EOO +for i: a@1 b@2 c@3 + print $i +EOI +a@1 +b@2 +c@3 +EOO -- cgit v1.1