@@ -663,20 +663,20 @@ ASTNode* generateAST(const std::vector<tokenPair>& tokens, int depth, ASTNode* p
663663 }
664664
665665 case Module_Define: {
666- node->nodeType = Module_Define_Node;
667666
668- ASTNode* bodyNode = new ASTNode ();
667+ // ASTNode* bodyNode = new ASTNode();
669668
670669 std::vector<tokenPair> subTokens = std::vector<tokenPair>();
671670
672671 // Step through all tokens to gather body until braces are closed
673672 GATHER_SCOPE_BODY (tokens, subTokens, 0 , i, true );
674673
675- bodyNode = generateAST (subTokens, depth + 1 );
676- bodyNode->nodeType = Scope_Body;
677- bodyNode->codegen = &ASTNode::generateScopeBody;
674+ generateAST (subTokens, depth + 1 , node);
675+
676+ node->nodeType = Module_Define_Node;
677+ node->token = token;
678+ node->codegen = &ASTNode::generateScopeBody;
678679
679- node->childNodes .push_back (bodyNode);
680680 break ;
681681 }
682682
@@ -1077,7 +1077,7 @@ ASTNode* generateAST(const std::vector<tokenPair>& tokens, int depth, ASTNode* p
10771077 bodyNode->nodeType = Scope_Body;
10781078 bodyNode->codegen = &ASTNode::generateScopeBody;
10791079
1080- node->token . first = identifier->token . first ;
1080+ node->token = identifier->token ;
10811081 node->childNodes .push_back (bodyNode);
10821082
10831083 // Check if extra type following :: but before {
@@ -1197,7 +1197,7 @@ ASTNode* generateAST(const std::vector<tokenPair>& tokens, int depth, ASTNode* p
11971197 else
11981198 node->codegen = &ASTNode::generatePrototype;
11991199
1200- node->token . first = identifier->token . first ;
1200+ node->token = identifier->token ;
12011201 node->childNodes .push_back (identifier);
12021202 node->childNodes .push_back (secondPart);
12031203 node->childNodes .push_back (argumentsNode);
@@ -1208,7 +1208,7 @@ ASTNode* generateAST(const std::vector<tokenPair>& tokens, int depth, ASTNode* p
12081208 // Check for special function types
12091209 if (identifier->token .first == " cast" ) {
12101210 if (secondPart->childNodes .size () > 0 )
1211- node->token . first = secondPart->childNodes [0 ]->token . first ;
1211+ node->token = secondPart->childNodes [0 ]->token ;
12121212 else {
12131213 printTokenError (secondPart->token , " Cast function must have a return type" );
12141214 exit (1 );
@@ -1926,20 +1926,21 @@ bool loadModule(std::string& modulePath, std::string& moduleName)
19261926 // Look through file to see if it contains the desired module
19271927 for (int j = 0 ; j < localRoot->childNodes .size (); j++) {
19281928 if (localRoot->childNodes [j]->nodeType == Compiler_Define)
1929- if (localRoot->childNodes [j]->childNodes .size () >= 1 )
1930- if (localRoot->childNodes [j]->childNodes [0 ]->childNodes .size () >= 1 )
1931- if (localRoot->childNodes [j]->childNodes [0 ]->childNodes [0 ]->nodeType == Module_Define_Node) {
1932- ASTNode* moduleNode = localRoot->childNodes [j]->childNodes [0 ]->childNodes [0 ];
1933- if (localRoot->childNodes [j]->token .first == moduleName) {
1934- for (int i = 0 ; i < moduleNode->childNodes [0 ]->childNodes .size (); i++) {
1935- importedNodes.push_back (moduleNode->childNodes [0 ]->childNodes [i]);
1936- // rootNode->childNodes.push_back(localRoot->childNodes[i]);
1937- }
1938- if (verbosity >= 2 )
1939- printModuleLoaded (moduleName, pathStr);
1940- return true ;
1941- }
1929+ if (localRoot->childNodes [j]->childNodes .size () >= 1 &&
1930+ localRoot->childNodes [j]->childNodes [0 ]->childNodes .size () >= 1 &&
1931+ localRoot->childNodes [j]->childNodes [0 ]->childNodes [0 ]->nodeType == Module_Define_Node) {
1932+
1933+ ASTNode* moduleNode = localRoot->childNodes [j]->childNodes [0 ]->childNodes [0 ];
1934+ if (localRoot->childNodes [j]->token .first == moduleName) {
1935+ for (int i = 0 ; i < moduleNode->childNodes [0 ]->childNodes .size (); i++) {
1936+ importedNodes.push_back (moduleNode->childNodes [0 ]->childNodes [i]);
1937+ // rootNode->childNodes.push_back(localRoot->childNodes[i]);
19421938 }
1939+ if (verbosity >= 2 )
1940+ printModuleLoaded (moduleName, pathStr);
1941+ return true ;
1942+ }
1943+ }
19431944 }
19441945 }
19451946 return false ;
@@ -1959,21 +1960,26 @@ void addModuleImports(ASTNode*& node)
19591960 if (node->childNodes .size () > 1 ) {
19601961 ASTNode* moduleNameNode = node->childNodes [1 ]->childNodes [0 ];
19611962 if (moduleNameNode->nodeType == Member_Access) {
1962- std::string modulePath = " " ;
1963+ std::string modulePath = moduleNameNode-> childNodes [ 0 ]-> token . first ;
19631964 bool moduleFound = false ;
1964- for (int i = 0 ; i < moduleNameNode->childNodes .size () - 1 ; i++)
1965- modulePath += moduleNameNode->childNodes [i]->token .first ;
1966- std::string moduleName = moduleNameNode->childNodes .back ()->token .first ;
1965+ ASTNode* secondExpression = moduleNameNode->childNodes [1 ];
1966+ // Get sub member accesses
1967+ while (secondExpression->nodeType == Member_Access) {
1968+ modulePath += " /" + secondExpression->childNodes [0 ]->token .first ;
1969+ secondExpression = secondExpression->childNodes [1 ];
1970+ }
1971+ std::string moduleName = secondExpression->token .first ;
19671972
19681973 if (importedModuleNames.find (moduleName) != importedModuleNames.end ()) {
19691974 node->nodeType = Nothing_Node;
1975+ node->showInASTOutput = false ;
19701976 return ;
19711977 }
19721978
19731979 std::string searchPath[2 ] = {projectDirectory + modulePath, executableDirectory + " modules/" + modulePath};
19741980 if (directoryExists (searchPath[0 ]))
19751981 moduleFound = loadModule (searchPath[0 ], moduleName);
1976- else if (directoryExists (searchPath[1 ]))
1982+ if (!moduleFound && directoryExists (searchPath[1 ]))
19771983 moduleFound = loadModule (searchPath[1 ], moduleName);
19781984
19791985 importedModuleNames.insert (moduleName);
@@ -1995,7 +2001,7 @@ void addModuleImports(ASTNode*& node)
19952001 std::string searchPath[2 ] = {projectDirectory, executableDirectory + " modules/" };
19962002 if (directoryExists (searchPath[0 ]))
19972003 moduleFound = loadModule (searchPath[0 ], moduleName);
1998- else if (directoryExists (searchPath[1 ]))
2004+ if (!moduleFound && directoryExists (searchPath[1 ]))
19992005 moduleFound = loadModule (searchPath[1 ], moduleName);
20002006
20012007 importedModuleNames.insert (moduleName);
0 commit comments