From b47091527e5951f410173202eefa307f419e84ea Mon Sep 17 00:00:00 2001 From: ala89 Date: Fri, 8 Dec 2023 16:19:41 +0100 Subject: [PATCH] Remove AST analysis dependency --- src/analysis.cpp | 9 --------- src/include/types.h | 2 +- src/include/utils.h | 5 +++++ src/interpreter.cpp | 9 ++++++--- src/tokenize.cpp | 3 --- src/utils.cpp | 9 +++++++++ 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/analysis.cpp b/src/analysis.cpp index 5524c3f..16ed260 100644 --- a/src/analysis.cpp +++ b/src/analysis.cpp @@ -48,15 +48,6 @@ bool is_arithmetic_type(Type type) { } } -Type string_to_type(string s, CodePosition pos) { - if (s == "int") - return Type::Int; - if (s == "double") - return Type::Double; - - throw TypeError("Unknown type", pos); -} - AnalysisResult analyze(Node &ast, Memory &memory) { if (holds_alternative(ast)) { Token token = get(ast); diff --git a/src/include/types.h b/src/include/types.h index 8f9f1c0..b7fa25b 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -12,7 +12,7 @@ using namespace std; /** * Tokens definition */ -enum class TokenType { Type, Identifier, Litteral, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Land, Lor, Lt, Gt, Leq, Geq, NotEqual, Not, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else, While, For, Comma }; +enum class TokenType { Identifier, Litteral, Plus, Minus, DoublePlus, DoubleMinus, DoubleEqual, Land, Lor, Lt, Gt, Leq, Geq, NotEqual, Not, Star, Slash, Percent, Equal, Semicolon, LParenthese, RParenthese, LCurlyBracket, RCurlyBracket, If, Else, While, For, Comma }; enum class Type { Int, Double }; using TokenData = variant; diff --git a/src/include/utils.h b/src/include/utils.h index d318134..b55702e 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -9,4 +9,9 @@ using namespace std; */ CodePosition get_node_pos(Node node); +/** + * Returns the Type associated with a type name +*/ +Type string_to_type(string type_name); + #endif \ No newline at end of file diff --git a/src/interpreter.cpp b/src/interpreter.cpp index 452b1d5..a8c7b5b 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -4,6 +4,7 @@ #include "include/parser.h" #include "include/interpreter.h" #include "include/memory.h" +#include "include/utils.h" using namespace std; bool bool_cast(EvalResult value) { @@ -253,21 +254,23 @@ EvalResult eval(Node &ast, Memory &memory) { case NodeType::Declaration: { Token typeTok = get(node.children[0]); Token identifierTok = get(node.children[1]); - Type type = get(typeTok.data); + string typeName = get(typeTok.data); string identifier = get(identifierTok.data); - memory.declare(identifier, type); + memory.declare(identifier, string_to_type(typeName)); return {}; } break; case NodeType::AssignedDeclaration: { Token typeTok = get(node.children[0]); Token identifierTok = get(node.children[1]); - Type type = get(typeTok.data); + string typeName = get(typeTok.data); string identifier = get(identifierTok.data); EvalResult value = eval(node.children[2], memory); + Type type = string_to_type(typeName); memory.declare(identifier, type); + if (type == Type::Int) { memory.update(identifier, int_cast(value)); } diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 468054b..257f4b9 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -94,9 +94,6 @@ void _debug_print_token(Token token) { case TokenType::Identifier: cout << "Identifier(" << get(token.data) << ")"; break; - case TokenType::Type: - cout << "Type("+_debug_get_type_name(get(token.data))+")"; - break; case TokenType::Plus: cout << "+"; break; diff --git a/src/utils.cpp b/src/utils.cpp index 641f442..6223ceb 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -4,4 +4,13 @@ CodePosition get_node_pos(Node node) { if (holds_alternative(node)) return get(node).pos; return get(node).pos; +} + +Type string_to_type(string type_name) { + if (type_name == "int") + return Type::Int; + if (type_name == "double") + return Type::Double; + + throw; } \ No newline at end of file