Skip to content

Commit

Permalink
Issue #154, #156, #157 : make moonscript compiler work
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Dec 25, 2017
1 parent 3d9c819 commit e37e169
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 14 deletions.
9 changes: 5 additions & 4 deletions _lua5.1-tests/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ assert(io.output():seek("end") == string.len("alo joao"))

assert(io.output():seek("set") == 0)

assert(io.write('"álo"', "{a}\n", "second line\n", "third line \n"))
assert(io.write('çfourth_line'))
assert(io.write('"?lo"', "{a}\n", "second line\n", "third line \n"))
assert(io.write('?fourth_line'))
io.output():close()
io.output(io.stdout)
-- collectgarbage() -- file should be closed by GC
Expand Down Expand Up @@ -82,14 +82,14 @@ do -- test error returns
assert(not a and type(b) == "string" and type(c) == "number")
end
assert(io.read(0) == "") -- not eof
assert(io.read(5, '*l') == '"álo"')
assert(io.read(5, '*l') == '"?lo"')
assert(io.read(0) == "")
assert(io.read() == "second line")
local x = io.input():seek()
assert(io.read() == "third line ")
assert(io.input():seek("set", x))
assert(io.read('*l') == "third line ")
assert(io.read(1) == "ç")
assert(io.read(1) == "?")
assert(io.read(string.len"fourth_line") == "fourth_line")
assert(io.input():seek("cur", -string.len"fourth_line"))
assert(io.read() == "fourth_line")
Expand Down Expand Up @@ -311,3 +311,4 @@ local s = os.date('%S')
io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a))
io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s))
io.write(string.format('%s\n', _VERSION))
io.write(string.format('%s\n', _GOPHER_LUA_VERSION))
7 changes: 6 additions & 1 deletion _vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,12 @@ func init() {
reg.Push(op)
reg.Push(lv)
L.Call(1, 1)
reg.Set(RA, reg.Pop())
ret := reg.Pop()
if ret.Type() == LTNumber {
reg.SetNumber(RA, ret.(LNumber))
} else {
reg.SetNumber(RA, LNumber(0))
}
} else if lv.Type() == LTTable {
reg.SetNumber(RA, LNumber(lv.(*LTable).Len()))
} else {
Expand Down
21 changes: 20 additions & 1 deletion baselib.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
func OpenBase(L *LState) int {
global := L.Get(GlobalsIndex).(*LTable)
L.SetGlobal("_G", global)
L.SetGlobal("_VERSION", LString(PackageName+" "+PackageVersion))
L.SetGlobal("_VERSION", LString(LuaVersion))
L.SetGlobal("_GOPHER_LUA_VERSION", LString(PackageName+" "+PackageVersion))
basemod := L.RegisterModule("_G", baseFuncs)
global.RawSetString("ipairs", L.NewClosure(baseIpairs, L.NewFunction(ipairsaux)))
global.RawSetString("pairs", L.NewClosure(basePairs, L.NewFunction(pairsaux)))
Expand Down Expand Up @@ -50,6 +51,8 @@ var baseFuncs = map[string]LGFunction{
// loadlib
"module": loModule,
"require": loRequire,
// hidden features
"newproxy": baseNewProxy,
}

func baseAssert(L *LState) int {
Expand Down Expand Up @@ -564,4 +567,20 @@ loopbreak:

/* }}} */

/* hidden features {{{ */

func baseNewProxy(L *LState) int {
ud := L.NewUserData()
L.SetTop(1)
if L.Get(1) == LTrue {
L.SetMetatable(ud, L.NewTable())
} else if d, ok := L.Get(1).(*LUserData); ok {
L.SetMetatable(ud, L.GetMetatable(d))
}
L.Push(ud)
return 1
}

/* }}} */

//
3 changes: 2 additions & 1 deletion compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ func compileAssignStmtLeft(context *funcContext, stmt *ast.AssignStmt) (int, []*
case *ast.AttrGetExpr:
ac := &assigncontext{&expcontext{ecTable, regNotDefined, 0}, 0, 0, false, false}
compileExprWithKMVPropagation(context, st.Object, &reg, &ac.ec.reg)
compileExprWithKMVPropagation(context, st.Key, &reg, &ac.keyrk)
ac.keyrk = reg
reg += compileExpr(context, reg, st.Key, ecnone(0))
if _, ok := st.Key.(*ast.StringExpr); ok {
ac.keyks = true
}
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type LNumber float64

const LNumberBit = 64
const LNumberScanFormat = "%f"
const LuaVersion = "Lua 5.1"

var LuaPath = "LUA_PATH"
var LuaLDir string
Expand Down
15 changes: 13 additions & 2 deletions debuglib.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,20 @@ func debugSetUpvalue(L *LState) int {
}

func debugTraceback(L *LState) int {
msg := L.OptString(1, "")
msg := ""
level := L.OptInt(2, 1)
traceback := strings.TrimSpace(L.stackTrace(level))
ls := L
if L.GetTop() > 0 {
if s, ok := L.Get(1).assertString(); ok {
msg = s
}
if l, ok := L.Get(1).(*LState); ok {
ls = l
msg = ""
}
}

traceback := strings.TrimSpace(ls.stackTrace(level))
if len(msg) > 0 {
traceback = fmt.Sprintf("%s\n%s", msg, traceback)
}
Expand Down
2 changes: 1 addition & 1 deletion package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ package lua
const PackageName = "GopherLua"
const PackageVersion = "0.1"
const PackageAuthors = "Yusuke Inuzuka"
const PackageCopyRight = PackageName + " " + PackageVersion + " Copyright (C) 2015 " + PackageAuthors
const PackageCopyRight = PackageName + " " + PackageVersion + " Copyright (C) 2015 -2017 " + PackageAuthors
6 changes: 5 additions & 1 deletion stringlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,11 @@ func strMatch(L *LState) int {
func strRep(L *LState) int {
str := L.CheckString(1)
n := L.CheckInt(2)
L.Push(LString(strings.Repeat(str, n)))
if n < 0 {
L.Push(LString(""))
} else {
L.Push(LString(strings.Repeat(str, n)))
}
return 1
}

Expand Down
3 changes: 3 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (tb *LTable) Len() int {

// Append appends a given LValue to this LTable.
func (tb *LTable) Append(value LValue) {
if value == LNil {
return
}
if tb.array == nil {
tb.array = make([]LValue, 0, defaultArrayCap)
}
Expand Down
2 changes: 1 addition & 1 deletion table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestTableForEach(t *testing.T) {
errorIfNotEqual(t, LNumber(2), value)
case 3:
errorIfNotEqual(t, LNumber(3), value)
case 5:
case 4:
errorIfNotEqual(t, LNumber(5), value)
default:
t.Fail()
Expand Down
6 changes: 5 additions & 1 deletion tablelib.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func tableConcat(L *LState) int {
//TODO should flushing?
retbottom := L.GetTop()
for ; i <= j; i++ {
L.Push(tbl.RawGetInt(i))
v := tbl.RawGetInt(i)
if !LVCanConvToString(v) {
L.RaiseError("invalid value (%s) at index %d in table for concat", v.Type().String(), i)
}
L.Push(v)
if i != j {
L.Push(sep)
}
Expand Down
7 changes: 6 additions & 1 deletion vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,12 @@ func init() {
reg.Push(op)
reg.Push(lv)
L.Call(1, 1)
reg.Set(RA, reg.Pop())
ret := reg.Pop()
if ret.Type() == LTNumber {
reg.SetNumber(RA, ret.(LNumber))
} else {
reg.SetNumber(RA, LNumber(0))
}
} else if lv.Type() == LTTable {
reg.SetNumber(RA, LNumber(lv.(*LTable).Len()))
} else {
Expand Down

0 comments on commit e37e169

Please sign in to comment.