G

G Programming Language
git clone http://git.omkov.net/G
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2021-09-12 12:13:55
Commit760381011b2ab87cd09106506a7305fcea141486
Parent230e4984ea9c63604fc78db6e526aad9009c712c

util: Add util/util.h

Diffstat

D src/cll/cll.h | 73 -------------------------------------------------------------------------
M src/compile.c | 4 ++--
M src/compile.h | 3 ++-
M src/g.h | 8 ++++----
M src/keyword.c | 4 ++--
M src/keyword.h | 3 +--
M src/lex.c | 9 +++++----
M src/llvm/gen.c | 3 ++-
M src/llvm/llvm.h | 2 +-
M src/main.c | 7 +++----
M src/map.c | 2 +-
M src/map.h | 4 ++--
M src/parse.c | 4 ++--
M src/type.h | 4 ++--
M src/util/ast.h | 8 ++++----
M src/util/stack.c | 5 +++--
M src/util/stack.h | 8 ++++----
A src/util/util.c | 10 ++++++++++
A src/util/util.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M src/value.c | 4 ++--
M src/value.h | 2 +-

21 files changed, 125 insertions, 114 deletions

diff --git a/src/cll/cll.h b/src/cll/cll.h
deleted file mode 100644
index 86642ab..0000000
--- a/src/cll/cll.h
+++ /dev/null
@@ -1,73 +0,0 @@
-// cll.h
-// cll header file for cll
-// Copyright (C) 2021, Jakob Wakeling
-// All rights reserved.
-
-
-
-#ifndef OMKOV_CLL_CLL_H_ZO7IRBJS
-#define OMKOV_CLL_CLL_H_ZO7IRBJS
-
-#include <float.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-
-typedef void *ptr;
-
-typedef uint8_t   u8;
-typedef uint16_t  u16;
-typedef uint32_t  u32;
-typedef uint64_t  u64;
-typedef uintptr_t uint_;
-
-#define UINT uint_
-
-typedef int8_t   s8;
-typedef int16_t  s16;
-typedef int32_t  s32;
-typedef int64_t  s64;
-typedef intptr_t sint;
-
-typedef float       f32;
-typedef double      f64;
-typedef long double f128;
-
-#define U8_MIN   UINT8_MIN
-#define U8_MAX   UINT8_MAX
-#define U16_MIN  UINT16_MIN
-#define U16_MAX  UINT16_MAX
-#define U32_MIN  UINT32_MIN
-#define U32_MAX  UINT32_MAX
-#define U64_MIN  UINT64_MIN
-#define U64_MAX  UINT64_MAX
-#define UINT_MIN UINTPTR_MIN
-#define UINT_MAX UINTPTR_MAX
-
-#define S8_MIN   INT8_MIN
-#define S8_MAX   INT8_MAX
-#define S16_MIN  INT16_MIN
-#define S16_MAX  INT16_MAX
-#define S32_MIN  INT32_MIN
-#define S32_MAX  INT32_MAX
-#define S64_MIN  INT64_MIN
-#define S64_MAX  INT64_MAX
-#define SINT_MIN INTPTR_MIN
-#define SINT_MAX INTPTR_MAX
-
-#define F32_MIN  FLT_MIN
-#define F32_MAX  FLT_MAX
-#define F64_MIN  DBL_MIN
-#define F64_MAX  DBL_MAX
-#define F128_MIN LDBL_MIN
-#define F128_MAX LDBL_MAX
-
-#define BIT(x) (1 << (x))
-
-#define IS_BIN(c) (c == '0' || c == '1')
-#define IS_OCT(c) (c >= '0' && c <= '7')
-#define IS_DEC(c) (c >= '0' && c <= '9')
-#define IS_DOZ(c) ((c >= '0' && c <= '9') || (c == 'A' || c == 'B'))
-#define IS_HEX(c) ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))
-
-#endif // OMKOV_CLL_CLL_H_ZO7IRBJS
diff --git a/src/compile.c b/src/compile.c
index fcb5e03..d9ea135 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -5,7 +5,7 @@
 

 
-#include "cll/cll.h"
+#include "util/util.h"
 #include "compile.h"
 #include "g.h"
 #include "llvm/llvm.h"
@@ -20,7 +20,7 @@
 
 bool lflag = false, pflag = false;
 
-void compile(u8 *src, UINT len) {
+void compile(u8 *src, usize len) {
 	lex l = lex_init(src, len);
 	if (lflag) { lex_debug(&l); goto ret; }
 
diff --git a/src/compile.h b/src/compile.h
index 342a2d7..e3c9131 100644
--- a/src/compile.h
+++ b/src/compile.h
@@ -8,11 +8,11 @@
 #ifndef G_COMPILE_H_DSDZQ0ZM
 #define G_COMPILE_H_DSDZQ0ZM
 
-#include "cll/cll.h"
+#include "util/util.h"
 
 extern bool lflag, pflag;
 
-extern void compile(u8 *src, UINT len);
+extern void compile(u8 *src, usize len);
 extern void compile_file(const char *file);
 
 #endif // G_COMPILE_H_DSDZQ0ZM
diff --git a/src/g.h b/src/g.h
index 97f975d..ade650e 100644
--- a/src/g.h
+++ b/src/g.h
@@ -11,8 +11,7 @@
 #include "type.h"
 #include "util/stack.h"
 #include "value.h"
-
-#include "cll/cll.h"
+#include "util/util.h"
 
 typedef enum {
 	LK_NIL, LK_EOF, LK_IDN,
@@ -37,10 +36,10 @@ typedef enum {
 	AK_PROC, AK_RETURN,
 } ast_k;
 
-typedef struct { UINT ln, cl; } pos;
+typedef struct { usize ln, cl; } pos;
 typedef struct { tok_k k; pos p; u8 *s; u64 h; } tok;
 
-typedef struct { u8 *s; UINT sp, sl, ln, cl; tok t; } lex;
+typedef struct { u8 *s; usize sp, sl, ln, cl; tok t; } lex;
 
 typedef struct ast {
 	ast_k k; pos p; type *t; val v; u8 *s;
@@ -50,7 +49,7 @@ typedef struct ast {
 extern char *tok_ks[];
 extern char *ast_ks[];
 
-extern lex lex_init(u8 *src, UINT len);
+extern lex lex_init(u8 *src, usize len);
 extern tok lex_next(lex *l);
 extern tok lex_kind(lex *l, tok_k k);
 extern tok lex_peek(lex *l);
diff --git a/src/keyword.c b/src/keyword.c
index e42526c..ef37447 100644
--- a/src/keyword.c
+++ b/src/keyword.c
@@ -6,8 +6,8 @@
 

 #include "keyword.h"
+#include "util/util.h"
 
-#include "cll/cll.h"
 #include "cll/fnv.h"
 
 #include <string.h>
@@ -23,7 +23,7 @@ kwd *k_proc = &kwds[KK_PROC]; kwd *k_return = &kwds[KK_RETURN];
 
 /* Return pointer to keyword or null if invalid. */
 kwd *kwd_find(u64 hash) {
-	for (UINT i = 0; i < sizeof (kwds) / sizeof (*kwds); ++i) {
+	for (usize i = 0; i < sizeof (kwds) / sizeof (*kwds); ++i) {
 		if (hash == kwds[i].h) { return &kwds[i]; }
 	} return NULL;
 }
diff --git a/src/keyword.h b/src/keyword.h
index 851848f..65b12ff 100644
--- a/src/keyword.h
+++ b/src/keyword.h
@@ -9,8 +9,7 @@
 #define G_KEYWORD_H_UEY4FULO
 
 #include "g.h"
-
-#include "cll/cll.h"
+#include "util/util.h"
 
 typedef struct { tok_k k; u64 h; char *name; } kwd;
 
diff --git a/src/lex.c b/src/lex.c
index ea5914b..ff6f7dd 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -8,8 +8,8 @@
 #include "g.h"
 #include "keyword.h"
 #include "type.h"
+#include "util/util.h"
 
-#include "cll/cll.h"
 #include "cll/error.h"
 #include "cll/fnv.h"
 
@@ -40,7 +40,7 @@ char *tok_ks[] = {
 };
 
 /* Initialise a lexer. */
-lex lex_init(u8 *src, UINT len) {
+lex lex_init(u8 *src, usize len) {
 	lex l = { src, 0, len, 0, 0 }; lex_next(&l); return l;
 }
 
@@ -49,7 +49,7 @@ tok lex_next(lex *l) {
 	/* Short circuit if the current token is EOF */
 	if (l->t.k == LK_EOF) { return l->t; }
 
-	tok t = l->t, n; u8 *ss = NULL; UINT sl = 0;
+	tok t = l->t, n; u8 *ss = NULL; usize sl = 0;
 
 skip:;
 	/* Skip null characters and whitespace */
diff --git a/src/llvm/gen.c b/src/llvm/gen.c
index 62e1129..c031591 100644
--- a/src/llvm/gen.c
+++ b/src/llvm/gen.c
@@ -7,9 +7,9 @@
 
 #include "../g.h"
 #include "../type.h"
+#include "../util/util.h"
 #include "llvm.h"
 
-#include "../cll/cll.h"
 #include "../cll/error.h"
 
 #include <llvm-c/Analysis.h>
diff --git a/src/llvm/llvm.h b/src/llvm/llvm.h
index 69d026f..17f710e 100644
--- a/src/llvm/llvm.h
+++ b/src/llvm/llvm.h
@@ -10,8 +10,7 @@
 
 #include "../g.h"
 #include "../type.h"
-
-#include "../cll/cll.h"
+#include "../util/util.h"
 
 #include <llvm-c/Types.h>
 
diff --git a/src/main.c b/src/main.c
index a02ed21..56d70ed 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,8 +6,8 @@
 

 #include "compile.h"
+#include "util/util.h"
 
-#include "cll/cll.h"
 #include "cll/error.h"
 #include "cll/optget.h"
 
@@ -29,11 +29,11 @@ static void ver(void);
 int main(int ac, char *av[]) { (void)(ac); A0 = av[0];
 	struct opt opt = OPTGET_INIT; opt.str = "LP"; opt.lops = lops;
 	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
-	case 'L': { lflag = true; break; }
-	case 'P': { pflag = true; break; }
+	case 'L': { lflag = true; } break;
+	case 'P': { pflag = true; } break;
 	case 256: { hlp(); return 0; }
 	case 257: { ver(); return 0; }
-	default: { return 1; }
+	default:  { return 1; }
 	}
 
 	if (opt.ind == ac) { error(1, "Missing operand"); }
diff --git a/src/map.c b/src/map.c
index a6b83f6..4c059a8 100644
--- a/src/map.c
+++ b/src/map.c
@@ -6,7 +6,7 @@
 

 #include "map.h"
-#include "cll/cll.h"
+#include "util/util.h"
 
 #include "cll/fnv.h"
 
diff --git a/src/map.h b/src/map.h
index 379d14b..22076c5 100644
--- a/src/map.h
+++ b/src/map.h
@@ -8,7 +8,7 @@
 #ifndef G_MAP_H_ISL5XLWM
 #define G_MAP_H_ISL5XLWM
 
-#include "cll/cll.h"
+#include "util/util.h"
 #include "type.h"
 
 typedef enum { SK_VOID, } ent_k; /* Entry Kind */
@@ -16,7 +16,7 @@ typedef enum { SF_VOID = BIT(0), } ent_f; /* Entry Flag */
 
 typedef struct sym { ent_k k; ent_f f; type t; } sym;
 typedef struct ent { u8 *k; sym v; struct ent *n, *c; } ent;
-typedef struct { ent **a, *c; UINT al, ac; } map;
+typedef struct { ent **a, *c; usize al, ac; } map;
 
 extern void map_init(map *m);
 extern void map_free(map *m);
diff --git a/src/parse.c b/src/parse.c
index a8b7676..8f32b12 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -8,9 +8,9 @@
 #include "g.h"
 #include "type.h"
 #include "util/stack.h"
+#include "util/util.h"
 #include "value.h"
 
-#include "cll/cll.h"
 #include "cll/error.h"
 
 #include <stdio.h>
diff --git a/src/type.h b/src/type.h
index 2348014..eca26e0 100644
--- a/src/type.h
+++ b/src/type.h
@@ -8,7 +8,7 @@
 #ifndef G_TYPE_H_QHH0TJJQ
 #define G_TYPE_H_QHH0TJJQ
 
-#include "cll/cll.h"
+#include "util/util.h"
 
 typedef enum {
 	TK_VOID, TK_PTR, TK_TYPE, TK_ANY,
@@ -46,7 +46,7 @@ typedef enum {
 } type_f;
 
 typedef struct type {
-	type_k k; type_f f; sint l;
+	type_k k; type_f f; ssize l;
 	u8 *name; struct type *base;
 } type;
 
diff --git a/src/util/ast.h b/src/util/ast.h
index 660b235..ceab4d7 100644
--- a/src/util/ast.h
+++ b/src/util/ast.h
@@ -9,18 +9,18 @@
 #define OMKOV_G_AST_H_TUNIMNOY
 
 #include "../g.h"
-#include "../cll/cll.h"
+#include "../util/util.h"
 
 #include <stdio.h>
 
-static void ast_print(ast *a, UINT i) {
-	for (UINT j = 0; j != i; ++j) { printf("    "); }
+static void ast_print(ast *a, usize i) {
+	for (usize j = 0; j != i; ++j) { printf("    "); }
 	printf("%zu:%zu: %s: %s\n", a->p.ln, a->p.cl, ast_ks[a->k], a->s);
 
 	if (a->c) { ast_print(a->c, i + 1); }
 	if (a->cl) { ast_print(a->cl, i + 1); }
 	if (a->cr) { ast_print(a->cr, i + 1); }
-	if (a->cs) for (UINT ci = 0; ci != a->cs->al; ++ci) {
+	if (a->cs) for (usize ci = 0; ci != a->cs->al; ++ci) {
 		ast_print(a->cs->a[ci], i + 1);
 	}
 
diff --git a/src/util/stack.c b/src/util/stack.c
index af02a98..f6ab99e 100644
--- a/src/util/stack.c
+++ b/src/util/stack.c
@@ -9,16 +9,16 @@
 
 #include <stdlib.h>
 
-stack *stack_init(void) { return calloc(1, sizeof (ptr)); }
+stack *stack_init(void) { return calloc(1, sizeof (void *)); }
 void stack_free(stack *a) { free(a->a); free(a); }
 
 /* Push a pointer to the top of the stack. */
-void stack_push(stack *a, ptr p) {
+void stack_push(stack *a, void *p) {
 	if (a->al == a->ac) { a->a = realloc(a->a, a->al * sizeof (*a->a)); }
 	a->a[a->al] = p; a->al += 1; return;
 }
 
 /* Pop a pointer from the top of the stack. */
-ptr *stack_pop(stack *a) {
+void *stack_pop(stack *a) {
 	return a->a[--a->al];
 }
diff --git a/src/util/stack.h b/src/util/stack.h
index d1a4acc..8351804 100644
--- a/src/util/stack.h
+++ b/src/util/stack.h
@@ -8,14 +8,14 @@
 #ifndef OMKOV_G_STACK_H_0R2AT1B5
 #define OMKOV_G_STACK_H_0R2AT1B5
 
-#include "../cll/cll.h"
+#include "../util/util.h"
 
-typedef struct { ptr *a; UINT al, ac; } stack;
+typedef struct { void **a; usize al, ac; } stack;
 
 extern stack *stack_init(void);
 extern void stack_free(stack *a);
 
-extern void stack_push(stack *a, ptr p);
-extern ptr *stack_pop(stack *a);
+extern void stack_push(stack *a, void *p);
+extern void *stack_pop(stack *a);
 
 #endif // OMKOV_G_STACK_H_0R2AT1B5
diff --git a/src/util/util.c b/src/util/util.c
new file mode 100644
index 0000000..f49b6ae
--- /dev/null
+++ b/src/util/util.c
@@ -0,0 +1,10 @@
+// util/util.c
+// Common utility source file for G
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+
+
+#include "util.h"
+
+
diff --git a/src/util/util.h b/src/util/util.h
new file mode 100644
index 0000000..3d08927
--- /dev/null
+++ b/src/util/util.h
@@ -0,0 +1,72 @@
+// util/util.h
+// Common utility header file for G
+// Copyright (C) 2021, Jakob Wakeling
+// All rights reserved.
+
+
+
+#ifndef G_UTIL_UTIL_H_QPTPGZ3N
+#define G_UTIL_UTIL_H_QPTPGZ3N
+
+#include <assert.h>
+#include <float.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+/* Type Definitions */
+typedef uint8_t  u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+typedef size_t   usize;
+
+typedef int8_t   s8;
+typedef int16_t  s16;
+typedef int32_t  s32;
+typedef int64_t  s64;
+typedef intptr_t ssize;
+
+typedef float       f32;
+typedef double      f64;
+typedef long double f128;
+
+/* Type Limits */
+#define U8_MIN    UINT8_MIN
+#define U8_MAX    UINT8_MAX
+#define U16_MIN   UINT16_MIN
+#define U16_MAX   UINT16_MAX
+#define U32_MIN   UINT32_MIN
+#define U32_MAX   UINT32_MAX
+#define U64_MIN   UINT64_MIN
+#define U64_MAX   UINT64_MAX
+#define USIZE_MIN SIZE_MIN
+#define USIZE_MAX SIZE_MAX
+
+#define S8_MIN    INT8_MIN
+#define S8_MAX    INT8_MAX
+#define S16_MIN   INT16_MIN
+#define S16_MAX   INT16_MAX
+#define S32_MIN   INT32_MIN
+#define S32_MAX   INT32_MAX
+#define S64_MIN   INT64_MIN
+#define S64_MAX   INT64_MAX
+#define SSIZE_MIN INTPTR_MIN
+#define SSIZE_MAX INTPTR_MAX
+
+#define F32_MIN   FLT_MIN
+#define F32_MAX   FLT_MAX
+#define F64_MIN   DBL_MIN
+#define F64_MAX   DBL_MAX
+#define F128_MIN  LDBL_MIN
+#define F128_MAX  LDBL_MAX
+
+/* Memory Allocation */
+#define assert_malloc(size) (assert(malloc(size)))
+#define assert_calloc(nmemb, size) (assert(calloc(nmemb, size)))
+#define assert_realloc(ptr, size) (assert(realloc(ptr, size)))
+
+/* Miscellaneous */
+#define BIT(x) (1 << (x))
+
+#endif // G_UTIL_UTIL_H_QPTPGZ3N
diff --git a/src/value.c b/src/value.c
index 0e0e6fa..d469690 100644
--- a/src/value.c
+++ b/src/value.c
@@ -5,7 +5,7 @@
 

 
-#include "cll/cll.h"
+#include "util/util.h"
 #include "value.h"
 
 #include <errno.h>
@@ -18,7 +18,7 @@ val val_f128(f128 f) { return (val){ VK_FLT,  .v_flt  = f }; }
 
 /* Convert an integer string to a val. */
 val val_strint(u8 *s) {
-	val v = { VK_INT, .v_int = 0 }; u64 c; UINT b = 10;
+	val v = { VK_INT, .v_int = 0 }; u64 c; usize b = 10;
 
 	if (s[0] == '0') switch (s[1]) {
 	case 'b': { s += 2; b = 2; } break;  case 'o': { s += 2; b = 8; } break;
diff --git a/src/value.h b/src/value.h
index c92fe69..580fcd4 100644
--- a/src/value.h
+++ b/src/value.h
@@ -8,7 +8,7 @@
 #ifndef G_VALUE_H_X2RKXBBA
 #define G_VALUE_H_X2RKXBBA
 
-#include "cll/cll.h"
+#include "util/util.h"
 
 typedef enum { VK_NULL, VK_BOOL, VK_INT, VK_FLT } val_k;