mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 08:55: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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/google/puffs/lang/builtin"
|
|
||||||
|
|
||||||
a "github.com/google/puffs/lang/ast"
|
a "github.com/google/puffs/lang/ast"
|
||||||
t "github.com/google/puffs/lang/token"
|
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:
|
case a.KReturn:
|
||||||
n := n.Return()
|
n := n.Return()
|
||||||
ret := status{}
|
retExpr := n.Value()
|
||||||
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
if g.currFunk.suspendible {
|
if g.currFunk.suspendible {
|
||||||
b.writes("status = ")
|
b.writes("status = ")
|
||||||
|
retKeyword := t.KeyStatus
|
||||||
if retExpr == nil {
|
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.
|
// TODO: check that retExpr has no call-suspendibles.
|
||||||
} else if err := g.writeExpr(
|
if err := g.writeExpr(
|
||||||
b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
b.writes(";")
|
b.writes(";")
|
||||||
if retExpr == nil {
|
|
||||||
switch ret.keyword.Key() {
|
switch retKeyword {
|
||||||
case 0:
|
case t.KeyError:
|
||||||
b.writes("goto ok;")
|
b.writes("goto exit;")
|
||||||
return nil
|
return nil
|
||||||
case t.KeyError:
|
case t.KeyStatus:
|
||||||
b.writes("goto exit;")
|
b.writes("goto ok;")
|
||||||
return nil
|
return nil
|
||||||
case t.KeySuspension:
|
|
||||||
// No-op.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return g.writeCoroSuspPoint(b, true)
|
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":
|
// Return is "return LHS":
|
||||||
// - ID0: <0|IDError|IDSuspension>
|
|
||||||
// - ID1: message
|
|
||||||
// - LHS: <nil|Expr>
|
// - LHS: <nil|Expr>
|
||||||
type Return Node
|
type Return Node
|
||||||
|
|
||||||
func (n *Return) Node() *Node { return (*Node)(n) }
|
func (n *Return) Node() *Node { return (*Node)(n) }
|
||||||
func (n *Return) Keyword() t.ID { return n.id0 }
|
func (n *Return) Value() *Expr { return n.lhs.Expr() }
|
||||||
func (n *Return) Message() t.ID { return n.id1 }
|
|
||||||
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{
|
return &Return{
|
||||||
kind: KReturn,
|
kind: KReturn,
|
||||||
id0: keyword,
|
|
||||||
id1: message,
|
|
||||||
lhs: value.Node(),
|
lhs: value.Node(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-18
@@ -158,24 +158,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
|||||||
|
|
||||||
case a.KReturn:
|
case a.KReturn:
|
||||||
n := n.Return()
|
n := n.Return()
|
||||||
if nk := n.Keyword(); nk != 0 {
|
if value := n.Value(); value != nil {
|
||||||
// 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 err := q.tcheckExpr(value, 0); err != nil {
|
if err := q.tcheckExpr(value, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-16
@@ -614,27 +614,14 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
|||||||
|
|
||||||
case t.KeyReturn:
|
case t.KeyReturn:
|
||||||
p.src = p.src[1:]
|
p.src = p.src[1:]
|
||||||
keyword, message, value, err := t.ID(0), t.ID(0), (*a.Expr)(nil), error(nil)
|
value, err := (*a.Expr)(nil), error(nil)
|
||||||
switch x := p.peek1(); x.Key() {
|
if p.peek1().Key() != t.KeySemicolon {
|
||||||
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 = p.parseExpr()
|
value, err = p.parseExpr()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return a.NewReturn(keyword, message, value).Node(), nil
|
return a.NewReturn(value).Node(), nil
|
||||||
|
|
||||||
case t.KeyVar:
|
case t.KeyVar:
|
||||||
p.src = p.src[1:]
|
p.src = p.src[1:]
|
||||||
|
|||||||
Reference in new issue
Block a user