Add make_fn_prototype function

This commit is contained in:
ala89 2024-01-03 12:01:40 +01:00
parent 13b067f803
commit b00f3c222d
3 changed files with 28 additions and 28 deletions

View File

@ -19,6 +19,11 @@ Type type_type_to_type(TypeType type_type);
*/
Type string_to_type(string type_name);
/**
* Turns a FunctionPrototype or FunctionDeclaration node into a FunctionPrototype object
*/
FunctionPrototype make_fn_prototype(InnerNode node);
/**
* Splits a string using the provided delimiter
*/

View File

@ -140,22 +140,9 @@ EvalResult eval(Node &ast, Memory &memory) {
case NodeType::FunctionPrototype: {
Token retTypeTok = get<Token>(node.children[0]);
Token fnIdentifierTok = get<Token>(node.children[1]);
string retTypeName = get<string>(retTypeTok.data);
string fnIdentifier = get<string>(fnIdentifierTok.data);
Type retType = string_to_type(retTypeName);
vector<Node> args = get<InnerNode>(node.children[2]).children;
FunctionPrototype prototype = { { retType, "" } };
for (Node arg : args) {
InnerNode argInner = get<InnerNode>(arg);
Token typeTok = get<Token>(argInner.children[0]);
Token identifierTok = get<Token>(argInner.children[1]);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data);
Type type = string_to_type(typeName);
prototype.push_back({ type, identifier });
}
FunctionPrototype prototype = make_fn_prototype(node);
memory.declare(fnIdentifier, {
.type = TypeType::Function,
@ -167,22 +154,9 @@ EvalResult eval(Node &ast, Memory &memory) {
case NodeType::FunctionDeclaration: {
Token retTypeTok = get<Token>(node.children[0]);
Token fnIdentifierTok = get<Token>(node.children[1]);
string retTypeName = get<string>(retTypeTok.data);
string fnIdentifier = get<string>(fnIdentifierTok.data);
Type retType = string_to_type(retTypeName);
vector<Node> args = get<InnerNode>(node.children[2]).children;
FunctionPrototype prototype = { { retType, "" } };
for (Node arg : args) {
InnerNode argInner = get<InnerNode>(arg);
Token typeTok = get<Token>(argInner.children[0]);
Token identifierTok = get<Token>(argInner.children[1]);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data);
Type type = string_to_type(typeName);
prototype.push_back({ type, identifier });
}
FunctionPrototype prototype = make_fn_prototype(node);
memory.declare(fnIdentifier, {
.type = TypeType::Function,

View File

@ -35,6 +35,27 @@ Type string_to_type(string type_name) {
throw exception();
}
FunctionPrototype make_fn_prototype(InnerNode node) {
Token retTypeTok = get<Token>(node.children[0]);
string retTypeName = get<string>(retTypeTok.data);
Type retType = string_to_type(retTypeName);
vector<Node> args = get<InnerNode>(node.children[2]).children;
FunctionPrototype prototype = { { retType, "" } };
for (Node arg : args) {
InnerNode argInner = get<InnerNode>(arg);
Token typeTok = get<Token>(argInner.children[0]);
Token identifierTok = get<Token>(argInner.children[1]);
string typeName = get<string>(typeTok.data);
string identifier = get<string>(identifierTok.data);
Type type = string_to_type(typeName);
prototype.push_back({ type, identifier });
}
return prototype;
}
vector<string> split_string(const string& input, char delimiter) {
vector<string> tokens;
string token;