Skip to content

Commit

Permalink
generics
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Mar 23, 2021
1 parent 73215c4 commit 0513f94
Show file tree
Hide file tree
Showing 13 changed files with 940 additions and 295 deletions.
9 changes: 5 additions & 4 deletions go2gno.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,23 +339,24 @@ func Go2Gno(gon ast.Node) (n Node) {
Stmt: toStmt(gon.Stmt),
}
case *ast.TypeSwitchStmt:
if as, ok := gon.Assign.(*ast.AssignStmt); ok {
switch as := gon.Assign.(type) {
case *ast.AssignStmt:
return &SwitchStmt{
Init: toStmt(gon.Init),
X: toExpr(as.Rhs[0].(*ast.TypeAssertExpr).X),
IsTypeSwitch: true,
Clauses: toClauses(gon.Body.List),
VarName: toName(as.Lhs[0].(*ast.Ident)),
}
} else if tax, ok := gon.Assign.(*ast.ExprStmt); ok {
case *ast.ExprStmt:
return &SwitchStmt{
Init: toStmt(gon.Init),
X: toExpr(tax.X.(*ast.TypeAssertExpr).X),
X: toExpr(as.X.(*ast.TypeAssertExpr).X),
IsTypeSwitch: true,
Clauses: toClauses(gon.Body.List),
VarName: "",
}
} else {
default:
panic(fmt.Sprintf(
"unexpected *ast.TypeSwitchStmt.Assign type %s",
reflect.TypeOf(gon.Assign).String()))
Expand Down
17 changes: 14 additions & 3 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func SliceT(elt interface{}) *SliceTypeExpr {
}
}

func MapT(key, value interface{}) *MapTypeExpr {
return &MapTypeExpr{
Key: X(key),
Value: X(value),
}
}

func Vrd(elt interface{}) *SliceTypeExpr {
return &SliceTypeExpr{
Elt: X(elt),
Expand All @@ -53,10 +60,14 @@ func InterfaceT(methods FieldTypeExprs) *InterfaceTypeExpr {
}
}

func AnyT(methods FieldTypeExprs) *InterfaceTypeExpr {
func AnyT() *InterfaceTypeExpr {
return InterfaceT(nil)
}

func GenT(generic Name, methods FieldTypeExprs) *InterfaceTypeExpr {
return &InterfaceTypeExpr{
Methods: methods,
IsUntyped: true,
Generic: generic,
Methods: methods,
}
}

Expand Down
6 changes: 4 additions & 2 deletions kind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ type SliceTypeExpr struct {

type InterfaceTypeExpr struct {
Attributes
Methods FieldTypeExprs // list of methods
IsUntyped bool // for uverse generics
Methods FieldTypeExprs // list of methods
Generic Name // for uverse generics
}

type ChanDir int
Expand Down
57 changes: 14 additions & 43 deletions op_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ func (m *Machine) doOpInterfaceType() {
}
// push interface type
it := &InterfaceType{
PkgPath: m.Package.PkgPath,
Methods: methods,
IsUntyped: x.IsUntyped,
PkgPath: m.Package.PkgPath,
Methods: methods,
Generic: x.Generic,
}
m.PushValue(TypedValue{
T: gTypeType,
Expand Down Expand Up @@ -192,43 +192,8 @@ func (m *Machine) doOpTypeOf() {
m.PushValue(asValue(UntypedBoolType))
}
case *CallExpr:
start := m.NumValues
m.PushOp(OpHalt)
m.PushExpr(x.Func)
m.PushOp(OpTypeOf)
m.Run() // XXX replace
t := m.ReapValues(start)[0].GetType()
switch bft := baseOf(t).(type) {
case *FuncType:
rs := bft.Results
if len(rs) != 1 {
panic(fmt.Sprintf(
"cannot get type of function call with %d results",
len(rs)))
}
m.PushValue(asValue(rs[0].Type))
case *TypeType:
start := m.NumValues
m.PushOp(OpHalt)
m.PushExpr(x.Func)
m.PushOp(OpEval)
m.Run() // XXX replace
t := m.ReapValues(start)[0].GetType()
m.PushValue(asValue(t))
case *nativeType:
numRes := bft.Type.NumOut()
if numRes != 1 {
panic(fmt.Sprintf(
"cannot get type of (native) function call with %d results",
numRes))
}
res0 := bft.Type.Out(0)
m.PushValue(asValue(&nativeType{Type: res0}))
default:
panic(fmt.Sprintf(
"unexpected call of expression type %s",
t.String()))
}
t := getTypeOf(x)
m.PushValue(asValue(t))
case *IndexExpr:
start := m.NumValues
m.PushOp(OpHalt)
Expand Down Expand Up @@ -372,9 +337,15 @@ func (m *Machine) doOpTypeOf() {
m.PushOp(OpTypeOf)
m.Run() // XXX replace
xt := m.ReapValues(start)[0].V.(TypeValue).Type
m.PushValue(asValue(&SliceType{
Elt: xt.Elem(),
}))
if pt, ok := xt.(PointerType); ok {
m.PushValue(asValue(&SliceType{
Elt: pt.Elt.Elem(),
}))
} else {
m.PushValue(asValue(&SliceType{
Elt: xt.Elem(),
}))
}
case *StarExpr:
start := m.NumValues
m.PushOp(OpHalt)
Expand Down
Loading

0 comments on commit 0513f94

Please sign in to comment.