aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2023-06-28 16:21:15 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2023-06-28 16:21:15 -0500
commit73843fa284968b4efb0ae51858cb37d0189c4b83 (patch)
tree278982b5b0b8fdb933d5d57f801145ab7cbf99aa
parent7d14243f769ad911d7c057b891ed89a95d7c1bfd (diff)
downloadsloth-73843fa284968b4efb0ae51858cb37d0189c4b83.tar.gz
filer added to std
-rw-r--r--examples/guessing.sloth8
-rw-r--r--examples/hello.sloth6
-rw-r--r--examples/read.sloth4
-rw-r--r--examples/read.txt1
-rw-r--r--examples/snake.sloth81
-rw-r--r--std/stdio.c20
-rw-r--r--std/stdio.sloth3
7 files changed, 70 insertions, 53 deletions
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");
}
-