summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/lcq/pit/runtime.h13
-rw-r--r--include/lcq/pit/types.h2
-rw-r--r--include/lcq/pit/utils.h39
3 files changed, 41 insertions, 13 deletions
diff --git a/include/lcq/pit/runtime.h b/include/lcq/pit/runtime.h
index f946a71..1cf8549 100644
--- a/include/lcq/pit/runtime.h
+++ b/include/lcq/pit/runtime.h
@@ -27,7 +27,7 @@ typedef struct {
i64 capacity, next;
pit_value data[];
} pit_values;
-pit_values *pit_values_new(i64 capacity);
+pit_values *pit_values_new(u8 *buf, i64 buf_len);
void pit_values_push(struct pit_runtime *rt, pit_values *s, pit_value x);
pit_value pit_values_pop(struct pit_runtime *rt, pit_values *s);
@@ -77,7 +77,7 @@ typedef struct {
i64 capacity, next;
pit_runtime_eval_program_entry data[];
} pit_runtime_eval_program;
-pit_runtime_eval_program *pit_runtime_eval_program_new(i64 capacity);
+pit_runtime_eval_program *pit_runtime_eval_program_new(u8 *buf, i64 buf_len);
void pit_runtime_eval_program_push_literal(struct pit_runtime *rt, pit_runtime_eval_program *s, pit_value x);
void pit_runtime_eval_program_push_apply(struct pit_runtime *rt, pit_runtime_eval_program *s, i64 arity);
@@ -101,7 +101,7 @@ typedef struct pit_runtime {
i64 source_line, source_column; /* for error reporting only; line and column of token start */
i64 error_line, error_column; /* line and column of token start at time of error */
} pit_runtime;
-pit_runtime *pit_runtime_new(void);
+pit_runtime *pit_runtime_new(u8 *buf, i64 len);
void pit_runtime_freeze(pit_runtime *rt); /* freeze the runtime at the current point - everything currently defined becomes immutable */
void pit_runtime_reset(pit_runtime *rt); /* restore the runtime to the frozen point, resetting everything that has happened since */
@@ -109,8 +109,8 @@ bool pit_runtime_print_error(pit_runtime *rt); /* return true if an error has oc
i64 pit_dump(pit_runtime *rt, char *buf, i64 len, pit_value v, bool readable); /* if readable is true, try to produce output that can be machine-read (quotes on strings, etc) */
#define pit_trace(rt, v) pit_trace_(rt, "Trace [" __FILE__ ":" PIT_STR(__LINE__) "] %s\n", v)
-void pit_trace_(pit_runtime *rt, const char *format, pit_value v);
-void pit_error(pit_runtime *rt, const char *format, ...);
+void pit_trace_(pit_runtime *rt, char *format, pit_value v);
+void pit_error(pit_runtime *rt, char *format, ...);
/* working with small values */
pit_value pit_value_new(pit_runtime *rt, enum pit_value_sort s, u64 data);
@@ -220,4 +220,7 @@ void pit_collect_garbage(pit_runtime *rt);
pit_value pit_load_file(pit_runtime *rt, char *path);
void pit_repl(pit_runtime *rt);
+/* test entrypoint */
+int pit_runtime_test(u8 *out, i64 out_len, u8 *buf, i64 len);
+
#endif
diff --git a/include/lcq/pit/types.h b/include/lcq/pit/types.h
index 384dc77..eb4fe64 100644
--- a/include/lcq/pit/types.h
+++ b/include/lcq/pit/types.h
@@ -3,12 +3,12 @@
#include <stdbool.h>
#include <stdint.h>
+#include <stddef.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
-
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
diff --git a/include/lcq/pit/utils.h b/include/lcq/pit/utils.h
index 6c297ea..b864637 100644
--- a/include/lcq/pit/utils.h
+++ b/include/lcq/pit/utils.h
@@ -1,22 +1,47 @@
#ifndef LCOLONQ_PIT_UTILS_H
#define LCOLONQ_PIT_UTILS_H
-#include <stdckdint.h>
+#include <stdarg.h>
#include <lcq/pit/types.h>
+/* ctype */
+static inline bool pit_ctype_isdigit(int a) { return a >= '0' && a <= '9'; }
+static inline bool pit_ctype_islower(int a) { return a >= 'a' && a <= 'z'; }
+static inline bool pit_ctype_isupper(int a) { return a >= 'A' && a <= 'Z'; }
+static inline bool pit_ctype_isalpha(int a) { return pit_ctype_islower(a) || pit_ctype_isupper(a); }
+static inline bool pit_ctype_isprint(int a) { return a >= 0x20 && a <= 0x7f; }
+static inline bool pit_ctype_isspace(int a) { return a == ' ' || a == '\r' || a == '\n' || a == '\t'; }
+
+/* string */
+static inline size_t pit_string_strlen(char *s) {
+ size_t idx = 0;
+ while (s[idx] != 0) ++idx;
+ return idx;
+}
+static inline u8 *pit_string_memcpy(u8 *dest, u8 *src, size_t n) {
+ size_t i = 0;
+ for (; i < n; ++i) dest[i] = src[i];
+ return dest;
+}
+int pit_string_vsnprintf(char *str, size_t size, char *format, va_list ap);
+int pit_string_snprintf(char *buf, size_t len, char *format, ...);
+
/* assorted utilities and debugging tools */
-#define PIT_STRSTR(x) #x
-#define PIT_STR(x) PIT_STRSTR(x)
-void pit_panic(const char *format, ...);
-void pit_debug(const char *format, ...);
-#define pit_mul(result, a, b) if (ckd_mul(result, a, b)) pit_panic("integer overflow during multiplication%s","");
+#define pit_mul(result, a, b) *result = (i64) (a) * (i64) (b)
/* arenas */
+static inline uintptr_t pit_align_down(uintptr_t addr, uintptr_t align) {
+ return addr & ~(align - 1); /* easy! just zero the low bits */
+}
+static inline uintptr_t pit_align_up(uintptr_t addr, uintptr_t align) {
+ return (addr + align - 1) /* increment past the next aligned address... */
+ & ~(align - 1); /* ...and then zero the low bits */
+}
typedef struct {
i64 elem_size, capacity, next, back;
u8 data[];
} pit_arena;
-pit_arena *pit_arena_new(i64 len, i64 elem_size);
+pit_arena *pit_arena_new(u8 *buf, i64 buf_len, i64 elem_size);
void pit_arena_reset(pit_arena *a);
i64 pit_arena_alloc_idx(pit_arena *a);
i64 pit_arena_alloc_bulk_idx(pit_arena *a, i64 num);