teal-language / tl

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

Fix Lua 5.1 error (load -> loadstring) #522

Closed Koeng101 closed 2 years ago

Koeng101 commented 2 years ago

I was attempting to run teal within gopher-lua - https://github.com/yuin/gopher-lua - and was running into a few odd errors. I was running the following scripts:

//main.go
package main

import (
        "github.com/yuin/gopher-lua"
)

var luaString string = `
tl = require("tl")
tl.loader()
addsub = require("addsub")
print (addsub.add(10, 20))
`

func main() {
        L := lua.NewState()
        defer L.Close()
        if err := L.DoString(luaString); err != nil {
                panic(err)
        }

}
--addsub.tl
local addsub = {}

function addsub.add(a: number, b: number): number
   return a + b
end

function addsub.sub(a: number, b: number): number
   return a - b
end

return addsub

The code block i, node.decltype = i, decltype would cause a bubbling up error (since all the lua types were checked by Go) where i would occasionally be set to "decltype" (I have no idea why). The second edit is just a little check for 5.1, since Lua 5.1 doesn't have load, I added in loadstring if it detects that version. These 2 edits let this script run properly so that I can run teal in Golang, which is pretty neat.

Happy to fix up formatting or anything else - tonight is the first time I've ever used lua, so I'm not sure about conventions here.

Note: on my computer, not all tests pass. Made issue #521 about that. All the tests that pass in master repo also pass in this updated repo.

Koeng101 commented 2 years ago

Simplifying this PR a little bit.