throw exception();

This commit is contained in:
augustin64 2023-12-09 11:41:14 +01:00
parent b47091527e
commit 5cce75f835
4 changed files with 23 additions and 15 deletions

View File

@ -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) { AnalysisResult analyze(Node &ast, Memory &memory) {
if (holds_alternative<Token>(ast)) { if (holds_alternative<Token>(ast)) {
Token token = get<Token>(ast); Token token = get<Token>(ast);
@ -59,7 +68,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
return Type::Double; return Type::Double;
} }
} }
throw; throw exception();
break; break;
case TokenType::Identifier: { case TokenType::Identifier: {
string identifier = get<string>(token.data); string identifier = get<string>(token.data);
@ -69,10 +78,10 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
return memory.get(identifier).type; return memory.get(identifier).type;
} }
throw; throw exception();
break; break;
default: default:
throw; throw exception();
} }
} else { } else {
InnerNode node = get<InnerNode>(ast); InnerNode node = get<InnerNode>(ast);
@ -194,7 +203,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
if (memory.contains(identifier)) if (memory.contains(identifier))
throw TypeError("Already defined identifier \""+identifier+"\"", token.pos); throw TypeError("Already defined identifier \""+identifier+"\"", token.pos);
Type type = string_to_type(get<string>(token.data), token.pos); Type type = try_string_to_type(get<string>(token.data), token.pos);
memory.declare(identifier, type); memory.declare(identifier, type);
return {}; return {};
@ -205,9 +214,8 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
if (memory.contains(identifier)) if (memory.contains(identifier))
throw TypeError("Already defined identifier \""+identifier+"\"", token.pos); throw TypeError("Already defined identifier \""+identifier+"\"", token.pos);
Type type = string_to_type(get<string>(token.data), token.pos); Type type = try_string_to_type(get<string>(token.data), token.pos);
memory.declare(identifier, type); memory.declare(identifier, type);
cout << "declared" << endl;
get_cast(type, analyze(node.children[2], memory), get_node_pos(node)); 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();
} }

View File

@ -15,7 +15,7 @@ bool bool_cast(EvalResult value) {
return get<double>(value) != 0; return get<double>(value) != 0;
} }
else { else {
throw; throw exception();
} }
} }
@ -26,7 +26,7 @@ int int_cast(EvalResult value) {
return int(get<double>(value)); return int(get<double>(value));
} }
else { else {
throw; throw exception();
} }
} }
@ -38,7 +38,7 @@ double double_cast(EvalResult value) {
return get<double>(value); return get<double>(value);
} }
else { else {
throw; throw exception();
} }
} }
@ -381,7 +381,7 @@ EvalResult eval(Node &ast, Memory &memory) {
return get<double>(token.data); return get<double>(token.data);
} }
else { else {
throw; throw exception();
} }
} break; } break;
case TokenType::Identifier: { case TokenType::Identifier: {
@ -394,7 +394,7 @@ EvalResult eval(Node &ast, Memory &memory) {
return var.value; return var.value;
} break; } break;
default: default:
throw; throw exception();
break; break;
} }
} }

View File

@ -37,7 +37,7 @@ MemoryVar& Memory::get(string identifier) {
if (scope.vars.contains(identifier)) return scope.vars[identifier]; if (scope.vars.contains(identifier)) return scope.vars[identifier];
} }
throw; throw exception();
} }
void Memory::declare(string identifier, Type type) { 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) { Scope& Memory::_debug_top(void) {

View File

@ -12,5 +12,5 @@ Type string_to_type(string type_name) {
if (type_name == "double") if (type_name == "double")
return Type::Double; return Type::Double;
throw; throw exception();
} }