Skip to content

Commit

Permalink
added istr to std
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 28, 2023
1 parent 00f695f commit 7d14243
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
14 changes: 10 additions & 4 deletions examples/guessing.sloth
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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;
}
11 changes: 7 additions & 4 deletions std/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>

int wait(int msec) {
struct timespec ts;
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion std/stdlib.sloth
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 7d14243

Please sign in to comment.