@@ -206,14 +206,17 @@ struct Environment
206
206
Environment (shared_ptr<SymbolScope> scope, shared_ptr<Environment> outer)
207
207
: scope(scope), outer(outer) {}
208
208
209
- int get_value (const string& ident) const {
209
+ int get_value (const shared_ptr<AstPL0> ast, const string& ident) const {
210
210
auto it = scope->constants .find (ident);
211
211
if (it != scope->constants .end ()) {
212
212
return it->second ;
213
213
} else if (scope->variables .count (ident)) {
214
+ if (variables.find (ident) == variables.end ()) {
215
+ throw_runtime_error (ast, " uninitialized variable '" + ident + " '..." );
216
+ }
214
217
return variables.at (ident);
215
218
}
216
- return outer->get_value (ident);
219
+ return outer->get_value (ast, ident);
217
220
}
218
221
219
222
void set_variable (const string& ident, int val) {
@@ -395,7 +398,7 @@ struct Interpreter
395
398
}
396
399
397
400
static int eval_ident (const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
398
- return env->get_value (ast->token );
401
+ return env->get_value (ast, ast ->token );
399
402
}
400
403
401
404
static int eval_number (const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
@@ -453,4 +456,3 @@ int main(int argc, const char** argv)
453
456
}
454
457
455
458
// vim: et ts=4 sw=4 cin cino={1s ff=unix
456
-
0 commit comments