Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/luakittens #7

Merged
merged 18 commits into from
Jul 28, 2024
Merged

Feat/luakittens #7

merged 18 commits into from
Jul 28, 2024

Conversation

Dich0tomy
Copy link
Owner

@Dich0tomy Dich0tomy commented Jul 28, 2024

The luakittens library is an attempt to redefine the type system and type validation methods in lua.

For now the plans is for it to consist of several parts:

  • the luaKITTENS annotation language, a type annotation is called kitty for short
  • a parser for the language
  • a type validator

Currently only the language and parser are done and tested, mostly. Several things are lacking for now.

There's no specification yet, to discover the language and it's capabilities one would have to look at the only_parse function and __test() -> describe('only_parse())


This implementation is incomplete and I will be rewriting it to a handwritten parser, which will allow for more fine-grained syntax control and better diagnostics.

Dich0tomy added 15 commits July 24, 2024 22:27
Removed require calls to iter (which referenced the same exact file)
and replaced them with `M.`.
The partitioning function takes a range and splits it to one satisfying
a predicate and one not satisfying a predicate.
This file defines settings for the lua language server.
Namely:
- Instructs luals to find busted test functions
- Instructs luals to find luassert monkey patched assert
- Ignores variables starting with _
By marking the `M.__test` files with `---@package` they're not displayed
in completion from outside.
The luaKITTENS library is an attempt to redefine a type system in lua.
It consists of a parser for parsing luaKITTEN type expressions and a
validator which can then take a type and check if it complies with a
given kitten.

Currently only the parsing part is done and it requires some more
polishing as well.

The parser is implemented in lpeg, but I will rewrite it to a more
sophisticated solution later, which will allow for much better error
diagnostics.
Optional nils - `nil?` don't make sense, as `?` is already a shorthand
of `T|nil`, which would make it `nil|nil`.

I prefer to disallow that in grammar, than sanitize later.
Previously the `parse()` function only returned a single match,
but the grammar is defined to return several matches if the alternative
grammar is used.
The grammar allows for alternative types, e.g. `string|number|(fn,
bool)`. This syntax, however, allows for specifying the same type twice.

I haven't found a way to disable that in the syntax itself, so the
resulting parse is postprocessed in the end to not carry additional,
unnecessary types around.
For now two transformations are being done:
- If `any` is listed as an alternative type, the entire type is turned
  into `any`
- If `nil` is listed as one of only two types, we turn the other type
  into `T?`, the difference here is that now instead of having
  `{ { kind = '...', ... }, { kind = 'fundamental', type = 'nil } }`
  we have
  `{ { kind = '...', opt = '?' ... } }`
  (if you're confused about why `opt` is `?` here instead of `true` -
  it's because the AST comes out formed already out of
  `vim.lpeg.Pattern.match`. The grammar doesn't allowed to specify a
  boolean type there and it doesn't really make sense to rewrite the
  grammar
In lua setting a table key to `nil` means deleting it, which means that
having a type like `{ [T]: nil }` or `{ foo: nil }` doesn't make sense
as per lua semantics.
@Dich0tomy Dich0tomy marked this pull request as draft July 28, 2024 13:49
It's hard to model the resulting AST type using luaCATS annotations as
they are very much incomplete, still. The syntax is also rapidly
changing, so there's no reason to keep around the constantly invalidated
annotations around, for now.
`nil`s as keys for  tables are also prohibited, so we disable them in
the syntax as well :)
@Dich0tomy Dich0tomy marked this pull request as ready for review July 28, 2024 18:43
@Dich0tomy Dich0tomy enabled auto-merge July 28, 2024 18:44
@Dich0tomy Dich0tomy disabled auto-merge July 28, 2024 18:44
@Dich0tomy Dich0tomy merged commit 99b5ae9 into trunk Jul 28, 2024
2 checks passed
@Dich0tomy Dich0tomy deleted the feat/luakittens branch July 28, 2024 18:48
@Dich0tomy Dich0tomy mentioned this pull request Aug 3, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant