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

Julia #305

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open

Julia #305

Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ca305eb
Copy Ruby classes to Julia{Compiler,Translator}
Jul 2, 2023
8324e62
WIP: handle Julia class compilation
Jul 31, 2023
c66eaf4
add Julia configuration to Main
Aug 8, 2023
abc3221
JuliaTranslator can handle basic expressions
Aug 8, 2023
c6ce289
Add fromfile functionality to JuliaClassCompiler
Aug 8, 2023
baeb13d
Fix getproperty and stream methods
Aug 8, 2023
6707eff
Use correct conversion
Aug 8, 2023
61d7cf3
add string to int parsing
Aug 8, 2023
4f670f3
fix kaitaistream method calls
Aug 8, 2023
ca5604f
Fix enum translation to string
Aug 9, 2023
44b8c46
fix julia indexing
Aug 10, 2023
5867b5f
fix julia functions calls from runtime lib
Aug 10, 2023
dc92064
fix byte array indexing
Aug 10, 2023
b1ee918
fix opaque class declataion
Aug 10, 2023
21c7593
move enum declaration to file header
Aug 10, 2023
1fcea5a
fix enum, strToInt and method calls
Aug 11, 2023
89dc9f5
introduce tree value logic
Aug 11, 2023
d8169ee
fix indexing and invalid enums
Aug 11, 2023
c77aebe
fix bytes operations and floor division
Aug 11, 2023
c9cf3ac
escape dollar sign
Oct 8, 2023
b60ba6d
introduce debug sequence support
Oct 8, 2023
dcea394
use KaitaiStruct functions explicitly
Oct 8, 2023
396c68b
bugfixing use KaiTaiStruct functions explicitly
Oct 8, 2023
1370577
add modules support
Oct 9, 2023
a086343
fix enum imports
Oct 9, 2023
c60a683
refactor enum imports
Nov 14, 2023
788ae6c
refactor imports, housekeeping
Nov 14, 2023
61194de
use package loading insted of code inclusion, use abstract UserType
Apr 8, 2024
8b889a5
Make UserType nullable
Apr 8, 2024
210a8fe
use package loading
Apr 8, 2024
40f2f30
remove unnecessay arguments from to_s methods
Apr 8, 2024
dcef9bb
add custom toString support for julia
Apr 9, 2024
27140e7
fix imports, assign _root directly to non root types
Apr 9, 2024
eef7faf
use ExternalType for external types
Apr 26, 2024
bb39c1c
delete commented code
Apr 29, 2024
479d14a
Specify data type of elements in condRepeatInitAttr
May 1, 2024
2354168
Introduce JulaCompiler.type2module to reduce code repetition
May 1, 2024
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
Prev Previous commit
Next Next commit
fix imports, assign _root directly to non root types
  • Loading branch information
Dias Rystin committed Apr 26, 2024
commit 27140e7020a9d697a1d676d0f4d37909daa88abd
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ class JuliaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

override def results(topClass: ClassSpec): Map[String, String] = {
Map(outFileName(topClass.nameAsStr) ->
(s"module ${types2class(topClass.name)}Module\n" + outHeader.result + outImports(topClass) + enums.result + out.result + "end\n")
(outHeader.result + outImports(topClass) + enums.result + out.result)
)
}

@@ -53,18 +53,24 @@ class JuliaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
importList.toList.mkString("", "\n", "\n")

override def fileHeader(topClassName: String): Unit = {
outHeader.puts(s"module ${type2class(topClassName)}Module")
outHeader.puts(s"# $headerComment")
outHeader.puts
importList.add("export from_file")
importList.add("using KaitaiStruct")
out.puts
}

override def fileFooter(topClassName: String): Unit = {
fromFile(topClassName)
out.puts("end")
}

override def externalClassDeclaration(classSpec: ClassSpec): Unit = {
// val name = classSpec.name.head
// importList.add(s"include(${'"'}../../compiled/julia/${classSpec.name.head}.jl${'"'})")
// importList.add(s"using .${types2class(classSpec.name)}Module: ${types2class(classSpec.name)}")
importList.add(s"import ${types2class(classSpec.name)}Module: ${types2class(classSpec.name)}")
importList.add(s"import ${type2class(classSpec.name.head)}Module: ${types2class(classSpec.name)}")
}

override def classHeader(name: List[String]): Unit = {
@@ -105,8 +111,12 @@ class JuliaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)

out.puts("this._io = _io")
out.puts("this._parent = _parent")
out.puts("this._root = _root === nothing ? this : _root")

if (name == rootClassName) {
out.puts("this._root = _root === nothing ? this : _root")
}
else {
out.puts("this._root = _root")
}
if (config.readStoresPos) {
out.puts("this._attrStart = Dict()")
out.puts("this._attrEnd = Dict()")
@@ -171,13 +181,15 @@ class JuliaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
out.puts(s"${idToStr(attrName)}::Union{Nothing,${kaitaiType2NativeType(attrType)}}")
}

def fromFile(name: List[String]): Unit = {
if (typeProvider.nowClass.params.isEmpty) {
out.puts(s"function from_file(filename::String)::${types2class(name)}")
out.inc
out.puts(s"${types2class(name)}($kstreamName(open(filename, ${'"'}r${'"'})))")
universalFooter
}


def fromFile(name: String): Unit = {
// if (typeProvider.nowClass.params.isEmpty) {
out.puts(s"function from_file(filename::String)::${type2class(name)}")
out.inc
out.puts(s"${type2class(name)}($kstreamName(open(filename, ${'"'}r${'"'})))")
universalFooter
// }
}

override def attributeReader(attrName: Identifier, attrType: DataType, isNullable: Boolean): Unit = {}
@@ -200,11 +212,11 @@ class JuliaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
case TextRef(text) =>
val seeAlso = new StringLanguageOutputWriter("")
seeAlso.putsLines(" ", text)
s"$extraNewline\n.. seealso::\n${seeAlso.result}"
s"$extraNewline\nSee also ${seeAlso.result}"
case ref: UrlRef =>
val seeAlso = new StringLanguageOutputWriter("")
seeAlso.putsLines(" ", s"${ref.text} - ${ref.url}")
s"$extraNewline\n.. seealso::\n${seeAlso.result}"
s"$extraNewline\nSee also ${seeAlso.result}"
}.mkString("\n")

out.putsLines("", "\"\"\"" + docStr + refStr + "\"\"\"")
@@ -412,7 +424,7 @@ class JuliaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
s"KaitaiStruct.read_bits_int_${bitEndian.toSuffix}($io, $width)"
case t: UserType =>
val addParams = Utils.join(t.args.map(a => translator.translate(a)), "", ", ", ", ")
val addArgs = if (t.isOpaque) {
val addArgs = if (t.isExternal(typeProvider.nowClass)) {
""
} else {
val parent = t.forcedParent match {
@@ -531,11 +543,8 @@ class JuliaCompiler(typeProvider: ClassTypeProvider, config: RuntimeConfig)
}

override def classToString(toStringExpr: Ast.expr): Unit = {
out.puts(s"Base.show(io::IO, this::${types2class(typeProvider.nowClass.name)}) = print(io, ${translator.translate(toStringExpr)})")
out.puts
// out.puts("def __repr__(self):")
// out.inc
// out.puts(s"return ${translator.translate(toStringExpr)}")
out.dec
}

override def idToStr(id: Identifier): String = JuliaCompiler.idToStr(id)
@@ -603,6 +612,7 @@ object JuliaCompiler extends LanguageCompilerStatic
override def kstructName: String = "Any"
override def ksErrorName(err: KSError): String = err match {
case EndOfStreamError => "ErrorException"
case ConversionError => "ArgumentError"
case _ => s"KaitaiStruct.${err.name}"
}

@@ -626,7 +636,7 @@ object JuliaCompiler extends LanguageCompilerStatic
case BitsType(_, _) => "UInt64"

case _: BooleanType => "Bool"
case CalcIntType => "Int64"
case CalcIntType => "Integer"
case CalcFloatType => "Float64"

case _: StrType => "String"
@@ -641,7 +651,8 @@ object JuliaCompiler extends LanguageCompilerStatic
// })
case _: UserType => "KaitaiStruct.UserType"

case t: EnumType => s"Union{${types2class(t.enumSpec.get.name)},Integer}"
// case t: EnumType => s"Union{${types2class(t.enumSpec.get.name)},Integer}"
case t: EnumType => s"Union{Enum,Integer}"

case at: ArrayType => s"Vector{${kaitaiType2NativeType(at.elType)}}"

Original file line number Diff line number Diff line change
@@ -78,17 +78,17 @@ class JuliaTranslator(provider: TypeProvider, importList: ImportList) extends Ba

override def doEnumByLabel(enumTypeAbs: List[String], label: String): String = {
if (!JuliaCompiler.type2class(enumTypeAbs.head).equals(JuliaCompiler.type2class(provider.nowClass.name.head))){
importList.add(s"using ${JuliaCompiler.type2class(enumTypeAbs.head)}Module: ${JuliaCompiler.types2class(enumTypeAbs)}")
importList.add(s"import ${JuliaCompiler.type2class(enumTypeAbs.head)}Module")
}

s"${JuliaCompiler.type2class(enumTypeAbs.head)}Module.${JuliaCompiler.enumToStr(enumTypeAbs, label)}"
}

override def doEnumById(enumTypeAbs: List[String], id: String): String = {
if (!JuliaCompiler.type2class(enumTypeAbs.head).equals(JuliaCompiler.type2class(provider.nowClass.name.head))){
importList.add(s"using ${JuliaCompiler.type2class(enumTypeAbs.head)}Module: ${JuliaCompiler.types2class(enumTypeAbs)}")
importList.add(s"import ${JuliaCompiler.type2class(enumTypeAbs.head)}Module")
}
s"KaitaiStruct.resolve_enum(${JuliaCompiler.types2class(enumTypeAbs)}, $id)"
s"KaitaiStruct.resolve_enum(${JuliaCompiler.type2class(enumTypeAbs.head)}Module.${JuliaCompiler.types2class(enumTypeAbs)}, $id)"
}

override def arraySubscript(container: Ast.expr, idx: Ast.expr): String =
@@ -158,4 +158,11 @@ class JuliaTranslator(provider: TypeProvider, importList: ImportList) extends Ba
s"KaitaiStruct.iseof(${translate(value)})"
override def kaitaiStreamPos(value: Ast.expr): String =
s"KaitaiStruct.pos(${translate(value)})"

override def doInterpolatedStringLiteral(exprs: Seq[Ast.expr]): String =
if (exprs.isEmpty) {
doStringLiteral("")
} else {
exprs.map(anyToStr).mkString(" * ")
}
}