diff options
-rw-r--r-- | better_string.c | 3 | ||||
-rw-r--r-- | builtins.c | 21 | ||||
-rw-r--r-- | builtins.h | 1 | ||||
-rw-r--r-- | main.c | 10 | ||||
-rw-r--r-- | parser.c | 22 | ||||
-rw-r--r-- | parser.h | 10 |
6 files changed, 53 insertions, 14 deletions
diff --git a/better_string.c b/better_string.c index b3bc931..e601586 100644 --- a/better_string.c +++ b/better_string.c @@ -9,9 +9,10 @@ string_t *init_string(char *s) { die("malloc in init_string"); /* If we pass in NULL we assume that we want to append things to this in the * future */ - if (s == NULL) { + if (s == NULL || strcmp(s, "") == 0) { str->bufsize = 10; str->value = calloc(str->bufsize, sizeof(char)); + str->value[0] = '\0'; if (str->value == NULL) die("calloc in init_string"); str->length = 0; @@ -6,7 +6,7 @@ #include <stdlib.h> #include <string.h> -#define MAX 50 +#define MAX 1000 #define JUSTDO(a) \ if (!(a)) { \ perror(#a); \ @@ -20,7 +20,7 @@ extern char *INBUF; extern parser_t *PARSER; extern ht_t *FLIT; -extern ht_t *OBJ_FREE_TABLE; +extern ht_t *OBJ_TABLE; /* TODO: rotr, rotl, dip, map, filter, errstr, join (for strings), switch * (for quotes), split (split array, string, word into two), del (deleting @@ -43,6 +43,7 @@ char *get_line(FILE *f) { } void print_value(value_t *v) { + custom_t *c; switch (v->type) { case VINT: printf("%.0Lf\n", v->int_float); @@ -66,7 +67,8 @@ void print_value(value_t *v) { printf("STACK ERR\n"); break; case VCUSTOM: - printf("CUSTOM VALUE\n"); + c = ht_get(OBJ_TABLE, v->str_word); + c->printfunc(v->custom); break; } } @@ -398,7 +400,7 @@ void stemexit(value_t *v) { free(INBUF); free(PARSER); array_free(EVAL_STACK); - ht_free(OBJ_FREE_TABLE, func_free); + ht_free(OBJ_TABLE, func_free); exit(0); } @@ -1037,6 +1039,17 @@ void add_func(ht_t *h, void (*func)(value_t *), char *key) { ht_add(h, s, func); } +void add_obj(ht_t *h, ht_t *h2, void (*printfunc)(void *), + void (*freefunc)(void *), void *(*copyfunc)(void *), + void (*createfunc)(void *), char *key) { + + custom_t *c = init_custom(printfunc, freefunc, copyfunc); + ht_add(h, init_string(key), c); + ht_add(h2, init_string(key), createfunc); +} + +void add_objs() {} + void add_funcs() { add_func(FLIT, period, "."); add_func(FLIT, questionmark, "?"); @@ -1,6 +1,7 @@ #ifndef BUILTINS_H_ #define BUILTINS_H_ #include "./parser.h" + void stemadd(value_t *v); void stemsub(value_t *v); void stemmul(value_t *v); @@ -1,16 +1,18 @@ #include "builtins.h" #include "parser.h" +#include <dlfcn.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> + extern ht_t *WORD_TABLE; extern array_t *STACK; extern char *INBUF; extern parser_t *PARSER; extern array_t *EVAL_STACK; -extern ht_t *OBJ_FREE_TABLE; +extern ht_t *OBJ_TABLE; extern ht_t *FLIT; void usage() { @@ -20,7 +22,7 @@ void usage() { void version() { printf("Author: Preston Pan, MIT License 2023\n"); - printf("stem, version 1.1\n"); + printf("stem, version 1.2 alpha\n"); exit(0); } @@ -55,7 +57,7 @@ int main(int argc, char **argv) { WORD_TABLE = init_ht(500); EVAL_STACK = init_array(10); FLIT = init_ht(500); - OBJ_FREE_TABLE = init_ht(500); + OBJ_TABLE = init_ht(500); add_funcs(); @@ -69,7 +71,7 @@ int main(int argc, char **argv) { free(INBUF); ht_free(WORD_TABLE, value_free); ht_free(FLIT, func_free); - ht_free(OBJ_FREE_TABLE, func_free); + ht_free(OBJ_TABLE, custom_free); array_free(STACK); free(PARSER); array_free(EVAL_STACK); @@ -14,7 +14,7 @@ char *INBUF; parser_t *PARSER; ht_t *FLIT; -ht_t *OBJ_FREE_TABLE; +ht_t *OBJ_TABLE; void func_free(void *f) {} @@ -86,6 +86,10 @@ value_t *value_copy(value_t *v) { a->escaped = v->escaped; } else if (v->type == VQUOTE) { a->quote = array_copy(v->quote); + } else if (v->type == VCUSTOM) { + custom_t *c = ht_get(OBJ_TABLE, a->str_word); + a->custom = c->copyfunc(v->custom); + a->str_word = string_copy(v->str_word); } return a; } @@ -99,12 +103,23 @@ void value_free(void *vtmp) { array_free(v->quote); } if (v->type == VCUSTOM) { - void (*freefunc)(void *) = ht_get(FLIT, v->str_word); + void (*freefunc)(void *) = ht_get(OBJ_TABLE, v->str_word); freefunc(v->custom); } free(v); } +custom_t *init_custom(void (*printfunc)(void *), void (*freefunc)(void *), + void *(*copyfunc)(void *)) { + custom_t *c = calloc(1, sizeof(custom_t)); + c->printfunc = printfunc; + c->freefunc = freefunc; + c->copyfunc = copyfunc; + return c; +} + +void custom_free(void *c) { free(c); } + parser_t *init_parser(char *source) { parser_t *p = calloc(1, sizeof(parser_t)); p->i = 0; @@ -401,9 +416,6 @@ bool eval_ht(value_t *v) { } bool eval_builtins(value_t *v) { - if (v->type != VSTR && v->type != VWORD) { - printf("what the fuck\n"); - } void (*func)(value_t *) = ht_get(FLIT, v->str_word); if (func == NULL) return false; @@ -46,6 +46,12 @@ typedef struct { 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); @@ -66,6 +72,10 @@ 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); |