From 73843fa284968b4efb0ae51858cb37d0189c4b83 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Wed, 28 Jun 2023 16:21:15 -0500 Subject: filer added to std --- examples/guessing.sloth | 8 ----- examples/hello.sloth | 6 +--- examples/read.sloth | 4 +++ examples/read.txt | 1 + examples/snake.sloth | 81 ++++++++++++++++++++++++++----------------------- std/stdio.c | 20 ++++++++++++ std/stdio.sloth | 3 +- 7 files changed, 70 insertions(+), 53 deletions(-) create mode 100644 examples/read.sloth create mode 100644 examples/read.txt diff --git a/examples/guessing.sloth b/examples/guessing.sloth index f88c35b..49c0bde 100644 --- a/examples/guessing.sloth +++ b/examples/guessing.sloth @@ -1,11 +1,3 @@ -foreign fn print(); -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() Int { var computer: Int = randGen(1, 10); var tries: Int = 0; diff --git a/examples/hello.sloth b/examples/hello.sloth index 75071c3..8f42051 100644 --- a/examples/hello.sloth +++ b/examples/hello.sloth @@ -1,7 +1,3 @@ -foreign fn printdeez(nutz: Int); -foreign fn print(str: String); -foreign fn randGen(min: Int, max: Int) Int; - fn main() Int { var x: [Int] = [5]; vpopi(x); @@ -15,7 +11,7 @@ fn main() Int { var y: Int = vgeti(x, 45); var y: Int = vgeti(x, 41); var y: Int = vgeti(x, 49); - printdeez(y); + println(istr(y)); print("Hello World\n"); diff --git a/examples/read.sloth b/examples/read.sloth new file mode 100644 index 0000000..cf947f8 --- /dev/null +++ b/examples/read.sloth @@ -0,0 +1,4 @@ +fn main() Int { + print(filer("examples/read.txt")); + return 0; +} diff --git a/examples/read.txt b/examples/read.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/examples/read.txt @@ -0,0 +1 @@ +Hello World! diff --git a/examples/snake.sloth b/examples/snake.sloth index c60819d..8283368 100644 --- a/examples/snake.sloth +++ b/examples/snake.sloth @@ -1,40 +1,45 @@ -var xPos = 0; -var yPos = 0; -# 0=right 1=down 2=left 3=up -var direction = 0; +fn main() Int { + var xPos: Int = 0; + var yPos: Int = 0; + # 0=right 1=down 2=left 3=up + var direction: Int = 0; + var x: Int = 0; + var y: Int = 0; -while true { - if direction == 0{ - var x = xPos + 1; - xPos = x; - } - if direction == 1 { - var y = yPos + 1; - yPos = y; - } - if direction == 2{ - var x = xPos - 1; - xPos = x; - } - if direction == 3 { - var y = yPos - 1; - yPos = y; - } - - var input = readln(); - if input == "w" && direction != 1 { - direction = 3; - } - if input == "a" && direction != 0 { - direction = 2; - } - if input == "s" && direction != 3 { - direction = 1; - } - if input == "d" && direction != 2 { - direction = 0; - } - - term_setpos(x, y); - print("*"); + while true { + if direction == 0{ + x = xPos + 1; + xPos = x; + } + if direction == 1 { + y = yPos + 1; + yPos = y; + } + if direction == 2{ + x = xPos - 1; + xPos = x; + } + if direction == 3 { + y = yPos - 1; + yPos = y; + } + + var input: String = readln(); + if input == "w" && direction != 1 { + direction = 3; + } + if input == "a" && direction != 0 { + direction = 2; + } + if input == "s" && direction != 3 { + direction = 1; + } + if input == "d" && direction != 2 { + direction = 0; + } + + termpos(x, y); + print("*"); + } + return 0; } diff --git a/std/stdio.c b/std/stdio.c index f6dbeaf..b5f1dd0 100644 --- a/std/stdio.c +++ b/std/stdio.c @@ -26,3 +26,23 @@ void curshide() { void cursshow() { print("\x1b[?25h"); } + +char* filer(char* path) { + FILE *fptr = fopen(path, "rb"); + char *contents = 0; + + if(fptr == NULL) { + return "File not found"; + } + fseek(fptr, 0, SEEK_END); + long size = ftell(fptr); + fseek(fptr, 0, SEEK_SET); + + contents = malloc(size); + fread(contents, 1, size, fptr); + fclose(fptr); + + return contents; +} + + diff --git a/std/stdio.sloth b/std/stdio.sloth index 77c9af0..37a2d07 100644 --- a/std/stdio.sloth +++ b/std/stdio.sloth @@ -1,6 +1,6 @@ foreign fn print(str: String) Void; foreign fn readln() String; - +foreign fn filer(path: String) String; foreign fn curshide(); foreign fn cursshow(); @@ -9,4 +9,3 @@ fn println(str: String) Void { print("\n"); } - -- cgit v1.2.3