implement chapter 13 in rust!
This commit is contained in:
parent
621c97102a
commit
32e5e8a41a
23 changed files with 318 additions and 19 deletions
6
lox/class_fields.lox
Normal file
6
lox/class_fields.lox
Normal file
|
@ -0,0 +1,6 @@
|
|||
class Dog{}
|
||||
|
||||
var dog = Dog();
|
||||
dog.name = "Leika";
|
||||
|
||||
print dog.name;
|
3
lox/class_instance.lox
Normal file
3
lox/class_instance.lox
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Bagel {}
|
||||
var bagel = Bagel();
|
||||
print bagel;
|
7
lox/class_methods.lox
Normal file
7
lox/class_methods.lox
Normal file
|
@ -0,0 +1,7 @@
|
|||
class Bacon {
|
||||
eat() {
|
||||
print "Crunch crunch crunch!";
|
||||
}
|
||||
}
|
||||
|
||||
Bacon().eat();
|
7
lox/class_name.lox
Normal file
7
lox/class_name.lox
Normal file
|
@ -0,0 +1,7 @@
|
|||
class DevonshireCream {
|
||||
serveOn() {
|
||||
return "Scones";
|
||||
}
|
||||
}
|
||||
|
||||
print DevonshireCream;
|
10
lox/class_this.lox
Normal file
10
lox/class_this.lox
Normal 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
9
lox/fibonacci_timed.lox
Normal 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
9
lox/init.lox
Normal file
|
@ -0,0 +1,9 @@
|
|||
class Foo {
|
||||
init() {
|
||||
this.name = "test";
|
||||
print this;
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo();
|
||||
print foo.name;
|
8
lox/init_early_return.lox
Normal file
8
lox/init_early_return.lox
Normal file
|
@ -0,0 +1,8 @@
|
|||
class Foo {
|
||||
init() {
|
||||
return;
|
||||
print this;
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo();
|
5
lox/init_return.lox
Normal file
5
lox/init_return.lox
Normal file
|
@ -0,0 +1,5 @@
|
|||
class Foo {
|
||||
init() {
|
||||
return "something else";
|
||||
}
|
||||
}
|
1
lox/self_inheritance.lox
Normal file
1
lox/self_inheritance.lox
Normal file
|
@ -0,0 +1 @@
|
|||
class Oops < Oops {}
|
14
lox/super_call.lox
Normal file
14
lox/super_call.lox
Normal 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();
|
6
lox/super_without_superclass.lox
Normal file
6
lox/super_without_superclass.lox
Normal file
|
@ -0,0 +1,6 @@
|
|||
class Eclair {
|
||||
cook() {
|
||||
super.cook();
|
||||
print "Pipe full of crème pâtissière.";
|
||||
}
|
||||
}
|
9
lox/superclass_method.lox
Normal file
9
lox/superclass_method.lox
Normal file
|
@ -0,0 +1,9 @@
|
|||
class Doughnut {
|
||||
cook() {
|
||||
print "Fry until golden brown.";
|
||||
}
|
||||
}
|
||||
|
||||
class BostonCream < Doughnut {}
|
||||
|
||||
BostonCream().cook();
|
3
lox/superclass_not_class.lox
Normal file
3
lox/superclass_not_class.lox
Normal file
|
@ -0,0 +1,3 @@
|
|||
var NotAClass = "I am totally not a class";
|
||||
|
||||
class Subclass < NotAClass {}
|
Loading…
Add table
Add a link
Reference in a new issue