Skip to content
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
43 changes: 16 additions & 27 deletions compiler/semantic/sem/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,6 @@ func NewStringError(n ast.Node, message string) Expr {
}

func NewLiteral(n ast.Node, val super.Value, defs *super.Context) Expr {
if val.Type() == super.TypeType {
typ, err := defs.LookupByValue(val.Bytes())
if err != nil {
panic(err)
}
return &TypeExpr{
Node: n,
ID: super.TypeID(typ),
}
}
return valueToExpr(n, val.Type(), val.Bytes(), defs)
}

Expand Down Expand Up @@ -484,22 +474,6 @@ func copyArrayElems(elems []ArrayElem) []ArrayElem {
}

func valueToExpr(loc ast.Node, typ super.Type, bytes scode.Bytes, defs *super.Context) Expr {
if !super.IsOptionType(typ) && super.IsPrimitiveType(typ) {
if super.TypeUnder(typ) == super.TypeType {
t, err := defs.LookupByValue(bytes)
if err != nil {
panic(err)
}
return &TypeExpr{
Node: loc,
ID: super.TypeID(t),
}
}
return &PrimitiveExpr{
Node: loc,
Value: sup.FormatValue(super.NewValue(typ, bytes)),
}
}
switch typ := typ.(type) {
case *super.TypeNamed:
_, _ = defs.LookupTypeNamed(typ.Name, typ.Type)
Expand All @@ -519,8 +493,23 @@ func valueToExpr(loc ast.Node, typ super.Type, bytes scode.Bytes, defs *super.Co
case *super.TypeError:
args := []Expr{valueToExpr(loc, typ.Type, bytes, defs)}
return NewCall(loc, "error", args)
case *super.TypeOfType:
t, err := defs.LookupByValue(bytes)
if err != nil {
panic(err)
}
return &TypeExpr{
Node: loc,
ID: super.TypeID(t),
}
default:
panic(typ)
if !super.IsPrimitiveType(typ) {
panic(typ)
}
return &PrimitiveExpr{
Node: loc,
Value: sup.FormatValue(super.NewValue(typ, bytes)),
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions compiler/ztests/const-named-type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spq: |
type port = uint16
const p = 2::port
values p

output: |
type port=uint16
2::port
Loading