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

Started Objective C compiler #163

Open
wants to merge 20 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 @@ -65,6 +65,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "2 < 3 ? \"foo\" : \"bar\"",
JavaScriptCompiler -> "2 < 3 ? \"foo\" : \"bar\"",
LuaCompiler -> "2 < 3 and \"foo\" or \"bar\"",
ObjcCompiler -> "2 < 3 ? \"foo\" : \"bar\"",
PerlCompiler -> "2 < 3 ? \"foo\" : \"bar\"",
PHPCompiler -> "2 < 3 ? \"foo\" : \"bar\"",
PythonCompiler -> "u\"foo\" if 2 < 3 else u\"bar\"",
Expand Down Expand Up @@ -97,6 +98,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "true",
JavaScriptCompiler -> "true",
LuaCompiler -> "true",
ObjcCompiler -> "YES",
PerlCompiler -> "1",
PHPCompiler -> "true",
PythonCompiler -> "True",
Expand All @@ -110,6 +112,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "false",
JavaScriptCompiler -> "false",
LuaCompiler -> "false",
ObjcCompiler -> "NO",
PerlCompiler -> "0",
PHPCompiler -> "false",
PythonCompiler -> "False",
Expand All @@ -127,6 +130,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "(someBool() ? 1 : 0)",
JavaScriptCompiler -> "(this.someBool | 0)",
LuaCompiler -> "self.some_bool and 1 or 0",
ObjcCompiler -> "(some_bool ? 1 : 0)",
PerlCompiler -> "$self->some_bool()",
PHPCompiler -> "intval($this->someBool())",
PythonCompiler -> "int(self.some_bool)",
Expand All @@ -141,6 +145,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "fooStr()",
JavaScriptCompiler -> "this.fooStr",
LuaCompiler -> "self.foo_str",
ObjcCompiler -> "self.foo_str",
PerlCompiler -> "$self->foo_str()",
PHPCompiler -> "$this->fooStr()",
PythonCompiler -> "self.foo_str",
Expand All @@ -154,6 +159,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "fooBlock()",
JavaScriptCompiler -> "this.fooBlock",
LuaCompiler -> "self.foo_block",
ObjcCompiler -> "self.foo_block",
PerlCompiler -> "$self->foo_block()",
PHPCompiler -> "$this->fooBlock()",
PythonCompiler -> "self.foo_block",
Expand All @@ -167,6 +173,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "foo().bar()",
JavaScriptCompiler -> "this.foo.bar",
LuaCompiler -> "self.foo.bar",
ObjcCompiler -> "self.foo.bar",
PerlCompiler -> "$self->foo()->bar()",
PHPCompiler -> "$this->foo()->bar()",
PythonCompiler -> "self.foo.bar",
Expand All @@ -180,6 +187,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "foo().inner().baz()",
JavaScriptCompiler -> "this.foo.inner.baz",
LuaCompiler -> "self.foo.inner.baz",
ObjcCompiler -> "self.foo.inner.baz",
PerlCompiler -> "$self->foo()->inner()->baz()",
PHPCompiler -> "$this->foo()->inner()->baz()",
PythonCompiler -> "self.foo.inner.baz",
Expand All @@ -193,6 +201,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "_root.foo()",
JavaScriptCompiler -> "this._root.foo",
LuaCompiler -> "self._root.foo",
ObjcCompiler -> "self._root.foo",
PerlCompiler -> "$self->_root()->foo()",
PHPCompiler -> "$this->_root()->foo()",
PythonCompiler -> "self._root.foo",
Expand All @@ -206,6 +215,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "a() != 2 && a() != 5",
JavaScriptCompiler -> "this.a != 2 && this.a != 5",
LuaCompiler -> "self.a ~= 2 and self.a ~= 5",
ObjcCompiler -> "a != 2 && a != 5",
PerlCompiler -> "$self->a() != 2 && $self->a() != 5",
PHPCompiler -> "$this->a() != 2 && $this->a() != 5",
PythonCompiler -> "self.a != 2 and self.a != 5",
Expand All @@ -219,6 +229,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new ArrayList<Integer>(Arrays.asList(0, 1, 100500))",
JavaScriptCompiler -> "[0, 1, 100500]",
LuaCompiler -> "{0, 1, 100500}",
ObjcCompiler -> "@[@(0), @(1), @(100500)]",
PerlCompiler -> "(0, 1, 100500)",
PHPCompiler -> "[0, 1, 100500]",
PythonCompiler -> "[0, 1, 100500]",
Expand All @@ -232,6 +243,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new byte[] { 34, 0, 10, 64, 65, 66, 92 }",
JavaScriptCompiler -> "[34, 0, 10, 64, 65, 66, 92]",
LuaCompiler -> "\"\\034\\000\\010\\064\\065\\066\\092\"",
ObjcCompiler -> "[NSData dataWithBytes:\"\\x22\\x00\\x0A\\x40\\x41\\x42\\x5C\" length:7]",
PerlCompiler -> "pack('C*', (34, 0, 10, 64, 65, 66, 92))",
PHPCompiler -> "\"\\x22\\x00\\x0A\\x40\\x41\\x42\\x5C\"",
PythonCompiler -> "b\"\\x22\\x00\\x0A\\x40\\x41\\x42\\x5C\"",
Expand All @@ -245,6 +257,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new byte[] { -1, 0, -1 }",
JavaScriptCompiler -> "[255, 0, 255]",
LuaCompiler -> "\"\\255\\000\\255\"",
ObjcCompiler -> "[NSData dataWithBytes:\"\\xFF\\x00\\xFF\" length:3]",
PerlCompiler -> "pack('C*', (255, 0, 255))",
PHPCompiler -> "\"\\xFF\\x00\\xFF\"",
PythonCompiler -> "b\"\\255\\000\\255\"",
Expand All @@ -256,6 +269,7 @@ class TranslatorSpec extends FunSuite {
GoCompiler -> "len(\"\\x00\\x01\\x02\")",
JavaCompiler -> "new byte[] { 0, 1, 2 }.length",
LuaCompiler -> "string.len(\"str\")",
ObjcCompiler -> "[NSData dataWithBytes:\"\\x00\\x01\\x02\" length:3].length",
PerlCompiler -> "length(pack('C*', (0, 1, 2)))",
PHPCompiler -> "strlen(\"\\x00\\x01\\x02\")",
PythonCompiler -> "len(b\"\\x00\\x01\\x02\")",
Expand All @@ -269,6 +283,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "a().get((int) 42)",
JavaScriptCompiler -> "this.a[42]",
LuaCompiler -> "self.a[43]",
ObjcCompiler -> "a[42],
PHPCompiler -> "$this->a()[42]",
PythonCompiler -> "self.a[42]",
RubyCompiler -> "a[42]"
Expand All @@ -281,6 +296,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "a().get((42 - 2))",
JavaScriptCompiler -> "this.a[(42 - 2)]",
LuaCompiler -> "self.a[(43 - 2)]",
ObjcCompiler -> "a[(42 - 2)]",
PHPCompiler -> "$this->a()[(42 - 2)]",
PythonCompiler -> "self.a[(42 - 2)]",
RubyCompiler -> "a[(42 - 2)]"
Expand All @@ -293,6 +309,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "a().get(0)",
JavaScriptCompiler -> "this.a[0]",
LuaCompiler -> "self.a[1]",
ObjcCompiler -> "a[0]",
PHPCompiler -> "$this->a()[0]",
PythonCompiler -> "self.a[0]",
RubyCompiler -> "a.first"
Expand All @@ -305,6 +322,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "a().get(a().size() - 1)",
JavaScriptCompiler -> "this.a[this.a.length - 1]",
LuaCompiler -> "self.a[#self.a]",
ObjcCompiler -> "[a lastObject]",
PHPCompiler -> "$this->a()[count($this->a()) - 1]",
PythonCompiler -> "self.a[-1]",
RubyCompiler -> "a.last"
Expand All @@ -317,6 +335,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "a().size()",
JavaScriptCompiler -> "this.a.length",
LuaCompiler -> "#self.a",
ObjcCompiler -> "a.length",
PHPCompiler -> "count($this->a())",
PerlCompiler -> "scalar($self->a())",
PythonCompiler -> "len(self.a)",
Expand All @@ -331,6 +350,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "\"str\"",
JavaScriptCompiler -> "\"str\"",
LuaCompiler -> "\"str\"",
ObjcCompiler -> "@\"str\"",
PerlCompiler -> "\"str\"",
PHPCompiler -> "\"str\"",
PythonCompiler -> "u\"str\"",
Expand All @@ -344,6 +364,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "\"str\\nnext\"",
JavaScriptCompiler -> "\"str\\nnext\"",
LuaCompiler -> "\"str\\nnext\"",
ObjcCompiler -> "@\"str\\nnext\"",
PerlCompiler -> "\"str\\nnext\"",
PHPCompiler -> "\"str\\nnext\"",
PythonCompiler -> "u\"str\\nnext\"",
Expand All @@ -357,6 +378,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "\"str\\nnext\"",
JavaScriptCompiler -> "\"str\\nnext\"",
LuaCompiler -> "\"str\\nnext\"",
ObjcCompiler -> "@\"str\\nnext\"",
PerlCompiler -> "\"str\\nnext\"",
PHPCompiler -> "\"str\\nnext\"",
PythonCompiler -> "u\"str\\nnext\"",
Expand All @@ -370,6 +392,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "\"str\\000next\"",
JavaScriptCompiler -> "\"str\\000next\"",
LuaCompiler -> "\"str\\000next\"",
ObjcCompiler -> "[NSString initWithBytes:\"str\\000next\" length:8 encoding:NSUTF8StringEncoding]",
PerlCompiler -> "\"str\\000next\"",
PHPCompiler -> "\"str\\000next\"",
PythonCompiler -> "u\"str\\000next\"",
Expand All @@ -379,6 +402,7 @@ class TranslatorSpec extends FunSuite {
everybodyExcept("\"str1\" + \"str2\"", "\"str1\" + \"str2\"", Map[LanguageCompilerStatic, String](
CppCompiler -> "std::string(\"str1\") + std::string(\"str2\")",
LuaCompiler -> "\"str1\" .. \"str2\"",
ObjcCompiler -> "[@\"str1\" appendString:@\"str2\""]",
PerlCompiler -> "\"str1\" . \"str2\"",
PHPCompiler -> "\"str1\" . \"str2\"",
PythonCompiler -> "u\"str1\" + u\"str2\""
Expand All @@ -388,6 +412,7 @@ class TranslatorSpec extends FunSuite {
CppCompiler -> "std::string(\"str1\") == (std::string(\"str2\"))",
JavaCompiler -> "\"str1\".equals(\"str2\")",
LuaCompiler -> "\"str1\" == \"str2\"",
ObjcCompiler -> "[@\"str1\" isEqualToString:@\"str2\""]",
PerlCompiler -> "\"str1\" eq \"str2\"",
PythonCompiler -> "u\"str1\" == u\"str2\""
), CalcBooleanType)
Expand All @@ -396,6 +421,7 @@ class TranslatorSpec extends FunSuite {
CppCompiler -> "std::string(\"str1\") != std::string(\"str2\")",
JavaCompiler -> "!(\"str1\").equals(\"str2\")",
LuaCompiler -> "\"str1\" ~= \"str2\"",
ObjcCompiler -> "![@\"str1\" isEqualToString:@\"str2\""]",
PerlCompiler -> "\"str1\" ne \"str2\"",
PythonCompiler -> "u\"str1\" != u\"str2\""
), CalcBooleanType)
Expand All @@ -405,6 +431,7 @@ class TranslatorSpec extends FunSuite {
CSharpCompiler -> "(\"str1\".CompareTo(\"str2\") < 0)",
JavaCompiler -> "(\"str1\".compareTo(\"str2\") < 0)",
LuaCompiler -> "\"str1\" < \"str2\"",
ObjcCompiler -> "([@\"str1\" compare:@\"str2\"" < 0)]",
PerlCompiler -> "\"str1\" lt \"str2\"",
PythonCompiler -> "u\"str1\" < u\"str2\""
), CalcBooleanType)
Expand All @@ -416,6 +443,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "\"str\".length()",
JavaScriptCompiler -> "\"str\".length",
LuaCompiler -> "string.len(\"str\")",
ObjcCompiler -> @"\"str\".length",
PerlCompiler -> "length(\"str\")",
PHPCompiler -> "strlen(\"str\")",
PythonCompiler -> "len(u\"str\")",
Expand All @@ -429,6 +457,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new StringBuilder(\"str\").reverse().toString()",
JavaScriptCompiler -> "Array.from(\"str\").reverse().join('')",
LuaCompiler -> "string.reverse(\"str\")",
ObjcCompiler -> "[@\"str\" KSReverse]",
PerlCompiler -> "scalar(reverse(\"str\"))",
PHPCompiler -> "strrev(\"str\")",
PythonCompiler -> "u\"str\"[::-1]",
Expand All @@ -442,6 +471,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "Long.parseLong(\"12345\", 10)",
JavaScriptCompiler -> "Number.parseInt(\"12345\", 10)",
LuaCompiler -> "tonumber(\"12345\")",
ObjcCompiler -> "@\"12345\".intValue",
PerlCompiler -> "\"12345\"",
PHPCompiler -> "intval(\"12345\", 10)",
PythonCompiler -> "int(u\"12345\")",
Expand All @@ -455,6 +485,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "Long.parseLong(\"1234fe\", 16)",
JavaScriptCompiler -> "Number.parseInt(\"1234fe\", 16)",
LuaCompiler -> "tonumber(\"1234fe\", 16)",
ObjcCompiler -> "[@\"1234fe\" ksToNumberWithBase:16]",
PerlCompiler -> "hex(\"1234fe\")",
PHPCompiler -> "intval(\"1234fe\", 16)",
PythonCompiler -> "int(u\"1234fe\", 16)",
Expand All @@ -469,6 +500,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "((Block) (other())).bar()",
JavaScriptCompiler -> "this.other.bar",
LuaCompiler -> "self.other.bar",
ObjcCompiler -> "((block_t *)other).bar",
PerlCompiler -> "$self->other()->bar()",
PHPCompiler -> "$this->other()->bar()",
PythonCompiler -> "self.other.bar",
Expand All @@ -482,6 +514,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "((Block.Innerblock) (other())).baz()",
JavaScriptCompiler -> "this.other.baz",
LuaCompiler -> "self.other.baz",
ObjcCompiler -> "???",
PerlCompiler -> "$self->other()->baz()",
PHPCompiler -> "$this->other()->baz()",
PythonCompiler -> "self.other.baz",
Expand All @@ -496,6 +529,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "((short) ((1 + 2)))",
JavaScriptCompiler -> "(1 + 2)",
LuaCompiler -> "(1 + 2)",
ObjcCompiler -> "((int16_t) ((1 + 2)))",
PerlCompiler -> "(1 + 2)",
PHPCompiler -> "(1 + 2)",
PythonCompiler -> "(1 + 2)",
Expand All @@ -510,6 +544,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new byte[] { }",
JavaScriptCompiler -> "[]",
LuaCompiler -> "\"\"",
ObjcCompiler -> "[NSData data]",
PerlCompiler -> "pack('C*', ())",
PHPCompiler -> "\"\"",
PythonCompiler -> "b\"\"",
Expand All @@ -523,6 +558,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new ArrayList<Integer>(Arrays.asList())",
JavaScriptCompiler -> "[]",
LuaCompiler -> "{}",
ObjcCompiler -> "[NSData data]",
PerlCompiler -> "()",
PHPCompiler -> "[]",
PythonCompiler -> "[]",
Expand All @@ -536,6 +572,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new ArrayList<Double>(Arrays.asList())",
JavaScriptCompiler -> "[]",
LuaCompiler -> "{}",
ObjcCompiler -> "[NSData data]",
PerlCompiler -> "()",
PHPCompiler -> "[]",
PythonCompiler -> "[]",
Expand All @@ -550,6 +587,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new byte[] { (0 + 1), 5 }",
JavaScriptCompiler -> "new Uint8Array([(0 + 1), 5])",
LuaCompiler -> "???",
ObjcCompiler -> "???",
PerlCompiler -> "pack('C*', ((0 + 1), 5))",
PHPCompiler -> "pack('C*', (0 + 1), 5)",
PythonCompiler -> "struct.pack('2b', (0 + 1), 5)",
Expand All @@ -563,6 +601,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> "new ArrayList<Integer>(Arrays.asList(0, 1, 2))",
JavaScriptCompiler -> "[0, 1, 2]",
LuaCompiler -> "{0, 1, 2}",
ObjcCompiler -> "@[@(0), @(1), @(2)]",
PerlCompiler -> "(0, 1, 2)",
PHPCompiler -> "[0, 1, 2]",
PythonCompiler -> "[0, 1, 2]",
Expand Down Expand Up @@ -610,6 +649,7 @@ class TranslatorSpec extends FunSuite {
JavaCompiler -> new JavaTranslator(tp, new ImportList()),
JavaScriptCompiler -> new JavaScriptTranslator(tp),
LuaCompiler -> new LuaTranslator(tp, new ImportList()),
ObjcCompiler -> new ObjcTranslator(tp),
PerlCompiler -> new PerlTranslator(tp, new ImportList()),
PHPCompiler -> new PHPTranslator(tp, RuntimeConfig()),
PythonCompiler -> new PythonTranslator(tp, new ImportList()),
Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/scala/io/kaitai/struct/ClassCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ClassCompiler(
compileClassDoc(curClass)

// Forward declarations for recursive types
curClass.types.foreach { case (typeName, _) => lang.classForwardDeclaration(List(typeName)) }
curClass.types.foreach { case (_, className) => lang.classForwardDeclaration(className.name) }

if (lang.innerEnums)
compileEnums(curClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
)
out.puts(s"{")
out.inc
handleAssignmentSimple(ParentIdentifier, pParent)
handleAssignmentSimple(ParentIdentifier, Some(parentType), pParent)

handleAssignmentSimple(
RootIdentifier,
RootIdentifier, None,
if (name == rootClassName) s"$pRoot ?? this" else pRoot
)

if (isHybrid)
handleAssignmentSimple(EndianIdentifier, "isLe")
handleAssignmentSimple(EndianIdentifier, None, "isLe")

// Store parameters passed to us
params.foreach((p) => handleAssignmentSimple(p.id, paramName(p.id)))
params.foreach((p) => handleAssignmentSimple(p.id, Some(p.dataType), paramName(p.id)))
}

override def classConstructorFooter: Unit = fileFooter(null)
Expand Down Expand Up @@ -280,7 +280,7 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.inc
}

override def handleAssignmentRepeatEos(id: Identifier, expr: String): Unit = {
override def handleAssignmentRepeatEos(id: Identifier, dataType: Option[DataType], expr: String): Unit = {
out.puts(s"${privateMemberName(id)}.Add($expr);")
}

Expand All @@ -303,7 +303,7 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.inc
}

override def handleAssignmentRepeatExpr(id: Identifier, expr: String): Unit = {
override def handleAssignmentRepeatExpr(id: Identifier, dataType: Option[DataType], expr: String): Unit = {
out.puts(s"${privateMemberName(id)}.Add($expr);")
}

Expand All @@ -323,7 +323,7 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.inc
}

override def handleAssignmentRepeatUntil(id: Identifier, expr: String, isRaw: Boolean): Unit = {
override def handleAssignmentRepeatUntil(id: Identifier, dataType: Option[DataType], expr: String, isRaw: Boolean): Unit = {
val (typeDecl, tempVar) = if (isRaw) {
("byte[] ", translator.doName(Identifier.ITERATOR2))
} else {
Expand All @@ -342,7 +342,7 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts("}")
}

override def handleAssignmentSimple(id: Identifier, expr: String): Unit =
override def handleAssignmentSimple(id: Identifier, dataType: Option[DataType], expr: String): Unit =
out.puts(s"${privateMemberName(id)} = $expr;")

override def handleAssignmentTempVar(dataType: DataType, id: String, expr: String): Unit =
Expand Down Expand Up @@ -513,7 +513,7 @@ class CSharpCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def instanceCalculate(instName: Identifier, dataType: DataType, value: expr): Unit =
// Perform explicit cast as unsigned integers can't be directly assigned to the default int type
handleAssignmentSimple(instName, s"(${kaitaiType2NativeType(dataType)}) (${expression(value)})")
handleAssignmentSimple(instName, Some(dataType), s"(${kaitaiType2NativeType(dataType)}) (${expression(value)})")

def flagForInstName(ksName: Identifier) = s"f_${idToStr(ksName)}"

Expand Down
Loading