Пример #1
0
func (w *World) compileSubAssign(a *ast.AssignStmt, lhs ast.Expr, r Expr) Expr {
	l := w.compileLvalue(lhs)
	x := typeConv(a.Pos(), l, float64_t)
	y := typeConv(a.Pos(), r, float64_t)
	sub := &sub{binaryExpr{x, y}}
	return &assignStmt{lhs: l, rhs: typeConv(a.Pos(), sub, inputType(l))}
}
Пример #2
0
// compile a := b
func (w *World) compileDefine(a *ast.AssignStmt, lhs ast.Expr, r Expr) Expr {
	ident, ok := lhs.(*ast.Ident)
	if !ok {
		panic(err(a.Pos(), "non-name on left side of :="))
	}
	addr := reflect.New(r.Type())
	ok = w.safeDeclare(ident.Name, &reflectLvalue{addr.Elem()})
	if !ok {
		panic(err(a.Pos(), "already defined: "+ident.Name))
	}
	return w.compileAssign(a, lhs, r)
}
Пример #3
0
// compiles a (single) assign statement lhs = rhs
func (w *World) compileAssignStmt(a *ast.AssignStmt) Expr {
	if len(a.Lhs) != 1 || len(a.Rhs) != 1 {
		panic(err(a.Pos(), "multiple assignment not allowed"))
	}
	lhs, rhs := a.Lhs[0], a.Rhs[0]
	r := w.compileExpr(rhs)

	switch a.Tok {
	default:
		panic(err(a.Pos(), a.Tok, "not allowed"))
	case token.ASSIGN: // =
		return w.compileAssign(a, lhs, r)
	case token.DEFINE: // :=
		return w.compileDefine(a, lhs, r)
	case token.ADD_ASSIGN: // +=
		return w.compileAddAssign(a, lhs, r)
	case token.SUB_ASSIGN: // -=
		return w.compileSubAssign(a, lhs, r)
	}
}
Пример #4
0
func (a *stmtCompiler) doAssignOp(s *ast.AssignStmt) {
	if len(s.Lhs) != 1 || len(s.Rhs) != 1 {
		a.diag("tuple assignment cannot be combined with an arithmetic operation")
		return
	}

	// Create temporary block for extractEffect
	bc := a.enterChild()
	defer bc.exit()

	l := a.compileExpr(bc.block, false, s.Lhs[0])
	r := a.compileExpr(bc.block, false, s.Rhs[0])
	if l == nil || r == nil {
		return
	}

	if l.evalAddr == nil {
		l.diag("cannot assign to %s", l.desc)
		return
	}

	effect, l := l.extractEffect(bc.block, "operator-assignment")

	binop := r.compileBinaryExpr(assignOpToOp[s.Tok], l, r)
	if binop == nil {
		return
	}

	assign := a.compileAssign(s.Pos(), bc.block, l.t, []*expr{binop}, "assignment", "value")
	if assign == nil {
		log.Panicf("compileAssign type check failed")
	}

	lf := l.evalAddr
	a.push(func(t *Thread) {
		effect(t)
		assign(lf(t), t)
	})
}
Пример #5
0
// checkAssignStmt checks for assignments of the form "<expr> = <expr>".
// These are almost always useless, and even when they aren't they are usually a mistake.
func (f *File) checkAssignStmt(stmt *ast.AssignStmt) {
	if !vet("assign") {
		return
	}
	if stmt.Tok != token.ASSIGN {
		return // ignore :=
	}
	if len(stmt.Lhs) != len(stmt.Rhs) {
		// If LHS and RHS have different cardinality, they can't be the same.
		return
	}
	for i, lhs := range stmt.Lhs {
		rhs := stmt.Rhs[i]
		if reflect.TypeOf(lhs) != reflect.TypeOf(rhs) {
			continue // short-circuit the heavy-weight gofmt check
		}
		le := f.gofmt(lhs)
		re := f.gofmt(rhs)
		if le == re {
			f.Badf(stmt.Pos(), "self-assignment of %s to %s", re, le)
		}
	}
}
Пример #6
0
// compile a = b
func (w *World) compileAssign(a *ast.AssignStmt, lhs ast.Expr, r Expr) Expr {
	l := w.compileLvalue(lhs)
	return &assignStmt{lhs: l, rhs: typeConv(a.Pos(), r, inputType(l))}
}