Skip to content

Commit

Permalink
shitty ptrs
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Dec 2, 2023
1 parent 52f12c6 commit d767828
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FILENAME="$1"
./target/release/sloth std/extern.sloth std/stdmath.sloth std/stdio.sloth $FILENAME

# Generate binary
clang -lm output.o std/stdsocket.c 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
2 changes: 1 addition & 1 deletion 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] = [1];
var life: [Int] = ["Hello World"];
vpopi(life);

# Fill the vector with random values
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 @@ -556,6 +556,7 @@ impl<'ctx> Codegen<'ctx> {
"",
)
};

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

Expand Down
5 changes: 5 additions & 0 deletions std/extern.sloth
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,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);
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.

0 comments on commit d767828

Please sign in to comment.