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 5 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
30 changes: 30 additions & 0 deletions ttcn3/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,35 @@ 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:
if strings.IndexFunc(n.Tok.String(), func(r rune) bool {
return r <= 32 || r >= 126
}) > -1 {
return Predefined["universal charstring"]
} else {
return Predefined["charstring"]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the flow hard to follow. What about:

Suggested change
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") {
return Predefined["hexstring"]
} else if strings.Contains(n.Tok.String(), "O") {
return Predefined["octetstring"]
}
return Predefined["bitstring"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the whole string for H or B is too much, also I don't think bitstring is the best default. I would use nil as default until we know better. How about something like:

Suggested change
if strings.Contains(n.Tok.String(), "H") {
return Predefined["hexstring"]
} else if strings.Contains(n.Tok.String(), "O") {
return Predefined["octetstring"]
}
return Predefined["bitstring"]
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