diff --git a/src/include/utils.h b/src/include/utils.h index 51c9c9a..91e07e6 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -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 */ diff --git a/src/interpreter.cpp b/src/interpreter.cpp index 2ce6247..84107ef 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -140,22 +140,9 @@ EvalResult eval(Node &ast, Memory &memory) { case NodeType::FunctionPrototype: { Token retTypeTok = get(node.children[0]); Token fnIdentifierTok = get(node.children[1]); - string retTypeName = get(retTypeTok.data); string fnIdentifier = get(fnIdentifierTok.data); - Type retType = string_to_type(retTypeName); - vector args = get(node.children[2]).children; - FunctionPrototype prototype = { { retType, "" } }; - - for (Node arg : args) { - InnerNode argInner = get(arg); - Token typeTok = get(argInner.children[0]); - Token identifierTok = get(argInner.children[1]); - string typeName = get(typeTok.data); - string identifier = get(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(node.children[0]); Token fnIdentifierTok = get(node.children[1]); - string retTypeName = get(retTypeTok.data); string fnIdentifier = get(fnIdentifierTok.data); - Type retType = string_to_type(retTypeName); - vector args = get(node.children[2]).children; - FunctionPrototype prototype = { { retType, "" } }; - - for (Node arg : args) { - InnerNode argInner = get(arg); - Token typeTok = get(argInner.children[0]); - Token identifierTok = get(argInner.children[1]); - string typeName = get(typeTok.data); - string identifier = get(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, diff --git a/src/utils.cpp b/src/utils.cpp index 7762852..07dd736 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -35,6 +35,27 @@ Type string_to_type(string type_name) { throw exception(); } +FunctionPrototype make_fn_prototype(InnerNode node) { + Token retTypeTok = get(node.children[0]); + string retTypeName = get(retTypeTok.data); + Type retType = string_to_type(retTypeName); + + vector args = get(node.children[2]).children; + FunctionPrototype prototype = { { retType, "" } }; + + for (Node arg : args) { + InnerNode argInner = get(arg); + Token typeTok = get(argInner.children[0]); + Token identifierTok = get(argInner.children[1]); + string typeName = get(typeTok.data); + string identifier = get(identifierTok.data); + Type type = string_to_type(typeName); + prototype.push_back({ type, identifier }); + } + + return prototype; +} + vector split_string(const string& input, char delimiter) { vector tokens; string token;