Skip to content

Commit

Permalink
fuck it we ball
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Nov 12, 2023
1 parent 84c7765 commit 52f12c6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 61 deletions.
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Build Sloth
cargo build
#cargo build
FILENAME="$1"
# Compile standard library
./target/debug/sloth std/extern.sloth std/stdmath.sloth std/stdio.sloth $FILENAME
./target/release/sloth std/extern.sloth std/stdmath.sloth std/stdio.sloth $FILENAME

# Generate binary
clang -lm output.o std/stdio.c std/stdlib.c std/stdmath.c -o "${FILENAME%.sloth}"
clang -lm output.o std/stdsocket.c std/stdio.c std/stdlib.c std/stdmath.c -o "${FILENAME%.sloth}"

# Move file
mv "${FILENAME%.sloth}" out/
Expand Down
50 changes: 30 additions & 20 deletions examples/cgol.sloth
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn populate() [Int]
{
# Initialize life vector
var life: [Int] = [0];
var life: [Int] = [1];
vpopi(life);

# Fill the vector with random values
Expand All @@ -28,26 +28,28 @@ fn coord(x: Int, y: Int) Int
return res;
}

fn cval(x: Int, y: Int, life: [Int]) Int
fn cval(x: Int, y: Int, life: [Int]) Int
{
# Check to make sure index exists before returning
var res: Int = 0;
var c: Int = coord(x, y);
if c >= 0 {
if c >= 0
{
res = vgeti(life, c);
}
return res;
}

fn gol(total: Int, alive: Bool) Int
{
#fn gol(total: Int, alive: Bool) Int
#{

if !alive && total == 3 {
return 1;
}
if alive && ()
return 0;
}
#if !alive && total == 3
#{
#return 1;
#}
#if alive && ()
#return 0;
#}

fn update(life: [Int], new: [Int])
{
Expand Down Expand Up @@ -97,32 +99,40 @@ fn update(life: [Int], new: [Int])
}
}

fn display(life: [Int]) {
fn display(life: [Int])
{
# Iterate through life
for x in 3..62 {
for y in 0..240 {
for x in 3..62
{
for y in 0..240
{
termpos(x-3, y);
if cval(x-3, y, life) == 1 {
if cval(x-3, y, life) == 1
{
print("█");
} else {
}
else
{
print(" ");
}
}
}
}

fn main() Int {
fn main() Int
{
# Populate
var life: [Int] = populate();
display(life);
curshide();
curshide();
# Play forever
while true {
while true
{
var new: [Int] = populate();
update(life, new);
display(new);
life = new;
wait(100);
# wait(100);
}
return 0;
}
2 changes: 1 addition & 1 deletion sloth/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl<'ctx> Codegen<'ctx> {

for stmt in stmts {
self.codegen_stmt(stmt);
self.current_func.unwrap().print_to_stderr();
}
}

Expand Down Expand Up @@ -555,7 +556,6 @@ impl<'ctx> Codegen<'ctx> {
"",
)
};

self.builder.build_store(value_ptr, value);
}

Expand Down
2 changes: 1 addition & 1 deletion sloth/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() {

// Parsing
let tokens = Lexer::new(&source).collect_vec();
println!("{tokens:#?}");
//println!("{tokens:#?}");
let global_symtable = mk_symtable();

let mut ast = match AstParser::parse(tokens, global_symtable) {
Expand Down
60 changes: 24 additions & 36 deletions std/stdio.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,36 @@
#include <stdio.h>
#include <stdlib.h>

char* readln() {
char* str = malloc(128);
scanf("%127s", str);
return str;
char *readln() {
char *str = malloc(128);
fgets(str, 127, stdin);
return str;
}

void print(char *str) {
fputs(str, stdout);
}
void print(char *str) { fputs(str, stdout); }

void termpos(int x, int y) {
printf("\x1b[%d;%dH", x, y);
}
void termpos(int x, int y) { printf("\x1b[%d;%dH", x, y); }

void termclear() {
printf("\x1b[2J\x1b[H");
}
void termclear() { printf("\x1b[2J\x1b[H"); }

void curshide() {
print("\x1b[?25l");
}
void curshide() { print("\x1b[?25l"); }

void cursshow() {
print("\x1b[?25h");
}
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;
}
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;
}
10 changes: 10 additions & 0 deletions test.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

fn getStr() Bool
{
return true;
}

fn main() Int {
val poggers: Int = getStr();
return 0;
}

0 comments on commit 52f12c6

Please sign in to comment.