method overriding works i think

This commit is contained in:
2026-06-05 18:23:55 +10:00
parent a97b2566f9
commit 8622d80f86
8 changed files with 77 additions and 21 deletions

View File

@@ -74,6 +74,10 @@ void freeNode(CometASTNode* node) {
freeNode(node->data.AST_STRUCT_DEF_STATEMENT.parentName);
break;
}
case AST_OVERRIDE_STATEMENT: {
freeNode(node->data.AST_OVERRIDE_STATEMENT.funcDef);
break;
}
case AST_ARG_DEF: {
freeNode(node->data.AST_ARG_DEF.type);
freeNode(node->data.AST_ARG_DEF.ident);

View File

@@ -1214,6 +1214,9 @@ ResultType(CometOperand, charptr) visitStructDefStatement(CometCompiler* c, Come
myMethodCount++;
break;
case AST_OVERRIDE_STATEMENT:
break;
default: {
Estr errMsg = CREATE_ESTR("Cannot define \"");
APPEND_ESTR(errMsg, ASTNodeTypeToCStr(fieldDef->nodeType));
@@ -1248,6 +1251,16 @@ ResultType(CometOperand, charptr) visitStructDefStatement(CometCompiler* c, Come
append(c->typeMap, typeMapEntry);
append(c->structs, structType);
// if we inherit from another struct then pull in its methods and fields
for (size_t i = 0; i < parentFieldCount; i++) {
structType->fieldNames[i] = parentStruct->fieldNames[i];
structType->fieldTypes[i] = parentStruct->fieldTypes[i];
}
for (size_t i = 0; i < parentMethodCount; i++) {
structType->vtable[i] = parentStruct->vtable[i];
}
uint32_t vtableIdx = parentMethodCount;
uint32_t fieldIdx = parentFieldCount;
for (size_t i = 0; i < structDef.fieldDefs.count; i++) {
@@ -1259,7 +1272,49 @@ ResultType(CometOperand, charptr) visitStructDefStatement(CometCompiler* c, Come
structType->fieldTypes[fieldIdx++] = getType(c, fieldDef->data.AST_ASSIGN_STATEMENT.type->data.AST_IDENTIFIER.ident);
break;
case AST_OVERRIDE_STATEMENT: {
// we're not inheriting from any struct so we cant override functions
if (parentStruct == NULL) {
Estr errMsg = CREATE_ESTR("Cannot use an override statement in struct \"");
APPEND_ESTR(errMsg, structName);
APPEND_ESTR(errMsg, "\" because it has no parent.");
return Error(CometOperand, charptr, errMsg.str);
}
ResultType(CometOperand, charptr) result = visitMethodDefStatement(c, fieldDef->data.AST_OVERRIDE_STATEMENT.funcDef, generalStructType);
if (result.error)
return result;
CometFunction* function = c->functions[result.as.success.symbolIdx];
int32_t parentMethodIdx = getMethodIndex(parentStruct, function->name);
// overriding a function that doesn't exist in the parent
if (parentMethodIdx == -1) {
Estr errMsg = CREATE_ESTR("Cannot override method \"");
APPEND_ESTR(errMsg, function->name);
APPEND_ESTR(errMsg, "\" because parent struct doesn't have it.");
return Error(CometOperand, charptr, errMsg.str);
}
Estr newFuncName = CREATE_ESTR(structName);
APPEND_ESTR(newFuncName, "_");
APPEND_ESTR(newFuncName, function->name);
CometMethod* newMethod = malloc(sizeof(CometMethod));
memcpy(newMethod->name, function->name, strlen(function->name) + 1);
memcpy(function->name, newFuncName.str, newFuncName.size + 1);
newMethod->argCount = function->argCount;
newMethod->startIdx = function->startIdx,
newMethod->symbolIdx = result.as.success.symbolIdx;
DESTROY_ESTR(newFuncName);
structType->vtable[parentMethodIdx] = newMethod;
break;
}
case AST_FUNC_DEF_STATEMENT: {
ResultType(CometOperand, charptr) result = visitMethodDefStatement(c, fieldDef, generalStructType);
if (result.error)
return result;
@@ -1287,16 +1342,6 @@ ResultType(CometOperand, charptr) visitStructDefStatement(CometCompiler* c, Come
}
}
// if we inherit from another struct then pull in its methods and fields
for (size_t i = 0; i < parentFieldCount; i++) {
structType->fieldNames[i] = parentStruct->fieldNames[i];
structType->fieldTypes[i] = parentStruct->fieldTypes[i];
}
for (size_t i = 0; i < parentMethodCount; i++) {
structType->vtable[i] = parentStruct->vtable[i];
}
// build constructor
if (!structDef.constructor) {
Estr errMsg = CREATE_ESTR("Struct \"");

View File

@@ -31,7 +31,6 @@ uint32_t defineVar(CometEnvironment* env, char* name, RecordType recordType, Com
// avoid duplication of keys
if (record != NULL) {
printf("Redeclaration of %s!\n", name);
return 0;
}

View File

@@ -5,8 +5,8 @@ struct Foo {
self.abc = abc
}
func foo(int a) -> int {
return self.abc + a
func testFunc() -> int {
return 24
}
}
@@ -15,12 +15,12 @@ struct OtherStruct : Foo {
super(self, abc)
}
func testFunc() -> int {
return 24
override func testFunc() -> int {
return 22
}
}
func main() -> int {
OtherStruct test = new OtherStruct(3)
return test.testFunc() + test.foo(3)
return test.testFunc()
}

View File

@@ -1,11 +1,13 @@
import io
func main() -> int {
print("Counting from 1 to 10...")
io.print("Counting from 1 to 10...")
for int i in 1 .. 10 {
print("%d\n", i)
}
print("Counting from 10 to 1...")
io.print("Counting from 10 to 1...")
for int i in 10 .. 0 step -1 {
print("%d\n", i)

View File

@@ -1,4 +1,6 @@
import io
func main() -> int {
print("Hello, World!\n")
io.print("Hello, World!\n")
return 0
}

View File

@@ -1,6 +1,8 @@
import io
func main() -> int {
for int i in 0 .. 100000 {
print("%d\n", i+1)
io.print("%d\n", i+1)
}
return 0
}

View File

@@ -1,9 +1,11 @@
import io
func add(int a, int b) -> int => a + b
func main() -> int {
int a = 2
int b = 6
print("%d + %d = %d\n", a, b, add(a, b))
io.print("%d + %d = %d\n", a, b, add(a, b))
return 0
}