Working on custom library for C development
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 May 2020 00:10:13 +0000 (19:10 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 May 2020 00:11:28 +0000 (19:11 -0500)
bh.h [new file with mode: 0644]
demo.onyx [new file with mode: 0644]
onyx
onyx.c

diff --git a/bh.h b/bh.h
new file mode 100644 (file)
index 0000000..24afa21
--- /dev/null
+++ b/bh.h
@@ -0,0 +1,102 @@
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <string.h> // 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 (file)
index 0000000..27b20f7
--- /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 e8adb76455dd27e120c61eb3bf1f51764899959d..8853feca6175d062c3fed89324cf3fc5f652b1dc 100755 (executable)
Binary files a/onyx and b/onyx differ
diff --git a/onyx.c b/onyx.c
index 59a7d7038596f1910171d75f3e65e7f7b4f7235d..ea8e8b041c272b79ac27d84f15fc1149b258e883 100644 (file)
--- a/onyx.c
+++ b/onyx.c
@@ -1,60 +1,30 @@
 #include <stdio.h> // TODO: Replace with custom lib
 #include <stdlib.h> // TODO: Replace with custom lib
-
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-
-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