diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2023-06-28 15:21:54 -0500 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2023-06-28 15:21:54 -0500 |
| commit | 7d14243f769ad911d7c057b891ed89a95d7c1bfd (patch) | |
| tree | a64f621b49b7e0c2199eac406e8ca21fdc6b174e | |
| parent | 00f695f1fd38e79c60cb8b37f90708a2186a42e8 (diff) | |
| download | sloth-7d14243f769ad911d7c057b891ed89a95d7c1bfd.tar.gz | |
added istr to std
| -rw-r--r-- | examples/guessing.sloth | 14 | ||||
| -rw-r--r-- | std/stdlib.c | 11 | ||||
| -rw-r--r-- | std/stdlib.sloth | 2 |
3 files changed, 18 insertions, 9 deletions
diff --git a/examples/guessing.sloth b/examples/guessing.sloth index 5a759e5..f88c35b 100644 --- a/examples/guessing.sloth +++ b/examples/guessing.sloth @@ -3,13 +3,15 @@ foreign fn println(); foreign fn readln() String; foreign fn random(min: Int, max: Int) Int; foreign fn parse_int(str: String) Int; +foreign fn randGen(min: Int, max: Int) Int; +foreign fn istr(x: Int) String; -fn main() { - var computer: Int = random(1, 10); +fn main() Int { + var computer: Int = randGen(1, 10); var tries: Int = 0; var correct: Bool = false; - while !correct { + while correct == false { print("Pick a number between 1 and 10: "); var human: Int = parse_int(readln()); @@ -25,5 +27,9 @@ fn main() { tries = tries + 1; } - println("It took you ", tries, " tries to guess correctly!"); + print("It took you "); + print(istr(tries)); + println(" to guess correctly!"); + + return 0; } diff --git a/std/stdlib.c b/std/stdlib.c index dcb7ec3..3878817 100644 --- a/std/stdlib.c +++ b/std/stdlib.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <string.h> #include <time.h> +#include <stdio.h> int wait(int msec) { struct timespec ts; @@ -39,7 +40,9 @@ int as_int(float x) { return (int) x; } -// char* istr(int x) { -// char snum[100]; -// return (char* )itoa(x, snum, 10); -// } +char* istr(int x) { + char* snum[100]; + sprintf(snum, "%d", x); + //char* result = snum; + return snum; +} diff --git a/std/stdlib.sloth b/std/stdlib.sloth index ea57e45..4213e0a 100644 --- a/std/stdlib.sloth +++ b/std/stdlib.sloth @@ -5,6 +5,6 @@ foreign fn slen(str: String) Int; foreign fn parse_int(str: String) Int; foreign fn termpos(x: Int, y: Int); foreign fn as_int(x: Float) Int; -#foreign fn istr(x: Int) Int; +foreign fn istr(x: Int) String; foreign fn termclear() Void; |
