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

Show file tree
Hide file tree
Changes from 35 commits
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
110 changes: 110 additions & 0 deletions shared/src/main/scala/io/kaitai/struct/JuliaClassCompiler.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.kaitai.struct

import io.kaitai.struct.datatype.DataType.{CalcIntType, KaitaiStreamType, UserType, UserTypeInstream}
import io.kaitai.struct.datatype.{BigEndian, CalcEndian, Endianness, FixedEndian, LittleEndian}
import io.kaitai.struct.exprlang.Ast
import io.kaitai.struct.format._
import io.kaitai.struct.languages.JuliaCompiler
import io.kaitai.struct.languages.components.ExtraAttrs

class JuliaClassCompiler(
classSpecs: ClassSpecs,
override val topClass: ClassSpec,
config: RuntimeConfig
) extends ClassCompiler(classSpecs, topClass, config, JuliaCompiler) {

private val julialang = lang.asInstanceOf[JuliaCompiler]

override def compileClass(curClass: ClassSpec): Unit = {
provider.nowClass = curClass

val extraAttrs = List(
AttrSpec(List(), IoIdentifier, KaitaiStreamType),
AttrSpec(List(), RootIdentifier, UserTypeInstream(topClassName, None)),
AttrSpec(List(), ParentIdentifier, curClass.parentType)
) ++ ExtraAttrs.forClassSpec(curClass, lang)

// curClass.types.foreach { case (typeName, _) => lang.classForwardDeclaration(curClass.name ++ List(typeName)) }

if (!curClass.doc.isEmpty)
lang.classDoc(curClass.name, curClass.doc)

// Enums declaration defines types, so they need to go first
compileEnums(curClass)

if (lang.config.readStoresPos)
lang.debugClassSequence(curClass.seq)

// Basic struct declaration
lang.classHeader(curClass.name)
compileAttrDeclarations(curClass.seq ++ curClass.params ++ extraAttrs)
curClass.instances.foreach { case (instName, instSpec) =>
compileInstanceDeclaration(instName, instSpec)
}
compileConstructor(curClass)
lang.classFooter(curClass.name)

// if (curClass.isTopLevel)
// julialang.fromFile(curClass.name)

compileEagerRead(curClass.seq, curClass.meta.endian)

julialang.overrideGetProperty(curClass.name, curClass.instances)
compileInstances(curClass)

compileAttrReaders(curClass.seq ++ extraAttrs)

curClass.toStringExpr.foreach(expr => lang.classToString(expr))
// Recursive types
compileSubclasses(curClass)
}

override def compileInstance(className: List[String], instName: InstanceIdentifier, instSpec: InstanceSpec, endian: Option[Endianness]): Unit = {
// Determine datatype
val dataType = instSpec.dataTypeComposite

// compileInstanceDeclaration(instName, instSpec)

if (!lang.innerDocstrings)
compileInstanceDoc(instName, instSpec)
lang.instanceHeader(className, instName, dataType, instSpec.isNullable)
if (lang.innerDocstrings)
compileInstanceDoc(instName, instSpec)
lang.instanceCheckCacheAndReturn(instName, dataType)

instSpec match {
case vi: ValueInstanceSpec =>
lang.attrParseIfHeader(instName, vi.ifExpr)
lang.instanceCalculate(instName, dataType, vi.value)
lang.attrParseIfFooter(vi.ifExpr)
lang.instanceSetCalculated(instName)
case pi: ParseInstanceSpec =>
lang.attrParse(pi, instName, endian)
}

lang.instanceReturn(instName, dataType)
lang.instanceFooter
}

// override def compileCalcEndian(ce: CalcEndian): Unit = {
// def renderProc(result: FixedEndian): Unit = {
// val v = result match {
// case LittleEndian => Ast.expr.Bool(true)
// case BigEndian => Ast.expr.Bool(false)
// }
// lang.instanceCalculate(IS_LE_ID, CalcIntType, v)
// }
// lang.switchCases[FixedEndian](IS_LE_ID, ce.on, ce.cases, renderProc, renderProc)
// }

override def compileAttrDeclarations(attrs: List[MemberSpec]): Unit = {
attrs.foreach { (attr) =>
val isNullable = if (lang.switchBytesOnlyAsRaw) {
attr.isNullableSwitchRaw
} else {
attr.isNullable || attr.dataType.isInstanceOf[UserType]
}
lang.attributeDeclaration(attr.id, attr.dataTypeComposite, isNullable)
}
}
}
4 changes: 3 additions & 1 deletion shared/src/main/scala/io/kaitai/struct/Main.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.kaitai.struct

import io.kaitai.struct.format.{ClassSpec, ClassSpecs, MetaSpec}
import io.kaitai.struct.languages.{GoCompiler, NimCompiler, RustCompiler}
import io.kaitai.struct.languages.{GoCompiler, NimCompiler, RustCompiler, JuliaCompiler}
import io.kaitai.struct.languages.components.LanguageCompilerStatic
import io.kaitai.struct.precompile._
import io.kaitai.struct.problems.CompilationProblem
Expand Down Expand Up @@ -80,6 +80,8 @@ object Main {
new GraphvizClassCompiler(specs, spec)
case GoCompiler =>
new GoClassCompiler(specs, spec, config)
case JuliaCompiler =>
new JuliaClassCompiler(specs, spec, config)
case RustCompiler =>
new RustClassCompiler(specs, spec, config)
case ConstructClassCompiler =>
Expand Down
Loading