Skip to content

Commit

Permalink
WIP: remove Scala specific code from Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
usr3-1415 committed Feb 3, 2024
1 parent ddfd1ab commit 594b790
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 69 deletions.
23 changes: 3 additions & 20 deletions BackendAst/DAstInitialize.fs
Original file line number Diff line number Diff line change
Expand Up @@ -685,33 +685,20 @@ let mergeMaps (m1:Map<'key,'value>) (m2:Map<'key,'value>) =
let createEnumeratedInitFunc (r: Asn1AcnAst.AstRoot) (lm: LanguageMacros) (t: Asn1AcnAst.Asn1Type) (o: Asn1AcnAst.Enumerated) (typeDefinition: TypeDefintionOrReference) iv =
let initEnumerated = lm.init.initEnumerated

let enumClassName =
match ST.lang with
| ProgrammingLanguage.Scala ->
match typeDefinition with
| ReferenceToExistingDefinition r -> r.typedefName
| TypeDefinition t -> t.typedefName
| _ -> ""

let getEnumBackendName (defOrRef: TypeDefintionOrReference option) (nm: Asn1AcnAst.NamedItem) =
let itemname =
match ST.lang with
| ProgrammingLanguage.Scala -> ToC nm.scala_name
| _ -> (lm.lg.getNamedItemBackendName defOrRef nm)
itemname

let funcBody (p:CallerScope) (v:Asn1ValueKind) =
let vl =
match v.ActualValue with
| EnumValue iv -> o.items |> Seq.find(fun x -> x.Name.Value = iv)
| _ -> raise(BugErrorException "UnexpectedValue")
initEnumerated (lm.lg.getValue p.arg) (getEnumBackendName (Some typeDefinition) vl) enumClassName
initEnumerated (lm.lg.getValue p.arg) (lm.lg.getNamedItemBackendName (Some typeDefinition) vl)

let testCaseFuncs =
EncodeDecodeTestCase.EnumeratedAutomaticTestCaseValues2 r t o |>
List.map (fun vl ->
{
AutomaticTestCase.initTestCaseFunc = (fun (p:CallerScope) -> {InitFunctionResult.funcBody = initEnumerated (lm.lg.getValue p.arg) (getEnumBackendName (Some typeDefinition) vl) enumClassName; localVariables=[]});
AutomaticTestCase.initTestCaseFunc = (fun (p:CallerScope) -> {InitFunctionResult.funcBody = initEnumerated (lm.lg.getValue p.arg) (lm.lg.getNamedItemBackendName (Some typeDefinition) vl); localVariables=[]});
testCaseTypeIDsMap = Map.ofList [(t.id, (TcvEnumeratedValue vl.Name.Value))]
})
let constantInitExpression = lm.lg.getNamedItemBackendName (Some typeDefinition) o.items.Head
Expand Down Expand Up @@ -1052,11 +1039,7 @@ let createSequenceInitFunc (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (t:Asn1Acn
List.map (fun c ->
let childName = lm.lg.getAsn1ChildBackendName c
let childExp = getChildExpr lm c.Type
let exprMethodCall =
match ST.lang with
| ProgrammingLanguage.Scala ->
scalaInitMethSuffix c.Type.Kind
| _ -> ""
let exprMethodCall = lm.lg.init.initMethSuffix c.Type.Kind
lm.init.initSequenceChildExpr childName (childExp + exprMethodCall))
let arrsOptionalChildren =
children |>
Expand Down
13 changes: 3 additions & 10 deletions BackendAst/DAstUPer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -657,17 +657,10 @@ let createChoiceFunction (r:Asn1AcnAst.AstRoot) (lm:LanguageMacros) (codec:Commo
| true -> chFunc.funcBody ({p with arg = lm.lg.getChChild p.arg (lm.lg.getAsn1ChChildBackendName child) child.chType.isIA5String})
let sChildName = (lm.lg.getAsn1ChChildBackendName child)
let sChildTypeDef = child.chType.typeDefintionOrReference.longTypedefName2 lm.lg.hasModules //child.chType.typeDefinition.typeDefinitionBodyWithinSeq
let isSequence = isSequenceForJVMelseFalse child.chType.Kind
let isEnum = isEnumForJVMelseFalse child.chType.Kind
let isOctetString = isOctetStringForJVMelseFalse child.chType.Kind
let isSequence = match child.chType.Kind with | Sequence _ -> true | _ -> false
let isEnum = match child.chType.Kind with | Enumerated _ -> true | _ -> false
let sChildInitExpr = child.chType.initFunction.initExpression
let exprMethodCall =
match ST.lang with
| Scala ->
match isSequence || sChildInitExpr.Equals("null") || isEnum || isOctetString with
| true -> ""
| false -> scalaInitMethSuffix child.chType.Kind
| _ -> ""
let exprMethodCall = lm.lg.uper.exprMethodCall child.chType.Kind sChildInitExpr
let sChoiceTypeName = typeDefinitionName
match uperChildRes with
| None ->
Expand Down
35 changes: 0 additions & 35 deletions BackendAst/DAstUtilFunctions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,6 @@ let hasInitMethSuffix (initMethName: string) (suffix: string): bool =
let isArrayInitialiser(initMethName: string): bool =
initMethName.Contains("Array.fill(")

let scalaInitMethSuffix (k: Asn1TypeKind) =
match ST.lang with
| Scala ->
match isJVMPrimitive k with
| false ->
match k with
| BitString bitString -> ""
| _ -> "()"
| true -> ""
| _ -> ""

let isEnumForJVMelseFalse (k: Asn1TypeKind): bool =
match ST.lang with
| Scala ->
match resolveReferenceType k with
| Enumerated e -> true
| _ -> false
| _ -> false

let isSequenceForJVMelseFalse (k: Asn1TypeKind): bool =
match ST.lang with
| Scala ->
match k with
| Sequence s -> true
| _ -> false
| _ -> false

let isOctetStringForJVMelseFalse (k: Asn1TypeKind): bool =
match ST.lang with
| Scala ->
match k with
| OctetString s -> true
| _ -> false
| _ -> false

type LocalVariable with
member this.VarName =
match this with
Expand Down
2 changes: 1 addition & 1 deletion CommonTypes/AbstractMacros.fs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ Generated by the C stg macros with the following command
abstract member assignAny : p:string -> sValue:string -> sTypeDecl:string -> string;
abstract member assignString : p:string -> sValue:string -> string;
abstract member initIA5String : sPtr:string -> sValue:string -> string;
abstract member initEnumerated : sVal:string -> sValue:string -> sEnum:string -> string;
abstract member initEnumerated : sVal:string -> sValue:string -> string;
abstract member initNull : sVal:string -> string;
abstract member initTestCaseIA5String : p:string -> sAcc:string -> nSize:BigInteger -> nMaxSizePlusOne:BigInteger -> i:string -> td:FE_StringTypeDefinition -> bAlpha:bool -> arrnAlphabetAsciiCodes:seq<BigInteger> -> nAlphabetLength:BigInteger -> bZero:bool -> string;
abstract member initBitOrOctStringFromCompoundLiteral : p:string -> sCompLiteral:string -> string;
Expand Down
2 changes: 2 additions & 0 deletions FrontEndAst/Language.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Uper_parts = {
catd : bool //if true then Choice Alternatives are Temporarily Decoded (i.e. in _tmp variables in curent scope)
//createBitStringFunction : (CallerScope -> CommonTypes.Codec -> ErroCode -> int -> BigInteger -> BigInteger -> BigInteger -> string -> BigInteger -> bool -> bool -> (string * LocalVariable list)) -> CommonTypes.Codec -> ReferenceToType -> TypeDefintionOrReference -> bool -> BigInteger -> BigInteger -> BigInteger -> ErroCode -> CallerScope -> UPERFuncBodyResult
seqof_lv : ReferenceToType -> BigInteger -> BigInteger -> LocalVariable list
exprMethodCall : Asn1TypeKind -> string -> string

}

Expand All @@ -30,6 +31,7 @@ type Acn_parts = {
type Initialize_parts = {
zeroIA5String_localVars : int -> LocalVariable list
choiceComponentTempInit : bool
initMethSuffix : Asn1TypeKind -> string
}

type Atc_parts = {
Expand Down
2 changes: 2 additions & 0 deletions StgAda/LangGeneric_a.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ type LangGeneric_a() =
[]
else
[SequenceOfIndex (id.SeqeuenceOfLevel + 1, None)])
exprMethodCall = fun _ _ -> ""
}
override this.acn =
{
Expand All @@ -308,6 +309,7 @@ type LangGeneric_a() =
{
Initialize_parts.zeroIA5String_localVars = fun ii -> [SequenceOfIndex (ii, None)]
choiceComponentTempInit = false
initMethSuffix = fun _ -> ""
}

override this.atc =
Expand Down
2 changes: 1 addition & 1 deletion StgAda/init_a.stg
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ initReal(p, dValue) ::= "<p> := <dValue>;"
initBoolean(p, bValue) ::= "<p> := <if(bValue)>True<else>FALSE<endif>;"

initIA5String(sPtr, sValue) ::= "<sPtr> := <sValue>;"
initEnumerated(sVal, sValue, sEnum) ::= "<sVal> := <sValue>;"
initEnumerated(sVal, sValue) ::= "<sVal> := <sValue>;"
initNull(sVal) ::= "<sVal> := 0;"


Expand Down
3 changes: 3 additions & 0 deletions StgC/LangGeneric_c.fs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ type LangGeneric_c() =
//createBitStringFunction = createBitStringFunction_funcBody_c
seqof_lv =
(fun id minSize maxSize -> [SequenceOfIndex (id.SeqeuenceOfLevel + 1, None)])
exprMethodCall = fun _ _ -> ""

}
override this.acn =
{
Expand All @@ -339,6 +341,7 @@ type LangGeneric_c() =
{
Initialize_parts.zeroIA5String_localVars = fun _ -> []
choiceComponentTempInit = false
initMethSuffix = fun _ -> ""
}
override this.atc =
{
Expand Down
2 changes: 1 addition & 1 deletion StgC/init_c.stg
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ assignString(p, sValue) ::= "memcpy(<p>, <sValue>, sizeof(<sValue>));"


initIA5String(sPtr, sValue) ::= "strcpy(<sPtr>,<sValue>);"
initEnumerated(sVal, sValue, sEnum) ::= "<sVal> = <sValue>;"
initEnumerated(sVal, sValue) ::= "<sVal> = <sValue>;"
initNull(sVal) ::= "<sVal> = 0;"

initTestCaseIA5String(p, sAcc, nSize, nMaxSizePlusOne, i, td/*:FE_StringTypeDefinition*/, bAlpha, arrnAlphabetAsciiCodes, nAlphabetLength, bZero) ::= <<
Expand Down
55 changes: 55 additions & 0 deletions StgScala/LangGeneric_scala.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,58 @@ open Language
open System.IO
open System


let rec resolveReferenceType(t: Asn1TypeKind): Asn1TypeKind =
match t with
| ReferenceType rt -> resolveReferenceType rt.resolvedType.Kind
| _ -> t

let isJVMPrimitive (t: Asn1TypeKind) =
match resolveReferenceType t with
| Integer _ | Real _ | NullType _ | Boolean _ -> true
| _ -> false

let initMethSuffix k =
match isJVMPrimitive k with
| false ->
match k with
| BitString bitString -> ""
| _ -> "()"
| true -> ""

let isEnumForJVMelseFalse (k: Asn1TypeKind): bool =
match ST.lang with
| Scala ->
match resolveReferenceType k with
| Enumerated e -> true
| _ -> false
| _ -> false

let isSequenceForJVMelseFalse (k: Asn1TypeKind): bool =
match ST.lang with
| Scala ->
match k with
| Sequence s -> true
| _ -> false
| _ -> false

let isOctetStringForJVMelseFalse (k: Asn1TypeKind): bool =
match ST.lang with
| Scala ->
match k with
| OctetString s -> true
| _ -> false
| _ -> false

let uperExprMethodCall k sChildInitExpr =
let isSequence = isSequenceForJVMelseFalse k
let isEnum = isEnumForJVMelseFalse k
let isOctetString = isOctetStringForJVMelseFalse k

match isSequence || sChildInitExpr.Equals("null") || isEnum || isOctetString with
| true -> ""
| false -> initMethSuffix k

let getAccess_scala (fpt:FuncParamType) =
match fpt with
| VALUE x -> "."
Expand Down Expand Up @@ -336,6 +388,8 @@ type LangGeneric_scala() =
//createBitStringFunction = createBitStringFunction_funcBody_c
seqof_lv =
(fun id minSize maxSize -> [SequenceOfIndex (id.SeqeuenceOfLevel + 1, None)])
exprMethodCall = uperExprMethodCall

}
override this.acn =
{
Expand All @@ -356,6 +410,7 @@ type LangGeneric_scala() =
{
Initialize_parts.zeroIA5String_localVars = fun _ -> []
choiceComponentTempInit = false
initMethSuffix = initMethSuffix
}
override this.atc =
{
Expand Down
2 changes: 1 addition & 1 deletion StgScala/init_scala.stg
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ assignString(p, sValue) ::= "<p> = <sValue>"


initIA5String(sPtr, sValue) ::= "<sPtr> = <sValue>"
initEnumerated(sVal, sValue, sEnum) ::= "<sVal> = <sEnum>.<sValue>"
initEnumerated(sVal, sValue) ::= "<sVal> = <sValue>"
initNull(sVal) ::= "<sVal> = 0"

initTestCaseIA5String(p, sAcc, nSize, nMaxSizePlusOne, i, td/*:FE_StringTypeDefinition*/, bAlpha, arrnAlphabetAsciiCodes, nAlphabetLength, bZero) ::= <<
Expand Down

0 comments on commit 594b790

Please sign in to comment.