Remove AST analysis dependency

This commit is contained in:
ala89 2023-12-08 16:19:41 +01:00
parent a8546755af
commit b47091527e
6 changed files with 21 additions and 16 deletions

View File

@ -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<Token>(ast)) {
Token token = get<Token>(ast);

View File

@ -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<int, double, string, Type>;

View File

@ -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

View File

@ -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<Token>(node.children[0]);
Token identifierTok = get<Token>(node.children[1]);
Type type = get<Type>(typeTok.data);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data);
memory.declare(identifier, type);
memory.declare(identifier, string_to_type(typeName));
return {};
} break;
case NodeType::AssignedDeclaration: {
Token typeTok = get<Token>(node.children[0]);
Token identifierTok = get<Token>(node.children[1]);
Type type = get<Type>(typeTok.data);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(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));
}

View File

@ -94,9 +94,6 @@ void _debug_print_token(Token token) {
case TokenType::Identifier:
cout << "Identifier(" << get<string>(token.data) << ")";
break;
case TokenType::Type:
cout << "Type("+_debug_get_type_name(get<Type>(token.data))+")";
break;
case TokenType::Plus:
cout << "+";
break;

View File

@ -4,4 +4,13 @@ CodePosition get_node_pos(Node node) {
if (holds_alternative<InnerNode>(node))
return get<InnerNode>(node).pos;
return get<Token>(node).pos;
}
Type string_to_type(string type_name) {
if (type_name == "int")
return Type::Int;
if (type_name == "double")
return Type::Double;
throw;
}