Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Thoughts / Concerns / Questions #697

Closed
vitiral opened this issue Sep 12, 2023 · 5 comments
Closed

Thoughts / Concerns / Questions #697

vitiral opened this issue Sep 12, 2023 · 5 comments

Comments

@vitiral
Copy link

vitiral commented Sep 12, 2023

Hey, I really love the language. I particularly like how it preserves Lua's design and "feel" while addressing the most common issues.

I'm working on some of my own languages (http://civboot.org) and Lua libraries, including a recursive-descent parser library (https://github.com/civboot/pegl) which in a 370 line library file makes expressing Lua's syntax a breeze -- Lua's syntax is expressed in a bit more than 200 lines of code (not quite complete but I think I'm close).

I'm also writing my own "base library" that includes a type system (https://github.com/civboot/civlib/blob/main/lua/civ.lua but don't read the code, I was learning Lua while I wrote it), the basics of which is something like:

-- creates a new type with `__call` overridden to be a "constructor" which calls `setmetatable(t, MyType)`
-- MyType table has a few fields like `#tys` which hold the types of the fields
-- runtime type checking can be enabled/disabled through a global flag (changes which `__call` gets set,
-- so zero runtime performance cost)
MyType = newRecord("MyType", {field1=Num, field2=List(Int)})

-- creating methods is done as normal: it's just a metatable
MyType.myMethod = function(self, a) ... end

I have a few thoughts/suggestions

  • My PEGL library makes writing a recursive descent parser (with decent error messages I think!) much more concise and readable, while the library itself is very lean (300 LoC). I noticed that tl.lua spent about 3000 lines parsing, I think with PEGL it would be more like 300-400 (plus another 300 for the PEGL library itself if you wanted to stay in a single file) -- I currently parse Lua itself in a bit more than 200 lines (although it doesn't quite work yet).

  • Looking at tl.lua I see no way to declare a TL type for lua to use. I'm looking for something like newRecord above. This is confusing to me, since one of the big benefits of including something like TL into your lua project would be the ability to stay with Lua for some things but get the benefits of expressing types (with auto setmetatable and string formatting etc provided)

  • In general I see very few library functions exported by TL. Is there a function for standard record formatting? For record equality? Does TL even use setmetatable?

  • https://github.com/teal-language/tl/blob/master/docs/developing.md seems to indicate that TL is bootstrapping itself. Why? Isn't lua a "good enough" language for writing a compiler in? I have a hard time believing the win is worth the overhead mentioned in that doc.

@lenscas
Copy link
Contributor

lenscas commented Sep 12, 2023

I currently parse Lua itself in a bit more than 200 lines (although it doesn't quite work yet).

Keep in mind that teal has to parse teal code, not lua code (Or maybe it needs to do both? Forgot) so that would add some of the extra lines. In addition, you would be surprised how many extra lines some of the finer details can bring. So depending on where your parser exactly is I don't find it that big of a surpise that the teal one is so much bigger than yours.

Looking at tl.lua I see no way to declare a TL type for lua to use. I'm looking for something like newRecord above. This is confusing to me, since one of the big benefits of including something like TL into your lua project would be the ability to stay with Lua for some things but get the benefits of expressing types (with auto setmetatable and string formatting etc provided)

I am not entirely sure I understand what you exactly want... Maybe .d.tl files are what you are looking for? Though that sounds like it goes the wrong way of what you want.

In general I see very few library functions exported by TL. Is there a function for standard record formatting? For record equality? Does TL even use setmetatable?

The API exposed by teal is for the time being very limited on purpose as it would be way too unstable. Also, formatting teal code isn't something that teal should be doing anyway (I think?)

https://github.com/teal-language/tl/blob/master/docs/developing.md seems to indicate that TL is bootstrapping itself. Why? Isn't lua a "good enough" language for writing a compiler in? I have a hard time believing the win is worth the overhead mentioned in that doc.

Most languages bootstrap themselves. It is a good way to test the compiler. It also helped the maintainer already by finding type errors. IIRC they mentioned how rewriting from lua to teal fixed many bugs in the compiler due to the better typesafety and how only nil related problems are left when it comes to stuff that can be checked at compile time.

@vitiral
Copy link
Author

vitiral commented Sep 12, 2023

formatting teal code isn't something that teal should be doing anyway

Sorry, I meant formatting teal records (like assert(tostring(myPoint) == "Point{x=42, y=7}")

I'll respond to the other points when I get an example of what I want published to luarocks :D

@vitiral
Copy link
Author

vitiral commented Sep 12, 2023

I am not entirely sure I understand what you exactly want... Maybe .d.tl files are what you are looking for? Though that sounds like it goes the wrong way of what you want.

This is what I want: https://github.com/civboot/metaty

Lua already has a powerful (runtime) type system, it's just annoying to express types in it. IMO it would be good to build on the system that exists and have a language+syntax (like teal) that compiles to a Lua-native type library.

If teal included something like metaty then it could just compile records directly to metaty calls. Of course, it would still do it's normal type checking/etc statically, but at runtime its types would just be metaty values.

... formatting

FYI metaty has cleaner formatting of Lua values (not just records but also raw tables), a huge pain point of Lua for me personally.

Most languages bootstrap themselves (...)

I think in a lot of languages it makes sense, however when the language is just a transpiler and the language it's trans-piling to is such a powerful and expressive language I think it makes less sense.

What if instead of teal being built in teal it was built in lua with type libraries (like metaty)?

It's y'alls project so it's your decision, I just wanted to push back on that point.

Feel free to close if you like and thanks for the discussion so far!

@vitiral
Copy link
Author

vitiral commented Sep 13, 2023

I moved the project to https://github.com/civboot/civlua/blob/main/metaty/README.md

I've added function types as well (only runtime checking right now).

@hishamhm
Copy link
Member

Hi! Thanks for the feedback. @lenscas's original reply already covered a lot of ground, so I'll just go through your original message and add some finer points from the author's perspective.

I noticed that tl.lua spent about 3000 lines parsing

Not only parsing but also building the AST. It also keeps the project self-contained with no dependencies (otherwise I'd be using LPeg or something). I took a look at your library; it looks cool, but since it's a grammar interpreter, I don't think performance would be on par with the handwritten recursive descent parser, and our current performance isn't ideal. I probably could make the parser shorter with a metaprogramming-based generator, but then code would be dynamic and I wouldn't have the Teal type-checker to help me write it, and I find the current parser code easy to maintain.

I'm looking for something like newRecord above

Teal doesn't provide a, let's call it, "metatable-use-pattern" model of its own (to not call it an object model). Everyone has different opinions on how one of those should look like; for discussions with links to further issues, start here: #97.

Isn't lua a "good enough" language for writing a compiler in?

Well, you like having static types when coding, why can't I have them too? 😁

On a more serious note, Lua is a scripting language; I have written entire applications in it, and it is on the back of that experience that I decided to create Teal — to make a Lua-like application-development language, first and foremost, for my own use! Plus everything @lenscas said. :)

@teal-language teal-language locked and limited conversation to collaborators Sep 13, 2023
@hishamhm hishamhm converted this issue into discussion #698 Sep 13, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants