summary refs log tree commit diff stats
path: root/lith.h
diff options
context:
space:
mode:
authorSudipto Mallick <smallick.dev+lith@gmail.com>2020-07-06 19:18:09 +0000
committerSudipto Mallick <smallick.dev+lith@gmail.com>2020-07-07 13:52:17 +0000
commit5a8c3e315ef9892dfb6e23ade18955b26d58643c (patch)
tree2047965ff7084ddb0e25df18abbe6bec0c3baf67 /lith.h
parentf843bdcb2e97a330cd72f91173895b0b37bb3dd8 (diff)
downloadlith-5a8c3e315ef9892dfb6e23ade18955b26d58643c.tar.gz
some more reformattings, library support master
Diffstat (limited to 'lith.h')
-rw-r--r--lith.h53
1 files changed, 41 insertions, 12 deletions
diff --git a/lith.h b/lith.h
index 3d9d1fb..719374e 100644
--- a/lith.h
+++ b/lith.h
@@ -3,6 +3,7 @@
 #define lith_h
 
 #include <stddef.h>
+#include <stdio.h>
 
 #define LITH_VERSION_STRING "0.1.0-alpha"
 
@@ -10,7 +11,9 @@ typedef struct lith_value lith_value;
 typedef struct lith_value lith_env;
 typedef struct lith_state lith_st;
 typedef struct lith_string lith_string;
+typedef struct lith_closure lith_closure;
 typedef enum lith_value_type lith_valtype;
+typedef struct lith_lib_fn *lith_lib;
 
 enum lith_error {
     LITH_ERR_OK,
@@ -18,6 +21,7 @@ enum lith_error {
     LITH_ERR_SYNTAX,
     LITH_ERR_NOMEM,
     LITH_ERR_UNBOUND,
+    LITH_ERR_REDEFINE,
     LITH_ERR_NARGS,
     LITH_ERR_TYPE,
     LITH_ERR_CUSTOM
@@ -46,10 +50,20 @@ struct lith_value {
         int boolean;
         long integer;
         double number;
-        struct lith_string { size_t len; char *buf; } string;
+        struct lith_string {
+            size_t len;
+            char *buf;
+        } string;
         char *symbol;
-        struct { struct lith_value *car, *cdr; } pair;
+        struct {
+            struct lith_value *car, *cdr;
+        } pair;
         lith_builtin_function function;
+        struct lith_closure {
+            lith_value *name;
+            lith_env *parent;
+            lith_value *args, *body;
+        } *closure;
     } value;
 };
 
@@ -63,14 +77,23 @@ struct lith_value {
 #define LITH_CDR(p) ((p)->value.pair.cdr)
 #define LITH_CONS lith_make_pair
 
+#define LITH_IS_CALLABLE(F) \
+    (LITH_IS(F, LITH_TYPE_MACRO) || LITH_IS(F, LITH_TYPE_CLOSURE))
+
 struct lith_state {
     enum lith_error error;
     struct lith_error_state {
         int success, manual;
         char *msg, *sym, *name;
         lith_value *expr;
-        struct lith_error_state__argsize { size_t expected, got; int exact; } nargs;
-        struct lith_error_state__type { lith_valtype expected, got; size_t narg; } type;
+        struct lith_error_state__argsize {
+            size_t expected, got;
+            int exact;
+        } nargs;
+        struct lith_error_state__type {
+            lith_valtype expected, got;
+            size_t narg;
+        } type;
     } error_state;
     char *types[LITH_NTYPES];
     lith_value *nil;
@@ -80,24 +103,29 @@ struct lith_state {
     char *filename;
 };
 
+struct lith_lib_fn {
+    char *name;
+    lith_builtin_function fn;
+};
+
+extern struct lith_lib_fn lith_builtins[];
+
 #define LITH_IS_ERR(L) ((L)->error != LITH_ERR_OK)
 #define LITH_AT_END_NO_ERR(L) (((L)->error == LITH_ERR_EOF) && (L)->error_state.success)
 
 #define LITH_TO_BOOL(B) ((!LITH_IS_NIL(B)) && !(LITH_IS(B, LITH_TYPE_BOOLEAN) && !((B)->value.boolean)))
 #define LITH_IN_BOOL(B) ((B) ? L->True : L->False)
 
-/* When a number is printed, how many digits you want after the decimal point */
-#ifndef LITH_NFP
-#define LITH_NFP 8
-#endif
+/* Public functions: the API of this library */
 
 void lith_init(lith_st *);
 void lith_free(lith_st *);
 void lith_clear_error_state(lith_st *);
 void lith_print_error(lith_st *, int);
+void lith_simple_error(lith_st *, enum lith_error, char *);
 
 lith_value *lith_new_value(lith_st *);
-void lith_print_value(lith_value *);
+void lith_print_value(lith_value *, FILE *);
 void lith_free_value(lith_value *);
 lith_value *lith_copy_value(lith_st *, lith_value *);
 
@@ -106,7 +134,7 @@ lith_value *lith_make_number(lith_st *, double);
 lith_value *lith_make_symbol(lith_st *, char *);
 lith_value *lith_make_string(lith_st *, char *, size_t);
 lith_value *lith_make_builtin(lith_st *, lith_builtin_function);
-lith_value *lith_make_closure(lith_st *, lith_env *, lith_value *, lith_value *);
+lith_value *lith_make_closure(lith_st *, lith_env *, lith_value *, lith_value *, lith_value *);
 lith_value *lith_make_pair(lith_st *, lith_value *, lith_value *);
 
 lith_value *lith_get_symbol(lith_st *, char *);
@@ -121,9 +149,10 @@ lith_env *lith_new_env(lith_st *, lith_env *);
 void lith_free_env(lith_env *);
 
 lith_value *lith_env_get(lith_st *, lith_env *, lith_value *);
+void lith_env_set(lith_st *, lith_env *, lith_value *, lith_value *);
 void lith_env_put(lith_st *, lith_env *, lith_value *, lith_value *);
 
-void lith_fill_env(lith_st *);
+void lith_fill_env(lith_st *, lith_lib);
 
 int lith_expect_type(lith_st *, char *, size_t, lith_valtype, lith_value *);
 int lith_expect_nargs(lith_st *, char *, size_t, lith_value *, int);
@@ -131,4 +160,4 @@ int lith_expect_nargs(lith_st *, char *, size_t, lith_value *, int);
 void lith_run_string(lith_st *, lith_env *, char *);
 void lith_run_file(lith_st *, lith_env *, char *);
 
-#endif
+#endif /* lith_h */