From 52d6bc8533616dd642c96f8b6e72f459e1b4d465 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Mon, 17 Jul 2023 23:00:30 -0500 Subject: Standard lib rework --- examples/arguments.sloth | 2 +- examples/cgol.sloth | 2 +- examples/mandelbrot.sloth | 67 +++++++++++++++++++++++++---------------------- examples/read.sloth | 2 +- 4 files changed, 38 insertions(+), 35 deletions(-) (limited to 'examples') diff --git a/examples/arguments.sloth b/examples/arguments.sloth index 3e44dcc..9daaec7 100644 --- a/examples/arguments.sloth +++ b/examples/arguments.sloth @@ -1,7 +1,7 @@ fn main(argc: Int, argv: [String]) Int { if argc == 2 { print("The argument supplied is "); - println(argv); + println(vgets(argv, 1)); } else { println("Wrong # of args"); } diff --git a/examples/cgol.sloth b/examples/cgol.sloth index 6cfe72d..fadb9c7 100644 --- a/examples/cgol.sloth +++ b/examples/cgol.sloth @@ -84,7 +84,7 @@ fn display(life: [Int]) { # if the cell is alive, print termpos(x, y); if cval(x, y, life) == 1{ - print("#"); + print("█"); } else { print(" "); } diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth index 391e86e..90c7f1c 100644 --- a/examples/mandelbrot.sloth +++ b/examples/mandelbrot.sloth @@ -1,36 +1,39 @@ -#foreign fn print(str: String); -#foreign fn printint(i: Int); -#foreign fn as_int(x: Float) Int; +fn main() Int{ + # Configure + var size: Float = 1000.0; + var maxVal: Float = 4.0; + var maxIter: Float = 50.0; + var plane: Float = 4.0; -#foreign fn termpos(x: Int, y: Int) Void; + # loop over coordinates + var x: Float = 0.0; + while x < size { + var y: Float = 0.0; + while y < size { + # Initialize + var cReal: Float = (x * plane / size) - 2.0; + var cImg: Float = (y * plane / size) - 2.0; + var zReal: Float = 0.0; + var zImg: Float = 0.0; + var count: Float = 0.0; -fn main() Int{ - var size: Float = 1000.0; - var maxVal: Float = 4.0; - var maxIter: Float = 50.0; - var plane: Float = 4.0; - var x: Float = 0.0; - while x < size { - var y: Float = 0.0; - while y < size { - var cReal: Float = (x * plane / size) - 2.0; - var cImg: Float = (y * plane / size) - 2.0; - var zReal: Float = 0.0; - var zImg: Float = 0.0; - var count: Float = 0.0; - while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter { - var temp: Float = (zReal * zReal) - (zImg * zImg) + cReal; - zImg = 2.0 * zReal * zImg + cImg; - zReal = temp; - count = count + 1.0; - } - if as_int(count) == as_int(maxIter) { - termpos(as_int(x), as_int(y)); - print("*"); - } - y = y + 1.0; - } - x = x + 1.0; + # Calculate + while (zReal * zReal + zImg * zImg) <= maxVal && count < maxIter { + var temp: Float = (zReal * zReal) - (zImg * zImg) + cReal; + zImg = 2.0 * zReal * zImg + cImg; + zReal = temp; + count = count + 1.0; + } + + # Check + if as_int(count) == as_int(maxIter) { + termpos(as_int(x), as_int(y)); + print("█"); + } + + y = y + 1.0; } - return 0; + x = x + 1.0; + } + return 0; } diff --git a/examples/read.sloth b/examples/read.sloth index cf947f8..df398bb 100644 --- a/examples/read.sloth +++ b/examples/read.sloth @@ -1,4 +1,4 @@ fn main() Int { - print(filer("examples/read.txt")); + print(filer("examples/server.sloth")); return 0; } -- cgit v1.2.3