diff --git a/src/interpreter.cpp b/src/interpreter.cpp index c42448d..acbc30d 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -285,6 +285,9 @@ EvalResult eval(Node &ast, Memory &memory) { string typeName = get(typeTok.data); string identifier = get(identifierTok.data); + if (memory.contains(identifier)) + throw RuntimeError("Already defined identifier \""+identifier+"\"", identifierTok.pos); + memory.declare(identifier, string_to_type(typeName)); return {}; @@ -296,6 +299,9 @@ EvalResult eval(Node &ast, Memory &memory) { string identifier = get(identifierTok.data); EvalResult value = eval(node.children[2], memory); + if (memory.contains(identifier)) + throw RuntimeError("Already defined identifier \""+identifier+"\"", identifierTok.pos); + Type type = string_to_type(typeName); memory.declare(identifier, type); @@ -313,6 +319,9 @@ EvalResult eval(Node &ast, Memory &memory) { string identifier = get(identifierTok.data); EvalResult value = eval(node.children[1], memory); + if (!memory.contains(identifier)) + throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos); + Type type = memory.get(identifier).type; if (type == Type::Int) { @@ -328,6 +337,9 @@ EvalResult eval(Node &ast, Memory &memory) { Token identifierTok = get(node.children[0]); string identifier = get(identifierTok.data); + if (!memory.contains(identifier)) + throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos); + MemoryVar& var = memory.get(identifier); if (!var.initialized) { @@ -345,6 +357,9 @@ EvalResult eval(Node &ast, Memory &memory) { Token identifierTok = get(node.children[0]); string identifier = get(identifierTok.data); + if (!memory.contains(identifier)) + throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos); + MemoryVar& var = memory.get(identifier); if (!var.initialized) { @@ -363,6 +378,9 @@ EvalResult eval(Node &ast, Memory &memory) { Token identifierTok = get(node.children[0]); string identifier = get(identifierTok.data); + if (!memory.contains(identifier)) + throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos); + MemoryVar& var = memory.get(identifier); if (!var.initialized) { @@ -380,6 +398,9 @@ EvalResult eval(Node &ast, Memory &memory) { Token identifierTok = get(node.children[0]); string identifier = get(identifierTok.data); + if (!memory.contains(identifier)) + throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos); + MemoryVar& var = memory.get(identifier); if (!var.initialized) { @@ -414,10 +435,14 @@ EvalResult eval(Node &ast, Memory &memory) { } break; case TokenType::Identifier: { string identifier = get(token.data); + + if (!memory.contains(identifier)) + throw RuntimeError("Unknown identifier \""+identifier+"\"", token.pos); MemoryVar& var = memory.get(identifier); - if (!var.initialized) throw RuntimeError("Accessing uninitialized identifier \""+identifier+"\"", token.pos); + if (!var.initialized) + throw RuntimeError("Accessing uninitialized identifier \""+identifier+"\"", token.pos); return var.value; } break;