From 5cce75f835bb054a9fb383ac6db3671355cab5b5 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sat, 9 Dec 2023 11:41:14 +0100 Subject: [PATCH] throw exception(); --- src/analysis.cpp | 22 +++++++++++++++------- src/interpreter.cpp | 10 +++++----- src/memory.cpp | 4 ++-- src/utils.cpp | 2 +- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/analysis.cpp b/src/analysis.cpp index 16ed260..e3c40be 100644 --- a/src/analysis.cpp +++ b/src/analysis.cpp @@ -48,6 +48,15 @@ bool is_arithmetic_type(Type type) { } } +Type try_string_to_type(string type_name, CodePosition pos) { + try { + Type type = string_to_type(type_name); + return type; + } catch (...) { + throw TypeError("Unknown type '"+type_name+"'", pos); + } +} + AnalysisResult analyze(Node &ast, Memory &memory) { if (holds_alternative(ast)) { Token token = get(ast); @@ -59,7 +68,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) { return Type::Double; } } - throw; + throw exception(); break; case TokenType::Identifier: { string identifier = get(token.data); @@ -69,10 +78,10 @@ AnalysisResult analyze(Node &ast, Memory &memory) { return memory.get(identifier).type; } - throw; + throw exception(); break; default: - throw; + throw exception(); } } else { InnerNode node = get(ast); @@ -194,7 +203,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) { if (memory.contains(identifier)) throw TypeError("Already defined identifier \""+identifier+"\"", token.pos); - Type type = string_to_type(get(token.data), token.pos); + Type type = try_string_to_type(get(token.data), token.pos); memory.declare(identifier, type); return {}; @@ -205,9 +214,8 @@ AnalysisResult analyze(Node &ast, Memory &memory) { if (memory.contains(identifier)) throw TypeError("Already defined identifier \""+identifier+"\"", token.pos); - Type type = string_to_type(get(token.data), token.pos); + Type type = try_string_to_type(get(token.data), token.pos); memory.declare(identifier, type); - cout << "declared" << endl; get_cast(type, analyze(node.children[2], memory), get_node_pos(node)); @@ -240,5 +248,5 @@ AnalysisResult analyze(Node &ast, Memory &memory) { } } } - throw; + throw exception(); } \ No newline at end of file diff --git a/src/interpreter.cpp b/src/interpreter.cpp index a8c7b5b..268de7d 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -15,7 +15,7 @@ bool bool_cast(EvalResult value) { return get(value) != 0; } else { - throw; + throw exception(); } } @@ -26,7 +26,7 @@ int int_cast(EvalResult value) { return int(get(value)); } else { - throw; + throw exception(); } } @@ -38,7 +38,7 @@ double double_cast(EvalResult value) { return get(value); } else { - throw; + throw exception(); } } @@ -381,7 +381,7 @@ EvalResult eval(Node &ast, Memory &memory) { return get(token.data); } else { - throw; + throw exception(); } } break; case TokenType::Identifier: { @@ -394,7 +394,7 @@ EvalResult eval(Node &ast, Memory &memory) { return var.value; } break; default: - throw; + throw exception(); break; } } diff --git a/src/memory.cpp b/src/memory.cpp index 695956b..f37d9c5 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -37,7 +37,7 @@ MemoryVar& Memory::get(string identifier) { if (scope.vars.contains(identifier)) return scope.vars[identifier]; } - throw; + throw exception(); } void Memory::declare(string identifier, Type type) { @@ -55,7 +55,7 @@ void Memory::update(string identifier, EvalResult value) { } } - throw; + throw exception(); } Scope& Memory::_debug_top(void) { diff --git a/src/utils.cpp b/src/utils.cpp index 6223ceb..db2a6de 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -12,5 +12,5 @@ Type string_to_type(string type_name) { if (type_name == "double") return Type::Double; - throw; + throw exception(); } \ No newline at end of file