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

Implement doByteArrayNonLiteral for Lua and C++ #281

Merged
Merged
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 @@ -854,12 +854,12 @@ class TranslatorSpec extends AnyFunSpec {
describe("to do type enforcement") {
// type enforcement: casting to non-literal byte array
full("[0 + 1, 5].as<bytes>", CalcIntType, CalcBytesType, ResultMap(
CppCompiler -> "???",
CppCompiler -> "std::string({static_cast<char>(0 + 1), static_cast<char>(5)})",
CSharpCompiler -> "new byte[] { 0 + 1, 5 }",
GoCompiler -> "[]uint8{0 + 1, 5}",
JavaCompiler -> "new byte[] { 0 + 1, 5 }",
JavaScriptCompiler -> "new Uint8Array([0 + 1, 5])",
LuaCompiler -> "???",
LuaCompiler -> "string.char(0 + 1, 5)",
PerlCompiler -> "pack('C*', (0 + 1, 5))",
PHPCompiler -> "pack('C*', 0 + 1, 5)",
PythonCompiler -> "struct.pack('2B', 0 + 1, 5)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,23 @@ class CppTranslator(provider: TypeProvider, importListSrc: CppImportList, import
// TODO: C++14
}
} else {
throw new RuntimeException("C++ literal arrays are not implemented yet")
throw new RuntimeException("literal arrays are not yet implemented for C++98 (pass `--cpp-standard 11` to target C++11)")
}
}

override def doByteArrayLiteral(arr: Seq[Byte]): String =
"std::string(\"" + Utils.hexEscapeByteArray(arr) + "\", " + arr.length + ")"
override def doByteArrayNonLiteral(values: Seq[Ast.expr]): String = {
// It is assumed that every expression produces integer in the range [0; 255]
if (config.cppConfig.useListInitializers) {
"std::string({" + values.map(value => s"static_cast<char>(${translate(value)})").mkString(", ") + "})"
} else {
// TODO: We need to produce an expression, but this is only possible using
// initializer lists or variadic templates (if we use a helper function),
// both of which are only available since C++11
throw new RuntimeException("non-literal byte arrays are not yet implemented for C++98 (pass `--cpp-standard 11` to target C++11)")
}
}

override def genericBinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr, extPrec: Int) = {
(detectType(left), detectType(right), op) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ 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(", ") + "}"
override def doByteArrayLiteral(arr: Seq[Byte]): String =
"\"" + decEscapeByteArray(arr) + "\""
override def doByteArrayNonLiteral(values: Seq[Ast.expr]): String =
// It is assumed that every expression produces integer in the range [0; 255]
"string.char(" + values.map(translate).mkString(", ") + ")"

override def doLocalName(s: String) = s match {
case Identifier.ITERATOR => "_"
Expand Down
Loading