Add #i"pathname" syntax for reading values from modular input files.

This commit is contained in:
Jesse D. McDonald 2009-11-22 21:38:32 -06:00
parent ea9b1734fd
commit 6254044280
2 changed files with 23 additions and 9 deletions

View File

@ -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_definition(reader_state_t *state);
static value_t read_placeholder(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 next_char(reader_state_t *state);
static void skip_whitespace(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); return read_definition(state);
case '=': case '=':
return read_placeholder(state); return read_placeholder(state);
case 'I':
case 'i':
next_char(state);
return read_indirect(read_string(state));
default: default:
release_assert(NOTREACHED("Invalid character in special value.")); release_assert(NOTREACHED("Invalid character in special value."));
return UNDEFINED; return UNDEFINED;
@ -432,6 +438,7 @@ static value_t read_string(reader_state_t *state)
value_t value; value_t value;
release_assert(buffer != NULL); release_assert(buffer != NULL);
release_assert(state->ch == '"');
next_char(state); 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) 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)) for (value_t item = state->ref_alist.value; !is_nil(item); item = _CDDR(item))

View File

@ -207,15 +207,9 @@ static void test_reader(void)
fflush(stdout); fflush(stdout);
v = read_value(stdin); v = read_value(stdin);
if (is_struct(v) && _get_struct(v)->type == lookup_builtin(BI_LAMBDA))
{ print_value(v); nl();
print_value(run_interpreter(v, NIL)); nl();
}
else
{
print_value(v);
}
nl(); nl();
} while (v != NIL); } while (v != NIL);
} }