Replace "= try foo" with "=? foo"

This commit is contained in:
Nigel Tao committed 2018-12-01 13:11:47 +11:00
1 parent ad9851ea75
commit 9741eada02
14 files changed
+75 -82

No files matched your search

-2
View File
@@ -303,8 +303,6 @@ func (h *resumabilityHelper) doExpr1(r resumabilities, n *a.Expr, sef subExprFil
switch n.Operator() {
case t.IDOpenParen:
r.raiseNoneToWeak()
case t.IDTry:
// No-op.
default:
return fmt.Errorf("unrecognized ast.Expr coroutine operator")
}
+17 -12
View File
@@ -402,11 +402,18 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
}
func (g *gen) writeStatementVar(b *buffer, n *a.Var, depth uint32) error {
if v := n.Value(); v != nil {
if err := g.writeSuspendibles(b, v, depth); err != nil {
if v := n.Value(); v != nil && v.Effect().Coroutine() {
eqQuestion := n.Operator() == t.IDEqQuestion
if !eqQuestion {
if err := g.writeCoroSuspPoint(b, false); err != nil {
return err
}
}
if err := g.writeCallSuspendibles(b, v, depth, eqQuestion); err != nil {
return err
}
}
if nTyp := n.XType(); nTyp.IsArrayType() {
if n.Value() != nil {
// TODO: something like:
@@ -512,15 +519,13 @@ func (g *gen) writeSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if !n.Effect().Coroutine() {
return nil
}
if n.Operator() != t.IDTry {
if err := g.writeCoroSuspPoint(b, false); err != nil {
return err
}
if err := g.writeCoroSuspPoint(b, false); err != nil {
return err
}
return g.writeCallSuspendibles(b, n, depth)
return g.writeCallSuspendibles(b, n, depth, false)
}
func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32, eqQuestion bool) error {
if depth > a.MaxExprDepth {
return fmt.Errorf("expression recursion depth too large")
}
@@ -531,14 +536,14 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if e := n.Effect(); !e.RootCause() || !e.Coroutine() {
for _, o := range n.AsNode().AsRaw().SubNodes() {
if o != nil && o.Kind() == a.KExpr {
if err := g.writeCallSuspendibles(b, o.AsExpr(), depth); err != nil {
if err := g.writeCallSuspendibles(b, o.AsExpr(), depth, false); err != nil {
return err
}
}
}
for _, o := range n.Args() {
if o != nil && o.Kind() == a.KExpr {
if err := g.writeCallSuspendibles(b, o.AsExpr(), depth); err != nil {
if err := g.writeCallSuspendibles(b, o.AsExpr(), depth, false); err != nil {
return err
}
}
@@ -554,7 +559,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
return err
}
if n.Operator() == t.IDTry {
if eqQuestion {
if g.currFunk.tempW > maxTemp {
return fmt.Errorf("too many temporary variables required")
}
@@ -575,7 +580,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
return err
}
if n.Operator() != t.IDTry {
if !eqQuestion {
b.writes("if (status) { goto suspend; }\n")
}
return nil
+2 -2
View File
@@ -180,7 +180,7 @@ func (g *gen) writeLoadExprDerivedVars(b *buffer, n *a.Expr) error {
if g.currFunk.derivedVars == nil {
return nil
}
if k := n.Operator(); k != t.IDOpenParen && k != t.IDTry {
if n.Operator() != t.IDOpenParen {
return nil
}
for _, o := range n.Args() {
@@ -205,7 +205,7 @@ func (g *gen) writeSaveExprDerivedVars(b *buffer, n *a.Expr) error {
if g.currFunk.derivedVars == nil {
return nil
}
if k := n.Operator(); k != t.IDOpenParen && k != t.IDTry {
if n.Operator() != t.IDOpenParen {
return nil
}
for _, o := range n.Args() {
+2 -1
View File
@@ -25,6 +25,7 @@
- Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc.
- Renamed `u32`, `io_reader`, etc to `base.u32`, `base.io_reader`, etc.
- Renamed `unread_u8?` to `undo_byte!`; added `can_undo_byte`.
- Replaced `= try foo` with `=? foo`.
- Redesigned `!` vs `?` effects; prohibited effect-ful subexpressions.
- Redesigned iterate blocks.
- Added `{frame,image,pixel}_config` and `pixel_buffer` types.
@@ -62,4 +63,4 @@
---
Updated on November 2018.
Updated on December 2018.
+4 -5
View File
@@ -326,8 +326,6 @@ 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.
@@ -447,15 +445,16 @@ func NewAssign(operator t.ID, lhs *Expr, rhs *Expr) *Assign {
}
}
// Var is "var ID2 LHS" or "var ID2 LHS = RHS" or an iterate variable
// declaration "ID1 LHS =: RHS":
// - ID0: <0|IDEq|IDEqColon>
// Var is "var ID2 LHS" or "var ID2 LHS = RHS" or "var ID2 LHS =? etc!?(etc)"
// or an iterate variable declaration "ID1 LHS =: RHS":
// - ID0: <0|IDEq|IDEqColon|IDEqQuestion>
// - ID2: name
// - LHS: <TypeExpr>
// - RHS: <nil|Expr>
type Var Node
func (n *Var) AsNode() *Node { return (*Node)(n) }
func (n *Var) Operator() t.ID { return n.id0 }
func (n *Var) IterateVariable() bool { return n.id0 == t.IDEqColon }
func (n *Var) Name() t.ID { return n.id2 }
func (n *Var) XType() *TypeExpr { return n.lhs.AsTypeExpr() }
-4
View File
@@ -89,10 +89,6 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
case 0:
buf = append(buf, tm.ByID(n.id2)...)
case t.IDTry:
buf = append(buf, "try "...)
fallthrough
case t.IDOpenParen:
buf = n.lhs.AsExpr().appendStr(buf, tm, true, depth)
buf = append(buf, n.flags.AsEffect().String()...)
+1 -1
View File
@@ -769,7 +769,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (a.Bounds, error) {
}
}
case t.IDOpenParen, t.IDTry:
case t.IDOpenParen:
lhs := n.LHS().AsExpr()
if _, err := q.bcheckExpr(lhs, depth); err != nil {
return a.Bounds{}, err
+12 -12
View File
@@ -437,7 +437,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return nil
}
case t.IDOpenParen, t.IDTry:
case t.IDOpenParen:
// n is a function call.
return q.tcheckExprCall(n, depth)
@@ -578,19 +578,19 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
setPlaceholderMBoundsMType(o.AsNode())
}
if n.Operator() == t.IDTry {
n.SetMType(typeExprStatus)
} else {
oTyp := f.Out()
if oTyp == nil {
n.SetMType(typeExprEmptyStruct)
} else if genericType1 != nil && oTyp.Eq(typeExprGeneric1) {
n.SetMType(genericType1)
} else if genericType2 != nil && oTyp.Eq(typeExprGeneric2) {
n.SetMType(genericType2)
oTyp := f.Out()
if oTyp == nil {
if n.Effect().Optional() {
n.SetMType(typeExprStatus)
} else {
n.SetMType(oTyp)
n.SetMType(typeExprEmptyStruct)
}
} else if genericType1 != nil && oTyp.Eq(typeExprGeneric1) {
n.SetMType(genericType1)
} else if genericType2 != nil && oTyp.Eq(typeExprGeneric2) {
n.SetMType(genericType2)
} else {
n.SetMType(oTyp)
}
return nil
}
+23 -30
View File
@@ -644,6 +644,9 @@ func (p *parser) parseStatement1() (*a.Node, error) {
}
if op := p.peek1(); op.IsAssign() {
if op == t.IDEqQuestion {
return nil, fmt.Errorf(`parse: TODO: support "=?" without "var" at %s:%d`, p.filename, p.line())
}
p.src = p.src[1:]
rhs, err := p.parseExpr()
if err != nil {
@@ -874,13 +877,20 @@ func (p *parser) parseVarNode(inIterate bool) (*a.Node, error) {
value.Str(p.tm), p.filename, p.line())
}
} else if p.peek1() == t.IDEq {
op = t.IDEq
} else if eqOp := p.peek1(); eqOp == t.IDEq || eqOp == t.IDEqQuestion {
op = eqOp
p.src = p.src[1:]
value, err = p.parseExpr()
if err != nil {
return nil, err
}
if op == t.IDEqQuestion {
if (value.Operator() != t.IDOpenParen) || (!value.Effect().Optional()) {
return nil, fmt.Errorf(`parse: expected ?-function call after "=?", got %q at %s:%d`,
value.Str(p.tm), p.filename, p.line())
}
}
}
return a.NewVar(op, id, typ, value).AsNode(), nil
@@ -985,35 +995,18 @@ func (p *parser) parseOperand() (*a.Expr, error) {
p.src = p.src[1:]
return a.NewExpr(0, 0, 0, x, nil, nil, nil, nil), nil
default:
switch x {
case t.IDTry:
p.src = p.src[1:]
call, err := p.parseOperand()
if err != nil {
return nil, err
}
callFlags := call.AsNode().AsRaw().Flags()
if (call.Operator() != t.IDOpenParen) || (!callFlags.AsEffect().Optional()) {
return nil, fmt.Errorf(`parse: expected ?-function call after "try", got %q at %s:%d`,
call.Str(p.tm), p.filename, p.line())
}
return a.NewExpr(callFlags, x, 0, call.Ident(),
call.LHS(), call.MHS(), call.RHS(), call.Args()), nil
case t.IDOpenParen:
p.src = p.src[1:]
expr, err := p.parseExpr()
if err != nil {
return nil, err
}
if x := p.peek1(); x != t.IDCloseParen {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
return expr, nil
case x == t.IDOpenParen:
p.src = p.src[1:]
expr, err := p.parseExpr()
if err != nil {
return nil, err
}
if x := p.peek1(); x != t.IDCloseParen {
got := p.tm.ByID(x)
return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
return expr, nil
}
id, err := p.parseIdent()
+10 -9
View File
@@ -239,8 +239,9 @@ const (
IDTildeSatPlusEq = ID(0x1D)
IDTildeSatMinusEq = ID(0x1E)
IDEq = ID(0x2E)
IDEqColon = ID(0x2F)
IDEq = ID(0x20)
IDEqColon = ID(0x21)
IDEqQuestion = ID(0x22)
)
const (
@@ -349,10 +350,9 @@ const (
IDPub = ID(0x8F)
IDPri = ID(0x90)
IDConst = ID(0x91)
IDTry = ID(0x92)
IDIterate = ID(0x93)
IDYield = ID(0x94)
IDIOBind = ID(0x95)
IDIterate = ID(0x92)
IDYield = ID(0x93)
IDIOBind = ID(0x94)
)
const (
@@ -622,8 +622,9 @@ var builtInsByID = [nBuiltInIDs]string{
IDTildeSatPlusEq: "~sat+=",
IDTildeSatMinusEq: "~sat-=",
IDEq: "=",
IDEqColon: "=:",
IDEq: "=",
IDEqColon: "=:",
IDEqQuestion: "=?",
IDPlus: "+",
IDMinus: "-",
@@ -673,7 +674,6 @@ var builtInsByID = [nBuiltInIDs]string{
IDPub: "pub",
IDPri: "pri",
IDConst: "const",
IDTry: "try",
IDIterate: "iterate",
IDYield: "yield",
IDIOBind: "io_bind",
@@ -988,6 +988,7 @@ var lexers = [256][]suffixLexer{
'=': {
{"=", IDEqEq},
{":", IDEqColon},
{"?", IDEqQuestion},
{"", IDEq},
},
'<': {
+1 -1
View File
@@ -124,7 +124,7 @@ pub struct decoder?(
pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
while true {
args.dst.set_mark!()
var z base.status = try this.decode_blocks!??(dst:args.dst, src:args.src)
var z base.status =? this.decode_blocks!??(dst:args.dst, src:args.src)
if not z.is_suspension() {
return z
}
+1 -1
View File
@@ -634,7 +634,7 @@ pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reade
// Should calling r.set_mark also require r.is_bound()?
args.src.set_limit!(l:block_size)
args.src.set_mark!()
var z base.status = try this.lzw.decode!??(dst:w, src:args.src)
var z base.status =? this.lzw.decode!??(dst:w, src:args.src)
this.uncompressed_wi = 4096 - (w.available().min(x:4096) as base.u32)
block_size ~sat-= args.src.since_mark().length()
}
+1 -1
View File
@@ -93,7 +93,7 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
var decoded_length_got base.u32
while true {
args.dst.set_mark!()
var z base.status = try this.flate.decode!??(dst:args.dst, src:args.src)
var z base.status =? this.flate.decode!??(dst:args.dst, src:args.src)
if not this.ignore_checksum {
checksum_got = this.checksum.update!(x:args.dst.since_mark())
decoded_length_got ~mod+= ((args.dst.since_mark().length() & 0xFFFFFFFF) as base.u32)
+1 -1
View File
@@ -51,7 +51,7 @@ pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
var checksum_got base.u32
while true {
args.dst.set_mark!()
var z base.status = try this.flate.decode!??(dst:args.dst, src:args.src)
var z base.status =? this.flate.decode!??(dst:args.dst, src:args.src)
if not this.ignore_checksum {
checksum_got = this.checksum.update!(x:args.dst.since_mark())
}