summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2010-02-10 10:23:19 +0100
committerJohannes Weiner <hannes@cmpxchg.org>2010-02-10 10:23:19 +0100
commita5e33ab4ba32510700076288bab987cd3934e1ca (patch)
treea96b4c241f5f7dbcd0acc710993bdbb48e6d673e
parent01b59cb9909c5f117a389a1a4592ca10cb6efe00 (diff)
parse: move code from compile.c
Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
-rw-r--r--include/sheep/compile.h8
-rw-r--r--include/sheep/parse.h21
-rw-r--r--sheep/Makefile2
-rw-r--r--sheep/compile.c144
-rw-r--r--sheep/core.c1
-rw-r--r--sheep/parse.c154
6 files changed, 178 insertions, 152 deletions
diff --git a/include/sheep/compile.h b/include/sheep/compile.h
index 003eaf4..86fe05e 100644
--- a/include/sheep/compile.h
+++ b/include/sheep/compile.h
@@ -12,7 +12,6 @@
#include <sheep/read.h>
#include <sheep/map.h>
#include <sheep/vm.h>
-#include <stdarg.h>
sheep_t __sheep_compile(struct sheep_vm *, struct sheep_module *,
struct sheep_expr *);
@@ -46,8 +45,6 @@ struct sheep_context {
* @sheep: object to compile
*/
-void sheep_parser_error(struct sheep_compile *, sheep_t, const char *, ...);
-
static inline int sheep_compile_object(struct sheep_compile *compile,
struct sheep_function *function,
struct sheep_context *context, sheep_t sheep)
@@ -66,9 +63,4 @@ int sheep_compile_list(struct sheep_compile *, struct sheep_function *,
void sheep_propagate_foreigns(struct sheep_function *, struct sheep_function *);
-int __sheep_parse(struct sheep_compile *, struct sheep_list *,
- struct sheep_list *, const char *, ...);
-int sheep_parse(struct sheep_compile *, struct sheep_list *,
- const char *, ...);
-
#endif /* _SHEEP_COMPILE_H */
diff --git a/include/sheep/parse.h b/include/sheep/parse.h
new file mode 100644
index 0000000..17fbf66
--- /dev/null
+++ b/include/sheep/parse.h
@@ -0,0 +1,21 @@
+/*
+ * include/hseep/parse.h
+ *
+ * Copyright (c) 2010 Johannes Weiner <hannes@cmpxchg.org>
+ */
+#ifndef _SHEEP_PARSE_H
+#define _SHEEP_PARSE_H
+
+#include <sheep/compile.h> /* mooh */
+#include <sheep/object.h>
+#include <sheep/list.h>
+#include <stdarg.h>
+
+void sheep_parser_error(struct sheep_compile *, sheep_t, const char *, ...);
+
+int __sheep_parse(struct sheep_compile *, struct sheep_list *,
+ struct sheep_list *, const char *, ...);
+int sheep_parse(struct sheep_compile *, struct sheep_list *,
+ const char *, ...);
+
+#endif /* _SHEEP_PARSE_H */
diff --git a/sheep/Makefile b/sheep/Makefile
index e939626..58d9f4a 100644
--- a/sheep/Makefile
+++ b/sheep/Makefile
@@ -1,5 +1,5 @@
sheep-obj := util.o vector.o map.o code.o gc.o
sheep-obj += object.o bool.o string.o name.o number.o list.o \
sequence.o function.o alien.o
-sheep-obj += unpack.o vm.o module.o read.o compile.o eval.o core.o
+sheep-obj += unpack.o vm.o module.o read.o parse.o compile.o eval.o core.o
sheep-obj += main.o
diff --git a/sheep/compile.c b/sheep/compile.c
index 3be6fd5..bfaba43 100644
--- a/sheep/compile.c
+++ b/sheep/compile.c
@@ -4,8 +4,8 @@
* Copyright (c) 2009 Johannes Weiner <hannes@cmpxchg.org>
*/
#include <sheep/function.h>
-#include <sheep/string.h>
#include <sheep/vector.h>
+#include <sheep/parse.h>
#include <sheep/code.h>
#include <sheep/list.h>
#include <sheep/name.h>
@@ -14,10 +14,6 @@
#include <sheep/map.h>
#include <sheep/gc.h>
#include <sheep/vm.h>
-#include <stdarg.h>
-#include <string.h>
-#include <ctype.h>
-#include <stdio.h>
#include <sheep/compile.h>
@@ -52,34 +48,6 @@ sheep_t __sheep_compile(struct sheep_vm *vm, struct sheep_module *module,
return sheep_make_object(vm, &sheep_function_type, function);
}
-void sheep_parser_error(struct sheep_compile *compile, sheep_t culprit,
- const char *fmt, ...)
-{
- struct sheep_expr *expr = compile->expr;
- unsigned long position;
- const char *repr;
- va_list ap;
-
- if (sheep_type(expr->object) == &sheep_list_type) {
- struct sheep_list *list = sheep_list(expr->object);
-
- position = 1; /* list itself is at 0 */
- sheep_list_search(list, culprit, &position);
- } else
- position = 0;
-
- fprintf(stderr, "%s:%lu: ", expr->filename,
- (unsigned long)expr->lines.items[position]);
-
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
-
- repr = sheep_repr(culprit);
- fprintf(stderr, " `%s'\n", repr);
- sheep_free(repr);
-}
-
int sheep_compile_constant(struct sheep_compile *compile,
struct sheep_function *function,
struct sheep_context *context, sheep_t sheep)
@@ -359,113 +327,3 @@ void sheep_propagate_foreigns(struct sheep_function *function,
var->slot = slot_foreign(function, var->dist - 1, var->slot);
}
}
-
-enum {
- PARSE_OK,
- PARSE_MISMATCH,
- PARSE_TOO_FEW,
- PARSE_TOO_MANY
-};
-
-static unsigned int __parse(struct sheep_list *form, const char *items,
- sheep_t *mismatchp, va_list ap)
-{
- while (*items) {
- struct sheep_name *name;
- struct sheep_list *list;
- sheep_t thing;
-
- if (!form->head && *items != 'r')
- return PARSE_TOO_FEW;
-
- if (tolower(*items) == 'r') {
- *va_arg(ap, struct sheep_list **) = form;
- return PARSE_OK;
- }
-
- thing = form->head;
-
- switch (*items) {
- case 'e':
- *va_arg(ap, sheep_t *) = thing;
- break;
- case 's':
- if (sheep_type(thing) != &sheep_name_type)
- goto mismatch;
- name = sheep_name(thing);
- if (name->nr_parts != 1)
- goto mismatch;
- *va_arg(ap, const char **) = name->parts[0];
- break;
- case 'n':
- if (sheep_type(thing) != &sheep_name_type)
- goto mismatch;
- name = sheep_name(thing);
- *va_arg(ap, struct sheep_name **) = name;
- break;
- case 'l':
- if (sheep_type(thing) != &sheep_list_type)
- goto mismatch;
- list = sheep_list(thing);
- *va_arg(ap, struct sheep_list **) = list;
- break;
- default:
- sheep_bug("invalid parser instruction");
- }
-
- form = sheep_list(form->tail);
- items++;
- continue;
- mismatch:
- *mismatchp = thing;
- return PARSE_MISMATCH;
- }
-
- if (form->head)
- return PARSE_TOO_MANY;
-
- return PARSE_OK;
-}
-
-static int parse(struct sheep_compile *compile, struct sheep_list *form,
- struct sheep_list *child, const char *items, va_list ap)
-{
- unsigned int ret;
- sheep_t mismatch;
-
- ret = __parse(child, items, &mismatch, ap);
- if (ret == PARSE_OK)
- return 0;
-
- if (ret == PARSE_MISMATCH)
- sheep_parser_error(compile, mismatch, "parser error at");
- else
- sheep_parser_error(compile, form->head, "too %s arguments to",
- ret == PARSE_TOO_MANY ? "many" : "few");
-
- return -1;
-}
-
-int __sheep_parse(struct sheep_compile *compile, struct sheep_list *form,
- struct sheep_list *child, const char *items, ...)
-{
- va_list ap;
- int ret;
-
- va_start(ap, items);
- ret = parse(compile, form, child, items, ap);
- va_end(ap);
- return ret;
-}
-
-int sheep_parse(struct sheep_compile *compile, struct sheep_list *form,
- const char *items, ...)
-{
- va_list ap;
- int ret;
-
- va_start(ap, items);
- ret = parse(compile, form, sheep_list(form->tail), items, ap);
- va_end(ap);
- return ret;
-}
diff --git a/sheep/core.c b/sheep/core.c
index 13dbd2d..a861ecc 100644
--- a/sheep/core.c
+++ b/sheep/core.c
@@ -6,6 +6,7 @@
#include <sheep/function.h>
#include <sheep/compile.h>
#include <sheep/module.h>
+#include <sheep/parse.h>
#include <sheep/code.h>
#include <sheep/list.h>
#include <sheep/name.h>
diff --git a/sheep/parse.c b/sheep/parse.c
new file mode 100644
index 0000000..42605ca
--- /dev/null
+++ b/sheep/parse.c
@@ -0,0 +1,154 @@
+/*
+ * sheep/parse.c
+ *
+ * Copyright (c) 2010 Johannes Weiner <hannes@cmpxchg.org>
+ */
+#include <sheep/object.h>
+#include <sheep/string.h>
+#include <sheep/list.h>
+#include <sheep/name.h>
+#include <sheep/read.h>
+#include <sheep/util.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <stdio.h>
+
+#include <sheep/parse.h>
+
+void sheep_parser_error(struct sheep_compile *compile, sheep_t culprit,
+ const char *fmt, ...)
+{
+ struct sheep_expr *expr = compile->expr;
+ unsigned long position;
+ const char *repr;
+ va_list ap;
+
+ if (sheep_type(expr->object) == &sheep_list_type) {
+ struct sheep_list *list = sheep_list(expr->object);
+
+ position = 1; /* list itself is at 0 */
+ sheep_list_search(list, culprit, &position);
+ } else
+ position = 0;
+
+ fprintf(stderr, "%s:%lu: ", expr->filename,
+ (unsigned long)expr->lines.items[position]);
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+
+ repr = sheep_repr(culprit);
+ fprintf(stderr, " `%s'\n", repr);
+ sheep_free(repr);
+}
+
+enum {
+ PARSE_OK,
+ PARSE_MISMATCH,
+ PARSE_TOO_FEW,
+ PARSE_TOO_MANY
+};
+
+static unsigned int __parse(struct sheep_list *form, const char *items,
+ sheep_t *mismatchp, va_list ap)
+{
+ while (*items) {
+ struct sheep_name *name;
+ struct sheep_list *list;
+ sheep_t thing;
+
+ if (!form->head && *items != 'r')
+ return PARSE_TOO_FEW;
+
+ if (tolower(*items) == 'r') {
+ *va_arg(ap, struct sheep_list **) = form;
+ return PARSE_OK;
+ }
+
+ thing = form->head;
+
+ switch (*items) {
+ case 'e':
+ *va_arg(ap, sheep_t *) = thing;
+ break;
+ case 's':
+ if (sheep_type(thing) != &sheep_name_type)
+ goto mismatch;
+ name = sheep_name(thing);
+ if (name->nr_parts != 1)
+ goto mismatch;
+ *va_arg(ap, const char **) = name->parts[0];
+ break;
+ case 'n':
+ if (sheep_type(thing) != &sheep_name_type)
+ goto mismatch;
+ name = sheep_name(thing);
+ *va_arg(ap, struct sheep_name **) = name;
+ break;
+ case 'l':
+ if (sheep_type(thing) != &sheep_list_type)
+ goto mismatch;
+ list = sheep_list(thing);
+ *va_arg(ap, struct sheep_list **) = list;
+ break;
+ default:
+ sheep_bug("invalid parser instruction");
+ }
+
+ form = sheep_list(form->tail);
+ items++;
+ continue;
+ mismatch:
+ *mismatchp = thing;
+ return PARSE_MISMATCH;
+ }
+
+ if (form->head)
+ return PARSE_TOO_MANY;
+
+ return PARSE_OK;
+}
+
+static int parse(struct sheep_compile *compile, struct sheep_list *form,
+ struct sheep_list *child, const char *items, va_list ap)
+{
+ unsigned int ret;
+ sheep_t mismatch;
+
+ ret = __parse(child, items, &mismatch, ap);
+ if (ret == PARSE_OK)
+ return 0;
+
+ if (ret == PARSE_MISMATCH)
+ sheep_parser_error(compile, mismatch, "parser error at");
+ else
+ sheep_parser_error(compile, form->head, "too %s arguments to",
+ ret == PARSE_TOO_MANY ? "many" : "few");
+
+ return -1;
+}
+
+int __sheep_parse(struct sheep_compile *compile, struct sheep_list *form,
+ struct sheep_list *child, const char *items, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, items);
+ ret = parse(compile, form, child, items, ap);
+ va_end(ap);
+ return ret;
+}
+
+int sheep_parse(struct sheep_compile *compile, struct sheep_list *form,
+ const char *items, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, items);
+ ret = parse(compile, form, sheep_list(form->tail), items, ap);
+ va_end(ap);
+ return ret;
+}