diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-06-27 01:59:59 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-06-27 01:59:59 -0500 |
| commit | 29bdd10ee3621ed875bfa34a0faa42c35a1e39ed (patch) | |
| tree | 97f7605b53fc77277d98d5ea8127391edefc4ab5 /examples | |
| parent | 9c2d8f5a10b8affd604cec6e394d43514ef93ca1 (diff) | |
| download | sloth-29bdd10ee3621ed875bfa34a0faa42c35a1e39ed.tar.gz | |
Standard library methinks
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/cgol.sloth | 73 | ||||
| -rw-r--r-- | examples/mandelbrot.sloth | 3 | ||||
| -rw-r--r-- | examples/mergesort.sloth | 4 |
3 files changed, 74 insertions, 6 deletions
diff --git a/examples/cgol.sloth b/examples/cgol.sloth new file mode 100644 index 0000000..eea14c3 --- /dev/null +++ b/examples/cgol.sloth @@ -0,0 +1,73 @@ +foreign fn print(str: String); +foreign fn randGen(min: Int, max: Int) Int; +foreign fn wait(time: Float); +foreign fn termpos(x: Int, y: Int); + + +fn populate() [Int] { + var life: [Int] = [0]; + vpopi(life); + var i: Int = 0; + while i < 100 { + n: Int = randGen(0,1); + vpushi(life, n); + } + return life; +} + +fn coord(x: Int, y: Int) Int { + if x >= 0 && y >= 0 { + return y*10 + x; + } + return -1; + +} + +fn cval(x: Int, y: Int, life: [Int]) Int { + c: Int = coord(x, y); + if c < 0 { + return 0; + } + return vgeti(life, c); +} + +fn update(life: [Int]) [Int] { + x: Int = 0; + while x < 10 Int { + y: Int = 0; + while y < 10 { + total: Int = cval(x-1, y-1) + cval(x-1, y) + cval(x-1, y+1) + cval(x, y-1) + cval(x, y+1) + cval(x+1, y-1) + cval(x+1, y) + cval(x+1, y+1); + if cval(x, y) == 1 && total < 2 || total > 3{ + vseti(life, 0); + } else if total == 3 { + vseti(life, 1); + } + } + } + return life; +} + +fn display(life: [Int]) { + x: Int = 0; + while x < 10 { + y: Int = 0; + while y < 10 { + alive: Bool = cval(x, y) == 1; + if alive { + termpos(x, y); + print("#"); + } + + + } + } +} + +fn main() Int { + var life: [Int] = populate(); + while true { + life = update(life); + display(life); + wait(0.5); + } +} diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth index 1765e8e..6bf7727 100644 --- a/examples/mandelbrot.sloth +++ b/examples/mandelbrot.sloth @@ -5,11 +5,10 @@ foreign fn as_int(x: Float) Int; foreign fn termpos(x: Int, y: Int) Void; fn main() Int{ - var size: Float = 200.0; + var size: Float = 1000.0; var maxVal: Float = 4.0; var maxIter: Float = 50.0; var plane: Float = 4.0; - # lmao var x: Float = 0.0; while x < size { var y: Float = 0.0; diff --git a/examples/mergesort.sloth b/examples/mergesort.sloth deleted file mode 100644 index f1e5901..0000000 --- a/examples/mergesort.sloth +++ /dev/null @@ -1,4 +0,0 @@ -fn merge_sort(list: List<Int>) { - print(list); - -} |
