Drop the "= RHS" out of "var x T = RHS"

This commit is contained in:
Nigel Tao committed 2018-12-27 11:29:47 +11:00
1 parent cbe19f50e0
commit 6dc68950c0
7 files changed
+20 -68

No files matched your search

+2 -8
View File
@@ -434,26 +434,20 @@ func NewAssign(operator t.ID, lhs *Expr, rhs *Expr) *Assign {
}
}
// Var is "var ID2 LHS" or "var ID2 LHS = RHS" or "var ID2 LHS =? etc!?(etc)":
// - ID0: <0|IDEq|IDEqQuestion>
// Var is "var ID2 LHS":
// - 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) Name() t.ID { return n.id2 }
func (n *Var) XType() *TypeExpr { return n.lhs.AsTypeExpr() }
func (n *Var) Value() *Expr { return n.rhs.AsExpr() }
func NewVar(op t.ID, name t.ID, xType *TypeExpr, value *Expr) *Var {
func NewVar(name t.ID, xType *TypeExpr) *Var {
return &Var{
kind: KVar,
id0: op,
id2: name,
lhs: xType.AsNode(),
rhs: value.AsNode(),
}
}
+4 -7
View File
@@ -699,13 +699,10 @@ func (q *checker) bcheckVar(n *a.Var) error {
lhs := a.NewExpr(0, 0, 0, n.Name(), nil, nil, nil, nil)
lhs.SetMType(n.XType())
rhs := n.Value()
if rhs == nil {
// "var x T" has an implicit "= 0".
//
// TODO: check that T is an integer type.
rhs = zeroExpr
}
// "var x T" has an implicit "= 0".
//
// TODO: check that T is an integer type.
rhs := zeroExpr
return q.bcheckAssignment(lhs, t.IDEq, rhs)
}
+1 -13
View File
@@ -193,19 +193,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
if n.XType().AsNode().MType() == nil {
return fmt.Errorf("check: internal error: unchecked type expression %q", n.XType().Str(q.tm))
}
if value := n.Value(); value != nil {
if err := q.tcheckExpr(value, 0); err != nil {
return err
}
lTyp := n.XType()
rTyp := value.MType()
if err := q.tcheckEq(n.Name(), nil, lTyp, value, rTyp); err != nil {
return err
}
} else {
// TODO: check that the default zero value is assignable to n.XType().
}
// TODO: check that the default zero value is assignable to n.XType().
case a.KWhile:
n := n.AsWhile()
+8 -25
View File
@@ -694,6 +694,13 @@ func (p *parser) parseAssignNode() (*a.Node, error) {
if err != nil {
return nil, err
}
if op == t.IDEqQuestion {
if (rhs.Operator() != t.IDOpenParen) || (!rhs.Effect().Optional()) {
return nil, fmt.Errorf(`parse: expected ?-function call after "=?", got %q at %s:%d`,
rhs.Str(p.tm), p.filename, p.line())
}
}
}
if p.funcEffect.WeakerThan(rhs.Effect()) {
@@ -997,31 +1004,7 @@ func (p *parser) parseVarNode() (*a.Node, error) {
if err != nil {
return nil, err
}
value := (*a.Expr)(nil)
op := t.ID(0)
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())
}
}
if p.funcEffect.WeakerThan(value.Effect()) {
return nil, fmt.Errorf(`parse: value %q's effect %q is stronger than the func's effect %q at %s:%d`,
value.Str(p.tm), value.Effect(), p.funcEffect, p.filename, p.line())
}
}
return a.NewVar(op, id, typ, value).AsNode(), nil
return a.NewVar(id, typ).AsNode(), nil
}
func (p *parser) parsePossibleListExprNode() (*a.Node, error) {