From: Brendan Hansen Date: Mon, 4 May 2020 00:10:13 +0000 (-0500) Subject: Working on custom library for C development X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fce10a08ad3202021481097902a90a253cc540e0;p=onyx.git Working on custom library for C development --- diff --git a/bh.h b/bh.h new file mode 100644 index 00000000..24afa216 --- /dev/null +++ b/bh.h @@ -0,0 +1,102 @@ +#include +#include +#include +#include + +#include // TODO: Replace with neede functions + +//------------------------------------------------------------------------------------- +// Better types +//------------------------------------------------------------------------------------- +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long u64; +typedef unsigned long long u128; +typedef signed char i8; +typedef signed short i16; +typedef signed int i32; +typedef signed long i64; +typedef signed long long i128; + +typedef struct bh_string { + u8* data; + u64 length; + u64 capacity; +} bh_string; + +#define bh_string_new(x) _Generic((x), \ + unsigned long: bh_string_new_cap, \ + const char*: bh_string_new_str, \ + char*: bh_string_new_str)(x) + +bh_string bh_string_new_cap(unsigned long cap) { + bh_string str; + str.data = (u8*) malloc(sizeof(u8) * cap); + str.length = 0; + str.capacity = cap; + return str; +} + +bh_string bh_string_new_str(const char* cstr) { + const int len = strlen(cstr); + bh_string str; + int i; + + str.data = (u8*) malloc(sizeof(u8) * len); + for (i = 0; i < len; i++) { + str.data[i] = cstr[i]; + } + + str.length = len; + str.capacity = len; + return str; +} + +int bh_string_delete(bh_string* str) { + free(str->data); + str->length = 0; + str->capacity = 0; + return 1; +} + + + +//------------------------------------------------------------------------------------- +// Better files +//------------------------------------------------------------------------------------- +typedef struct File { + i32 fd; + u8 open : 1; + const char* path; +} File; + +int file_open(File *f, const char* path, int flags) { + f->fd = open(path, flags, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH // +rw-rw-rw + ); + + if (f->fd < 0) + return 1; + + f->open = 1; + f->path = path; + return 0; +} + +int file_close(File* f) { + if (close(f->fd) < 0) { + return errno; + } + + f->open = 0; + return 0; +} + +unsigned char file_read_byte(File* f) { + if (!f->open) return 0; + + unsigned char byte; + read(f->fd, &byte, 1); + return byte; +} diff --git a/demo.onyx b/demo.onyx new file mode 100644 index 00000000..27b20f74 --- /dev/null +++ b/demo.onyx @@ -0,0 +1,10 @@ + +use "core"; + +Foo :: struct { + x, y i32; +} + +main :: (argc i32, argv []*u8) int { + print("Hello World!"); +} diff --git a/onyx b/onyx index e8adb764..8853feca 100755 Binary files a/onyx and b/onyx differ diff --git a/onyx.c b/onyx.c index 59a7d703..ea8e8b04 100644 --- a/onyx.c +++ b/onyx.c @@ -1,60 +1,30 @@ #include // TODO: Replace with custom lib #include // TODO: Replace with custom lib - -#include -#include -#include -#include - -typedef struct File { - int fd; - int open : 1; - const char* path; -} File; - -int file_open(File *f, const char* path, int flags) { - f->fd = open(path, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - - if (f->fd < 0) - return 1; - - f->open = 1; - f->path = path; - return 0; -} - -int file_close(File* f) { - if (close(f->fd) < 0) { - return errno; - } - - f->open = 0; - return 0; -} - -unsigned char file_read_byte(File* f) { - if (!f->open) return 0; - - unsigned char byte; - read(f->fd, &byte, 1); - return byte; -} +#include "bh.h" +// +// int main(int argc, char *argv[]) { +// if (argc < 2) { +// fprintf(stderr, "Expected file to compile\n"); +// return -1; +// } +// +// File file; +// +// if (file_open(&file, argv[1], O_RDONLY)) { +// fprintf(stderr, "Failed to open file: %s\n", argv[1]); +// return -1; +// } +// +// file_close(&file); +// +// return 0; +// } +// int main(int argc, char *argv[]) { + bh_string test_str = bh_string_new("Hello World!"); - if (argc < 2) { - fprintf(stderr, "Expected file to compile\n"); - return -1; - } - - File file; - - if (!file_open(&file, argv[1], O_RDONLY)) { - fprintf(stderr, "Failed to open file: %s\n", argv[1]); - return -1; - } - - file_close(&file); + - return 0; -} + bh_string_delete(&test_str); +} \ No newline at end of file