mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Remove the return error "foo" special case.
This commit is contained in:
1 parent
7cefcccc98
commit
4b572702f9
4 files changed
+25
-78
No files matched your search
@@ -16,9 +16,6 @@ package cgen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/puffs/lang/builtin"
|
||||
|
||||
a "github.com/google/puffs/lang/ast"
|
||||
t "github.com/google/puffs/lang/token"
|
||||
@@ -186,44 +183,30 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
|
||||
case a.KReturn:
|
||||
n := n.Return()
|
||||
ret := status{}
|
||||
retExpr := (*a.Expr)(nil)
|
||||
if n.Keyword() != 0 {
|
||||
ret = g.statusMap[n.Message()]
|
||||
if ret.name == "" {
|
||||
msg := builtin.TrimQuotes(n.Message().String(g.tm))
|
||||
z := builtin.StatusMap[msg]
|
||||
if z.Message == "" {
|
||||
return fmt.Errorf("no status code for %q", msg)
|
||||
}
|
||||
ret.name = strings.ToUpper(g.cName(z.String()))
|
||||
ret.keyword = z.Keyword
|
||||
}
|
||||
} else if retExpr = n.Value(); retExpr == nil {
|
||||
ret.name = g.PKGPREFIX + "STATUS_OK"
|
||||
}
|
||||
retExpr := n.Value()
|
||||
|
||||
if g.currFunk.suspendible {
|
||||
b.writes("status = ")
|
||||
retKeyword := t.KeyStatus
|
||||
if retExpr == nil {
|
||||
b.writes(ret.name)
|
||||
b.printf("%s%s", g.PKGPREFIX, "STATUS_OK")
|
||||
} else {
|
||||
retKeyword = retExpr.ID0().Key()
|
||||
// TODO: check that retExpr has no call-suspendibles.
|
||||
} else if err := g.writeExpr(
|
||||
b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
if err := g.writeExpr(
|
||||
b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
b.writes(";")
|
||||
if retExpr == nil {
|
||||
switch ret.keyword.Key() {
|
||||
case 0:
|
||||
b.writes("goto ok;")
|
||||
return nil
|
||||
case t.KeyError:
|
||||
b.writes("goto exit;")
|
||||
return nil
|
||||
case t.KeySuspension:
|
||||
// No-op.
|
||||
}
|
||||
|
||||
switch retKeyword {
|
||||
case t.KeyError:
|
||||
b.writes("goto exit;")
|
||||
return nil
|
||||
case t.KeyStatus:
|
||||
b.writes("goto ok;")
|
||||
return nil
|
||||
}
|
||||
return g.writeCoroSuspPoint(b, true)
|
||||
}
|
||||
|
||||
+4
-10
@@ -481,22 +481,16 @@ func NewIf(condition *Expr, elseIf *If, bodyIfTrue []*Node, bodyIfFalse []*Node)
|
||||
}
|
||||
}
|
||||
|
||||
// Return is "return LHS", "return error ID1" or "return suspension ID1":
|
||||
// - ID0: <0|IDError|IDSuspension>
|
||||
// - ID1: message
|
||||
// Return is "return LHS":
|
||||
// - LHS: <nil|Expr>
|
||||
type Return Node
|
||||
|
||||
func (n *Return) Node() *Node { return (*Node)(n) }
|
||||
func (n *Return) Keyword() t.ID { return n.id0 }
|
||||
func (n *Return) Message() t.ID { return n.id1 }
|
||||
func (n *Return) Value() *Expr { return n.lhs.Expr() }
|
||||
func (n *Return) Node() *Node { return (*Node)(n) }
|
||||
func (n *Return) Value() *Expr { return n.lhs.Expr() }
|
||||
|
||||
func NewReturn(keyword t.ID, message t.ID, value *Expr) *Return {
|
||||
func NewReturn(value *Expr) *Return {
|
||||
return &Return{
|
||||
kind: KReturn,
|
||||
id0: keyword,
|
||||
id1: message,
|
||||
lhs: value.Node(),
|
||||
}
|
||||
}
|
||||
|
||||
+1
-18
@@ -158,24 +158,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
|
||||
case a.KReturn:
|
||||
n := n.Return()
|
||||
if nk := n.Keyword(); nk != 0 {
|
||||
// TODO: delete.
|
||||
sk := t.ID(0)
|
||||
if s, ok := q.c.statuses[n.Message()]; ok {
|
||||
sk = s.Status.Keyword()
|
||||
} else {
|
||||
msg := builtin.TrimQuotes(n.Message().String(q.tm))
|
||||
z, ok := builtin.StatusMap[msg]
|
||||
if !ok {
|
||||
return fmt.Errorf("check: no error or status with message %q", msg)
|
||||
}
|
||||
sk = z.Keyword
|
||||
}
|
||||
if nk != sk {
|
||||
return fmt.Errorf("check: return statement says %q but declaration says %q",
|
||||
nk.String(q.tm), sk.String(q.tm))
|
||||
}
|
||||
} else if value := n.Value(); value != nil {
|
||||
if value := n.Value(); value != nil {
|
||||
if err := q.tcheckExpr(value, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+3
-16
@@ -614,27 +614,14 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
|
||||
case t.KeyReturn:
|
||||
p.src = p.src[1:]
|
||||
keyword, message, value, err := t.ID(0), t.ID(0), (*a.Expr)(nil), error(nil)
|
||||
switch x := p.peek1(); x.Key() {
|
||||
case t.KeySemicolon:
|
||||
// No-op.
|
||||
case t.KeyError, t.KeySuspension:
|
||||
// TODO: delete.
|
||||
keyword = x
|
||||
p.src = p.src[1:]
|
||||
message = p.peek1()
|
||||
if !message.IsStrLiteral() {
|
||||
got := p.tm.ByID(message)
|
||||
return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
default:
|
||||
value, err := (*a.Expr)(nil), error(nil)
|
||||
if p.peek1().Key() != t.KeySemicolon {
|
||||
value, err = p.parseExpr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return a.NewReturn(keyword, message, value).Node(), nil
|
||||
return a.NewReturn(value).Node(), nil
|
||||
|
||||
case t.KeyVar:
|
||||
p.src = p.src[1:]
|
||||
|
||||
Reference in new issue
Block a user