Пример #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
func AppendTryBlock(block []ast.Stmt, node ast.Node, errBlock []ast.Stmt) []ast.Stmt {
	var try, call *ast.CallExpr
	var assign *ast.AssignStmt
	_err, _nil := ast.NewIdent("err"), ast.NewIdent("nil")
	// err != nil
	errNil := &ast.BinaryExpr{_err, 0, token.NEQ, _nil}
	// if errNil { stmt }
	ifBlock := &ast.IfStmt{0, nil, errNil, &ast.BlockStmt{0, errBlock, 0}, nil}

	switch n := node.(type) {
	case *ast.AssignStmt:
		assign = n
		try = n.Rhs[0].(*ast.CallExpr)
	case *ast.ExprStmt:
		try = n.X.(*ast.CallExpr)
	default:
		log.Fatalf("unhandled try() node type: %T\n", node)
	}
	if assign == nil {
		assign = &ast.AssignStmt{nil, 0, token.ASSIGN, nil}
	}
	call = StripTry(try)
	assign.Rhs = []ast.Expr{call}
	assign.Lhs = append(assign.Lhs, _err)

	block = append(block, assign)
	block = append(block, ifBlock)
	return block
}
Пример #3
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)
}
Пример #4
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)
	}
}
Пример #5
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)
	})
}
Пример #6
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)
		}
	}
}
Пример #7
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))}
}