Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value literal inference #699

Merged
merged 6 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading