diff --git a/build.sh b/build.sh index 2bdd204..760f2ab 100755 --- a/build.sh +++ b/build.sh @@ -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/ diff --git a/examples/mem.sloth b/examples/mem.sloth new file mode 100644 index 0000000..cb52191 --- /dev/null +++ b/examples/mem.sloth @@ -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)); +} diff --git a/sloth/src/codegen/mod.rs b/sloth/src/codegen/mod.rs index e2c8082..abad70d 100644 --- a/sloth/src/codegen/mod.rs +++ b/sloth/src/codegen/mod.rs @@ -71,6 +71,7 @@ impl<'ctx> Codegen<'ctx> { for stmt in stmts { self.codegen_stmt(stmt); + self.current_func.unwrap().print_to_stderr(); } } diff --git a/sloth/src/main.rs b/sloth/src/main.rs index f66a1be..c0a76ad 100644 --- a/sloth/src/main.rs +++ b/sloth/src/main.rs @@ -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) { diff --git a/std/extern.sloth b/std/extern.sloth index a31c2c1..89676c7 100644 --- a/std/extern.sloth +++ b/std/extern.sloth @@ -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); diff --git a/std/stdio.c b/std/stdio.c index b5f1dd0..e9cefd0 100644 --- a/std/stdio.c +++ b/std/stdio.c @@ -1,48 +1,36 @@ #include #include -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; +} diff --git a/std/stdmem.c b/std/stdmem.c new file mode 100644 index 0000000..c3d0300 --- /dev/null +++ b/std/stdmem.c @@ -0,0 +1,20 @@ +#include +#include +#include +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]); +} diff --git a/std/stdmem.sloth b/std/stdmem.sloth new file mode 100644 index 0000000..e69de29 diff --git a/test.sloth b/test.sloth new file mode 100644 index 0000000..be3b1de --- /dev/null +++ b/test.sloth @@ -0,0 +1,10 @@ + +fn getStr() Bool +{ + return true; +} + +fn main() Int { + val poggers: Int = getStr(); + return 0; +}