Skip to content

Commit

Permalink
Merge pull request #20 from geode-lang/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nickwanninger authored Sep 21, 2018
2 parents 823a7e0 + 2b0a91c commit dea9d4a
Show file tree
Hide file tree
Showing 53 changed files with 612 additions and 763 deletions.
44 changes: 22 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

BUILDPATH = ./bin/geode
BINPATH = /usr/local/bin/geode
GCPATH = lib/gc/gc.a
# GCPATH = lib/gc/gc.a
LIBDIR = /usr/local/lib/geodelib

# tell go to install to the current directory
Expand Down Expand Up @@ -35,30 +35,30 @@ gen:
default:

lib.clean:
@rm -rf gc
# @rm -rf gc
@rm -rf lib/gc/*
@rm -rf lib/include/gc

$(GCPATH):
mkdir -p dl
wget -O dl/libatomic_ops.tar.gz https://github.com/ivmai/libatomic_ops/releases/download/v7.6.6/libatomic_ops-7.6.6.tar.gz
wget -O dl/gc.tar.gz https://github.com/ivmai/bdwgc/releases/download/v7.6.8/gc-7.6.8.tar.gz
rm -rf gc
tar -C ./ -xvf dl/gc.tar.gz
mv gc-* gc
tar -C ./ -xvf dl/libatomic_ops.tar.gz
mv libatomic_ops* gc/libatomic_ops
rm -rf dl
cd gc && make -f Makefile.direct
mkdir -p lib/gc/
cp gc/gc.a lib/gc/gc.a
cd lib/gc && ar x gc.a
mkdir -p lib/include/gc
cp -a gc/include/* lib/include/gc
rm -rf gc


install.lib: $(GCPATH)
# $(GCPATH):
# mkdir -p dl
# wget -O dl/libatomic_ops.tar.gz https://github.com/ivmai/libatomic_ops/releases/download/v7.6.6/libatomic_ops-7.6.6.tar.gz
# wget -O dl/gc.tar.gz https://github.com/ivmai/bdwgc/releases/download/v7.6.8/gc-7.6.8.tar.gz
# rm -rf gc
# tar -C ./ -xvf dl/gc.tar.gz
# mv gc-* gc
# tar -C ./ -xvf dl/libatomic_ops.tar.gz
# mv libatomic_ops* gc/libatomic_ops
# rm -rf dl
# cd gc && make -f Makefile.direct
# mkdir -p lib/gc/
# cp gc/gc.a lib/gc/gc.a
# cd lib/gc && ar x gc.a
# mkdir -p lib/include/gc
# cp -a gc/include/* lib/include/gc
# rm -rf gc


install.lib:# $(GCPATH)
@rm -rf $(LIBDIR)
@mkdir -p $(LIBDIR)
@cp -a lib/* $(LIBDIR)
Expand Down
11 changes: 3 additions & 8 deletions example/main.g
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
is main

include "io"
include "mem"

func main int {
h = io:open("LICENSE", "rb")
content = h.readall()
h.close()
io:print("%s\n", content)
return 0
}
func main {
io:print("hello, world\n")
}
39 changes: 23 additions & 16 deletions lib/encoding/encoding.g
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ func b64_encode(string src, int len) string ...

include "str"
include "mem"
include "io"


# hex converts type T (unknown) to a string containing
# the hex representation of val
func hex(T? val) string {
string charset := "0123456789abcdef";
string buffer := mem:zero(info(T).size * 2 + 1);
byte* offset := &val;
for int i := info(T).size - 1; i >= 0; i -= 1 {
byte b := offset[i];
int o := (info(T).size - i - 1) * 2;
buffer[o] = charset[b >> 4 && 0xf];
buffer[o+1] = charset[b && 0xf];

tInfo = info(T)

charset = "0123456789abcdef"
buffer = mem:zero(tInfo.size * 2 + 1)
byte* offset = &val

for i = tInfo.size - 1; i >= 0; i -= 1 {

b = *(offset + i)

o = (tInfo.size - i - 1) * 2
buffer[o] = charset[b >> 4 && 0xf]
buffer[o+1] = charset[b && 0xf]
}
return buffer;
}
Expand All @@ -28,13 +35,13 @@ func hex(T? val) string {
# the binary representation of val
func binary(T? val) string {
# the actual string that will be changed to contain the binary string
string buffer := "";
string bin_buffer := "00000000";
byte* offset := &val;
for int i := info(T).size - 1; i >= 0; i -= 1 {
byte b := offset[i];
for int o := 7; o >= 0; o -= 1 {
byte bit := (b >> o) && 1;
string buffer = "";
string bin_buffer = "00000000";
byte* offset = &val;
for int i = info(T).size - 1; i >= 0; i -= 1 {
byte b = offset[i];
for int o = 7; o >= 0; o -= 1 {
byte bit = (b >> o) && 1;
bin_buffer[7 - o] = bit + '0';
}
buffer = str:concat(buffer, bin_buffer);
Expand All @@ -43,7 +50,7 @@ func binary(T? val) string {
}

func base64(T? val) string {
byte* source := &val;
byte* source = &val;

return b64_encode(source, info(T).size);
}
3 changes: 1 addition & 2 deletions lib/include/mem.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef __MEM__
#define __MEM__

#include "../include/gc/gc.h"

#include <gc/gc.h>

#endif
4 changes: 2 additions & 2 deletions lib/include/runtime.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __GEODEruntime__
#define __GEODEruntime__

#include "./gc/gc.h"
#include "xmalloc.h"
#include <gc/gc.h>

#endif
#endif
30 changes: 12 additions & 18 deletions lib/include/xmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@

#include <stdlib.h>

#include "./gc/gc.h"
#include <gc/gc.h>

typedef struct {
long size;
int alloc_count;
long size;
int alloc_count;
long alloc_index;
} xmalloc_prelude_t;


typedef struct {
unsigned long memoryused;
unsigned int blocksallocated;
char readable[20];
unsigned long memoryused;
unsigned int blocksallocated;
char readable[20];
} xmalloc_stat_t;


#define PRELUDE_SIZE (sizeof(xmalloc_prelude_t))





xmalloc_stat_t xmemstat();
long xmalloc_size(void* ptr);
void xfree(void* ptr);
void* xmalloc(size_t size);
void* xcalloc(unsigned count, unsigned size);
void* xrealloc(void* ptr, size_t newsize);

long xmalloc_size(void *ptr);
void xfree(void *ptr);
void *xmalloc(size_t size);
void *xcalloc(unsigned count, unsigned size);
void *xrealloc(void *ptr, size_t newsize);

#endif
50 changes: 37 additions & 13 deletions lib/io/file.g
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,49 @@ FILE_DESCRIPTOR* stderr = fopen("/dev/stderr", "w+");
FILE_DESCRIPTOR* stdin = fopen("/dev/stdin", "r+");



# File is a class wrapper around the FILE_DESCRIPTOR that has more methods
# that allow you to act on `this` instead of passing around a FILE_DESCRIPTOR
class File {

# handle is a pointer to an OS Specific file descriptor
# returned by the c bindings for fopen
FILE_DESCRIPTOR* handle


# write some string to the file handle
func puts(string msg) {
io:fputs(msg, this.handle)
this.flush()
}
func flush -> io:fflush(this.handle)
func pos long -> io:ftell(this.handle)
func seek(int off, int loc) long -> io:fseek(this.handle, off, loc)
func close -> io:fclose(this.handle)
func read(string where, int size, int nmemb) -> io:fread(where, size, nmemb, this.handle)

# wrap around the stdlib io:fflush
func flush {
io:fflush(this.handle)
}

# wrap around the stdlib io:ftell
func pos long {
return io:ftell(this.handle)
}

# wrap around the stdlib io:fseek
func seek(int off, int loc) long {
return io:fseek(this.handle, off, loc)
}

# wrap around the stdlib io:fclose
func close {
io:fclose(this.handle)
}

# wrap around the stdlib io:fread
func read(string where, int size, int nmemb) {
io:fread(where, size, nmemb, this.handle)
}


# Return the filesize in bytes
func size long {
this.seek(0, 2);
fsize = this.pos();
Expand All @@ -42,18 +70,15 @@ class File {
}

func readall byte* {
# get the size of the file
fsize = this.size()

# allocate enough space for the buffer
buffer = mem:get(fsize + 1);
# read the file into the buffer entirely
res = io:fread(buffer, fsize, 1, this.handle);


# ensure the string is null terminated
buffer[fsize] = 0;
# for i = 0; i < fsize; i += 1 {
# io:print("%02x ", buffer[i])
# }
# io:print("\n")
# return the buffer we read
return buffer
}
}
Expand All @@ -67,7 +92,6 @@ func open(string path, string mode) File* {




# Create external linkages to the C stdlib.h files
func fopen(string path, string mode) FILE_DESCRIPTOR* ...
func fseek(FILE_DESCRIPTOR* handle, int offset, int whence) int ...
Expand Down
49 changes: 22 additions & 27 deletions lib/io/io.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

#include "io.h"
#include "../include/mem.h"
#include "../include/gc/gc.h"
#include "../include/xmalloc.h"

#include "io.h"
#include <gc/gc.h>

// the print function wrapper.
void print(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}


// the format function wrapper.
char* format(char *fmt, ...) {
va_list checkArgs;
va_start(checkArgs, fmt);
long size = vsnprintf(NULL, 0, fmt, checkArgs);
va_end(checkArgs);

// Allocate memory for the string
char* buffer = xmalloc(size + 1);

// Reparse the args... There is no way around this, sadly
va_list args;
va_start(args, fmt);
vsnprintf(buffer, size + 1, fmt, args);
va_end(args);
return buffer;
}
char *format(char *fmt, ...) {
va_list checkArgs;
va_start(checkArgs, fmt);
long size = vsnprintf(NULL, 0, fmt, checkArgs);
va_end(checkArgs);

// Allocate memory for the string
char *buffer = xmalloc(size + 1);

void sleepms(double ms) {
usleep(ms*1000);
// Reparse the args... There is no way around this, sadly
va_list args;
va_start(args, fmt);
vsnprintf(buffer, size + 1, fmt, args);
va_end(args);
return buffer;
}

void sleepms(double ms) { usleep(ms * 1000); }
2 changes: 1 addition & 1 deletion lib/io/io.g
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ func println(string message) {

func getchar() byte ...
func system(string command) int ...
func sleepms(float ms) ...
func sleepms(float ms) ...
6 changes: 3 additions & 3 deletions lib/mem/mem.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "../include/gc/gc.h"

#include <gc/gc.h>
5 changes: 5 additions & 0 deletions lib/mem/mem.g
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ is mem

link "mem.c"



func bytes_used long ...
func blocks_used long ...

# garbage collected malloc
func GC_gcollect() ...

Expand Down
6 changes: 6 additions & 0 deletions lib/os/os.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
is os

# package os contains bindings to
# a few posix systemcalls

func fork() int ...
Loading

0 comments on commit dea9d4a

Please sign in to comment.