summaryrefslogtreecommitdiff
path: root/examples/foreach.nl
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@redhat.com>2014-03-02 13:14:28 -0500
committerSøren Sandmann Pedersen <ssp@redhat.com>2014-03-02 13:17:13 -0500
commit283a5e6be5d83fc0c23ee5f7c3b0a3fff9bd91a3 (patch)
treeb641f978c8826232eee93d0e85b5044c7bd0bd14 /examples/foreach.nl
parent3619578cb107e90e008f02ce3f73347d4314240c (diff)
Add foreach.nlHEADmaster
Diffstat (limited to 'examples/foreach.nl')
-rw-r--r--examples/foreach.nl50
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/foreach.nl b/examples/foreach.nl
new file mode 100644
index 0000000..ef3e55e
--- /dev/null
+++ b/examples/foreach.nl
@@ -0,0 +1,50 @@
+class person_t
+{
+ name: string;
+ age: int32;
+};
+
+class table_t
+{
+ entries: array[person_t];
+ n_entries: int32;
+
+ add (name: string, age: int32)
+ {
+ if (entries == null)
+ entries = new array[person_t] (200);
+
+ p: person_t = new person_t ();
+
+ p.name = name;
+ p.age = age;
+
+ entries[n_entries++] = p;
+ }
+
+ foreach (f: fn (p: person_t))
+ {
+ for (i := 0; i < n_entries; ++i)
+ f (entries[i]);
+ }
+};
+
+table: table_t = new table_t ();
+
+table.add ("Kathryn A. Baley", 49);
+table.add ("Dennis Clark", 31);
+table.add ("Doris R. Jones", 75);
+table.add ("Steven Arcuri", 80);
+table.add ("Robert Whicker", 61);
+table.add ("Angelo J. Tran", 79);
+table.add ("Irma Clapp", 37);
+
+table.foreach (fn (p: person_t) {
+ print p.name;
+ print p.age;
+
+ if (p.age > 50)
+ goto done;
+ });
+
+@done: