From 62540442800bac90ebeb79a672a2f020cfa1bdff Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Sun, 22 Nov 2009 21:38:32 -0600 Subject: [PATCH] Add #i"pathname" syntax for reading values from modular input files. --- reader.c | 20 ++++++++++++++++++++ rosella.c | 12 +++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/reader.c b/reader.c index 5b5d974..25db31a 100644 --- a/reader.c +++ b/reader.c @@ -34,6 +34,8 @@ static value_t read_weak_box(reader_state_t *state); static value_t read_definition(reader_state_t *state); static value_t read_placeholder(reader_state_t *state); +static value_t read_indirect(value_t path); + static void next_char(reader_state_t *state); static void skip_whitespace(reader_state_t *state); @@ -155,6 +157,10 @@ static value_t read_special(reader_state_t *state) return read_definition(state); case '=': return read_placeholder(state); + case 'I': + case 'i': + next_char(state); + return read_indirect(read_string(state)); default: release_assert(NOTREACHED("Invalid character in special value.")); return UNDEFINED; @@ -432,6 +438,7 @@ static value_t read_string(reader_state_t *state) value_t value; release_assert(buffer != NULL); + release_assert(state->ch == '"'); next_char(state); @@ -666,6 +673,19 @@ static value_t read_placeholder(reader_state_t *state) } } +static value_t read_indirect(value_t path) +{ + char *name = value_to_string(path); + FILE *f = fopen(name, "r"); + value_t v; + free(name); + + release_assert(f != NULL); + v = read_value(f); + fclose(f); + return v; +} + static bool is_placeholder(reader_state_t *state, value_t value) { for (value_t item = state->ref_alist.value; !is_nil(item); item = _CDDR(item)) diff --git a/rosella.c b/rosella.c index 37bc309..dced4b4 100644 --- a/rosella.c +++ b/rosella.c @@ -207,15 +207,9 @@ static void test_reader(void) fflush(stdout); v = read_value(stdin); - if (is_struct(v) && _get_struct(v)->type == lookup_builtin(BI_LAMBDA)) - { - print_value(run_interpreter(v, NIL)); - } - else - { - print_value(v); - } - nl(); nl(); + + print_value(v); nl(); + nl(); } while (v != NIL); }