pointer.zy 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // to point to non-records in a persistent fashion.
  2. (var a int64)
  3. (ptr = (& a))
  4. (assert (== 0 a))
  5. (assert (== a 0))
  6. (ptr2 = (&a))
  7. (assert (== ptr ptr2))
  8. (var s string)
  9. (assert (== s ""))
  10. (assert (== "" s))
  11. (sptr = (& s))
  12. &a
  13. &s
  14. (ptr2 = (& a))
  15. ptr2
  16. // derefSet is a setter that is equivalent to *ptr = 1 in Go.
  17. (derefSet ptr 1)
  18. (assert (== 1 a))
  19. // deref with only 1 argument is a getter; same as (* ptr)
  20. (assert (== 1 (deref ptr)))
  21. (assert (== 1 (deref ptr2)))
  22. (assert (== 1 (* ptr)))
  23. (assert (== 1 (* ptr2)))
  24. // set a string through a pointer
  25. (derefSet sptr "hiya")
  26. (assert (== s "hiya"))
  27. // cross type assignment doesn't type check
  28. (expectError "Error calling 'derefSet': type mismatch: value of type 'int64' is not assignable to type 'string'" (derefSet sptr 3))
  29. (expectError "Error calling 'derefSet': type mismatch: value of type 'string' is not assignable to 'int64'" (derefSet ptr "a-string"))
  30. // set a struct through a pointer
  31. (struct Dog [
  32. (field Name: string e:0)
  33. (field Number: int64 e:1)
  34. ])
  35. (def d (Dog Name:"Rover"))
  36. (pdog = (& d))
  37. (derefSet pdog (Dog Name:"Benicia"))
  38. (assert (== d.Name "Benicia"))
  39. (expectError "Error calling 'derefSet': cannot assign type 'Dog' to type 'string'" (derefSet sptr d))
  40. (expectError "Error calling 'derefSet': type mismatch: value of type 'string' is not assignable to 'Dog'" (derefSet pdog "hi"))
  41. (derefSet pdog (Dog Name:"Rov2"))
  42. (struct Cat [(field Name:string)])
  43. (expectError "Error calling 'derefSet': cannot assign type 'Cat' to type 'Dog'"
  44. (derefSet pdog (Cat Name:"meower")))
  45. (var pcat (* Cat))
  46. (expectError "Error calling 'derefSet': cannot assign type 'Cat' to type '*Cat'"
  47. (derefSet pcat (Cat Name:"James")))
  48. (pcat = (& (Cat Name:"Earl")))
  49. (assert (== (:Name (* pcat)) "Earl"))
  50. (expectError "Error calling 'derefSet': cannot assign type 'Dog' to type 'Cat'"
  51. (derefSet pcat (Dog Name:"barker")))
  52. (def iii (& 34))
  53. (derefSet iii 5)
  54. (assert (== (deref iii) 5))
  55. (def sss (& "sad"))
  56. (derefSet sss "happy")
  57. (assert (== (* sss) "happy"))
  58. // derefSet doesn't work now...
  59. (def h (hash a:(& 1) b:2))
  60. (derefSet (* h.a) 45)
  61. (assert (== (* (* h.a)) 45))
  62. (def cat (Cat Name:"Claude"))
  63. (expectError "Error calling 'derefSet': derefSet only operates on pointers (*SexpPointer); we saw *zygo.SexpStr instead"
  64. (derefSet (:Name cat) "Jupiter"))
  65. (struct Kanga [(field roo: (* Cat))])
  66. (def kanga (Kanga roo: (& cat)))
  67. (assert (== (:Name (*(* kanga.roo))) "Claude"))
  68. (def jup (Cat Name:"Jupiter"))
  69. (derefSet (:roo kanga) jup)
  70. (assert (== (:Name (*(* kanga.roo))) "Jupiter"))
  71. (def sn1 (snoopy of:"charlie"))
  72. (def sn2 (snoopy of:"sarah"))
  73. (psnoop = (& sn1))
  74. (* psnoop)
  75. (derefSet psnoop sn2)