catscript: núcleo C, stdlib (TOML), família de arquivos e front-end self-hosted
- Modelo de objetos Cat_Value + vtable (interfaces abstratas) - VM de bytecode, sem GIL (VM por-thread, nativas como valores) - Stdlib com TOML embutido (toml_parse/toml_encode) - Família de arquivos: AbstractFile (abstrata), File::open, StringIO, BytesIO - Front-end (lexer/parser/checker/compiler) implementado em catscript - cpk (cat package manager) como primeiro programa; manifest package.toml - Type hints advisory, inclusive DynSized() e sentinel_sized<...> - 25/25 testes passando (make test)
This commit is contained in:
commit
52d118211f
69 files changed
+6938
No files matched your search
@@ -0,0 +1,14 @@
|
||||
-- examples/arith.cat -- arithmetic over int, float and byte.
|
||||
-- Type hints (`: int`) are advisory: catscript is structurally typed.
|
||||
|
||||
local a : int = 10
|
||||
local b : int = 3
|
||||
print("a + b =", a + b) -- 13
|
||||
print("a - b =", a - b) -- 7
|
||||
print("a * b =", a * b) -- 30
|
||||
print("a / b =", a / b) -- 3.33333 (division yields float)
|
||||
print("a % b =", a % b) -- 1
|
||||
|
||||
local x : float = 1.5
|
||||
print("x + b =", x + b) -- 4.5
|
||||
print("x * 2 =", x * 2) -- 3
|
||||
@@ -0,0 +1,29 @@
|
||||
-- examples/control.cat -- if/elseif/else, while and numeric for.
|
||||
|
||||
function classify(n)
|
||||
if n < 0 then
|
||||
return "negative"
|
||||
elseif n == 0 then
|
||||
return "zero"
|
||||
else
|
||||
return "positive"
|
||||
end
|
||||
end
|
||||
|
||||
print(classify(-5)) -- negative
|
||||
print(classify(0)) -- zero
|
||||
print(classify(42)) -- positive
|
||||
|
||||
local sum = 0
|
||||
local i = 1
|
||||
while i <= 5 do
|
||||
sum = sum + i
|
||||
i = i + 1
|
||||
end
|
||||
print("sum 1..5 =", sum) -- 15
|
||||
|
||||
local acc = 0
|
||||
for j = 1, 4 do
|
||||
acc = acc + j
|
||||
end
|
||||
print("sum 1..4 =", acc) -- 10
|
||||
@@ -0,0 +1,54 @@
|
||||
-- examples/cpk.cat -- cpk, the Cat Package Manager.
|
||||
--
|
||||
-- This is the first real program written in catscript. cpk reads a
|
||||
-- package's `package.toml` manifest (every package has one) using the
|
||||
-- standard library's TOML parser. Commands:
|
||||
--
|
||||
-- cpk show [file.toml] -- parse and print a manifest (default: package.toml)
|
||||
-- cpk new <name> -- print a starter manifest
|
||||
--
|
||||
-- Run: catscript examples/cpk.cat show examples/package.toml
|
||||
|
||||
function cpk_show(path)
|
||||
local text = read_file(path)
|
||||
if text == nil then
|
||||
print("cpk: cannot read " .. path)
|
||||
return 1
|
||||
end
|
||||
local pkg = toml_parse(text)
|
||||
print("package: " .. tostring(pkg.name))
|
||||
print("version: " .. tostring(pkg.version))
|
||||
print("author: " .. tostring(pkg.author))
|
||||
return 0
|
||||
end
|
||||
|
||||
function cpk_new(name)
|
||||
print("# package.toml for " .. name)
|
||||
print("name = \"" .. name .. "\"")
|
||||
print("version = \"0.1.0\"")
|
||||
print("author = \"\"")
|
||||
return 0
|
||||
end
|
||||
|
||||
function cpk_usage()
|
||||
print("cpk -- cat package manager")
|
||||
print("usage: cpk show [file.toml] | cpk new <name>")
|
||||
return 0
|
||||
end
|
||||
|
||||
function cpk_main(argv)
|
||||
local cmd = argv[1]
|
||||
if cmd == "show" then
|
||||
local path = argv[2]
|
||||
if path == nil then
|
||||
path = "package.toml"
|
||||
end
|
||||
return cpk_show(path)
|
||||
elseif cmd == "new" then
|
||||
return cpk_new(argv[2])
|
||||
else
|
||||
return cpk_usage()
|
||||
end
|
||||
end
|
||||
|
||||
cpk_main({ "show", "examples/package.toml" })
|
||||
@@ -0,0 +1,31 @@
|
||||
-- examples/files.cat -- the file family and classmethods.
|
||||
--
|
||||
-- AbstractFile is the abstract base; File, StringIO and BytesIO share
|
||||
-- its method set (read/write/close), so they are interchangeable.
|
||||
-- `File::open(path)` is a classmethod producing a File value.
|
||||
|
||||
-- in-memory text stream
|
||||
local s = StringIO("hello, catscript")
|
||||
print("stringio:", s:read())
|
||||
|
||||
-- in-memory byte stream, written then read back
|
||||
local b = BytesIO()
|
||||
b:write("bytes ")
|
||||
b:write("io")
|
||||
print("bytesio:", b:read())
|
||||
|
||||
-- a real file, opened with the File::open classmethod
|
||||
local path = "/tmp/catscript_demo.txt"
|
||||
local w = File::open(path, "w")
|
||||
w:write("line one\n")
|
||||
w:close()
|
||||
|
||||
local r = File::open(path, "r")
|
||||
print("file:", r:read())
|
||||
r:close()
|
||||
|
||||
-- type hints: advisory, including DynSized() and sentinel_sized
|
||||
function describe(n : cint, buf : cbyte[sentinel_sized<0>]) -> ascii_text
|
||||
return "n=" .. tostring(n) .. " buf=" .. tostring(buf)
|
||||
end
|
||||
print(describe(3, "abc"))
|
||||
@@ -0,0 +1,5 @@
|
||||
-- examples/hello.cat -- the smallest catscript program.
|
||||
-- Run: catscript examples/hello.cat
|
||||
|
||||
print("hello from catscript")
|
||||
print("2 + 3 =", 2 + 3)
|
||||
@@ -0,0 +1,18 @@
|
||||
-- examples/oop.cat -- tables as objects, with methods.
|
||||
-- catscript is 100% object-oriented: a method is just a function value
|
||||
-- stored in a table, called with `:`.
|
||||
|
||||
local counter = {
|
||||
value = 0,
|
||||
increment = function(self, by)
|
||||
self.value = self.value + by
|
||||
return self.value
|
||||
end,
|
||||
get = function(self)
|
||||
return self.value
|
||||
end
|
||||
}
|
||||
|
||||
counter:increment(1)
|
||||
counter:increment(2)
|
||||
print("counter:", counter:get()) -- 3
|
||||
@@ -0,0 +1,9 @@
|
||||
# package.toml -- manifest for a catscript package.
|
||||
# This is the file cpk reads: every package has a `package.toml`.
|
||||
name = "hello-world"
|
||||
version = "0.1.0"
|
||||
author = "Minus"
|
||||
description = "an example catscript package"
|
||||
|
||||
[dependencies]
|
||||
toml = "1.0.0"
|
||||
@@ -0,0 +1,10 @@
|
||||
-- examples/text.cat -- text values, concatenation and `length`.
|
||||
-- ascii_text / utf8_text are Cat_Text; `#x` reads the length attribute.
|
||||
|
||||
local name : ascii_text = "cat"
|
||||
local greeting = "hello, " .. name
|
||||
print(greeting) -- hello, cat
|
||||
print("length:", #greeting) -- 11
|
||||
|
||||
local u : utf8_text = "café"
|
||||
print("utf8 length:", u.length) -- 4 code points
|
||||
@@ -0,0 +1,7 @@
|
||||
-- examples/toml.cat -- the standard library TOML parser in action.
|
||||
|
||||
local doc = toml_parse("title = \"catscript\"\nversion = 1\npi = 3.14\n")
|
||||
|
||||
print("title:", doc.title)
|
||||
print("version:", doc.version)
|
||||
print("pi:", doc.pi)
|
||||
Reference in new issue
Block a user