@@ -15,7 +15,30 @@ sealed trait TranslatorResult
1515case class ResultString (s : String ) extends TranslatorResult
1616case class ResultLocalVar (n : Int ) extends TranslatorResult
1717
18- class GoTranslator (out : StringLanguageOutputWriter , provider : TypeProvider , importList : ImportList )
18+ class GoOutputWriter (indentStr : String ) extends StringLanguageOutputWriter (indentStr) {
19+ /**
20+ * Puts to the output code to check variable `err` for error value and emit
21+ * a premature return with value of error.
22+ *
23+ * @param result If not none this value will be returned as the first value of
24+ * the returned tuple, otherwise only `err` is returned
25+ */
26+ def putsErrCheck (result : Option [String ]): Unit = {
27+ puts(" if err != nil {" )
28+ inc
29+
30+ val noValueAndErr = result match {
31+ case None => " err"
32+ case Some (r) => s " $r, err "
33+ }
34+
35+ puts(s " return $noValueAndErr" )
36+ dec
37+ puts(" }" )
38+ }
39+ }
40+
41+ class GoTranslator (out : GoOutputWriter , provider : TypeProvider , importList : ImportList )
1942 extends TypeDetector (provider)
2043 with AbstractTranslator
2144 with CommonLiterals
@@ -26,6 +49,10 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
2649
2750 import io .kaitai .struct .languages .GoCompiler ._
2851
52+ /**
53+ * Dummy return value that should be returned in case of error just because
54+ * we cannot return nothing.
55+ */
2956 var returnRes : Option [String ] = None
3057
3158 override def translate (v : Ast .expr, extPrec : Int ): String = resToStr(translateExpr(v, extPrec))
@@ -470,7 +497,7 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
470497 val addParams = t.args.map((a) => translate(a)).mkString(" , " )
471498 out.puts(s " ${localVarName(v)} := New ${GoCompiler .types2class(t.classSpec.get.name)}( $addParams) " )
472499 out.puts(s " err = ${localVarName(v)}.Read( $io, $parent, $root) " )
473- outAddErrCheck( )
500+ out.putsErrCheck(returnRes )
474501 ResultLocalVar (v)
475502 }
476503
@@ -502,7 +529,7 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
502529 def outVarCheckRes (expr : String ): ResultLocalVar = {
503530 val v1 = allocateLocalVar()
504531 out.puts(s " ${localVarName(v1)}, err := $expr" )
505- outAddErrCheck( )
532+ out.putsErrCheck(returnRes )
506533 ResultLocalVar (v1)
507534 }
508535
@@ -521,20 +548,6 @@ class GoTranslator(out: StringLanguageOutputWriter, provider: TypeProvider, impo
521548
522549 def localVarName (n : Int ) = s " tmp $n"
523550
524- def outAddErrCheck (): Unit = {
525- out.puts(" if err != nil {" )
526- out.inc
527-
528- val noValueAndErr = returnRes match {
529- case None => " err"
530- case Some (r) => s " $r, err "
531- }
532-
533- out.puts(s " return $noValueAndErr" )
534- out.dec
535- out.puts(" }" )
536- }
537-
538551 override def byteSizeOfValue (attrName : String , valType : DataType ): TranslatorResult =
539552 trIntLiteral(CommonSizeOf .bitToByteSize(CommonSizeOf .getBitsSizeOfType(attrName, valType)))
540553}
0 commit comments