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

-6
View File
@@ -420,12 +420,6 @@ func (h *resumabilityHelper) doRet(r resumabilities, n *a.Ret, depth uint32) err
}
func (h *resumabilityHelper) doVar(r resumabilities, n *a.Var, depth uint32) error {
if n.Value() != nil {
if err := h.doExpr(r, n.Value()); err != nil {
return err
}
}
name := n.Name()
if i, ok := h.vars[name]; !ok {
return fmt.Errorf("unrecognized variable %q", name.Str(h.tm))
+4 -9
View File
@@ -43,10 +43,9 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
switch n.Kind() {
case a.KAssign:
n := n.AsAssign()
mightIntroduceTemporaries = n.LHS().Effect().Coroutine() || n.RHS().Effect().Coroutine()
case a.KVar:
v := n.AsVar().Value()
mightIntroduceTemporaries = v != nil && v.Effect().Coroutine()
mightIntroduceTemporaries = n.RHS().Effect().Coroutine()
case a.KExpr:
// TODO.
}
if mightIntroduceTemporaries {
// Put n's code into its own block, to restrict the scope of the
@@ -86,11 +85,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
return g.writeStatementRet(b, n.AsRet(), depth)
case a.KVar:
n := n.AsVar()
op := n.Operator()
if n.Value() == nil {
op = t.IDEq
}
return g.writeStatementAssign(b, n.Name(), nil, n.XType(), op, n.Value(), depth)
return g.writeStatementAssign(b, n.Name(), nil, n.XType(), t.IDEq, nil, depth)
case a.KWhile:
return g.writeStatementWhile(b, n.AsWhile(), depth)
}
+1
View File
@@ -12,6 +12,7 @@
- Added a `yield` keyword.
- Made the `return` value mandatory; added `ok` literal.
- Restricted `var` statements to the top of functions.
- Dropped the `= RHS` out of `var x T = RHS`.
- Changed func out-type from struct to bare type.
- Renamed the implicit `in` variable to `args`.
- Added `std/adler32`, `std/crc32` and `std/gzip`.
+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) {