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

+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},
},
'<': {