From ff2cf6c8ff743af8c6efd437c10593dd923c09ef Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Mon, 31 Jul 2023 12:31:23 +0200 Subject: [PATCH 1/6] Convert syntax Kinds to type Kinds --- ttcn3/types/types.go | 22 ++++++++++++++++++++++ ttcn3/types/types_test.go | 4 ++++ 2 files changed, 26 insertions(+) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index 144623bb..a43cf30e 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -110,6 +110,26 @@ var kindNames = map[Kind]string{ Testcase: "testcase", } +var kindConvert = map[syntax.Kind]Kind{ + syntax.INT: Integer, + syntax.FLOAT: Float, + syntax.FALSE: Boolean, + syntax.TRUE: Boolean, + syntax.FAIL: Verdict, + syntax.PASS: Verdict, + syntax.INCONC: Verdict, + syntax.NONE: Verdict, + syntax.ERROR: Verdict, + syntax.ENUMERATED: Enumerated, + syntax.CHARSTRING: Charstring, + syntax.STRING: UniversalCharstring, + syntax.COMPONENT: Component, + syntax.PORT: Port, + syntax.TIMER: Timer, + syntax.RECORD: Record, + syntax.SET: Set, +} + // String returns a string describing the kind, such as "error or integer or float". func (k Kind) String() string { if k == 0 { @@ -399,5 +419,7 @@ func printExpr(e syntax.Expr) string { } func TypeOf(n syntax.Expr) Type { + //types to check: + //ValueLiteral, CompositeLiteral, BinaryExpr, UnaryExpr, Ident return nil } diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index 98b217a6..4d0e8324 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -311,10 +311,12 @@ func TestTypeInference(t *testing.T) { expect string skip bool }{ + // Identifiers {skip: true, input: `integer`, expect: `integer`}, {skip: true, input: `float`, expect: `float`}, {skip: true, input: `boolean`, expect: `boolean`}, + // ValueLiterals {skip: true, input: `0`, expect: `integer`}, {skip: true, input: `0.0`, expect: `float`}, {skip: true, input: `infinity`, expect: `float`}, @@ -328,10 +330,12 @@ func TestTypeInference(t *testing.T) { {skip: true, input: `'111'O`, expect: `octettstring`}, {skip: true, input: `pass`, expect: `verdicttype`}, + // Unary Expressions {skip: true, input: `+0`, expect: `integer`}, {skip: true, input: `-0`, expect: `integer`}, {skip: true, input: `not4b '111'B`, expect: `bitstring`}, + // Binary Expressions {skip: true, input: `1+2`, expect: `integer`}, {skip: true, input: `1+2-3`, expect: `integer`}, {skip: true, input: `1.0+2.0`, expect: `float`}, From ae806abfeb5fe1c637cd62f4b7c5555cfdca5c18 Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Mon, 31 Jul 2023 15:58:19 +0200 Subject: [PATCH 2/6] Add Type Inference for Primitive Value Literals --- ttcn3/types/types.go | 12 +++++++++++- ttcn3/types/types_test.go | 12 ++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index a43cf30e..0adaaa4f 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -113,6 +113,7 @@ var kindNames = map[Kind]string{ var kindConvert = map[syntax.Kind]Kind{ syntax.INT: Integer, syntax.FLOAT: Float, + syntax.NAN: Float, syntax.FALSE: Boolean, syntax.TRUE: Boolean, syntax.FAIL: Verdict, @@ -121,7 +122,6 @@ var kindConvert = map[syntax.Kind]Kind{ syntax.NONE: Verdict, syntax.ERROR: Verdict, syntax.ENUMERATED: Enumerated, - syntax.CHARSTRING: Charstring, syntax.STRING: UniversalCharstring, syntax.COMPONENT: Component, syntax.PORT: Port, @@ -421,5 +421,15 @@ func printExpr(e syntax.Expr) string { func TypeOf(n syntax.Expr) Type { //types to check: //ValueLiteral, CompositeLiteral, BinaryExpr, UnaryExpr, Ident + switch n := n.(type) { + case *syntax.ValueLiteral: + kind := kindConvert[n.Tok.Kind()] + switch kind { + case Integer, Float, Boolean: + return Predefined[kind.String()] + case Verdict: + return Predefined["verdicttype"] + } + } return nil } diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index 4d0e8324..0ae0b5cb 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -317,18 +317,18 @@ func TestTypeInference(t *testing.T) { {skip: true, input: `boolean`, expect: `boolean`}, // ValueLiterals - {skip: true, input: `0`, expect: `integer`}, - {skip: true, input: `0.0`, expect: `float`}, + {input: `0`, expect: `integer`}, + {input: `0.0`, expect: `float`}, {skip: true, input: `infinity`, expect: `float`}, - {skip: true, input: `not_a_number`, expect: `float`}, - {skip: true, input: `true`, expect: `boolean`}, - {skip: true, input: `false`, expect: `boolean`}, + {input: `not_a_number`, expect: `float`}, + {input: `true`, expect: `boolean`}, + {input: `false`, expect: `boolean`}, {skip: true, input: `"hello"`, expect: `charstring`}, {skip: true, input: `"wörld"`, expect: `universal charstring`}, {skip: true, input: `'111'H`, expect: `hexstring`}, {skip: true, input: `'111'B`, expect: `bitstring`}, {skip: true, input: `'111'O`, expect: `octettstring`}, - {skip: true, input: `pass`, expect: `verdicttype`}, + {input: `pass`, expect: `verdicttype`}, // Unary Expressions {skip: true, input: `+0`, expect: `integer`}, From e0ec0bde72468db125975d4bf1ab1dee20dfd64a Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Mon, 31 Jul 2023 16:01:13 +0200 Subject: [PATCH 3/6] Add Type Inference for Charstring Value Literals --- ttcn3/types/types.go | 8 ++++++++ ttcn3/types/types_test.go | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index 0adaaa4f..a909d93e 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -429,6 +429,14 @@ func TypeOf(n syntax.Expr) Type { return Predefined[kind.String()] case Verdict: return Predefined["verdicttype"] + case UniversalCharstring: + if strings.IndexFunc(n.Tok.String(), func(r rune) bool { + return r <= 32 || r >= 126 + }) > -1 { + return Predefined["universal charstring"] + } else { + return Predefined["charstring"] + } } } return nil diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index 0ae0b5cb..76033ffa 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -323,8 +323,8 @@ func TestTypeInference(t *testing.T) { {input: `not_a_number`, expect: `float`}, {input: `true`, expect: `boolean`}, {input: `false`, expect: `boolean`}, - {skip: true, input: `"hello"`, expect: `charstring`}, - {skip: true, input: `"wörld"`, expect: `universal charstring`}, + {input: `"hello"`, expect: `charstring`}, + {input: `"wörld"`, expect: `universal charstring`}, {skip: true, input: `'111'H`, expect: `hexstring`}, {skip: true, input: `'111'B`, expect: `bitstring`}, {skip: true, input: `'111'O`, expect: `octettstring`}, From 25fc2e06f8a02fa69305b6e91ac2292d8673809f Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Mon, 31 Jul 2023 16:12:01 +0200 Subject: [PATCH 4/6] Add Type Inference for Bitstring Value Literals --- ttcn3/types/types.go | 8 ++++++++ ttcn3/types/types_test.go | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index a909d93e..c66348ed 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -123,6 +123,7 @@ var kindConvert = map[syntax.Kind]Kind{ syntax.ERROR: Verdict, syntax.ENUMERATED: Enumerated, syntax.STRING: UniversalCharstring, + syntax.BSTRING: Bitstring, syntax.COMPONENT: Component, syntax.PORT: Port, syntax.TIMER: Timer, @@ -437,6 +438,13 @@ func TypeOf(n syntax.Expr) Type { } else { return Predefined["charstring"] } + case Bitstring: + if strings.Contains(n.Tok.String(), "H") { + return Predefined["hexstring"] + } else if strings.Contains(n.Tok.String(), "O") { + return Predefined["octetstring"] + } + return Predefined["bitstring"] } } return nil diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index 76033ffa..ad8ec72b 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -325,9 +325,9 @@ func TestTypeInference(t *testing.T) { {input: `false`, expect: `boolean`}, {input: `"hello"`, expect: `charstring`}, {input: `"wörld"`, expect: `universal charstring`}, - {skip: true, input: `'111'H`, expect: `hexstring`}, - {skip: true, input: `'111'B`, expect: `bitstring`}, - {skip: true, input: `'111'O`, expect: `octettstring`}, + {input: `'111'H`, expect: `hexstring`}, + {input: `'111'B`, expect: `bitstring`}, + {input: `'111'O`, expect: `octetstring`}, {input: `pass`, expect: `verdicttype`}, // Unary Expressions From b3776cc78ae0cf8b6b20f664b6c484ee4c84e803 Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Tue, 1 Aug 2023 10:30:28 +0200 Subject: [PATCH 5/6] Simplify Type Inference --- ttcn3/types/types.go | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index c66348ed..ee423ef8 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -110,27 +110,6 @@ var kindNames = map[Kind]string{ Testcase: "testcase", } -var kindConvert = map[syntax.Kind]Kind{ - syntax.INT: Integer, - syntax.FLOAT: Float, - syntax.NAN: Float, - syntax.FALSE: Boolean, - syntax.TRUE: Boolean, - syntax.FAIL: Verdict, - syntax.PASS: Verdict, - syntax.INCONC: Verdict, - syntax.NONE: Verdict, - syntax.ERROR: Verdict, - syntax.ENUMERATED: Enumerated, - syntax.STRING: UniversalCharstring, - syntax.BSTRING: Bitstring, - syntax.COMPONENT: Component, - syntax.PORT: Port, - syntax.TIMER: Timer, - syntax.RECORD: Record, - syntax.SET: Set, -} - // String returns a string describing the kind, such as "error or integer or float". func (k Kind) String() string { if k == 0 { @@ -424,13 +403,16 @@ func TypeOf(n syntax.Expr) Type { //ValueLiteral, CompositeLiteral, BinaryExpr, UnaryExpr, Ident switch n := n.(type) { case *syntax.ValueLiteral: - kind := kindConvert[n.Tok.Kind()] - switch kind { - case Integer, Float, Boolean: - return Predefined[kind.String()] - case Verdict: + switch n.Tok.Kind() { + case syntax.INT: + return Predefined["integer"] + case syntax.FLOAT, syntax.NAN: + return Predefined["float"] + case syntax.TRUE, syntax.FALSE: + return Predefined["boolean"] + case syntax.PASS, syntax.FAIL, syntax.INCONC, syntax.NONE, syntax.ERROR: return Predefined["verdicttype"] - case UniversalCharstring: + case syntax.STRING: if strings.IndexFunc(n.Tok.String(), func(r rune) bool { return r <= 32 || r >= 126 }) > -1 { @@ -438,7 +420,7 @@ func TypeOf(n syntax.Expr) Type { } else { return Predefined["charstring"] } - case Bitstring: + case syntax.BSTRING: if strings.Contains(n.Tok.String(), "H") { return Predefined["hexstring"] } else if strings.Contains(n.Tok.String(), "O") { From cfecc62a2097c0ef6533ddcbd7f260971e24f001 Mon Sep 17 00:00:00 2001 From: nico-blaser <88314447+nnnnblaser@users.noreply.github.com> Date: Tue, 1 Aug 2023 15:22:27 +0200 Subject: [PATCH 6/6] Simplify String Value Literal Inference --- ttcn3/types/types.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index ee423ef8..746cafa8 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -413,20 +413,27 @@ func TypeOf(n syntax.Expr) Type { case syntax.PASS, syntax.FAIL, syntax.INCONC, syntax.NONE, syntax.ERROR: return Predefined["verdicttype"] case syntax.STRING: - if strings.IndexFunc(n.Tok.String(), func(r rune) bool { - return r <= 32 || r >= 126 - }) > -1 { - return Predefined["universal charstring"] - } else { - return Predefined["charstring"] + for _, r := range n.Tok.String() { + if r < 32 || 126 < r { + return Predefined["universal charstring"] + } } + return Predefined["charstring"] + case syntax.BSTRING: - if strings.Contains(n.Tok.String(), "H") { + s := n.Tok.String() + if len(s) == 0 { + return nil + } + switch s[len(s)-1] { + case 'H', 'h': return Predefined["hexstring"] - } else if strings.Contains(n.Tok.String(), "O") { + case 'O', 'o': return Predefined["octetstring"] + case 'B', 'b': + return Predefined["bitstring"] } - return Predefined["bitstring"] + return nil } } return nil