selectors: a.b sends with self, a::b without; drop a:b
The `.` and `::` selectors swap meaning and `a:b` is removed, so every access is one of: a.b read the member a.b(args) call the member WITH a as self a::b static read (no self) a::b(args) call the member WITHOUT self - compiler: postfix `.` emits CALLMETHOD (with self) when followed by `(`, else GETATTR; `::` emits GETATTR + CALL (no self); the `:` form is gone (with a helpful error suggesting `.`/`::`). - `:` remains only for type hints (local x : int), unchanged. - ffi: `lib.func(...)` (with self) and `ffi::load(...)` (no self); the native loaders no longer skip a receiver. - file: `File::open` is static, so cm_open reads args[0]/args[1]. - lexer: fix cat_lexer_kind_name, whose table had drifted from the enum (missing `nil`), mislabelling tokens. - self-hosted parser (std/lang/parser.cat): `.name(` -> method (self), `::name(` -> call (no self); compiler/checker unchanged. - tools/migrate_selectors.py: one-pass migration of the call forms. - regenerated std/glfw and std/vulkan; migrated examples/tests/std and the README; new tests/selectors.cat (130 green). Verified live: GLFW window + Vulkan device still work; `a:b` now errors.
This commit is contained in:
1 parent
04009c3c5e
commit
6f6f444beb
35 files changed
+1114
-964
No files matched your search
+10
-10
@@ -12,37 +12,37 @@ print("bad load empty:", bad == empty)
|
||||
print("bad load type:", type(bad))
|
||||
|
||||
-- symbol binding
|
||||
local pow = m:func("pow", "double(double,double)")
|
||||
local pow = m.func("pow", "double(double,double)")
|
||||
print("pow bound:", pow ~= empty)
|
||||
print("missing sym empty:", m:func("no_such_symbol_xyz", "int()") == empty)
|
||||
print("missing sym empty:", m.func("no_such_symbol_xyz", "int()") == empty)
|
||||
|
||||
-- invalid signature -> empty
|
||||
print("bad sig empty:", m:func("pow", "not a signature") == empty)
|
||||
print("bad rettype empty:", m:func("pow", "nope(double)") == empty)
|
||||
print("bad sig empty:", m.func("pow", "not a signature") == empty)
|
||||
print("bad rettype empty:", m.func("pow", "nope(double)") == empty)
|
||||
|
||||
-- return conversions
|
||||
print("pow 2,10:", pow(2, 10))
|
||||
print("pow 3,4:", pow(3, 4))
|
||||
local floor = m:func("floor", "double(double)")
|
||||
local floor = m.func("floor", "double(double)")
|
||||
print("floor 3.7:", floor(3.7))
|
||||
local sqrt = m:func("sqrt", "double(double)")
|
||||
local sqrt = m.func("sqrt", "double(double)")
|
||||
print("sqrt 144:", sqrt(144))
|
||||
|
||||
-- int return
|
||||
local abs = c:func("abs", "int(int)")
|
||||
local abs = c.func("abs", "int(int)")
|
||||
print("abs -42:", abs(-42))
|
||||
|
||||
-- long + string arg
|
||||
local strlen = c:func("strlen", "long(string)")
|
||||
local strlen = c.func("strlen", "long(string)")
|
||||
print("strlen abc:", strlen("abc"))
|
||||
print("strlen empty:", strlen(""))
|
||||
|
||||
-- void return -> empty
|
||||
local srand = c:func("srand", "void(uint32)")
|
||||
local srand = c.func("srand", "void(uint32)")
|
||||
print("void ret empty:", srand(1) == empty)
|
||||
|
||||
-- pointer-ish spelling accepted
|
||||
local strchr = c:func("strchr", "string(string,int)")
|
||||
local strchr = c.func("strchr", "string(string,int)")
|
||||
print("strchr bound:", strchr ~= empty)
|
||||
|
||||
-- library is callable like :func
|
||||
|
||||
@@ -22,7 +22,7 @@ end
|
||||
Calc.add._doc_ = "add(a, b) on Calc."
|
||||
print("method doc:", Calc.add._doc_)
|
||||
local c = Calc()
|
||||
print("method call:", c:add(4, 5))
|
||||
print("method call:", c.add(4, 5))
|
||||
|
||||
-- help() reads a function's _doc_
|
||||
help(add)
|
||||
+11
-11
@@ -7,26 +7,26 @@
|
||||
use glfw
|
||||
|
||||
local version = memory(12)
|
||||
glfw.get_version(version, version:ptr() + 4, version:ptr() + 8)
|
||||
print("glfw:", version:u32(0), version:u32(4), version:u32(8))
|
||||
glfw::get_version(version, version.ptr() + 4, version.ptr() + 8)
|
||||
print("glfw:", version.u32(0), version.u32(4), version.u32(8))
|
||||
|
||||
local ok = glfw.init()
|
||||
local ok = glfw::init()
|
||||
print("init:", ok)
|
||||
|
||||
-- a hidden window with no client API (for Vulkan surfaces)
|
||||
glfw.window_hint(glfw.visible, glfw.false)
|
||||
glfw.window_hint(0x00022001, 0) -- CLIENT_API = NO_API
|
||||
local win = glfw.create_window(320, 240, "cat", 0, 0)
|
||||
glfw::window_hint(glfw.visible, glfw.false)
|
||||
glfw::window_hint(0x00022001, 0) -- CLIENT_API = NO_API
|
||||
local win = glfw::create_window(320, 240, "cat", 0, 0)
|
||||
print("window:", win > 0)
|
||||
|
||||
if win > 0 then
|
||||
local fw = memory(8)
|
||||
local fh = memory(8)
|
||||
glfw.get_framebuffer_size(win, fw, fh)
|
||||
print("framebuffer:", fw:u32(0), fh:u32(0))
|
||||
print("should close:", glfw.window_should_close(win) == glfw.true)
|
||||
glfw.destroy_window(win)
|
||||
glfw::get_framebuffer_size(win, fw, fh)
|
||||
print("framebuffer:", fw.u32(0), fh.u32(0))
|
||||
print("should close:", glfw::window_should_close(win) == glfw.true)
|
||||
glfw::destroy_window(win)
|
||||
end
|
||||
|
||||
glfw.terminate()
|
||||
glfw::terminate()
|
||||
print("done")
|
||||
+16
-16
@@ -5,35 +5,35 @@
|
||||
-- corrupting memory.
|
||||
|
||||
local m = memory(16)
|
||||
m:set_u32(0, 1234)
|
||||
print("u32:", m:u32(0))
|
||||
m:set_u64(8, 9999999999)
|
||||
print("u64:", m:u64(8))
|
||||
m:set_text(4, "hi")
|
||||
print("text:", m:text(4))
|
||||
print("size:", m:size())
|
||||
m:set_f32(0, 3.5)
|
||||
print("f32:", m:f32(0))
|
||||
m:set_f64(8, 2.5)
|
||||
print("f64:", m:f64(8))
|
||||
m.set_u32(0, 1234)
|
||||
print("u32:", m.u32(0))
|
||||
m.set_u64(8, 9999999999)
|
||||
print("u64:", m.u64(8))
|
||||
m.set_text(4, "hi")
|
||||
print("text:", m.text(4))
|
||||
print("size:", m.size())
|
||||
m.set_f32(0, 3.5)
|
||||
print("f32:", m.f32(0))
|
||||
m.set_f64(8, 2.5)
|
||||
print("f64:", m.f64(8))
|
||||
|
||||
-- zeroed on allocation
|
||||
local z = memory(4)
|
||||
print("zero:", z:u32(0))
|
||||
print("zero:", z.u32(0))
|
||||
|
||||
-- a freed-pointer-style address is available for FFI calls
|
||||
local p = m:ptr()
|
||||
local p = m.ptr()
|
||||
print("ptr:", p > 0)
|
||||
|
||||
-- fill and re-read
|
||||
local f = memory(8)
|
||||
f:fill(0xAB)
|
||||
print("fill:", f:u8(0))
|
||||
f.fill(0xAB)
|
||||
print("fill:", f.u8(0))
|
||||
|
||||
-- out-of-bounds raises (caught here)
|
||||
try
|
||||
local small = memory(2)
|
||||
small:u32(0)
|
||||
small.u32(0)
|
||||
print("no raise")
|
||||
except e
|
||||
print("caught oob")
|
||||
|
||||
@@ -174,6 +174,15 @@ check "repr-list" "list: [a, b, _repr_]" "`echo "$out" | sed -
|
||||
out=`printf 'x = 50\nx\nx + 5\n' | $BIN | tr '>' '\n' | grep -oE '[0-9]+'`
|
||||
check "repl-echo" "50" "`echo "$out" | sed -n 1p`"
|
||||
check "repl-state" "55" "`echo "$out" | sed -n 2p`"
|
||||
# ---- selectors: a.b (with self) / a::b (no self) ----------------------
|
||||
|
||||
out=`$BIN tests/selectors.cat`
|
||||
check "sel-self" "with self: 7" "`echo "$out" | sed -n 1p`"
|
||||
check "sel-field" "field x: 10" "`echo "$out" | sed -n 2p`"
|
||||
check "sel-static" "static: 42" "`echo "$out" | sed -n 3p`"
|
||||
check "sel-method" "self=obj" "`echo "$out" | sed -n 4p`"
|
||||
check "sel-noself" "static no self: 5" "`echo "$out" | sed -n 5p`"
|
||||
|
||||
# ---- GPU bindings (GLFW + Vulkan) ------------------------------------
|
||||
|
||||
if command -v xvfb-run >/dev/null 2>&1 && [ -f vendor/lib/libglfw.so.3 ]; then
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
-- tests/selectors.cat -- the a.b / a::b selector syntax.
|
||||
--
|
||||
-- a.b -- read the member `b` of `a`
|
||||
-- a.b(args) -- call member `b` WITH `a` as self
|
||||
-- a::b -- static read (no self)
|
||||
-- a::b(args) -- call `b` WITHOUT self
|
||||
--
|
||||
-- `a:b` is gone.
|
||||
|
||||
local counter = { n = 0 }
|
||||
counter.bump = function(self, by)
|
||||
self.n = self.n + by
|
||||
return self.n
|
||||
end
|
||||
|
||||
counter.bump(3)
|
||||
counter.bump(4)
|
||||
print("with self:", counter.n)
|
||||
|
||||
-- a plain table field
|
||||
local point = { x = 10, y = 20 }
|
||||
print("field x:", point.x)
|
||||
|
||||
-- static access: no receiver is passed
|
||||
local util = { twice = function(x) return x * 2 end }
|
||||
print("static:", util::twice(21))
|
||||
|
||||
-- method vs static on the same member
|
||||
local obj = { val = "obj" }
|
||||
obj.who = function(self) return "self=" .. self.val end
|
||||
print(obj.who())
|
||||
|
||||
-- static form passes no receiver: the first arg is the real first arg
|
||||
local math2 = { add = function(a, b) return a + b end }
|
||||
print("static no self:", math2::add(2, 3))
|
||||
+10
-10
@@ -8,23 +8,23 @@ use vulkan
|
||||
|
||||
-- VkApplicationInfo
|
||||
local ai = memory(vulkan.size_vk_application_info)
|
||||
ai:fill(0)
|
||||
ai:set_u32(0, vulkan.structure_type_application_info)
|
||||
ai:set_u32(24, 0x00400000) -- apiVersion 1.0.0
|
||||
ai.fill(0)
|
||||
ai.set_u32(0, vulkan.structure_type_application_info)
|
||||
ai.set_u32(24, 0x00400000) -- apiVersion 1.0.0
|
||||
|
||||
-- VkInstanceCreateInfo
|
||||
local ci = memory(vulkan.size_vk_instance_create_info)
|
||||
ci:fill(0)
|
||||
ci:set_u32(0, vulkan.structure_type_instance_create_info)
|
||||
ci:set_u64(24, ai:ptr()) -- pApplicationInfo
|
||||
ci.fill(0)
|
||||
ci.set_u32(0, vulkan.structure_type_instance_create_info)
|
||||
ci.set_u64(24, ai.ptr()) -- pApplicationInfo
|
||||
|
||||
local inst = memory(8)
|
||||
local r = vulkan.create_instance(ci, 0, inst)
|
||||
local r = vulkan::create_instance(ci, 0, inst)
|
||||
print("create_instance:", r == vulkan.success)
|
||||
|
||||
local n = memory(4)
|
||||
vulkan.enumerate_physical_devices(inst:u64(0), n, 0)
|
||||
print("devices > 0:", n:u32(0) > 0)
|
||||
vulkan::enumerate_physical_devices(inst.u64(0), n, 0)
|
||||
print("devices > 0:", n.u32(0) > 0)
|
||||
|
||||
vulkan.destroy_instance(inst:u64(0), 0)
|
||||
vulkan::destroy_instance(inst.u64(0), 0)
|
||||
print("done")
|
||||
Reference in new issue
Block a user