Implement proper reading of "nil" in REPL

This commit is contained in:
Konstantin Nazarov 2024-08-26 13:45:13 +01:00
parent 56ccf2b054
commit f73de2675d
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 15 additions and 0 deletions

View file

@ -211,6 +211,7 @@ Result<Expression> Compiler::compile_expr(Context& context, Value& expr) {
case Tag::String: case Tag::String:
return compile_constant(context, expr); return compile_constant(context, expr);
case Tag::Nil: case Tag::Nil:
return compile_constant(context, expr);
case Tag::Float: case Tag::Float:
case Tag::Syntax: case Tag::Syntax:
case Tag::Array: case Tag::Array:

View file

@ -51,6 +51,9 @@ Result<Value> Reader::read_one() {
} else if (match("true") || match("false")) { } else if (match("true") || match("false")) {
auto res = read_bool(); auto res = read_bool();
if (res.has_value()) return res; if (res.has_value()) return res;
} else if (match("nil")) {
auto res = read_nil();
if (res.has_value()) return res;
} else if (is_string_start()) { } else if (is_string_start()) {
return read_string(); return read_string();
} else if (match('(')) { } else if (match('(')) {
@ -157,6 +160,16 @@ Result<Value> Reader::read_bool() {
return ERROR(ReadError); return ERROR(ReadError);
} }
Result<Value> Reader::read_nil() {
if (match("nil")) {
forward(3);
if (!is_separator(get()) && !is_eof()) return ERROR(ReadError);
return Value(TRY(Nil::create()));
}
return ERROR(ReadError);
}
Result<Value> Reader::read_string() { Result<Value> Reader::read_string() {
if (!match('"')) return ERROR(ReadError); if (!match('"')) return ERROR(ReadError);
size_t start = position_.offset + 1; size_t start = position_.offset + 1;

View file

@ -33,6 +33,7 @@ class Reader {
Result<Value> read_number(); Result<Value> read_number();
Result<Value> read_symbol(); Result<Value> read_symbol();
Result<Value> read_bool(); Result<Value> read_bool();
Result<Value> read_nil();
char32_t get(size_t offset = 0); char32_t get(size_t offset = 0);
bool match(const char* str); bool match(const char* str);