summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2009-12-01 19:16:38 +0100
committerJohannes Weiner <hannes@cmpxchg.org>2009-12-01 19:16:38 +0100
commit133b32b4161ae03816097029950c2fc40a39a85e (patch)
treee22b068c6e453f5c4e842f03aca53a4e3b7e5ba2
parentd9c2ada8076a325ad0979980b39f8723521a72d7 (diff)
unpack: move unpack interface to unpack.c
The idea was to have these basic unpack helpers in unpack.c and then different wrappers for compiler paths and evaluator paths, respectively, to do appropriate error handling. But for now, it's fprintf() only anyway, so keep it simple. Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
-rw-r--r--include/sheep/core.h4
-rw-r--r--include/sheep/unpack.h5
-rw-r--r--sheep/bool.c2
-rw-r--r--sheep/core.c59
-rw-r--r--sheep/function.c2
-rw-r--r--sheep/list.c2
-rw-r--r--sheep/number.c2
-rw-r--r--sheep/sequence.c2
-rw-r--r--sheep/string.c2
-rw-r--r--sheep/unpack.c61
10 files changed, 72 insertions, 69 deletions
diff --git a/include/sheep/core.h b/include/sheep/core.h
index b0a54b6..ef487bd 100644
--- a/include/sheep/core.h
+++ b/include/sheep/core.h
@@ -10,10 +10,6 @@
#include <sheep/vm.h>
#include <stdarg.h>
-int sheep_unpack_list(const char *, struct sheep_list *, const char *, ...);
-int sheep_unpack_stack(const char *, struct sheep_vm *, unsigned int,
- const char *, ...);
-
void sheep_core_init(struct sheep_vm *);
void sheep_core_exit(struct sheep_vm *);
diff --git a/include/sheep/unpack.h b/include/sheep/unpack.h
index edc42c9..8d58f07 100644
--- a/include/sheep/unpack.h
+++ b/include/sheep/unpack.h
@@ -20,7 +20,12 @@ enum sheep_unpack {
enum sheep_unpack __sheep_unpack_list(const char **, sheep_t *,
struct sheep_list *, const char *, va_list);
+int sheep_unpack_list(const char *, struct sheep_list *, const char *, ...);
+
enum sheep_unpack __sheep_unpack_stack(const char **, sheep_t *,
struct sheep_vector *, const char *, va_list);
+int sheep_unpack_stack(const char *, struct sheep_vm *, unsigned int,
+ const char *, ...);
+
#endif /* _SHEEP_UNPACK_H */
diff --git a/sheep/bool.c b/sheep/bool.c
index e4f4f18..9a037ac 100644
--- a/sheep/bool.c
+++ b/sheep/bool.c
@@ -5,7 +5,7 @@
*/
#include <sheep/compile.h>
#include <sheep/object.h>
-#include <sheep/core.h>
+#include <sheep/unpack.h>
#include <sheep/util.h>
#include <sheep/vm.h>
#include <stdio.h>
diff --git a/sheep/core.c b/sheep/core.c
index e7fb69d..9f0fa5f 100644
--- a/sheep/core.c
+++ b/sheep/core.c
@@ -18,35 +18,6 @@
#include <sheep/core.h>
-int sheep_unpack_list(const char *caller, struct sheep_list *list,
- const char *items, ...)
-{
- enum sheep_unpack status;
- const char *wanted;
- sheep_t mismatch;
- va_list ap;
-
- va_start(ap, items);
- status = __sheep_unpack_list(&wanted, &mismatch, list, items, ap);
- va_end(ap);
-
- switch (status) {
- case SHEEP_UNPACK_OK:
- return 0;
- case SHEEP_UNPACK_MISMATCH:
- fprintf(stderr, "%s: expected %s, got %s\n",
- caller, wanted, sheep_type(mismatch)->name);
- return -1;
- case SHEEP_UNPACK_TOO_MANY:
- fprintf(stderr, "%s: too few arguments\n", caller);
- return -1;
- case SHEEP_UNPACK_TOO_FEW:
- fprintf(stderr, "%s: too many arguments\n", caller);
- default: /* weird compiler... */
- return -1;
- }
-}
-
/* (quote expr) */
static int compile_quote(struct sheep_vm *vm, struct sheep_function *function,
struct sheep_context *context, struct sheep_list *args)
@@ -402,36 +373,6 @@ static int compile_set(struct sheep_vm *vm, struct sheep_function *function,
return sheep_compile_set(vm, function, context, name);
}
-int sheep_unpack_stack(const char *caller, struct sheep_vm *vm,
- unsigned int nr_args, const char *items, ...)
-{
- enum sheep_unpack status;
- const char *wanted;
- sheep_t mismatch;
- va_list ap;
-
- if (strlen(items) != nr_args) {
- fprintf(stderr, "%s: too %s arguments\n", caller,
- strlen(items) > nr_args ? "few" : "many");
- return -1;
- }
-
- va_start(ap, items);
- status = __sheep_unpack_stack(&wanted, &mismatch, &vm->stack, items,ap);
- va_end(ap);
-
- switch (status) {
- case SHEEP_UNPACK_OK:
- return 0;
- case SHEEP_UNPACK_MISMATCH:
- fprintf(stderr, "%s: expected %s, got %s\n",
- caller, wanted, sheep_type(mismatch)->name);
- return -1;
- default: /* should not happen, nr_args is trustworthy */
- return -1;
- }
-}
-
void sheep_core_init(struct sheep_vm *vm)
{
sheep_map_set(&vm->specials, "quote", compile_quote);
diff --git a/sheep/function.c b/sheep/function.c
index b4bfa40..045287b 100644
--- a/sheep/function.c
+++ b/sheep/function.c
@@ -4,9 +4,9 @@
* Copyright (c) 2009 Johannes Weiner <hannes@cmpxchg.org>
*/
#include <sheep/object.h>
+#include <sheep/unpack.h>
#include <sheep/bool.h>
#include <sheep/code.h>
-#include <sheep/core.h>
#include <sheep/util.h>
#include <sheep/vm.h>
#include <stdio.h>
diff --git a/sheep/list.c b/sheep/list.c
index 327f0fa..ae002c2 100644
--- a/sheep/list.c
+++ b/sheep/list.c
@@ -6,8 +6,8 @@
#include <sheep/compile.h>
#include <sheep/object.h>
#include <sheep/string.h>
+#include <sheep/unpack.h>
#include <sheep/eval.h>
-#include <sheep/core.h>
#include <sheep/util.h>
#include <sheep/vm.h>
#include <stdio.h>
diff --git a/sheep/number.c b/sheep/number.c
index 1758c75..0e2bb0c 100644
--- a/sheep/number.c
+++ b/sheep/number.c
@@ -6,8 +6,8 @@
#include <sheep/compile.h>
#include <sheep/object.h>
#include <sheep/string.h>
+#include <sheep/unpack.h>
#include <sheep/bool.h>
-#include <sheep/core.h>
#include <sheep/util.h>
#include <sheep/vm.h>
#include <limits.h>
diff --git a/sheep/sequence.c b/sheep/sequence.c
index a2bae0d..25d007c 100644
--- a/sheep/sequence.c
+++ b/sheep/sequence.c
@@ -5,7 +5,7 @@
*/
#include <sheep/number.h>
#include <sheep/object.h>
-#include <sheep/core.h>
+#include <sheep/unpack.h>
#include <sheep/vm.h>
#include <stdio.h>
diff --git a/sheep/string.c b/sheep/string.c
index 582f7d2..fd0afcf 100644
--- a/sheep/string.c
+++ b/sheep/string.c
@@ -5,7 +5,7 @@
*/
#include <sheep/compile.h>
#include <sheep/object.h>
-#include <sheep/core.h>
+#include <sheep/unpack.h>
#include <sheep/util.h>
#include <sheep/vm.h>
#include <string.h>
diff --git a/sheep/unpack.c b/sheep/unpack.c
index 1d59c69..cc82bd1 100644
--- a/sheep/unpack.c
+++ b/sheep/unpack.c
@@ -13,9 +13,11 @@
#include <sheep/list.h>
#include <sheep/name.h>
#include <sheep/util.h>
+#include <sheep/vm.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
+#include <stdio.h>
#include <sheep/unpack.h>
@@ -112,6 +114,35 @@ enum sheep_unpack __sheep_unpack_list(const char **wanted, sheep_t *mismatch,
return SHEEP_UNPACK_OK;
}
+int sheep_unpack_list(const char *caller, struct sheep_list *list,
+ const char *items, ...)
+{
+ enum sheep_unpack status;
+ const char *wanted;
+ sheep_t mismatch;
+ va_list ap;
+
+ va_start(ap, items);
+ status = __sheep_unpack_list(&wanted, &mismatch, list, items, ap);
+ va_end(ap);
+
+ switch (status) {
+ case SHEEP_UNPACK_OK:
+ return 0;
+ case SHEEP_UNPACK_MISMATCH:
+ fprintf(stderr, "%s: expected %s, got %s\n",
+ caller, wanted, sheep_type(mismatch)->name);
+ return -1;
+ case SHEEP_UNPACK_TOO_MANY:
+ fprintf(stderr, "%s: too few arguments\n", caller);
+ return -1;
+ case SHEEP_UNPACK_TOO_FEW:
+ fprintf(stderr, "%s: too many arguments\n", caller);
+ default: /* weird compiler... */
+ return -1;
+ }
+}
+
enum sheep_unpack __sheep_unpack_stack(const char **wanted, sheep_t *mismatch,
struct sheep_vector *stack,
const char *items, va_list ap)
@@ -153,3 +184,33 @@ enum sheep_unpack __sheep_unpack_stack(const char **wanted, sheep_t *mismatch,
stack->nr_items = base;
return SHEEP_UNPACK_OK;
}
+
+int sheep_unpack_stack(const char *caller, struct sheep_vm *vm,
+ unsigned int nr_args, const char *items, ...)
+{
+ enum sheep_unpack status;
+ const char *wanted;
+ sheep_t mismatch;
+ va_list ap;
+
+ if (strlen(items) != nr_args) {
+ fprintf(stderr, "%s: too %s arguments\n", caller,
+ strlen(items) > nr_args ? "few" : "many");
+ return -1;
+ }
+
+ va_start(ap, items);
+ status = __sheep_unpack_stack(&wanted, &mismatch, &vm->stack, items,ap);
+ va_end(ap);
+
+ switch (status) {
+ case SHEEP_UNPACK_OK:
+ return 0;
+ case SHEEP_UNPACK_MISMATCH:
+ fprintf(stderr, "%s: expected %s, got %s\n",
+ caller, wanted, sheep_type(mismatch)->name);
+ return -1;
+ default: /* should not happen, nr_args is trustworthy */
+ return -1;
+ }
+}