Implemented method to display array object.

pull/3/head
yhirose 9 years ago
parent d2d396c351
commit 02e1c8d38f
  1. 17
      language/interpreter.cc
  2. 31
      language/interpreter.hpp

@ -101,8 +101,7 @@ private:
}; };
static Value eval_function_call(const Ast& ast, shared_ptr<Environment> env) { static Value eval_function_call(const Ast& ast, shared_ptr<Environment> env) {
const auto& var = ast.nodes[0]->token; const auto& f = eval(*ast.nodes[0], env);
const auto& f = env->get(var);
const auto& fv = f.to_function(); const auto& fv = f.to_function();
const auto& args = ast.nodes[1]->nodes; const auto& args = ast.nodes[1]->nodes;
@ -122,7 +121,7 @@ private:
return fv.eval(callEnv); return fv.eval(callEnv);
} }
string msg = "arguments error in '" + var + "'..."; string msg = "arguments error...";
throw runtime_error(msg); throw runtime_error(msg);
} }
@ -141,15 +140,11 @@ private:
} }
static Value eval_array_reference(const Ast& ast, shared_ptr<Environment> env) { static Value eval_array_reference(const Ast& ast, shared_ptr<Environment> env) {
const auto& var = ast.nodes[0]->token; const auto& a = eval(*ast.nodes[0], env).to_array();
const auto& i = eval(*ast.nodes[1], env).to_long();
const auto& a = env->get(var); if (0 <= i && i < a.values.size()) {
const auto& av = a.to_array(); return a.values[i];
const auto& idx = eval(*ast.nodes[1], env).to_long();
if (0 <= idx && idx < av.values.size()) {
return av.values[idx];
} }
return Value(); return Value();

@ -92,6 +92,19 @@ struct Value
throw std::runtime_error("type error."); throw std::runtime_error("type error.");
} }
std::string str_array() const {
const auto& values = to_array().values;
std::string s = "[";
for (auto i = 0u; i < values.size(); i++) {
if (i != 0) {
s += ", ";
}
s += values[i].str();
}
s += "]";
return s;
}
std::string str() const { std::string str() const {
switch (type) { switch (type) {
case Undefined: return "undefined"; case Undefined: return "undefined";
@ -99,22 +112,14 @@ struct Value
case Long: return std::to_string(to_long()); break; case Long: return std::to_string(to_long()); break;
case String: return to_string(); case String: return to_string();
case Function: return "[function]"; case Function: return "[function]";
case Array: return "[array]"; case Array: return str_array();
default: throw std::logic_error("invalid internal condition."); default: throw std::logic_error("invalid internal condition.");
} }
// NOTREACHED // NOTREACHED
} }
std::ostream& out(std::ostream& os) const { std::ostream& out(std::ostream& os) const {
switch (type) { os << str();
case Undefined: os << "undefined"; break;
case Bool: os << (to_bool() ? "true" : "false"); break;
case Long: os << to_long(); break;
case String: os << "'" << to_string() << "'"; break;
case Function: os << "[function]"; break;
case Array: os << "[array]"; break;
default: throw std::logic_error("invalid internal condition.");
}
return os; return os;
} }
@ -124,6 +129,7 @@ struct Value
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();
// TODO: Array support
default: throw std::logic_error("invalid internal condition."); default: throw std::logic_error("invalid internal condition.");
} }
// NOTREACHED // NOTREACHED
@ -135,6 +141,7 @@ struct Value
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();
// TODO: Array support
default: throw std::logic_error("invalid internal condition."); default: throw std::logic_error("invalid internal condition.");
} }
// NOTREACHED // NOTREACHED
@ -146,6 +153,7 @@ struct Value
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();
// TODO: Array support
default: throw std::logic_error("invalid internal condition."); default: throw std::logic_error("invalid internal condition.");
} }
// NOTREACHED // NOTREACHED
@ -157,6 +165,7 @@ struct Value
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();
// TODO: Array support
default: throw std::logic_error("invalid internal condition."); default: throw std::logic_error("invalid internal condition.");
} }
// NOTREACHED // NOTREACHED
@ -168,6 +177,7 @@ struct Value
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();
// TODO: Array support
default: throw std::logic_error("invalid internal condition."); default: throw std::logic_error("invalid internal condition.");
} }
// NOTREACHED // NOTREACHED
@ -179,6 +189,7 @@ struct Value
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();
// TODO: Array support
default: throw std::logic_error("invalid internal condition."); default: throw std::logic_error("invalid internal condition.");
} }
// NOTREACHED // NOTREACHED

Loading…
Cancel
Save