Skip to content

Commit 21124b6

Browse files
committed
Fix imports to be better, and error printing
1 parent 1a88d1f commit 21124b6

File tree

7 files changed

+160
-143
lines changed

7 files changed

+160
-143
lines changed

examples/start/main.asa

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#import Builtin.Casts;
44
//#import Builtin.Operators;
55
#import Builtin.Memory;
6+
#import Builtin.Strings;
67

78
//#import GameLogic;
89

@@ -18,9 +19,9 @@ Or individual components:
1819
#import Rendering.Window;
1920
*/
2021

21-
string :: struct {
22-
c : int;
23-
}
22+
//string :: struct {
23+
// c : int;
24+
//}
2425
string::string(){
2526
return *(#new string);
2627
}

modules/Builtin/asa.asa

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ Casts :: module {
101101

102102
Operators::module{
103103
operator++ :: int(i : ref int) #inline; #noast {
104-
i += 1;
104+
i = i+1;
105105
return i;
106106
}
107107
operator-- :: int(i : ref int) #inline; #noast {
108-
i -= 1;
108+
i = i-1;
109109
return i;
110110
}
111111
}
@@ -116,61 +116,61 @@ Memory :: module{
116116
#extern free::(p : *int);
117117
}
118118

119-
//Strings :: module{
120-
// #import Builtin.Memory;
121-
// #import Builtin.Operators;
122-
//
123-
// string :: struct {
124-
// address : *char;
125-
// length : uint32;
126-
//
127-
// size::uint32(){
128-
// return length;
129-
// }
130-
//
131-
// cast::string(c : *char){
132-
// len = 0;
133-
// for(i : 0..4_294_967_296){
134-
// if(c[i] == '\0')
135-
// break;
136-
// len++;
137-
// }
138-
// address = malloc(len);
139-
// for(i : 0..len){
140-
// address[i] = c[i];
141-
// }
142-
// length = len;
143-
// }
144-
//
145-
// binary+ :: string(s : ref string, c : char) #inline {
146-
// s.length += 1;
147-
// newAddress : *char = malloc(s.length);
148-
// for(i : 0..s.length-1){
149-
// newAddress[i] = s.address[i];
150-
// }
151-
// free(s.address);
152-
// newAddress[s.length-1] = c;
153-
// s.address = newAddress;
154-
// return s;
155-
// }
156-
//
157-
// binary+ :: string(s : ref string, otherStr : string) #inline {
158-
// oldLength = s.length;
159-
// s.length += otherStr.length;
160-
// newAddress : *char = malloc(s.length);
161-
// for(i : 0..oldLength){
162-
// newAddress[i] = s.address[i];
163-
// }
164-
// free(s.address);
165-
// for(i : oldLength..otherStr.length){
166-
// newAddress[i] = otherStr.address[i-oldLength];
167-
// }
168-
// s.address = newAddress;
169-
// return s;
170-
// }
171-
//
172-
// binary[] :: char(s : ref string, i : int) #inline {
173-
// return s.address[i];
174-
// }
175-
// }
176-
//}
119+
Strings :: module{
120+
//#import Builtin.Memory;
121+
//#import Builtin.Operators;
122+
123+
string :: struct {
124+
//address : *char;
125+
length : uint32;
126+
127+
//size::uint32(){
128+
// return length;
129+
//}
130+
131+
//cast::string(c : *char){
132+
// len = 0;
133+
// for(i : 0..4_294_967_296){
134+
// if(c[i] == '\0')
135+
// break;
136+
// len++;
137+
// }
138+
// address = malloc(len);
139+
// for(i : 0..len){
140+
// address[i] = c[i];
141+
// }
142+
// length = len;
143+
//}
144+
145+
//binary+ :: string(s : ref string, c : char) #inline {
146+
// s.length += 1;
147+
// newAddress : *char = malloc(s.length);
148+
// for(i : 0..s.length-1){
149+
// newAddress[i] = s.address[i];
150+
// }
151+
// free(s.address);
152+
// newAddress[s.length-1] = c;
153+
// s.address = newAddress;
154+
// return s;
155+
//}
156+
157+
//binary+ :: string(s : ref string, otherStr : string) #inline {
158+
// oldLength = s.length;
159+
// s.length += otherStr.length;
160+
// newAddress : *char = malloc(s.length);
161+
// for(i : 0..oldLength){
162+
// newAddress[i] = s.address[i];
163+
// }
164+
// free(s.address);
165+
// for(i : oldLength..otherStr.length){
166+
// newAddress[i] = otherStr.address[i-oldLength];
167+
// }
168+
// s.address = newAddress;
169+
// return s;
170+
//}
171+
172+
//binary[] :: char(s : ref string, i : int) #inline {
173+
// return s.address[i];
174+
//}
175+
}
176+
}

src/codegen.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ void* ASTNode::generateMemberAccess(int pass)
14041404
printf("member index: %d\n", memberIndex);
14051405
//std::cout << structDefinitions[v->type].members << std::endl;
14061406
printf("member list size: %d\n", structDefinition->members.size());
1407-
printf("member name: %s\n", structDefinition->members[memberIndex].typeString);
1407+
printf("member name: %s\n", structDefinition->members[memberIndex].typeString.c_str());
14081408
Type* elementType = getLLVMTypeFromString(structDefinition->members[memberIndex].typeString, 0, token);
14091409
//Type* elementType = getLLVMTypeFromString(v->type, -1, childNodes[0]->token);
14101410
//Type* elementType = getLLVMTypeFromString(baseType);
@@ -1644,6 +1644,12 @@ void* ASTNode::generateStruct(int pass)
16441644
{
16451645
std::string structName = token.first;
16461646

1647+
// Do not create a struct with the same name
1648+
if (structDefinitions.find(structName) != structDefinitions.end()) {
1649+
printTokenError(token, "Struct cannot be redefined");
1650+
exit(1);
1651+
}
1652+
16471653
argumentList members = argumentList();
16481654
std::vector<Type*> fieldTypes;
16491655
std::vector<std::string> fieldNames;
@@ -1682,6 +1688,9 @@ void* ASTNode::generateStruct(int pass)
16821688
}
16831689
}
16841690

1691+
// Make nothing node to not be regenerated
1692+
nodeType = Nothing_Node;
1693+
16851694
StructType* structTy = StructType::create(*TheContext, fieldTypes, structName);
16861695

16871696
//structTy->setBody(fieldTypes, false);
@@ -1920,7 +1929,7 @@ void* ASTNode::generatePrototype(int pass)
19201929
//Function* theFunction = TheModule->getFunction(token.first);
19211930
functionID* theFunctionID = getExactFunctionFromID(fnName, argList, token);
19221931
if (theFunctionID) {
1923-
if (verbosity >= 3) {
1932+
if (verbosity >= 5) {
19241933
console::printIndent(2);
19251934
console::Write("-- Pre-existing function definition found for: ");
19261935
console::Write(fnName, console::yellowFGColor);
@@ -1948,8 +1957,10 @@ void* ASTNode::generatePrototype(int pass)
19481957
}
19491958

19501959
functionIDs.emplace_back(fnName, mangledName, rTypeString, argList, fn, variableNumArguments);
1951-
console::printIndent(depth + 2);
1952-
console::WriteLine("-- Added function \"" + fnName + "\" to functionIDs");
1960+
if (verbosity >= 5) {
1961+
console::printIndent(depth + 2);
1962+
console::WriteLine("-- Added function \"" + fnName + "\" to functionIDs");
1963+
}
19531964

19541965
return fn;
19551966
}
@@ -1973,7 +1984,7 @@ void* ASTNode::generateFunction(int pass)
19731984
}
19741985

19751986
if (!theFunction->empty()) {
1976-
printTokenError(token, "Function cannot be redefined");
1987+
printTokenError(token, "Function cannot be redefined, requires unique identity.");
19771988
return nullptr;
19781989
}
19791990

src/console.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace console {
9090
Write(std::string(" on line: \"") + std::to_string(lineNumber) + "\"");
9191
Write(": ");
9292

93-
Write(s + "\n\n");
93+
Write(s + "\n");
9494
}
9595

9696
void PrintWarning(std::string s, int lineNumber, std::string fileName)
@@ -102,7 +102,7 @@ namespace console {
102102
Write(std::string(" on line: \"") + std::to_string(lineNumber) + "\"");
103103
Write(": ");
104104

105-
Write(s + "\n\n");
105+
Write(s + "\n");
106106
}
107107

108108

src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ int main(int argc, char** argv)
154154
// File includes
155155
addFileIncludes(rootNode);
156156
for (int i = 0; i < importedNodes.size(); i++)
157-
rootNode->childNodes.push_back(importedNodes[i]);
157+
rootNode->childNodes.insert(rootNode->childNodes.begin(), importedNodes[i]);
158158
if (importedNodes.size() > 0)
159159
noImports = false;
160160
importedNodes = std::vector<ASTNode*>();
161161

162162
// Module imports
163163
addModuleImports(rootNode);
164164
for (int i = 0; i < importedNodes.size(); i++)
165-
rootNode->childNodes.push_back(importedNodes[i]);
165+
rootNode->childNodes.insert(rootNode->childNodes.begin(), importedNodes[i]);
166166
if (importedNodes.size() > 0)
167167
noImports = false;
168168
importedNodes = std::vector<ASTNode*>();
@@ -200,7 +200,7 @@ int main(int argc, char** argv)
200200
console::WriteLine("\n\nCompiling:", console::greenFGColor);
201201
// First pass
202202
generateOutputCode(rootNode, 0, 0);
203-
// Second pass
203+
// Final pass
204204
generateOutputCode(rootNode, 0, 1);
205205
// Cleanup unused code
206206
removeUnusedPrototypes();

src/parser.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)