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

lua5.1 compatibility #206

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LuaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def fileHeader(topClassName: String): Unit = {
outHeader.puts(s"-- $headerComment")
outHeader.puts("--")
outHeader.puts("-- This file is compatible with Lua 5.3")
outHeader.puts("-- This file is compatible with Lua 5.1")
outHeader.puts

importList.add("local class = require(\"class\")")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,27 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
)

override def strLiteralUnicode(code: Char): String =
"\\u{%04x}".format(code.toInt)
decEscapeByteArray(String.valueOf(code).getBytes("UTF-8"))

override def numericBinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr) = {
override def numericBinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr): String = {
(detectType(left), detectType(right), op) match {
case (_: IntType, _: IntType, Ast.operator.Div) =>
s"math.floor(${translate(left)} / ${translate(right)})"
case _ =>
super.numericBinOp(left, op, right)
op match {
case Ast.operator.BitAnd =>
s"bit.band(${translate(left)}, ${translate(right)})"
case Ast.operator.BitOr =>
s"bit.bor(${translate(left)}, ${translate(right)})"
case Ast.operator.BitXor =>
s"bit.bxor(${translate(left)}, ${translate(right)})"
case Ast.operator.LShift =>
s"bit.lshift(${translate(left)}, ${translate(right)})"
case Ast.operator.RShift =>
s"bit.rshift(${translate(left)}, ${translate(right)})"
case _ =>
super.numericBinOp(left, op, right)
}
}
}

Expand All @@ -48,11 +61,11 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
override def doBoolLiteral(n: Boolean): String =
if (n) "true" else "false"
override def doArrayLiteral(t: DataType, value: Seq[Ast.expr]): String =
"{" + value.map((v) => translate(v)).mkString(", ") + "}"
"{" + value.map(v => translate(v)).mkString(", ") + "}"
override def doByteArrayLiteral(arr: Seq[Byte]): String =
"\"" + decEscapeByteArray(arr) + "\""

override def doLocalName(s: String) = s match {
override def doLocalName(s: String): String = s match {
case Identifier.ITERATOR => "_"
case Identifier.INDEX => "i"
case _ => s"self.${doName(s)}"
Expand Down Expand Up @@ -110,7 +123,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
s"string.byte(${translate(a)}, 1)"
override def bytesLast(a: Ast.expr): String = {
val table = translate(a)
s"string.byte(${table}, #${table})"
s"string.byte($table, #$table)"
}
override def bytesMin(a: Ast.expr): String = {
importList.add("local utils = require(\"utils\")")
Expand All @@ -133,7 +146,7 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
s"${translate(a)}[1]"
override def arrayLast(a: Ast.expr): String = {
val table = translate(a)
s"${table}[#${table}]"
s"$table[#$table]"
}
override def arraySize(a: Ast.expr): String =
s"#${translate(a)}"
Expand Down Expand Up @@ -180,5 +193,5 @@ class LuaTranslator(provider: TypeProvider, importList: ImportList) extends Base
* @return array contents decimal-escaped as string
*/
private def decEscapeByteArray(arr: Seq[Byte]): String =
arr.map((x) => "\\%03d".format(x & 0xff)).mkString
arr.map(x => "\\%03d".format(x & 0xff)).mkString
}