Skip to content

Commit

Permalink
Fix OOP tutorial (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Mar 6, 2024
1 parent eb38e30 commit cb3faf1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/labeler.yml
Expand Up @@ -19,7 +19,7 @@
'documentation':
- changed-files:
- any-glob-to-any-file:
- 'doc/**/*'
- 'docs/**/*'
- 'media/specs/**/*'

'ci':
Expand Down
2 changes: 1 addition & 1 deletion .run/spice.run.xml
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="build -O0 -d -g ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -O0 -d ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="LLVM_BUILD_INCLUDE_DIR" value="D:/LLVM/build-release/include" />
<env name="LLVM_INCLUDE_DIR" value="D:/LLVM/llvm/include" />
Expand Down
53 changes: 44 additions & 9 deletions docs/docs/how-to/oop.md
Expand Up @@ -47,12 +47,18 @@ type Human struct : MakeSound, Speak {
unsigned int age
}
p Human.ctor(string firstName, string lastName, unsigned int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
p Human.makeSound() {
// Sigh ...
printf("Sigh...\n");
}
p Human.sayHello() {
// Hi!
p Human.sayHello(string name) {
printf("Hi, %s!\n", name);
}
type Car struct : MakeSound {
Expand All @@ -61,25 +67,50 @@ type Car struct : MakeSound {
unsigned int seats
}
p Car.ctor(string brand, string model, unsigned int seats) {
this.brand = brand;
this.model = model;
this.seats = seats;
}
p Car.makeSound() {
// Wroom, wroom
printf("Wroom, wroom!\n");
}
type Parrot struct : MakeSound, Speak {
string name
unsigned int age
}
p Parrot.ctor(string name, unsigned int age) {
this.name = name;
this.age = age;
}
p Parrot.makeSound() {
// Sqawk!
printf("Sqawk!\n");
}
p Parrot.sayHello() {
// Hello, squawk!
p Parrot.sayHello(string name) {
printf("Hello %s, squawk!\n", name);
}
f<int> main() {
Human human = Human("John", "Doe", 25);
Car car = Car("Toyota", "Corolla", 5);
Parrot parrot = Parrot("Polly", 3);
human.makeSound();
car.makeSound();
parrot.makeSound();
human.sayHello("Jane");
parrot.sayHello("Jane");
return 0;
}
```

As all living beings, parrots and humans have an age. So you might want to extract the `unsigned int age`, that exists
All living beings, parrots and humans have an age. So you might want to extract the `unsigned int age`, that exists
in both structs to a separate struct called `LivingBeing`.

```spice
Expand All @@ -101,4 +132,8 @@ type Parrot struct : MakeSound, Speak {
}
// ...
```
```

The `compose` keyword is used to include the fields and methods of another struct into the current struct. The members of the
parent struct are accessible directly via name. e.g. `parrot.age` or through the compose member itself, e.g.
`parrot.livingBeing.age`.
78 changes: 70 additions & 8 deletions media/test-project/test.spice
@@ -1,15 +1,77 @@
import "std/data/hash-table";
type Speak interface {
p sayHello(string);
}

type MakeSound interface {
p makeSound();
}

type Human struct : MakeSound, Speak {
string firstName
string lastName
unsigned int age
}

p Human.ctor(string firstName, string lastName, unsigned int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

p Human.makeSound() {
printf("Sigh...\n");
}

p Human.sayHello(string name) {
printf("Hi, %s!\n", name);
}

type Car struct : MakeSound {
string brand
string model
unsigned int seats
}

p Car.ctor(string brand, string model, unsigned int seats) {
this.brand = brand;
this.model = model;
this.seats = seats;
}

p Car.makeSound() {
printf("Wroom, wroom!\n");
}

type Parrot struct : MakeSound, Speak {
string name
unsigned int age
}

p Parrot.ctor(string name, unsigned int age) {
this.name = name;
this.age = age;
}

p Parrot.makeSound() {
printf("Sqawk!\n");
}

p Parrot.sayHello(string name) {
printf("Hello %s, squawk!\n", name);
}

f<int> main() {
HashTable<int, int> hashTable = HashTable<int, int>();
hashTable.upsert(1, 2);
hashTable.upsert(2, 3);
Human human = Human("John", "Doe", 25);
Car car = Car("Toyota", "Corolla", 5);
Parrot parrot = Parrot("Polly", 3);

Optional<int> value = hashTable.get(1);
assert(value.get() == 2);
value = hashTable.get(2);
assert(value.get() == 3);
human.makeSound();
car.makeSound();
parrot.makeSound();

human.sayHello("Jane");
parrot.sayHello("Jane");
return 0;
}

/*import "bootstrap/util/block-allocator";
Expand Down

0 comments on commit cb3faf1

Please sign in to comment.