Skip to content

Commit

Permalink
filer added to std
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 28, 2023
1 parent 7d14243 commit 73843fa
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 53 deletions.
8 changes: 0 additions & 8 deletions examples/guessing.sloth
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 1 addition & 5 deletions examples/hello.sloth
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions examples/read.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() Int {
print(filer("examples/read.txt"));
return 0;
}
1 change: 1 addition & 0 deletions examples/read.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
81 changes: 43 additions & 38 deletions examples/snake.sloth
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions std/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


3 changes: 1 addition & 2 deletions std/stdio.sloth
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -9,4 +9,3 @@ fn println(str: String) Void {
print("\n");
}


0 comments on commit 73843fa

Please sign in to comment.