Add a "status" type and "try" keyword.

This commit is contained in:
Nigel Tao committed 2017-09-01 16:41:42 +10:00
1 parent 363dcbe714
commit 935cd73686
11 files changed
+94 -24

No files matched your search

+26 -7
View File
@@ -722,13 +722,22 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
case a.KReturn:
n := n.Return()
ret := status{}
if n.Keyword() == 0 {
ret.name = fmt.Sprintf("%sSTATUS_OK", g.PKGPREFIX)
} else {
retExpr := (*a.Expr)(nil)
if n.Keyword() != 0 {
ret = g.statusMap[n.Message()]
} else if retExpr = n.Value(); retExpr == nil {
ret.name = fmt.Sprintf("%sSTATUS_OK", g.PKGPREFIX)
}
if g.currFunk.suspendible {
b.printf("status = %s;", ret.name)
b.writes("status = ")
if retExpr == nil {
b.writes(ret.name)
// TODO: check that retExpr has no call-suspendibles.
} else if err := g.writeExpr(
b, retExpr, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err
}
b.writes(";")
if ret.isError {
b.writes("goto exit;")
} else {
@@ -1104,12 +1113,19 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_blocks", 2) {
b.printf("status = %s%s_decode_blocks(self, %sdst, %ssrc);\n",
// TODO: don't hard code being inside a try call.
if g.currFunk.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
temp := g.currFunk.tempW
g.currFunk.tempW++
b.printf("%sstatus %s%d = %s%s_decode_blocks(self, %sdst, %ssrc);\n",
g.pkgPrefix, tPrefix, temp,
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix)
if err := g.writeLoadExprDerivedVars(b, n); err != nil {
return err
}
b.writes("if (status) { goto suspend; }\n")
} else if isThisMethod(g.tm, n, "decode_uncompressed", 2) {
b.printf("status = %s%s_decode_uncompressed(self, %sdst, %ssrc);\n",
@@ -1465,7 +1481,10 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
// TODO: check that n.Args() is "(src:in.src)".
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != nArgs {
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return false
}
if !n.CallSuspendible() || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
+10 -4
View File
@@ -161,6 +161,7 @@ typedef struct {
struct {
uint32_t coro_susp_point;
puffs_flate_status v_z;
} c_decode[1];
struct {
uint32_t coro_susp_point;
@@ -522,17 +523,21 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
}
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
puffs_flate_status v_z;
uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point;
if (coro_susp_point) {
v_z = self->private_impl.c_decode[0].v_z;
}
switch (coro_susp_point) {
PUFFS_COROUTINE_SUSPENSION_POINT(0);
PUFFS_COROUTINE_SUSPENSION_POINT(1);
status = puffs_flate_decoder_decode_blocks(self, a_dst, a_src);
if (status) {
goto suspend;
}
puffs_flate_status t_0 =
puffs_flate_decoder_decode_blocks(self, a_dst, a_src);
v_z = t_0;
status = v_z;
goto suspend;
self->private_impl.c_decode[0].coro_susp_point = 0;
goto exit;
}
@@ -540,6 +545,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
goto suspend;
suspend:
self->private_impl.c_decode[0].coro_susp_point = coro_susp_point;
self->private_impl.c_decode[0].v_z = v_z;
exit:
self->private_impl.status = status;
+1
View File
@@ -161,6 +161,7 @@ typedef struct {
struct {
uint32_t coro_susp_point;
puffs_flate_status v_z;
} c_decode[1];
struct {
uint32_t coro_susp_point;
+2
View File
@@ -201,6 +201,8 @@ const MaxExprDepth = 255
//
// For function calls, like "LHS(List0)", ID0 is IDOpenParen.
//
// For try/function calls, like "try LHS(List0)", ID0 is IDTry.
//
// For indexes, like "LHS[RHS]", ID0 is IDOpenBracket.
//
// For slices, like "LHS[MHS:RHS]", ID0 is IDColon.
+5 -1
View File
@@ -31,6 +31,10 @@ func (n *Expr) appendString(buf []byte, tm *t.Map, parenthesize bool, depth uint
case 0:
buf = append(buf, tm.ByID(n.id1)...)
case t.KeyTry:
buf = append(buf, "try "...)
fallthrough
case t.KeyOpenParen:
buf = n.lhs.Expr().appendString(buf, tm, true, depth)
if n.flags&FlagsSuspendible != 0 {
@@ -77,7 +81,7 @@ func (n *Expr) appendString(buf []byte, tm *t.Map, parenthesize bool, depth uint
buf = append(buf, ')')
case t.KeyLimit:
// TODO.
buf = append(buf, "TODO(limit)"...)
}
case t.FlagsUnaryOp:
+1 -1
View File
@@ -641,7 +641,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
case 0:
// No-op.
case t.KeyOpenParen:
case t.KeyOpenParen, t.KeyTry:
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
if isInSrc(q.tm, n, t.KeyReadU8, 0) ||
isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU32BE, 0) ||
+5 -4
View File
@@ -56,10 +56,11 @@ func trimQuotes(s string) string {
// typeExprFoo is an *ast.Node MType (implicit type).
var (
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil)
typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
// TODO: delete this.
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
+10 -4
View File
@@ -340,7 +340,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
// TODO.
}
case t.KeyOpenParen:
case t.KeyOpenParen, t.KeyTry:
// n is a function call.
// TODO: delete this hack that only matches "in.src.read_u8?()" etc.
if isInSrc(q.tm, n, t.KeyReadU8, 0) ||
@@ -364,7 +364,9 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return err
}
}
if isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) {
if n.ID0().Key() == t.KeyTry {
n.SetMType(typeExprStatus)
} else if isInSrc(q.tm, n, t.KeyReadU32BE, 0) || isInSrc(q.tm, n, t.KeyReadU32LE, 0) {
n.SetMType(typeExprPlaceholder32) // HACK.
} else if isInSrc(q.tm, n, t.KeyReadU16BE, 0) || isInSrc(q.tm, n, t.KeyReadU16LE, 0) {
n.SetMType(typeExprPlaceholder16) // HACK.
@@ -528,7 +530,10 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
// TODO: check that n.Args() is "(src:in.src)".
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != nArgs {
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
return false
}
if !n.CallSuspendible() || len(n.Args()) != nArgs {
return false
}
n = n.LHS().Expr()
@@ -885,7 +890,8 @@ swtch:
if n.Min() != nil || n.Max() != nil {
// TODO: reject. You can only refine numeric types.
}
if name := n.Name().Key(); name == t.KeyBool || name == t.KeyReader1 || name == t.KeyWriter1 {
if name := n.Name().Key(); name == t.KeyBool || name == t.KeyStatus ||
name == t.KeyReader1 || name == t.KeyWriter1 {
break
}
for _, s := range q.c.structs {
+26 -2
View File
@@ -614,12 +614,18 @@ func (p *parser) parseStatement1() (*a.Node, error) {
value := (*a.Expr)(nil)
if p.peek1().Key() == t.KeyEq {
p.src = p.src[1:]
if p.peek1().Key() == t.KeyLimit {
switch p.peek1().Key() {
case t.KeyLimit:
value, err = p.parseLimitExpr()
if err != nil {
return nil, err
}
} else {
case t.KeyTry:
value, err = p.parseTryExpr()
if err != nil {
return nil, err
}
default:
value, err = p.parseExpr()
if err != nil {
return nil, err
@@ -735,6 +741,24 @@ func (p *parser) parseLimitExpr() (*a.Expr, error) {
return a.NewExpr(0, t.IDLimit, 0, lhs.Node(), nil, rhs.Node(), nil), nil
}
func (p *parser) parseTryExpr() (*a.Expr, error) {
if x := p.peek1().Key(); x != t.KeyTry {
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected "try", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
call, err := p.parseExpr()
if err != nil {
return nil, err
}
if call.ID0() != t.IDOpenParen {
return nil, fmt.Errorf(`parse: expected function call after "try", got %q at %s:%d`,
call.String(p.tm), p.filename, p.line())
}
return a.NewExpr(call.Node().Raw().Flags(), t.IDTry, call.ID1(),
call.LHS(), call.MHS(), call.RHS(), call.Args()), nil
}
func (p *parser) parseExprNode() (*a.Node, error) {
n, err := p.parseExpr()
if err != nil {
+6
View File
@@ -215,6 +215,7 @@ const (
KeyLimit = Key(IDLimit >> KeyShift)
KeyPackageID = Key(IDPackageID >> KeyShift)
KeyConst = Key(IDConst >> KeyShift)
KeyTry = Key(IDTry >> KeyShift)
KeyFalse = Key(IDFalse >> KeyShift)
KeyTrue = Key(IDTrue >> KeyShift)
@@ -234,6 +235,7 @@ const (
KeyBuf2 = Key(IDBuf2 >> KeyShift)
KeyReader1 = Key(IDReader1 >> KeyShift)
KeyWriter1 = Key(IDWriter1 >> KeyShift)
KeyStatus = Key(IDStatus >> KeyShift)
KeyUnderscore = Key(IDUnderscore >> KeyShift)
KeyThis = Key(IDThis >> KeyShift)
@@ -383,6 +385,7 @@ const (
IDLimit = ID(0x75<<KeyShift | FlagsOther)
IDPackageID = ID(0x76<<KeyShift | FlagsOther)
IDConst = ID(0x77<<KeyShift | FlagsOther)
IDTry = ID(0x78<<KeyShift | FlagsOther)
IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
@@ -402,6 +405,7 @@ const (
IDBuf2 = ID(0x9B<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDReader1 = ID(0x9C<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDWriter1 = ID(0x9D<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDStatus = ID(0x9E<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDUnderscore = ID(0xA0<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
IDThis = ID(0xA1<<KeyShift | FlagsIdent | FlagsImplicitSemicolon)
@@ -571,6 +575,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyLimit: {"limit", IDLimit},
KeyPackageID: {"packageid", IDPackageID},
KeyConst: {"const", IDConst},
KeyTry: {"try", IDTry},
KeyFalse: {"false", IDFalse},
KeyTrue: {"true", IDTrue},
@@ -592,6 +597,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyBuf2: {"buf2", IDBuf2},
KeyReader1: {"reader1", IDReader1},
KeyWriter1: {"writer1", IDWriter1},
KeyStatus: {"status", IDStatus},
KeyUnderscore: {"_", IDUnderscore},
KeyThis: {"this", IDThis},
+2 -1
View File
@@ -91,7 +91,8 @@ pub struct decoder?(
pub func decoder.decode?(dst writer1, src reader1)() {
// TODO: copy from dst to history, if we aren't finished.
this.decode_blocks?(dst:in.dst, src:in.src)
var z status = try this.decode_blocks?(dst:in.dst, src:in.src)
return z
}
pri func decoder.decode_blocks?(dst writer1, src reader1)() {