mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-23 04:15:31 +00:00
Changed 'undefined' to 'nil'.
This commit is contained in:
parent
f9ef592bd3
commit
4a58f35d3d
@ -38,7 +38,7 @@ const auto grammar_ = R"(
|
|||||||
WHILE <- while _ EXPRESSION _ BLOCK
|
WHILE <- while _ EXPRESSION _ BLOCK
|
||||||
IF <- if _ EXPRESSION _ BLOCK (_ else _ if _ EXPRESSION _ BLOCK)* (_ else _ BLOCK)?
|
IF <- if _ EXPRESSION _ BLOCK (_ else _ if _ EXPRESSION _ BLOCK)* (_ else _ BLOCK)?
|
||||||
|
|
||||||
PRIMARY <- WHILE / IF / FUNCTION / OBJECT / ARRAY / UNDEFINED / BOOLEAN / NUMBER / IDENTIFIER / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION _ ')'
|
PRIMARY <- WHILE / IF / FUNCTION / OBJECT / ARRAY / NIL / BOOLEAN / NUMBER / IDENTIFIER / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION _ ')'
|
||||||
|
|
||||||
FUNCTION <- fn _ PARAMETERS _ BLOCK
|
FUNCTION <- fn _ PARAMETERS _ BLOCK
|
||||||
PARAMETERS <- '(' _ (PARAMETER (_ ',' _ PARAMETER)*)? _ ')'
|
PARAMETERS <- '(' _ (PARAMETER (_ ',' _ PARAMETER)*)? _ ')'
|
||||||
@ -63,7 +63,7 @@ const auto grammar_ = R"(
|
|||||||
|
|
||||||
ARRAY <- '[' _ SEQUENCE _ ']' (_ '(' _ EXPRESSION (_ ',' _ EXPRESSION)? _ ')')?
|
ARRAY <- '[' _ SEQUENCE _ ']' (_ '(' _ EXPRESSION (_ ',' _ EXPRESSION)? _ ')')?
|
||||||
|
|
||||||
UNDEFINED <- < 'undefined' _wd_ >
|
NIL <- < 'nil' _wd_ >
|
||||||
BOOLEAN <- < ('true' / 'false') _wd_ >
|
BOOLEAN <- < ('true' / 'false') _wd_ >
|
||||||
|
|
||||||
NUMBER <- < [0-9]+ >
|
NUMBER <- < [0-9]+ >
|
||||||
@ -160,9 +160,9 @@ struct ArrayValue : public ObjectValue {
|
|||||||
|
|
||||||
struct Value
|
struct Value
|
||||||
{
|
{
|
||||||
enum Type { Undefined, Bool, Long, String, Object, Array, Function };
|
enum Type { Nil, Bool, Long, String, Object, Array, Function };
|
||||||
|
|
||||||
Value() : type(Undefined) {}
|
Value() : type(Nil) {}
|
||||||
Value(const Value& rhs) : type(rhs.type), v(rhs.v) {}
|
Value(const Value& rhs) : type(rhs.type), v(rhs.v) {}
|
||||||
Value(Value&& rhs) : type(rhs.type), v(rhs.v) {}
|
Value(Value&& rhs) : type(rhs.type), v(rhs.v) {}
|
||||||
|
|
||||||
@ -256,7 +256,7 @@ struct Value
|
|||||||
|
|
||||||
std::string str() const {
|
std::string str() const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Undefined: return "undefined";
|
case Nil: return "nil";
|
||||||
case Bool: return to_bool() ? "true" : "false";
|
case Bool: return to_bool() ? "true" : "false";
|
||||||
case Long: return std::to_string(to_long());
|
case Long: return std::to_string(to_long());
|
||||||
case String: return "'" + to_string() + "'";
|
case String: return "'" + to_string() + "'";
|
||||||
@ -275,7 +275,7 @@ struct Value
|
|||||||
|
|
||||||
bool operator==(const Value& rhs) const {
|
bool operator==(const Value& rhs) const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Undefined: return rhs.type == Undefined;
|
case Nil: return rhs.type == Nil;
|
||||||
case Bool: return to_bool() == rhs.to_bool();
|
case Bool: return to_bool() == rhs.to_bool();
|
||||||
case Long: return to_long() == rhs.to_long();
|
case Long: return to_long() == rhs.to_long();
|
||||||
case String: return to_string() == rhs.to_string();
|
case String: return to_string() == rhs.to_string();
|
||||||
@ -291,7 +291,7 @@ struct Value
|
|||||||
|
|
||||||
bool operator<=(const Value& rhs) const {
|
bool operator<=(const Value& rhs) const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Undefined: return false;
|
case Nil: return false;
|
||||||
case Bool: return to_bool() <= rhs.to_bool();
|
case Bool: return to_bool() <= rhs.to_bool();
|
||||||
case Long: return to_long() <= rhs.to_long();
|
case Long: return to_long() <= rhs.to_long();
|
||||||
case String: return to_string() <= rhs.to_string();
|
case String: return to_string() <= rhs.to_string();
|
||||||
@ -303,7 +303,7 @@ struct Value
|
|||||||
|
|
||||||
bool operator<(const Value& rhs) const {
|
bool operator<(const Value& rhs) const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Undefined: return false;
|
case Nil: return false;
|
||||||
case Bool: return to_bool() < rhs.to_bool();
|
case Bool: return to_bool() < rhs.to_bool();
|
||||||
case Long: return to_long() < rhs.to_long();
|
case Long: return to_long() < rhs.to_long();
|
||||||
case String: return to_string() < rhs.to_string();
|
case String: return to_string() < rhs.to_string();
|
||||||
@ -315,7 +315,7 @@ struct Value
|
|||||||
|
|
||||||
bool operator>=(const Value& rhs) const {
|
bool operator>=(const Value& rhs) const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Undefined: return false;
|
case Nil: return false;
|
||||||
case Bool: return to_bool() >= rhs.to_bool();
|
case Bool: return to_bool() >= rhs.to_bool();
|
||||||
case Long: return to_long() >= rhs.to_long();
|
case Long: return to_long() >= rhs.to_long();
|
||||||
case String: return to_string() >= rhs.to_string();
|
case String: return to_string() >= rhs.to_string();
|
||||||
@ -327,7 +327,7 @@ struct Value
|
|||||||
|
|
||||||
bool operator>(const Value& rhs) const {
|
bool operator>(const Value& rhs) const {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Undefined: return false;
|
case Nil: return false;
|
||||||
case Bool: return to_bool() > rhs.to_bool();
|
case Bool: return to_bool() > rhs.to_bool();
|
||||||
case Long: return to_long() > rhs.to_long();
|
case Long: return to_long() > rhs.to_long();
|
||||||
case String: return to_string() > rhs.to_string();
|
case String: return to_string() > rhs.to_string();
|
||||||
@ -575,7 +575,7 @@ struct Interpreter
|
|||||||
case "IDENTIFIER"_: return eval_identifier(ast, env);
|
case "IDENTIFIER"_: return eval_identifier(ast, env);
|
||||||
case "OBJECT"_: return eval_object(ast, env);
|
case "OBJECT"_: return eval_object(ast, env);
|
||||||
case "ARRAY"_: return eval_array(ast, env);
|
case "ARRAY"_: return eval_array(ast, env);
|
||||||
case "UNDEFINED"_: return eval_undefined(ast, env);
|
case "NIL"_: return eval_nil(ast, env);
|
||||||
case "BOOLEAN"_: return eval_bool(ast, env);
|
case "BOOLEAN"_: return eval_bool(ast, env);
|
||||||
case "NUMBER"_: return eval_number(ast, env);
|
case "NUMBER"_: return eval_number(ast, env);
|
||||||
case "INTERPOLATED_STRING"_: return eval_interpolated_string(ast, env);
|
case "INTERPOLATED_STRING"_: return eval_interpolated_string(ast, env);
|
||||||
@ -813,7 +813,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool is_keyword(const std::string& ident) const {
|
bool is_keyword(const std::string& ident) const {
|
||||||
static std::set<std::string> keywords = { "undefined", "true", "false", "mut", "debugger", "return", "while", "if", "else", "fn" };
|
static std::set<std::string> keywords = { "nil", "true", "false", "mut", "debugger", "return", "while", "if", "else", "fn" };
|
||||||
return keywords.find(ident) != keywords.end();
|
return keywords.find(ident) != keywords.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -924,7 +924,7 @@ private:
|
|||||||
return Value(std::move(arr));
|
return Value(std::move(arr));
|
||||||
}
|
}
|
||||||
|
|
||||||
Value eval_undefined(const peg::Ast& ast, std::shared_ptr<Environment> env) {
|
Value eval_nil(const peg::Ast& ast, std::shared_ptr<Environment> env) {
|
||||||
return Value();
|
return Value();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -27,23 +27,23 @@ test_return = fn () {
|
|||||||
assert(val == 1)
|
assert(val == 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
test_undefined = fn () {
|
test_nil = fn () {
|
||||||
assert(undefined == undefined)
|
assert(nil == nil)
|
||||||
assert(!(undefined != undefined))
|
assert(!(nil != nil))
|
||||||
|
|
||||||
a = undefined
|
a = nil
|
||||||
assert(a == undefined)
|
assert(a == nil)
|
||||||
assert(!(a != undefined))
|
assert(!(a != nil))
|
||||||
assert(!(a <= undefined))
|
assert(!(a <= nil))
|
||||||
assert(!(a < undefined))
|
assert(!(a < nil))
|
||||||
assert(!(a >= undefined))
|
assert(!(a >= nil))
|
||||||
assert(!(a > undefined))
|
assert(!(a > nil))
|
||||||
assert(undefined == a)
|
assert(nil == a)
|
||||||
assert(!(undefined != a))
|
assert(!(nil != a))
|
||||||
assert(!(undefined <= a))
|
assert(!(nil <= a))
|
||||||
assert(!(undefined < a))
|
assert(!(nil < a))
|
||||||
assert(!(undefined >= a))
|
assert(!(nil >= a))
|
||||||
assert(!(undefined > a))
|
assert(!(nil > a))
|
||||||
}
|
}
|
||||||
|
|
||||||
test_closure = fn () {
|
test_closure = fn () {
|
||||||
@ -83,7 +83,7 @@ test_array = fn () {
|
|||||||
assert(e.size() == 3 && e[-1] == 3)
|
assert(e.size() == 3 && e[-1] == 3)
|
||||||
|
|
||||||
f = [1,2,3](5)
|
f = [1,2,3](5)
|
||||||
assert(f.size() == 5 && f[-1] == undefined)
|
assert(f.size() == 5 && f[-1] == nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
g_ = 1
|
g_ = 1
|
||||||
@ -114,7 +114,7 @@ test_object = fn () {
|
|||||||
|
|
||||||
a = {}
|
a = {}
|
||||||
a.b = 1
|
a.b = 1
|
||||||
assert(a.a == undefined)
|
assert(a.a == nil)
|
||||||
assert(a.b == 1)
|
assert(a.b == 1)
|
||||||
assert(a.size() == 1)
|
assert(a.size() == 1)
|
||||||
}
|
}
|
||||||
@ -233,7 +233,7 @@ debugger
|
|||||||
test_call()
|
test_call()
|
||||||
test_return()
|
test_return()
|
||||||
test_closure()
|
test_closure()
|
||||||
test_undefined()
|
test_nil()
|
||||||
test_array()
|
test_array()
|
||||||
test_function()
|
test_function()
|
||||||
test_object()
|
test_object()
|
||||||
|
Loading…
Reference in New Issue
Block a user