std/ tree, use (import) and class syntax
- 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
This commit is contained in:
1 parent
3c20496c78
commit
ef305e7824
19 files changed
+370
-9
No files matched your search
@@ -0,0 +1,32 @@
|
||||
-- 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))
|
||||
Reference in new issue
Block a user