Esempio n. 1
0
File: op.go Progetto: yingmsky/e8vm
func buildUnaryOpExpr(b *builder, expr *ast.OpExpr) *ref {
	op := expr.Op.Lit
	B := b.buildExpr(expr.B)
	if B == nil {
		return nil
	}

	opPos := expr.Op.Pos

	if !B.IsSingle() {
		b.Errorf(opPos, "%q on expression list", op)
		return nil
	}

	btyp := B.Type()
	if types.IsBasic(btyp, types.Int) {
		switch op {
		case "+", "-", "^":
			ret := b.newTemp(types.Int)
			b.b.Arith(ret.IR(), nil, op, B.IR())
			return ret
		default:
			b.Errorf(opPos, "%q on int", op)
			return nil
		}
	} else if types.IsBasic(btyp, types.Bool) {
		switch op {
		case "!":
			ret := b.newTemp(types.Bool)
			b.b.Arith(ret.IR(), nil, op, B.IR())
			return ret
		default:
			b.Errorf(opPos, "%q on boolean", op)
			return nil
		}
	}

	b.Errorf(opPos, "invalid unary operator %q", op)
	return nil
}
Esempio n. 2
0
File: ref.go Progetto: yingmsky/e8vm
func (r *ref) IsBool() bool {
	if !r.IsSingle() {
		return false
	}
	return types.IsBasic(r.Type(), types.Bool)
}