Skip to content

Commit

Permalink
Value literal inference (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnnnblaser authored Aug 1, 2023
1 parent 5c89a41 commit 223d9f4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
37 changes: 37 additions & 0 deletions ttcn3/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
28 changes: 16 additions & 12 deletions ttcn3/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`},
Expand Down

0 comments on commit 223d9f4

Please sign in to comment.