mirror of
https://github.com/KarolS/millfork.git
synced 2026-04-21 09:16:34 +00:00
Function pointers – initial version
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
|
||||
* [Fizzbuzz](crossplatform/fizzbuzz.mfk) (C64/C16/PET/VIC-20/PET/Atari/Apple II/BBC Micro/ZX Spectrum/PC-88/Armstrad CPC/MSX) – everyone's favourite programming task
|
||||
|
||||
* [Fizzbuzz 2](crossplatform/fizzbuzz2.mfk) (C64/C16/PET/VIC-20/PET/Atari/Apple II/BBC Micro/ZX Spectrum/PC-88/Armstrad CPC/MSX) – an alternative, more extensible implemententation of fizzbuzz
|
||||
|
||||
* [Text encodings](crossplatform/text_encodings.mfk) (C64/ZX Spectrum) – examples of text encoding features
|
||||
|
||||
* [Echo](crossplatform/echo.mfk) (C64/C16/ZX Spectrum/PC-88/MSX)– simple text input and output
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
|
||||
import stdio
|
||||
|
||||
bool divisible3(byte x) = x %% 3 == 0
|
||||
bool divisible5(byte x) = x %% 5 == 0
|
||||
|
||||
struct stage {
|
||||
function.byte.to.bool predicate
|
||||
pointer text
|
||||
}
|
||||
|
||||
// can't put text literals directly in struct constructors yet
|
||||
array fizz = "fizz"z
|
||||
array buzz = "buzz"z
|
||||
|
||||
array(stage) stages = [
|
||||
stage(divisible3.pointer, fizz),
|
||||
stage(divisible5.pointer, buzz)
|
||||
]
|
||||
|
||||
void main() {
|
||||
byte i, s
|
||||
bool printed
|
||||
for i,1,to,100 {
|
||||
printed = false
|
||||
for s,0,until,stages.length {
|
||||
if call(stages[s].predicate, i) {
|
||||
printed = true
|
||||
putstrz(stages[js].text)
|
||||
}
|
||||
}
|
||||
if not(printed) {
|
||||
putword(i)
|
||||
}
|
||||
putchar(' ')
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user