Implement proper reading of "nil" in REPL
This commit is contained in:
parent
56ccf2b054
commit
f73de2675d
3 changed files with 15 additions and 0 deletions
|
@ -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:
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue