commit c3faeff722ca1b93f3938488a5b940bfb854c121
parent e99a36bd3cefac12636b6c1a35b3373d3bbac3ec
Author: Luxferre <lux@ferre>
Date: Thu, 11 Aug 2022 18:38:02 +0300
Implemented FizzBuzz example for current spec version
Diffstat:
2 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -128,6 +128,8 @@ Note that, due to the dynamic nature of word allocation and ability to reconfigu
Please also note that Equi doesn't specify any graphical or sound output capabilities. If such support is required, it generally must be implemented, as with any other peripheral, via the port I/O interface (`P`) instruction specific to a particular hardware/software implementation. Same goes for how standard serial terminal input/output is processed: Equi specification doesn't enforce any particular way. On the desktop/laptop PCs, however, it is advised, especially for software-based implementations/VMs, that the terminal I/O should be VT100-compatible, including, for instance, control character support and the output of an audiovisual bell for ASCII 0x07 (`\a` or `^G`). Depending on the target, these features may already be supported by the underlying OS's terminal emulator or may be implemented as a part of the VM itself.
+See [FizzBuzz](examples/fizzbuzz.equi) for a more thorough example of how different features of the current Equi specification are used.
+
## Reference implementation
Being a purely PC-oriented low-level runtime/programming environment, Equi has the reference implementation emulator/VM written in C (ANSI C89 standard), `equi.c`, compilable and runnable on all the systems supporting standard I/O. Note that, for portability reasons, this emulator:
diff --git a/examples/fizzbuzz.equi b/examples/fizzbuzz.equi
@@ -0,0 +1,55 @@
+(Classical FizzBuzz in Equi because why not)
+(by Luxferre, 2022, public domain)
+
+nl: 000A#D..; (define a word to output a newline)
+d: 0030+.; (define a word to output a dec digit)
+
+(I want capital letters to output properly so defining words to output Fizz and Buzz)
+
+fizz: 0046.izz"...;
+buzz: 0042.uzz"...;
+
+(routine to output a decimal byte-sized integer without leading zeroes)
+
+pdb: ( val -- )
+ 0064 /%$ ( rem div div -- )
+ 3I ( rem div -- jump 3 instructions forward if div > 0 )
+ !5J ( drop and jump 5 instructions forward otherwise )
+ d' ( rem -- output the digit )
+ 1GS ( store flag 1 at general area )
+ 000A /%$ ( rem div div -- )
+ 3I ( rem div -- jump 2 instructions forward if div > 0 )
+ !7J ( drop and jump 7 instructions forward otherwise )
+ d' ( rem -- output the digit )
+ 1GS ( store flag 1 at general area )
+ 8J ( jump 8 instructions forward, to the remaining digit )
+ GL ( rem div flag -- load the flag from the general area )
+ 2I ( rem div -- jump if flag set )
+ 2J ( rem div -- jump over the output )
+ d' ( rem -- output the zero digit)
+ d' ( -- output the remaining digit )
+;
+
+(now, the main FizzBuzz code)
+
+0001# ( c -- )
+(loop start)
+0G2+S (reset flag)
+(try dividing by 3)
+$3/%! ( c rem -- )
+AI ( c -- )
+fizz'
+1G2+S (set flag)
+(try dividing by 5)
+$5/%! ( c rem -- )
+AI ( c -- )
+buzz'
+1G2+S (set flag)
+G2+L (load flag)
+5I (skip the print if fizz or buzz)
+$pdb' (print the number if neither)
+nl'
+0001+ ( c -- )
+$ 65< ( c [c<101] -- )
+42N I (jump 66 instructions backwards if the counter is less than 101)
+Q