cast.go 735 B

123456789101112131415161718192021222324252627
  1. package checker
  2. import "kumachan/transformer/node"
  3. func CheckCast(cast node.Cast, ctx ExprContext) (SemiExpr, *ExprError) {
  4. var info = ctx.GetExprInfo(cast.Node)
  5. var type_ctx = ctx.GetTypeContext()
  6. var target, err1 = TypeFrom(cast.Target.Type, type_ctx)
  7. if err1 != nil { return SemiExpr{}, &ExprError {
  8. Point: err1.Point,
  9. Concrete: E_TypeErrorInExpr { err1 },
  10. } }
  11. var semi, err2 = Check(cast.Expr, ctx)
  12. if err2 != nil { return SemiExpr{}, err2 }
  13. var typed, err3 = AssignTo(target, semi, ctx)
  14. if err3 != nil { return SemiExpr{}, err3 }
  15. if !(AreTypesEqualInSameCtx(typed.Type, target)) {
  16. panic("something went wrong")
  17. }
  18. return LiftTyped(Expr {
  19. Type: typed.Type,
  20. Value: typed.Value,
  21. Info: info,
  22. }), nil
  23. }