Added Object support.

This commit is contained in:
yhirose 2015-07-22 21:14:55 -04:00
parent 111d5d8476
commit 672f7740d8
8 changed files with 273 additions and 101 deletions

View File

@ -17,12 +17,12 @@
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)* MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
CALL <- PRIMARY (ARGUMENTS / INDEX / METHOD)* CALL <- PRIMARY (ARGUMENTS / INDEX / DOT)*
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _ ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
INDEX <- '[' _ EXPRESSION ']' _ INDEX <- '[' _ EXPRESSION ']' _
METHOD <- '.' _ IDENTIFIER ARGUMENTS DOT <- '.' _ IDENTIFIER
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _ PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / OBJECT / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
FUNCTION <- 'fn' _ PARAMETERS BLOCK FUNCTION <- 'fn' _ PARAMETERS BLOCK
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _ PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
@ -39,7 +39,11 @@
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _ IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
OBJECT <- '{' _ (OBJECT_PROPERTY (',' _ OBJECT_PROPERTY)*)? '}' _
OBJECT_PROPERTY <- IDENTIFIER ':' _ EXPRESSION
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*)? ']' _ ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*)? ']' _
NUMBER <- < [0-9]+ > _ NUMBER <- < [0-9]+ > _
BOOLEAN <- < ('true' / 'false') > _ BOOLEAN <- < ('true' / 'false') > _
STRING <- ['] < (!['] .)* > ['] _ STRING <- ['] < (!['] .)* > ['] _

View File

@ -23,6 +23,7 @@ struct Eval
case UnaryNot: return eval_unary_not(ast, env); case UnaryNot: return eval_unary_not(ast, env);
case BinExpresion: return eval_bin_expression(ast, env); case BinExpresion: return eval_bin_expression(ast, env);
case Identifier: return eval_identifier(ast, env); case Identifier: return eval_identifier(ast, env);
case Object: return eval_object(ast, env);
case Array: return eval_array(ast, env); case Array: return eval_array(ast, env);
case Number: return eval_number(ast, env); case Number: return eval_number(ast, env);
case Boolean: return eval_bool(ast, env); case Boolean: return eval_bool(ast, env);
@ -93,7 +94,7 @@ private:
auto f = Value::FunctionValue( auto f = Value::FunctionValue(
params, params,
[=](shared_ptr<Environment> callEnv) { [=](shared_ptr<Environment> callEnv) {
callEnv->set_outer(env); callEnv->append_outer(env);
return eval(*body, callEnv); return eval(*body, callEnv);
} }
); );
@ -106,7 +107,7 @@ private:
for (auto i = 1u; i < ast.nodes.size(); i++) { for (auto i = 1u; i < ast.nodes.size(); i++) {
const auto& n = *ast.nodes[i]; const auto& n = *ast.nodes[i];
if (n.tag == AstTag::Arguments) { if (n.original_tag == AstTag::Arguments) {
// Function call // Function call
const auto& f = val.to_function(); const auto& f = val.to_function();
const auto& params = f.data->params; const auto& params = f.data->params;
@ -131,16 +132,16 @@ private:
string msg = "arguments error..."; string msg = "arguments error...";
throw runtime_error(msg); throw runtime_error(msg);
} }
} else if (n.tag == AstTag::Index) { } else if (n.original_tag == AstTag::Index) {
// Array reference // Array reference
const auto& arr = val.to_array(); const auto& arr = val.to_array();
auto idx = eval(*n.nodes[0], env).to_long(); auto idx = eval(n, env).to_long();
if (0 <= idx && idx < static_cast<long>(arr.data->values.size())) { if (0 <= idx && idx < static_cast<long>(arr.data->values.size())) {
val = arr.data->values.at(idx); val = arr.data->values.at(idx);
} }
} else { // n.tag == AstTag::Property } else if (n.original_tag == AstTag::Dot) {
// Property // Property
auto name = n.nodes[0]->token; auto name = n.token;
auto prop = val.get_property(name); auto prop = val.get_property(name);
if (prop.get_type() == Value::Function) { if (prop.get_type() == Value::Function) {
@ -149,11 +150,10 @@ private:
auto f = Value::FunctionValue( auto f = Value::FunctionValue(
pf.data->params, pf.data->params,
[=](shared_ptr<Environment> callEnv) { [=](shared_ptr<Environment> callEnv) {
auto thisEnv = make_shared<Environment>(); callEnv->initialize("this", val, false);
thisEnv->set_outer(env); if (val.get_type() == Value::Object) {
thisEnv->initialize("this", val, false); callEnv->set_object(val.to_object());
}
callEnv->set_outer(thisEnv);
return pf.data->eval(callEnv); return pf.data->eval(callEnv);
} }
); );
@ -162,6 +162,8 @@ private:
} else { } else {
val = prop; val = prop;
} }
} else {
throw std::logic_error("invalid internal condition.");
} }
} }
@ -277,6 +279,19 @@ private:
return env->get(var); return env->get(var);
}; };
static Value eval_object(const Ast& ast, shared_ptr<Environment> env) {
Value::ObjectValue obj;
for (auto i = 0u; i < ast.nodes.size(); i++) {
const auto& prop = *ast.nodes[i];
const auto& name = prop.nodes[0]->token;
auto val = eval(*prop.nodes[1], env);
obj.data->props.emplace(name, val);
}
return Value(std::move(obj));
}
static Value eval_array(const Ast& ast, shared_ptr<Environment> env) { static Value eval_array(const Ast& ast, shared_ptr<Environment> env) {
Value::ArrayValue arr; Value::ArrayValue arr;
@ -307,6 +322,20 @@ private:
}; };
}; };
std::map<std::string, Value> Value::ObjectValue::prototypes = {
{
"size",
Value(FunctionValue(
{},
[](shared_ptr<Environment> callEnv) {
const auto& val = callEnv->get("this");
long n = val.to_object().data->props.size();
return Value(n);
}
))
}
};
std::map<std::string, Value> Value::ArrayValue::prototypes = { std::map<std::string, Value> Value::ArrayValue::prototypes = {
{ {
"size", "size",
@ -330,7 +359,7 @@ std::map<std::string, Value> Value::ArrayValue::prototypes = {
return Value(); return Value();
} }
}) })
}, }
}; };
bool run( bool run(

View File

@ -32,7 +32,17 @@ struct Value
}; };
struct ObjectValue { struct ObjectValue {
bool has_property(const std::string& name) const {
if (data->props.find(name) == data->props.end()) {
return prototypes.find(name) != prototypes.end();
}
return true;
}
Value get_property(const std::string& name) const { Value get_property(const std::string& name) const {
if (data->props.find(name) == data->props.end()) {
return prototypes.at(name);
}
return data->props.at(name); return data->props.at(name);
} }
@ -40,6 +50,8 @@ struct Value
std::map<std::string, Value> props; std::map<std::string, Value> props;
}; };
std::shared_ptr<Data> data = std::make_shared<Data>(); std::shared_ptr<Data> data = std::make_shared<Data>();
static std::map<std::string, Value> prototypes;
}; };
struct ArrayValue { struct ArrayValue {
@ -90,7 +102,7 @@ struct Value
explicit Value(bool b) : type(Bool), v(b) {} explicit Value(bool b) : type(Bool), v(b) {}
explicit Value(long l) : type(Long), v(l) {} explicit Value(long l) : type(Long), v(l) {}
explicit Value(std::string&& s) : type(String), v(s) {} explicit Value(std::string&& s) : type(String), v(s) {}
explicit Value(ObjectValue&& o) : type(Object), v(0) {} explicit Value(ObjectValue&& o) : type(Object), v(o) {}
explicit Value(ArrayValue&& a) : type(Array), v(a) {} explicit Value(ArrayValue&& a) : type(Array), v(a) {}
explicit Value(FunctionValue&& f) : type(Function), v(f) {} explicit Value(FunctionValue&& f) : type(Function), v(f) {}
@ -287,21 +299,39 @@ struct Environment
{ {
Environment() = default; Environment() = default;
void set_object(const Value::ObjectValue& obj) {
obj_ = obj;
}
void set_outer(std::shared_ptr<Environment> outer) { void set_outer(std::shared_ptr<Environment> outer) {
outer_ = outer; outer_ = outer;
} }
void append_outer(std::shared_ptr<Environment> outer) {
if (outer_) {
outer_->append_outer(outer);
} else {
outer_ = outer;
}
}
bool has(const std::string& s) const { bool has(const std::string& s) const {
if (dic_.find(s) != dic_.end()) { if (dic_.find(s) != dic_.end()) {
return true; return true;
} }
if (obj_.has_property(s)) {
return true;
}
return outer_ && outer_->has(s); return outer_ && outer_->has(s);
} }
const Value& get(const std::string& s) const { Value get(const std::string& s) const {
if (dic_.find(s) != dic_.end()) { if (dic_.find(s) != dic_.end()) {
return dic_.at(s).val; return dic_.at(s).val;
} }
if (obj_.has_property(s)) {
return obj_.get_property(s);
}
if (outer_) { if (outer_) {
return outer_->get(s); return outer_->get(s);
} }
@ -333,7 +363,18 @@ struct Environment
dic_[s] = Symbol{val, mut}; dic_[s] = Symbol{val, mut};
} }
void setup_built_in_functions() { private:
struct Symbol {
Value val;
bool mut;
};
std::shared_ptr<Environment> outer_;
std::map<std::string, Symbol> dic_;
Value::ObjectValue obj_;
};
inline void setup_built_in_functions(Environment& env) {
{ {
auto f = Value::FunctionValue( auto f = Value::FunctionValue(
{ {"arg", true} }, { {"arg", true} },
@ -342,7 +383,7 @@ struct Environment
return Value(); return Value();
} }
); );
initialize("puts", Value(std::move(f)), false); env.initialize("puts", Value(std::move(f)), false);
} }
{ {
@ -359,20 +400,10 @@ struct Environment
return Value(); return Value();
} }
); );
initialize("assert", Value(std::move(f)), false); env.initialize("assert", Value(std::move(f)), false);
} }
} }
private:
struct Symbol {
Value val;
bool mut;
};
std::shared_ptr<Environment> outer_;
std::map<std::string, Symbol> dic_;
};
bool run( bool run(
const std::string& path, const std::string& path,
std::shared_ptr<Environment> env, std::shared_ptr<Environment> env,

View File

@ -46,7 +46,7 @@ int main(int argc, const char** argv)
try { try {
auto env = make_shared<Environment>(); auto env = make_shared<Environment>();
env->setup_built_in_functions(); setup_built_in_functions(*env);
for (auto path: path_list) { for (auto path: path_list) {
vector<char> buff; vector<char> buff;

View File

@ -23,12 +23,12 @@ static auto g_grammar = R"(
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)* MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
CALL <- PRIMARY (ARGUMENTS / INDEX / PROPERTY)* CALL <- PRIMARY (ARGUMENTS / INDEX / DOT)*
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _ ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
INDEX <- '[' _ EXPRESSION ']' _ INDEX <- '[' _ EXPRESSION ']' _
PROPERTY <- '.' _ IDENTIFIER DOT <- '.' _ IDENTIFIER
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _ PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / OBJECT / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
FUNCTION <- 'fn' _ PARAMETERS BLOCK FUNCTION <- 'fn' _ PARAMETERS BLOCK
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _ PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
@ -45,7 +45,11 @@ static auto g_grammar = R"(
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _ IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
OBJECT <- '{' _ (OBJECT_PROPERTY (',' _ OBJECT_PROPERTY)*)? '}' _
OBJECT_PROPERTY <- IDENTIFIER ':' _ EXPRESSION
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*)? ']' _ ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*)? ']' _
NUMBER <- < [0-9]+ > _ NUMBER <- < [0-9]+ > _
BOOLEAN <- < ('true' / 'false') > _ BOOLEAN <- < ('true' / 'false') > _
STRING <- ['] < (!['] .)* > ['] _ STRING <- ['] < (!['] .)* > ['] _
@ -79,20 +83,21 @@ peg& get_parser()
throw logic_error("invalid peg grammar"); throw logic_error("invalid peg grammar");
} }
parser.enable_ast(true, { parser.enable_ast(
/* true, // Optimize AST nodes
Definition, Tag Optimize {
---------------------- ------------------ ---------- */ /* Definition Tag Optimize
{ "STATEMENTS", Statements, false }, ----------------------- ------------------ ---------- */
{ "WHILE", While, false }, { "STATEMENTS", Statements, true },
{ "ASSIGNMENT", Assignment, false }, { "WHILE", While, true },
{ "IF", If, false }, { "ASSIGNMENT", Assignment, true },
{ "FUNCTION", Function, false }, { "IF", If, true },
{ "FUNCTION", Function, true },
{ "PARAMETERS", Default, false }, { "PARAMETERS", Default, false },
{ "CALL", Call, true }, { "CALL", Call, true },
{ "ARGUMENTS", Arguments, false }, { "ARGUMENTS", Arguments, false },
{ "INDEX", Index, false }, { "INDEX", Index, true },
{ "PROPERTY", Property, false }, { "DOT", Dot, true },
{ "LOGICAL_OR", LogicalOr, true }, { "LOGICAL_OR", LogicalOr, true },
{ "LOGICAL_AND", LogicalAnd, true }, { "LOGICAL_AND", LogicalAnd, true },
{ "CONDITION", Condition, true }, { "CONDITION", Condition, true },
@ -101,11 +106,12 @@ peg& get_parser()
{ "UNARY_MINUS", UnaryMinus, true }, { "UNARY_MINUS", UnaryMinus, true },
{ "UNARY_NOT", UnaryNot, true }, { "UNARY_NOT", UnaryNot, true },
{ "MULTIPLICATIVE", BinExpresion, true }, { "MULTIPLICATIVE", BinExpresion, true },
{ "ARRAY", Array, false }, { "OBJECT", Object, true },
{ "NUMBER", Number, false }, { "ARRAY", Array, true },
{ "BOOLEAN", Boolean, false }, { "NUMBER", Number, true },
{ "IDENTIFIER", Identifier, false }, { "BOOLEAN", Boolean, true },
{ "INTERPOLATED_STRING", InterpolatedString, false }, { "IDENTIFIER", Identifier, true },
{ "INTERPOLATED_STRING", InterpolatedString, true },
}); });
} }

View File

@ -3,11 +3,35 @@
enum AstTag enum AstTag
{ {
Default = peglib::AstDefaultTag, Default = peglib::AstDefaultTag,
Statements, While, If, Call, Assignment,
Arguments, Index, Property, Statements,
LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion, While,
Identifier, InterpolatedString, If,
Number, Boolean, Function, Array Call,
Assignment,
Arguments,
Index,
Dot,
LogicalOr,
LogicalAnd,
Condition,
UnaryPlus,
UnaryMinus,
UnaryNot,
BinExpresion,
Identifier,
Object,
Array,
Function,
InterpolatedString,
Number,
Boolean,
}; };
peglib::peg& get_parser(); peglib::peg& get_parser();

View File

@ -21,17 +21,6 @@ test_closure = fn () {
assert(ret == 319) assert(ret == 319)
} }
test_sum = fn () {
mut i = 1
mut ret = 0
while i <= 10 {
ret = ret + i
i = i + 1
}
assert(ret == 55)
}
test_array = fn () { test_array = fn () {
a = [1,2,3] a = [1,2,3]
assert(a.size() == 3) assert(a.size() == 3)
@ -43,6 +32,68 @@ test_array = fn () {
assert(b.size() == 0) assert(b.size() == 0)
} }
g_ = 1
test_function = fn () {
a = 1
make = fn () {
b = 1
fn (c) {
g_ + a + b + c
}
}
f = make()
assert(f(1) == 4)
}
test_object = fn () {
n = 1
o = {
n: 123,
s: 'str',
f1: fn (x) { x + this.n },
f2: fn (x) { x + n }
}
assert(o.size() == 4)
assert(o.f1(10) == 133)
assert(o.f2(10) == 133)
}
test_object_factory = fn () {
ctor = fn (init) {
mut n = init
{
add: fn (x) {
n = n + x
},
sub: fn (x) {
n = n - x
},
val: fn () {
n
}
}
}
calc = ctor(10)
assert(calc.val() == 10)
assert(calc.add(1) == 11)
assert(calc.sub(1) == 10)
}
test_sum = fn () {
mut i = 1
mut ret = 0
while i <= 10 {
ret = ret + i
i = i + 1
}
assert(ret == 55)
}
test_fib = fn () { test_fib = fn () {
fib = fn (x) { fib = fn (x) {
if x < 2 { if x < 2 {
@ -57,8 +108,19 @@ test_fib = fn () {
assert(ret == 610) assert(ret == 610)
} }
test_interpolated_string = fn () {
hello = "Hello"
world = "World!"
ret = "{hello} {world}"
assert(ret == 'Hello World!')
}
test_call() test_call()
test_closure() test_closure()
test_array() test_array()
test_function()
test_object()
test_object_factory()
test_sum() test_sum()
test_fib() test_fib()
test_interpolated_string()

View File

@ -1944,10 +1944,15 @@ const int AstDefaultTag = -1;
struct Ast struct Ast
{ {
Ast(size_t _line, size_t _column, const char* _name, int _tag, const std::vector<std::shared_ptr<Ast>>& _nodes) Ast(size_t _line, size_t _column, const char* _name, int _tag, const std::vector<std::shared_ptr<Ast>>& _nodes)
: line(_line), column(_column), name(_name), tag(_tag), is_token(false), nodes(_nodes) {} : line(_line), column(_column), name(_name), tag(_tag), original_tag(_tag), is_token(false), nodes(_nodes) {}
Ast(size_t _line, size_t _column, const char* _name, int _tag, const std::string& _token) Ast(size_t _line, size_t _column, const char* _name, int _tag, const std::string& _token)
: line(_line), column(_column), name(_name), tag(_tag), is_token(true), token(_token) {} : line(_line), column(_column), name(_name), tag(_tag), original_tag(_tag), is_token(true), token(_token) {}
Ast(const Ast& ast, int original_tag)
: line(ast.line), column(ast.column), name(ast.name), tag(ast.tag), original_tag(original_tag), is_token(ast.is_token), token(ast.token), nodes(ast.nodes) {}
const Ast& get_smallest_ancestor() const;
void print() const; void print() const;
@ -1955,6 +1960,7 @@ struct Ast
const size_t column; const size_t column;
const std::string name; const std::string name;
const int tag; const int tag;
const int original_tag;
const bool is_token; const bool is_token;
const std::string token; const std::string token;
const std::vector<std::shared_ptr<Ast>> nodes; const std::vector<std::shared_ptr<Ast>> nodes;
@ -1980,6 +1986,16 @@ private:
int level_; int level_;
}; };
inline const Ast& Ast::get_smallest_ancestor() const {
assert(nodes.size() <= 1);
if (nodes.empty()) {
return *this;
}
return nodes[0]->get_smallest_ancestor();
}
inline void Ast::print() const { inline void Ast::print() const {
AstPrint().print(*this); AstPrint().print(*this);
} }
@ -2181,7 +2197,7 @@ private:
return std::make_shared<Ast>(line.first, line.second, info.name, info.tag, std::string(sv.s, sv.n)); return std::make_shared<Ast>(line.first, line.second, info.name, info.tag, std::string(sv.s, sv.n));
} }
if (info.optimize_nodes && sv.size() == 1) { if (info.optimize_nodes && sv.size() == 1) {
std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>(); auto ast = std::make_shared<Ast>(*sv[0].get<std::shared_ptr<Ast>>(), info.tag);
return ast; return ast;
} }
auto line = line_info(sv.ss, sv.s); auto line = line_info(sv.ss, sv.s);
@ -2201,7 +2217,7 @@ private:
return std::make_shared<Ast>(line.first, line.second, name.c_str(), AstDefaultTag, std::string(sv.s, sv.n)); return std::make_shared<Ast>(line.first, line.second, name.c_str(), AstDefaultTag, std::string(sv.s, sv.n));
} }
if (optimize_nodes && sv.size() == 1) { if (optimize_nodes && sv.size() == 1) {
std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>(); auto ast = std::make_shared<Ast>(*sv[0].get<std::shared_ptr<Ast>>(), AstDefaultTag);
return ast; return ast;
} }
auto line = line_info(sv.ss, sv.s); auto line = line_info(sv.ss, sv.s);