Make the return value mandatory; add ok

This commit is contained in:
Nigel Tao committed 2018-11-24 15:09:28 +11:00
1 parent 24ace1c144
commit 762b320b5e
11 files changed
+45 -50

No files matched your search

+2 -4
View File
@@ -411,10 +411,8 @@ func (h *resumabilityHelper) doJump(r resumabilities, n *a.Jump, depth uint32) e
}
func (h *resumabilityHelper) doRet(r resumabilities, n *a.Ret, depth uint32) error {
if n.Value() != nil {
if err := h.doExpr(r, n.Value()); err != nil {
return err
}
if err := h.doExpr(r, n.Value()); err != nil {
return err
}
switch n.Keyword() {
+3 -8
View File
@@ -327,7 +327,7 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
if g.currFunk.suspendible {
isError, isOK := false, false
b.writes("status = ")
if retExpr == nil {
if retExpr.Operator() == 0 && retExpr.Ident() == t.IDOk {
b.writes("NULL")
isOK = true
} else {
@@ -364,13 +364,8 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
}
b.writes("return ")
if g.currFunk.astFunc.Out() == nil {
if retExpr != nil {
return fmt.Errorf("return expression %q incompatible with empty return type", retExpr.Str(g.tm))
}
} else if retExpr == nil {
// TODO: should a bare "return" imply "return out"?
return fmt.Errorf("empty return expression incompatible with non-empty return type")
if g.currFunk.astFunc.Out() == nil { // TODO: Out() can be nil if the func has ? effect.
return fmt.Errorf("TODO: allow empty return type (when not suspendible)")
} else if err := g.writeExpr(b, retExpr, replaceCallSuspendibles, depth); err != nil {
return err
}
+2 -1
View File
@@ -10,6 +10,7 @@
- Added an `io_bind` keyword.
- Added a `use` keyword.
- Added a `yield` keyword.
- Made the `return` value mandatory; added `ok` literal.
- Changed func out-type from struct to bare type.
- Renamed the implicit `in` variable to `args`.
- Added `std/adler32`, `std/crc32` and `std/gzip`.
@@ -60,4 +61,4 @@
---
Updated on May 2018.
Updated on November 2018.
+1 -1
View File
@@ -596,7 +596,7 @@ func NewIf(condition *Expr, bodyIfTrue []*Node, bodyIfFalse []*Node, elseIf *If)
// Ret is "return LHS" or "yield LHS":
// - ID0: <IDReturn|IDYield>
// - LHS: <nil|Expr>
// - LHS: <Expr>
type Ret Node
func (n *Ret) AsNode() *Node { return (*Node)(n) }
+2 -4
View File
@@ -298,10 +298,8 @@ func (q *checker) bcheckStatement(n *a.Node) error {
} else if lTyp == nil {
return fmt.Errorf("TODO: allow returning nothing")
}
if v := n.Value(); v != nil {
if err := q.bcheckAssignment1(nil, lTyp, t.IDEq, v); err != nil {
return err
}
if err := q.bcheckAssignment1(nil, lTyp, t.IDEq, n.Value()); err != nil {
return err
}
case a.KVar:
+13 -9
View File
@@ -191,15 +191,14 @@ func (q *checker) tcheckStatement(n *a.Node) error {
} else if lTyp == nil {
return fmt.Errorf("TODO: allow returning nothing")
}
if value := n.Value(); value != nil {
if err := q.tcheckExpr(value, 0); err != nil {
return err
}
rTyp := value.MType()
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinements(rTyp) {
return fmt.Errorf("check: cannot return %q (of type %q) as type %q",
value.Str(q.tm), rTyp.Str(q.tm), lTyp.Str(q.tm))
}
value := n.Value()
if err := q.tcheckExpr(value, 0); err != nil {
return err
}
rTyp := value.MType()
if !(rTyp.IsIdeal() && lTyp.IsNumType()) && !lTyp.EqIgnoringRefinements(rTyp) {
return fmt.Errorf("check: cannot return %q (of type %q) as type %q",
value.Str(q.tm), rTyp.Str(q.tm), lTyp.Str(q.tm))
}
case a.KVar:
@@ -431,6 +430,11 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
n.SetConstValue(zero)
n.SetMType(typeExprNullptr)
return nil
case t.IDOk:
n.SetConstValue(zero)
n.SetMType(typeExprStatus)
return nil
}
case t.IDOpenParen, t.IDTry:
+3 -6
View File
@@ -603,12 +603,9 @@ func (p *parser) parseStatement1() (*a.Node, error) {
case t.IDReturn, t.IDYield:
p.src = p.src[1:]
value, err := (*a.Expr)(nil), error(nil)
if p.peek1() != t.IDSemicolon {
value, err = p.parseExpr()
if err != nil {
return nil, err
}
value, err := p.parseExpr()
if err != nil {
return nil, err
}
return a.NewRet(x, value).AsNode(), nil
+13 -11
View File
@@ -368,24 +368,25 @@ const (
const (
minBuiltInLiteral = 0xB0
minBuiltInNumLiteral = 0xB3
minBuiltInNumLiteral = 0xB4
maxBuiltInNumLiteral = 0xBF
maxBuiltInLiteral = 0xBF
IDFalse = ID(0xB0)
IDTrue = ID(0xB1)
IDNullptr = ID(0xB2)
IDOk = ID(0xB3)
ID0 = ID(0xB3)
ID1 = ID(0xB4)
ID2 = ID(0xB5)
ID4 = ID(0xB6)
ID8 = ID(0xB7)
ID16 = ID(0xB8)
ID32 = ID(0xB9)
ID64 = ID(0xBA)
ID128 = ID(0xBB)
ID256 = ID(0xBC)
ID0 = ID(0xB4)
ID1 = ID(0xB5)
ID2 = ID(0xB6)
ID4 = ID(0xB7)
ID8 = ID(0xB8)
ID16 = ID(0xB9)
ID32 = ID(0xBA)
ID64 = ID(0xBB)
ID128 = ID(0xBC)
ID256 = ID(0xBD)
)
const (
@@ -653,6 +654,7 @@ var builtInsByID = [nBuiltInIDs]string{
IDFalse: "false",
IDTrue: "true",
IDNullptr: "nullptr",
IDOk: "ok",
ID0: "0",
ID1: "1",
+1 -1
View File
@@ -231,7 +231,7 @@ pri func decoder.decode_uncompressed!??(dst base.io_writer, src base.io_reader)
while true {
var n_copied base.u32 = args.dst.copy_n_from_reader!(n:length, r:args.src)
if length <= n_copied {
return
return ok
}
length -= n_copied
if args.dst.available() == 0 {
+4 -4
View File
@@ -366,10 +366,10 @@ pri func decoder.decode_extension!??(src base.io_reader) {
var label base.u8 = args.src.read_u8!??()
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
this.decode_gc!??(src:args.src)
return
return ok
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
this.decode_ae!??(src:args.src)
return
return ok
}
// We skip over all other extensions, including 0x01 "Plain Text Label" and
// 0xFE "Comment Label".
@@ -380,7 +380,7 @@ pri func decoder.skip_blocks!??(src base.io_reader) {
while true {
var block_size base.u8 = args.src.read_u8!??()
if block_size == 0 {
return
return ok
}
args.src.skip!??(n:block_size as base.u32)
}
@@ -404,7 +404,7 @@ pri func decoder.decode_ae!??(src base.io_reader) {
var c base.u8
var block_size base.u8 = args.src.read_u8!??()
if block_size == 0 {
return
return ok
}
// Look only for an 11 byte "ANIMEXTS1.0" or "NETSCAPE2.0" extension,
+1 -1
View File
@@ -256,7 +256,7 @@ pri func decoder.flush!??(dst base.io_writer) {
var s slice base.u8 = this.output[i:j]
var n base.u64 = args.dst.copy_from_slice!(s:s)
if n == s.length() {
return
return ok
}
i = (i + ((n & 8191) as base.u32)) & 8191
yield base."$short write"