teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.03k stars 101 forks source link

Thoughts / Concerns / Questions #697

Closed vitiral closed 9 months ago

vitiral commented 10 months ago

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

lenscas commented 10 months ago

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 commented 10 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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? :grin:

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. :)