From d1f1340d47d7c56045a6bed686e6b110949b10ec Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 3 Feb 2019 14:01:19 +1100 Subject: [PATCH] Add a nothing built-in literal --- cmd/wuffs-c/internal/cgen/statement.go | 2 +- lang/check/bounds.go | 2 +- lang/check/type.go | 7 ++++++- lang/token/list.go | 6 ++++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index aaaad05c..64f65ea6 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -446,7 +446,7 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error { b.writes("return ") if g.currFunk.astFunc.Out() == nil { - return fmt.Errorf("TODO: allow empty return type (when not suspendible)") + b.writes("((wuffs_base__empty_struct){})") } else if err := g.writeExpr(b, retExpr, depth); err != nil { return err } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 615dfc7f..9acb3f33 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -308,7 +308,7 @@ func (q *checker) bcheckStatement(n *a.Node) error { if q.astFunc.Effect().Coroutine() { lTyp = typeExprStatus } else if lTyp == nil { - return fmt.Errorf("TODO: allow returning nothing") + lTyp = typeExprEmptyStruct } if _, err := q.bcheckAssignment1(nil, lTyp, t.IDEq, n.Value()); err != nil { return err diff --git a/lang/check/type.go b/lang/check/type.go index eedc54ec..668e94b9 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -173,7 +173,7 @@ func (q *checker) tcheckStatement(n *a.Node) error { if q.astFunc.Effect().Coroutine() { lTyp = typeExprStatus } else if lTyp == nil { - return fmt.Errorf("TODO: allow returning nothing") + lTyp = typeExprEmptyStruct } value := n.Value() if err := q.tcheckExpr(value, 0); err != nil { @@ -391,6 +391,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.SetMType(typeExprBool) return nil + case t.IDNothing: + n.SetConstValue(zero) + n.SetMType(typeExprEmptyStruct) + return nil + case t.IDNullptr: n.SetConstValue(zero) n.SetMType(typeExprNullptr) diff --git a/lang/token/list.go b/lang/token/list.go index 0dd118cf..254a0261 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -382,8 +382,9 @@ const ( IDFalse = ID(0xB0) IDTrue = ID(0xB1) - IDNullptr = ID(0xB2) - IDOk = ID(0xB3) + IDNothing = ID(0xB2) + IDNullptr = ID(0xB3) + IDOk = ID(0xB4) ID0 = ID(0xC0) ID1 = ID(0xC1) @@ -702,6 +703,7 @@ var builtInsByID = [nBuiltInIDs]string{ IDFalse: "false", IDTrue: "true", + IDNothing: "nothing", IDNullptr: "nullptr", IDOk: "ok",