implement chapter 13 in rust!

This commit is contained in:
Sebastian Hugentobler 2025-05-26 10:19:18 +02:00
parent 621c97102a
commit 32e5e8a41a
Signed by: shu
SSH key fingerprint: SHA256:ppcx6MlixdNZd5EUM1nkHOKoyQYoJwzuQKXM6J/t66M
23 changed files with 318 additions and 19 deletions

6
lox/class_fields.lox Normal file
View file

@ -0,0 +1,6 @@
class Dog{}
var dog = Dog();
dog.name = "Leika";
print dog.name;

3
lox/class_instance.lox Normal file
View file

@ -0,0 +1,3 @@
class Bagel {}
var bagel = Bagel();
print bagel;

7
lox/class_methods.lox Normal file
View file

@ -0,0 +1,7 @@
class Bacon {
eat() {
print "Crunch crunch crunch!";
}
}
Bacon().eat();

7
lox/class_name.lox Normal file
View file

@ -0,0 +1,7 @@
class DevonshireCream {
serveOn() {
return "Scones";
}
}
print DevonshireCream;

10
lox/class_this.lox Normal file
View file

@ -0,0 +1,10 @@
class Cake {
taste() {
var adjective = "delicious";
print "The " + this.flavor + " cake is " + adjective + "!";
}
}
var cake = Cake();
cake.flavor = "German chocolate";
cake.taste();

9
lox/fibonacci_timed.lox Normal file
View file

@ -0,0 +1,9 @@
fun fib(n) {
if (n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
var before = clock();
print fib(40);
var after = clock();
print after - before;

9
lox/init.lox Normal file
View file

@ -0,0 +1,9 @@
class Foo {
init() {
this.name = "test";
print this;
}
}
var foo = Foo();
print foo.name;

View file

@ -0,0 +1,8 @@
class Foo {
init() {
return;
print this;
}
}
var foo = Foo();

5
lox/init_return.lox Normal file
View file

@ -0,0 +1,5 @@
class Foo {
init() {
return "something else";
}
}

1
lox/self_inheritance.lox Normal file
View file

@ -0,0 +1 @@
class Oops < Oops {}

14
lox/super_call.lox Normal file
View file

@ -0,0 +1,14 @@
class Doughnut {
cook() {
print "Fry until golden brown.";
}
}
class BostonCream < Doughnut {
cook() {
super.cook();
print "Pipe full of custard and coat with chocolate.";
}
}
BostonCream().cook();

View file

@ -0,0 +1,6 @@
class Eclair {
cook() {
super.cook();
print "Pipe full of crème pâtissière.";
}
}

View file

@ -0,0 +1,9 @@
class Doughnut {
cook() {
print "Fry until golden brown.";
}
}
class BostonCream < Doughnut {}
BostonCream().cook();

View file

@ -0,0 +1,3 @@
var NotAClass = "I am totally not a class";
class Subclass < NotAClass {}