- std/ layout: std/lang/{lexer,parser,checker,compiler}.cat moved from src/catscript
- std/io/io.cat, std/fs/fs.cat, std/math/math.cat modules
- use "path.cat": import another unit (compile+run in the same VM)
- class NAME [extends BASE] ... end: methods, inheritance via __index,
default new constructor (runs optional init), tables are callable
- examples/classes.cat, tests/std.cat; 48/48 tests
33 lines
652 B
Plaintext
33 lines
652 B
Plaintext
-- examples/classes.cat -- the `class` syntax and `use` (import).
|
|
|
|
class Animal
|
|
function init(self, name)
|
|
self.name = name
|
|
end
|
|
function speak(self)
|
|
return self.name .. " makes a sound"
|
|
end
|
|
function get_name(self)
|
|
return self.name
|
|
end
|
|
end
|
|
|
|
class Dog extends Animal
|
|
function speak(self)
|
|
return self.name .. " barks"
|
|
end
|
|
end
|
|
|
|
local a = Animal("cat")
|
|
print(a:speak())
|
|
print(a:get_name())
|
|
|
|
-- subclassing: Dog inherits init/get_name, overrides speak
|
|
local d = Dog("Rex")
|
|
print(d:speak())
|
|
print(d:get_name())
|
|
|
|
-- use: import another catscript file (its definitions become global here)
|
|
use "std/math/math.cat"
|
|
print("double 21 =", double(21))
|