summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2009-12-11 03:15:23 +0100
committerJohannes Weiner <hannes@cmpxchg.org>2009-12-11 03:18:20 +0100
commit8fc1bc7359126845c60594bc3e773b1e0841659c (patch)
treea0159525437cd3ba7be76ac4f9f02cf5ad088024
parent5921de9563a5a08dcdb2d794e31bd57333754110 (diff)
string: add (print)
Also adds sheep_repr(), switches earlier sheep_format() callsites and all implementations of type->format(). Booyah. Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
-rw-r--r--examples/ean.sheep31
-rw-r--r--include/sheep/object_types.h2
-rw-r--r--include/sheep/string.h3
-rw-r--r--sheep/alien.c2
-rw-r--r--sheep/bool.c2
-rw-r--r--sheep/code.c2
-rw-r--r--sheep/compile.c2
-rw-r--r--sheep/function.c2
-rw-r--r--sheep/list.c4
-rw-r--r--sheep/main.c8
-rw-r--r--sheep/module.c2
-rw-r--r--sheep/name.c2
-rw-r--r--sheep/number.c2
-rw-r--r--sheep/string.c46
14 files changed, 76 insertions, 34 deletions
diff --git a/examples/ean.sheep b/examples/ean.sheep
index e336074..5312242 100644
--- a/examples/ean.sheep
+++ b/examples/ean.sheep
@@ -46,15 +46,22 @@ take the difference from the next multiple of ten."
(reverse (digits ean))))
10))
-(function concat-numbers (a b)
- "Make a number that consists of the digits in
-A concatenated with the digits in B."
- (number (concat (string a) (string b))))
-
-# (function ean-checksum-range (base count)
-# (if count
-# (block
-# (ddump (concat-numbers base (ean-checksum base)))
-# (ean-checksum-range (+ base 1) (- count 1)))))
-
-# (ean-checksum-range 12345 2000)
+(function ean-checksum-range (base count)
+ (if count
+ (block
+ (print base (ean-checksum base))
+ (ean-checksum-range (+ base 1) (- count 1)))))
+
+# xxx: should move to lib.sheep but (load lib) is not yet working with
+# modules relative to the calling module.
+(function nth (list nr)
+ (if (and list nr)
+ (nth (tail list) (- nr 1))
+ (head list)))
+
+# xxx: should be able to check for being loaded or executed? or just
+# have the library in libean.sheep and the executable in ean.sheep?
+(if (not (= (length argv) 3))
+ (print "usage: " (nth argv 0) " base count")
+ (ean-checksum-range (number (nth argv 1))
+ (number (nth argv 2))))
diff --git a/include/sheep/object_types.h b/include/sheep/object_types.h
index 5f98f54..8924fa8 100644
--- a/include/sheep/object_types.h
+++ b/include/sheep/object_types.h
@@ -34,7 +34,7 @@ struct sheep_type {
const struct sheep_sequence *sequence;
- void (*format)(sheep_t, char **, size_t *);
+ void (*format)(sheep_t, char **, size_t *, int);
};
struct sheep_object {
diff --git a/include/sheep/string.h b/include/sheep/string.h
index c235559..2fc336c 100644
--- a/include/sheep/string.h
+++ b/include/sheep/string.h
@@ -21,8 +21,9 @@ static inline const char *sheep_rawstring(sheep_t sheep)
return sheep_data(sheep);
}
-void __sheep_format(sheep_t, char **, size_t *);
+void __sheep_format(sheep_t, char **, size_t *, int);
char *sheep_format(sheep_t);
+char *sheep_repr(sheep_t);
void sheep_string_builtins(struct sheep_vm *);
diff --git a/sheep/alien.c b/sheep/alien.c
index b2431da..060cae1 100644
--- a/sheep/alien.c
+++ b/sheep/alien.c
@@ -15,7 +15,7 @@ static void free_alien(struct sheep_vm *vm, sheep_t sheep)
sheep_free(sheep_data(sheep));
}
-static void format_alien(sheep_t sheep, char **bufp, size_t *posp)
+static void format_alien(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
struct sheep_alien *alien;
diff --git a/sheep/bool.c b/sheep/bool.c
index bd3544b..453b102 100644
--- a/sheep/bool.c
+++ b/sheep/bool.c
@@ -17,7 +17,7 @@ static int test_bool(sheep_t sheep)
return sheep != &sheep_false;
}
-static void format_bool(sheep_t sheep, char **bufp, size_t *posp)
+static void format_bool(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
if (sheep == &sheep_false)
sheep_addprintf(bufp, posp, "false");
diff --git a/sheep/code.c b/sheep/code.c
index fdecdf2..4552913 100644
--- a/sheep/code.c
+++ b/sheep/code.c
@@ -81,7 +81,7 @@ void sheep_code_dump(struct sheep_vm *vm, struct sheep_function *function,
return;
}
- str = sheep_format(sheep);
+ str = sheep_repr(sheep);
printf("; %s\n", str);
sheep_free(str);
}
diff --git a/sheep/compile.c b/sheep/compile.c
index 011d223..b343e30 100644
--- a/sheep/compile.c
+++ b/sheep/compile.c
@@ -198,7 +198,7 @@ static int compile_name(struct sheep_vm *vm,
sheep_emit(&function->code, SHEEP_GLOBAL, (unsigned long)entry);
return 0;
err:
- tmp = sheep_format(expr);
+ tmp = sheep_repr(expr);
fprintf(stderr, "unbound name: %s\n", tmp);
sheep_free(tmp);
return -1;
diff --git a/sheep/function.c b/sheep/function.c
index efbb315..0de48f7 100644
--- a/sheep/function.c
+++ b/sheep/function.c
@@ -36,7 +36,7 @@ static void free_function(struct sheep_vm *vm, sheep_t sheep)
sheep_free(function);
}
-static void format_function(sheep_t sheep, char **bufp, size_t *posp)
+static void format_function(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
struct sheep_function *function;
diff --git a/sheep/list.c b/sheep/list.c
index 867b895..6ba25ec 100644
--- a/sheep/list.c
+++ b/sheep/list.c
@@ -137,7 +137,7 @@ static int equal_list(sheep_t a, sheep_t b)
return !(la->head || lb->head);
}
-static void format_list(sheep_t sheep, char **bufp, size_t *posp)
+static void format_list(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
struct sheep_list *list;
@@ -145,7 +145,7 @@ static void format_list(sheep_t sheep, char **bufp, size_t *posp)
sheep_addprintf(bufp, posp, "(");
while (list->head) {
sheep = list->head;
- __sheep_format(sheep, bufp, posp);
+ __sheep_format(sheep, bufp, posp, 1);
list = sheep_list(list->tail);
if (!list->head)
break;
diff --git a/sheep/main.c b/sheep/main.c
index 185cab4..158457b 100644
--- a/sheep/main.c
+++ b/sheep/main.c
@@ -59,7 +59,7 @@ static int do_stdin(int ac, char **av)
while (1) {
sheep_t exp, fun, val;
- char *res;
+ char *repr;
printf("> ");
fflush(stdout);
@@ -74,9 +74,9 @@ static int do_stdin(int ac, char **av)
val = sheep_eval(&vm, fun);
if (!val)
continue;
- res = sheep_format(val);
- puts(res);
- sheep_free(res);
+ repr = sheep_repr(val);
+ puts(repr);
+ sheep_free(repr);
}
puts("bye");
diff --git a/sheep/module.c b/sheep/module.c
index 3bf0a11..4e9cd3f 100644
--- a/sheep/module.c
+++ b/sheep/module.c
@@ -31,7 +31,7 @@ static void free_module(struct sheep_vm *vm, sheep_t sheep)
do_free_module(sheep_data(sheep));
}
-static void format_module(sheep_t sheep, char **bufp, size_t *posp)
+static void format_module(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
struct sheep_module *mod = sheep_data(sheep);
diff --git a/sheep/name.c b/sheep/name.c
index fd27266..8c0dbcb 100644
--- a/sheep/name.c
+++ b/sheep/name.c
@@ -39,7 +39,7 @@ static int equal_name(sheep_t a, sheep_t b)
return 1;
}
-static void format_name(sheep_t sheep, char **bufp, size_t *posp)
+static void format_name(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
struct sheep_name *name;
unsigned int i;
diff --git a/sheep/number.c b/sheep/number.c
index 831eb16..883ffb3 100644
--- a/sheep/number.c
+++ b/sheep/number.c
@@ -21,7 +21,7 @@ static int test_number(sheep_t sheep)
return !!sheep_fixnum(sheep);
}
-static void format_number(sheep_t sheep, char **bufp, size_t *posp)
+static void format_number(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
sheep_addprintf(bufp, posp, "%ld", sheep_fixnum(sheep));
}
diff --git a/sheep/string.c b/sheep/string.c
index 5370e18..0bd73e9 100644
--- a/sheep/string.c
+++ b/sheep/string.c
@@ -6,6 +6,7 @@
#include <sheep/compile.h>
#include <sheep/object.h>
#include <sheep/unpack.h>
+#include <sheep/bool.h> /* &sheep_true... fix me */
#include <sheep/util.h>
#include <sheep/gc.h>
#include <sheep/vm.h>
@@ -76,9 +77,11 @@ static int equal_string(sheep_t a, sheep_t b)
return !strcmp(sheep_rawstring(a), sheep_rawstring(b));
}
-static void format_string(sheep_t sheep, char **bufp, size_t *posp)
+static void format_string(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
- sheep_addprintf(bufp, posp, "\"%s\"", sheep_rawstring(sheep));
+ const char *fmt = repr ? "\"%s\"" : "%s";
+
+ sheep_addprintf(bufp, posp, fmt, sheep_rawstring(sheep));
}
const struct sheep_type sheep_string_type = {
@@ -101,9 +104,9 @@ sheep_t sheep_make_string(struct sheep_vm *vm, const char *str)
return __sheep_make_string(vm, sheep_strdup(str));
}
-void __sheep_format(sheep_t sheep, char **bufp, size_t *posp)
+void __sheep_format(sheep_t sheep, char **bufp, size_t *posp, int repr)
{
- return sheep_type(sheep)->format(sheep, bufp, posp);
+ sheep_type(sheep)->format(sheep, bufp, posp, repr);
}
char *sheep_format(sheep_t sheep)
@@ -111,7 +114,16 @@ char *sheep_format(sheep_t sheep)
char *buf = NULL;
size_t pos = 0;
- __sheep_format(sheep, &buf, &pos);
+ __sheep_format(sheep, &buf, &pos, 0);
+ return buf;
+}
+
+char *sheep_repr(sheep_t sheep)
+{
+ char *buf = NULL;
+ size_t pos = 0;
+
+ __sheep_format(sheep, &buf, &pos, 1);
return buf;
}
@@ -127,7 +139,7 @@ static sheep_t builtin_string(struct sheep_vm *vm, unsigned int nr_args)
if (sheep_type(sheep) == &sheep_string_type)
return sheep;
- buf = sheep_format(sheep);
+ buf = sheep_repr(sheep);
return __sheep_make_string(vm, buf);
}
@@ -247,9 +259,31 @@ out:
return NULL;
}
+/* (print &rest objects) */
+static sheep_t builtin_print(struct sheep_vm *vm, unsigned int nr_args)
+{
+ unsigned int offset = nr_args;
+
+ while (offset) {
+ unsigned long index;
+ char *tmp;
+
+ index = vm->stack.nr_items - offset;
+ tmp = sheep_format(vm->stack.items[index]);
+ printf("%s", tmp);
+ sheep_free(tmp);
+ offset--;
+ }
+ puts("");
+ vm->stack.nr_items -= nr_args;
+
+ return &sheep_true;
+}
+
void sheep_string_builtins(struct sheep_vm *vm)
{
sheep_vm_function(vm, "string", builtin_string);
sheep_vm_function(vm, "split", builtin_split);
sheep_vm_function(vm, "join", builtin_join);
+ sheep_vm_function(vm, "print", builtin_print);
}