aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2024-01-09 18:39:49 -0800
committerPreston Pan <preston@nullring.xyz>2024-01-09 18:39:49 -0800
commitac6004730fa54a756d1627a4e8450cd32df86f75 (patch)
tree8cb4d59438a1252fa069788b2ffb78b2a1bbad3e /include
parent6ccf0572469dfc8cd8fa7b8537b2ac6c265d2df6 (diff)
reorganize directory structure
Diffstat (limited to 'include')
-rw-r--r--include/better_string.h28
-rw-r--r--include/builtins.h54
-rw-r--r--include/macros.h80
-rw-r--r--include/stem.h119
4 files changed, 281 insertions, 0 deletions
diff --git a/include/better_string.h b/include/better_string.h
new file mode 100644
index 0000000..03bc6f6
--- /dev/null
+++ b/include/better_string.h
@@ -0,0 +1,28 @@
+#ifndef BETTER_STRING_H
+#define BETTER_STRING_H
+#include <stdlib.h>
+/* We want better strings to realloc less and keep track of the length of the
+ * string so we don't have to call strlen every time */
+typedef struct STRING_STRUCT {
+ /* length of string */
+ size_t length;
+ /* Size of current value buffer */
+ size_t bufsize;
+ char *value;
+} string_t;
+
+/* Allocates memory for new string */
+string_t *init_string(char *value);
+
+/* Copies string to another string */
+string_t *string_copy(string_t *s);
+
+/* Concatenates a string_t type and a char * type */
+void string_concat(string_t *s1, string_t *s2);
+
+/* Appends single characters */
+void string_append(string_t *s, char c);
+
+/* Frees space for string */
+void string_free(string_t *s);
+#endif
diff --git a/include/builtins.h b/include/builtins.h
new file mode 100644
index 0000000..148fba7
--- /dev/null
+++ b/include/builtins.h
@@ -0,0 +1,54 @@
+#ifndef BUILTINS_H_
+#define BUILTINS_H_
+#include <stem.h>
+
+void stemadd(value_t *v);
+void stemsub(value_t *v);
+void stemmul(value_t *v);
+void stemdiv(value_t *v);
+void stemfunc(value_t *v);
+void stempow(value_t *v);
+void stemsin(value_t *v);
+void stemcos(value_t *v);
+void stemexp(value_t *v);
+void stemeval(value_t *v);
+void stemln(value_t *v);
+void stemceil(value_t *v);
+void stemfloor(value_t *v);
+void strquote(value_t *v);
+void curry(value_t *v);
+void nop(value_t *v);
+void stemfread(value_t *v);
+void stemread(value_t *v);
+void stemexit(value_t *v);
+void quote(value_t *v);
+void stemtype(value_t *v);
+void dsc(value_t *v);
+void swap(value_t *v);
+void isdef(value_t *v);
+void stemdup(value_t *v);
+void questionmark(value_t *v);
+void period(value_t *v);
+void stemlen(value_t *v);
+void dip(value_t *v);
+void keep(value_t *v);
+void del(value_t *v);
+void clear(value_t *v);
+void stemif(value_t *v);
+void gtequals(value_t *v);
+void ltequals(value_t *v);
+void gthan(value_t *v);
+void lthan(value_t *v);
+void equals(value_t *v);
+void nequals(value_t *v);
+void wtostr(value_t *v);
+void compose(value_t *v);
+void isnum(value_t *v);
+void stoi(value_t *v);
+void ssize(value_t *v);
+void qstack(value_t *v);
+void vat(value_t *v);
+void stemfwrite(value_t *v);
+
+void add_funcs();
+#endif // BUILTINS_H_
diff --git a/include/macros.h b/include/macros.h
new file mode 100644
index 0000000..0fc94c8
--- /dev/null
+++ b/include/macros.h
@@ -0,0 +1,80 @@
+#ifndef MACROS_H
+#define MACROS_H
+#define BLK "\e[0;30m"
+#define RED "\e[0;31m"
+#define GRN "\e[0;32m"
+#define YEL "\e[0;33m"
+#define BLU "\e[0;34m"
+#define MAG "\e[0;35m"
+#define CYN "\e[0;36m"
+#define WHT "\e[0;37m"
+
+/* Regular bold text */
+#define BBLK "\e[1;30m"
+#define BRED "\e[1;31m"
+#define BGRN "\e[1;32m"
+#define BYEL "\e[1;33m"
+#define BBLU "\e[1;34m"
+#define BMAG "\e[1;35m"
+#define BCYN "\e[1;36m"
+#define BWHT "\e[1;37m"
+
+/* Regular underline text */
+#define UBLK "\e[4;30m"
+#define URED "\e[4;31m"
+#define UGRN "\e[4;32m"
+#define UYEL "\e[4;33m"
+#define UBLU "\e[4;34m"
+#define UMAG "\e[4;35m"
+#define UCYN "\e[4;36m"
+#define UWHT "\e[4;37m"
+
+/* Regular background */
+#define BLKB "\e[40m"
+#define REDB "\e[41m"
+#define GRNB "\e[42m"
+#define YELB "\e[43m"
+#define BLUB "\e[44m"
+#define MAGB "\e[45m"
+#define CYNB "\e[46m"
+#define WHTB "\e[47m"
+
+/* High intensty background */
+#define BLKHB "\e[0;100m"
+#define REDHB "\e[0;101m"
+#define GRNHB "\e[0;102m"
+#define YELHB "\e[0;103m"
+#define BLUHB "\e[0;104m"
+#define MAGHB "\e[0;105m"
+#define CYNHB "\e[0;106m"
+#define WHTHB "\e[0;107m"
+
+/* High intensty text */
+#define HBLK "\e[0;90m"
+#define HRED "\e[0;91m"
+#define HGRN "\e[0;92m"
+#define HYEL "\e[0;93m"
+#define HBLU "\e[0;94m"
+#define HMAG "\e[0;95m"
+#define HCYN "\e[0;96m"
+#define HWHT "\e[0;97m"
+
+/* Bold high intensity text */
+#define BHBLK "\e[1;90m"
+#define BHRED "\e[1;91m"
+#define BHGRN "\e[1;92m"
+#define BHYEL "\e[1;93m"
+#define BHBLU "\e[1;94m"
+#define BHMAG "\e[1;95m"
+#define BHCYN "\e[1;96m"
+#define BHWHT "\e[1;97m"
+
+/* Reset */
+#define reset "\e[0m"
+#define CRESET "\e[0m"
+#define COLOR_RESET "\e[0m"
+
+/* Die in case of catastrophic memory problem */
+void die(char *message);
+
+#endif
diff --git a/include/stem.h b/include/stem.h
new file mode 100644
index 0000000..2643d36
--- /dev/null
+++ b/include/stem.h
@@ -0,0 +1,119 @@
+#ifndef PARSER_H_
+#define PARSER_H_
+#include <better_string.h>
+#include <stdbool.h>
+#include <stdlib.h>
+
+typedef struct VALUE_STRUCT value_t;
+typedef struct ARRAY_STRUCT array_t;
+
+struct ARRAY_STRUCT {
+ value_t **items;
+ size_t size;
+ size_t capacity;
+};
+
+struct VALUE_STRUCT {
+ enum { VWORD, VINT, VFLOAT, VSTR, VQUOTE, VERR, VCUSTOM } type;
+ union {
+ long double int_float;
+ array_t *quote;
+ string_t *str_word;
+ };
+ void *custom;
+ bool escaped;
+};
+
+typedef struct PARSER_STRUCT {
+ char *source;
+ int i;
+ char c;
+} parser_t;
+
+typedef struct NODE_STRUCT {
+ string_t *key;
+ void *value;
+ struct NODE_STRUCT *next;
+} node_t;
+
+typedef struct {
+ node_t *head;
+ size_t size;
+} sll_t;
+
+typedef struct {
+ sll_t **buckets;
+ size_t size;
+} ht_t;
+
+typedef struct {
+ void (*printfunc)(void *);
+ void *(*copyfunc)(void *);
+ void (*freefunc)(void *);
+} custom_t;
+
+void func_free(void *f);
+
+array_t *init_array(size_t size);
+
+void array_append(array_t *a, value_t *v);
+
+value_t *array_pop(array_t *a);
+
+array_t *array_copy(array_t *a);
+
+void array_extend(array_t *a1, array_t *a2);
+
+void array_free(array_t *a);
+
+value_t *init_value(int type);
+
+value_t *value_copy(value_t *v);
+
+void value_free(void *v);
+
+custom_t *init_custom(void (*)(void *), void (*)(void *), void *(*)(void *));
+
+void custom_free(void *);
+
+parser_t *init_parser(char *source);
+
+void parser_reset(parser_t *p, char *source);
+
+void parser_move(parser_t *p);
+
+void parser_skip_whitespace(parser_t *p);
+
+value_t *parser_get_next(parser_t *p);
+
+node_t *init_node(string_t *key, void *v);
+
+sll_t *init_sll();
+
+void sll_add(sll_t *l, string_t *key, void *v);
+
+void *sll_get(sll_t *l, string_t *key);
+
+void sll_free(sll_t *l, void (*freefunc)(void *));
+
+ht_t *init_ht(size_t size);
+
+void ht_add(ht_t *h, string_t *key, void *v);
+
+void *ht_get(ht_t *h, string_t *key);
+
+bool ht_exists(ht_t *h, string_t *key);
+
+void ht_free(ht_t *h, void (*freefunc)(void *));
+
+unsigned long hash(ht_t *h, char *key);
+
+bool eval_builtins(value_t *v);
+
+bool eval_ht(value_t *v);
+
+bool eval_quote(value_t *v);
+
+void eval(value_t *v);
+
+#endif // PARSER_H_