Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Dec 5, 2023
2 parents c748aed + d767828 commit f655a86
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 40 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 std/stdmem.c -o "${FILENAME%.sloth}"

# Move file
mv "${FILENAME%.sloth}" out/
Expand Down
9 changes: 9 additions & 0 deletions examples/mem.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() {
var intpointer: Int = memalloc(64);
println("We vibin");
assignrefi(intpointer, 10);
println("Still vibin");
var derefd: Int = drefi(intpointer);
print("Val of drefd: ");
println(istr(derefd));
}
1 change: 1 addition & 0 deletions 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
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
5 changes: 5 additions & 0 deletions std/extern.sloth
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ foreign fn clientsock(port: Int, addr: String) Int;
foreign fn closesock(soc: Int, server:Bool);
foreign fn sendsock(msg: String, soc: Int);
foreign fn recvsock(soc: Int) String;

#stdmem
foreign fn memalloc(size: Int) Int;
foreign fn drefi(loc: Int) Int;
foreign fn assignrefi(loc: Int, num: Int);
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;
}
20 changes: 20 additions & 0 deletions std/stdmem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
char *heap[100000];
int heaploc = 0;

int memalloc(int size) {
const int chunk = heaploc;
heap[heaploc++] = malloc(size);
printf("MEMALLOC: heap[%d]\n", chunk);
return chunk;
}
int drefi(int loc) {
printf("DREF: *heap[%d] = %d\n", loc, *heap[loc]);
return *heap[loc];
}
void assignrefi(int loc, int num) {
*heap[loc] = num;
printf("ASSREF: *heap[%d] = %d\n", loc, *heap[loc]);
}
Empty file added std/stdmem.sloth
Empty file.
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 f655a86

Please sign in to comment.