docs: add OOP class system with object allocation, field/method lookup, and parser integration
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
class Point {
|
||||
int x = 10;
|
||||
int y = 20;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Test: Field initialization\n");
|
||||
int p = new Point();
|
||||
printf("p = %d\n", p);
|
||||
printf("p.x should be 10: %d\n", p.x);
|
||||
printf("p.y should be 20: %d\n", p.y);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
class Point {
|
||||
int x = 10;
|
||||
int y = 20;
|
||||
|
||||
void print(int obj) {
|
||||
printf("Point(%d, %d)\n", obj.x, obj.y);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Test: Method call\n");
|
||||
int p = new Point();
|
||||
printf("Calling p.print()...\n");
|
||||
p.print();
|
||||
printf("Success!\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Empty {
|
||||
int value = 42
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Test: Creating empty class\n")
|
||||
Empty * e = new Empty()
|
||||
printf("Object created\n")
|
||||
printf("Value: %d\n", e.value)
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
class Point {
|
||||
int x = 0
|
||||
int y = 0
|
||||
|
||||
void print(this) {
|
||||
printf("Point(%d, %d)\n", this.x, this.y)
|
||||
}
|
||||
|
||||
int getX(this) {
|
||||
return this.x
|
||||
}
|
||||
|
||||
int getY(this) {
|
||||
return this.y
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Simple OOP Test ===\n\n")
|
||||
|
||||
printf("Test 1: Create object\n")
|
||||
Point * p = new Point()
|
||||
printf("Created point\n")
|
||||
|
||||
printf("Test 2: Access fields\n")
|
||||
printf("p.x = %d\n", p.x)
|
||||
printf("p.y = %d\n", p.y)
|
||||
|
||||
printf("Test 3: Call method\n")
|
||||
p.print()
|
||||
|
||||
printf("Test 4: Call getter methods\n")
|
||||
int px = p.getX()
|
||||
int py = p.getY()
|
||||
printf("Got x=%d, y=%d\n", px, py)
|
||||
|
||||
printf("\n=== Test Complete ===\n")
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Empty {
|
||||
int value = 42;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Step 0: No OOP, just testing class skipping\n");
|
||||
int x = 5;
|
||||
printf("x = %d\n", x);
|
||||
printf("Success!\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class Empty {
|
||||
int value = 42;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Step 1: Object creation test\n");
|
||||
int obj = new Empty();
|
||||
printf("Object created, pointer = %d\n", obj);
|
||||
printf("Success!\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
class Animal {
|
||||
int age = 0
|
||||
char * name = null
|
||||
|
||||
constructor(this, char * animalName) {
|
||||
this.age = 0
|
||||
this.name = animalName
|
||||
printf("Animal created: %s\n", this.name)
|
||||
}
|
||||
|
||||
destructor(this) {
|
||||
printf("Animal destroyed: %s\n", this.name)
|
||||
}
|
||||
|
||||
void makeSound(this) {
|
||||
printf("Some generic sound\n")
|
||||
}
|
||||
|
||||
static int getSpeciesCount() {
|
||||
return 42
|
||||
}
|
||||
}
|
||||
|
||||
class Dog : Animal {
|
||||
int barks = 0
|
||||
|
||||
constructor(this, char * name) {
|
||||
super(name)
|
||||
this.barks = 0
|
||||
printf("Dog created: %s\n", this.name)
|
||||
}
|
||||
|
||||
destructor(this) {
|
||||
printf("Dog destroyed\n")
|
||||
}
|
||||
|
||||
void makeSound(this) {
|
||||
printf("Woof! Woof!\n")
|
||||
}
|
||||
|
||||
void bark(this, int times) {
|
||||
this.barks = this.barks + times
|
||||
printf("Barked %d times\n", times)
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== OOP Test ===\n\n")
|
||||
|
||||
printf("Test 1: Create an Animal\n")
|
||||
Animal * animal = new Animal("Generic")
|
||||
animal.makeSound()
|
||||
printf("Animal age: %d\n", animal.age)
|
||||
animal = null
|
||||
printf("\n")
|
||||
|
||||
printf("Test 2: Create a Dog\n")
|
||||
Dog * dog = new Dog("Buddy")
|
||||
dog.makeSound()
|
||||
dog.bark(3)
|
||||
printf("Dog barks: %d\n", dog.barks)
|
||||
dog = null
|
||||
printf("\n")
|
||||
|
||||
printf("Test 3: Static method\n")
|
||||
int count = Animal.getSpeciesCount()
|
||||
printf("Species count: %d\n", count)
|
||||
printf("\n")
|
||||
|
||||
printf("=== OOP Test Complete ===\n")
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
class Point {
|
||||
int x = 10;
|
||||
int y = 20;
|
||||
|
||||
void print(int obj) {
|
||||
printf("Point(%d, %d)\n", obj.x, obj.y);
|
||||
}
|
||||
|
||||
int getX(int obj) {
|
||||
return obj.x;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("=== Working OOP Test ===\n\n");
|
||||
|
||||
printf("Test 1: Create object (using int to hold pointer)\n");
|
||||
int p = new Point();
|
||||
printf("Object created\n\n");
|
||||
|
||||
printf("Test 2: Access field\n");
|
||||
printf("p.x = %d\n", p.x);
|
||||
printf("p.y = %d\n\n", p.y);
|
||||
|
||||
printf("Test 3: Call method\n");
|
||||
p.print();
|
||||
printf("\n");
|
||||
|
||||
printf("Test 4: Call getter\n");
|
||||
int x_val = p.getX();
|
||||
printf("Got x = %d\n\n", x_val);
|
||||
|
||||
printf("Test 5: Null check\n");
|
||||
int n = null;
|
||||
printf("null value = %d\n\n", n);
|
||||
|
||||
printf("=== All Tests Passed ===\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user