diff --git a/language/pl0/pl0.cc b/language/pl0/pl0.cc index 44bd3da..7de347c 100644 --- a/language/pl0/pl0.cc +++ b/language/pl0/pl0.cc @@ -206,14 +206,17 @@ struct Environment Environment(shared_ptr scope, shared_ptr outer) : scope(scope), outer(outer) {} - int get_value(const string& ident) const { + int get_value(const shared_ptr ast, const string& ident) const { auto it = scope->constants.find(ident); if (it != scope->constants.end()) { return it->second; } else if (scope->variables.count(ident)) { + if (variables.find(ident) == variables.end()) { + throw_runtime_error(ast, "uninitialized variable '" + ident + "'..."); + } return variables.at(ident); } - return outer->get_value(ident); + return outer->get_value(ast, ident); } void set_variable(const string& ident, int val) { @@ -395,7 +398,7 @@ private: } static int eval_ident(const shared_ptr ast, shared_ptr env) { - return env->get_value(ast->token); + return env->get_value(ast, ast->token); } static int eval_number(const shared_ptr ast, shared_ptr env) { @@ -453,4 +456,3 @@ int main(int argc, const char** argv) } // vim: et ts=4 sw=4 cin cino={1s ff=unix -