Fix variables tests

This commit is contained in:
augustin64 2023-12-13 14:40:25 +01:00
parent 62eb2bece6
commit 1d571f36b1

View File

@ -285,6 +285,9 @@ EvalResult eval(Node &ast, Memory &memory) {
string typeName = get<string>(typeTok.data); string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data); string identifier = get<string>(identifierTok.data);
if (memory.contains(identifier))
throw RuntimeError("Already defined identifier \""+identifier+"\"", identifierTok.pos);
memory.declare(identifier, string_to_type(typeName)); memory.declare(identifier, string_to_type(typeName));
return {}; return {};
@ -296,6 +299,9 @@ EvalResult eval(Node &ast, Memory &memory) {
string identifier = get<string>(identifierTok.data); string identifier = get<string>(identifierTok.data);
EvalResult value = eval(node.children[2], memory); 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); Type type = string_to_type(typeName);
memory.declare(identifier, type); memory.declare(identifier, type);
@ -313,6 +319,9 @@ EvalResult eval(Node &ast, Memory &memory) {
string identifier = get<string>(identifierTok.data); string identifier = get<string>(identifierTok.data);
EvalResult value = eval(node.children[1], memory); EvalResult value = eval(node.children[1], memory);
if (!memory.contains(identifier))
throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos);
Type type = memory.get(identifier).type; Type type = memory.get(identifier).type;
if (type == Type::Int) { if (type == Type::Int) {
@ -328,6 +337,9 @@ EvalResult eval(Node &ast, Memory &memory) {
Token identifierTok = get<Token>(node.children[0]); Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data); string identifier = get<string>(identifierTok.data);
if (!memory.contains(identifier))
throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos);
MemoryVar& var = memory.get(identifier); MemoryVar& var = memory.get(identifier);
if (!var.initialized) { if (!var.initialized) {
@ -345,6 +357,9 @@ EvalResult eval(Node &ast, Memory &memory) {
Token identifierTok = get<Token>(node.children[0]); Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data); string identifier = get<string>(identifierTok.data);
if (!memory.contains(identifier))
throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos);
MemoryVar& var = memory.get(identifier); MemoryVar& var = memory.get(identifier);
if (!var.initialized) { if (!var.initialized) {
@ -363,6 +378,9 @@ EvalResult eval(Node &ast, Memory &memory) {
Token identifierTok = get<Token>(node.children[0]); Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data); string identifier = get<string>(identifierTok.data);
if (!memory.contains(identifier))
throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos);
MemoryVar& var = memory.get(identifier); MemoryVar& var = memory.get(identifier);
if (!var.initialized) { if (!var.initialized) {
@ -380,6 +398,9 @@ EvalResult eval(Node &ast, Memory &memory) {
Token identifierTok = get<Token>(node.children[0]); Token identifierTok = get<Token>(node.children[0]);
string identifier = get<string>(identifierTok.data); string identifier = get<string>(identifierTok.data);
if (!memory.contains(identifier))
throw RuntimeError("Unknown identifier \""+identifier+"\"", identifierTok.pos);
MemoryVar& var = memory.get(identifier); MemoryVar& var = memory.get(identifier);
if (!var.initialized) { if (!var.initialized) {
@ -415,9 +436,13 @@ EvalResult eval(Node &ast, Memory &memory) {
case TokenType::Identifier: { case TokenType::Identifier: {
string identifier = get<string>(token.data); string identifier = get<string>(token.data);
if (!memory.contains(identifier))
throw RuntimeError("Unknown identifier \""+identifier+"\"", token.pos);
MemoryVar& var = memory.get(identifier); 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; return var.value;
} break; } break;