diff --git a/ttcn3/types/types.go b/ttcn3/types/types.go index 144623bb..746cafa8 100644 --- a/ttcn3/types/types.go +++ b/ttcn3/types/types.go @@ -399,5 +399,42 @@ 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: + 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 syntax.STRING: + for _, r := range n.Tok.String() { + if r < 32 || 126 < r { + return Predefined["universal charstring"] + } + } + return Predefined["charstring"] + + case syntax.BSTRING: + s := n.Tok.String() + if len(s) == 0 { + return nil + } + switch s[len(s)-1] { + case 'H', 'h': + return Predefined["hexstring"] + case 'O', 'o': + return Predefined["octetstring"] + case 'B', 'b': + return Predefined["bitstring"] + } + return nil + } + } return nil } diff --git a/ttcn3/types/types_test.go b/ttcn3/types/types_test.go index 98b217a6..ad8ec72b 100644 --- a/ttcn3/types/types_test.go +++ b/ttcn3/types/types_test.go @@ -311,27 +311,31 @@ 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`}, - {skip: true, input: `0`, expect: `integer`}, - {skip: true, input: `0.0`, expect: `float`}, + // ValueLiterals + {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`}, - {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: `not_a_number`, expect: `float`}, + {input: `true`, expect: `boolean`}, + {input: `false`, expect: `boolean`}, + {input: `"hello"`, expect: `charstring`}, + {input: `"wörld"`, expect: `universal charstring`}, + {input: `'111'H`, expect: `hexstring`}, + {input: `'111'B`, expect: `bitstring`}, + {input: `'111'O`, expect: `octetstring`}, + {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`},