etng-r2/calc.tng
author Tony Garnock-Jones <tonygarnockjones@gmail.com>
Wed, 16 Jan 2019 17:15:58 +0000
changeset 438 1fe179d53161
parent 285 034958cf32d9
permissions -rw-r--r--
Add missing primitive implementation for the plain interpreter.
285
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     1
define expr = {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     2
  number:n1 "+" expr:n2 -> n1 + n2;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     3
};
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     4
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     5
define number = {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     6
  spaces digit+:digits -> digits |listToString |stringToNumber;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     7
};
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     8
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
     9
define token xs -> {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    10
  spaces <{xs}>;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    11
};
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    12
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    13
define digit = {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    14
  c -> (charNumeric c) { .true -> c; .false -> error("expected", "charNumeric", c); };
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    15
};
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    16
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    17
spaces = {
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    18
  (c -> (charWhitespace c) { .true -> c; .false -> error("expected", "charWhitespace", c) };)+
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    19
    spaces;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    20
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    21
  $- $- ...
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    22
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    23
  -> #t;
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    24
};
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    25
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    26
034958cf32d9 Notes and incomplete work.
Tony Garnock-Jones <tonygarnockjones@gmail.com>
parents:
diff changeset
    27
expr 1 + 2 + 3;