val.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package modern
  2. import (
  3. "fmt"
  4. "fw/cp"
  5. "fw/cp/node"
  6. "fw/cp/object"
  7. "fw/rt2/scope"
  8. "math/big"
  9. "reflect"
  10. "ypk/assert"
  11. "ypk/halt"
  12. )
  13. type data struct {
  14. link object.Object
  15. val interface{}
  16. }
  17. func (x *data) Id() cp.ID {
  18. return 0
  19. }
  20. func (d *data) Set(v scope.Value) {
  21. fmt.Println("set data")
  22. switch x := v.(type) {
  23. case *data:
  24. assert.For(d.link.Type() == x.link.Type(), 20)
  25. d.val = x.val
  26. case INTEGER:
  27. switch d.link.Type() {
  28. case object.INTEGER:
  29. d.val = x
  30. case object.LONGINT:
  31. d.val = LONGINT(x)
  32. default:
  33. halt.As(20, d.link.Type())
  34. }
  35. case BOOLEAN:
  36. assert.For(d.link.Type() == object.BOOLEAN, 20)
  37. d.val = x
  38. case SHORTCHAR:
  39. assert.For(d.link.Type() == object.SHORTCHAR, 20)
  40. d.val = x
  41. case CHAR:
  42. assert.For(d.link.Type() == object.CHAR, 20)
  43. d.val = x
  44. case SHORTINT:
  45. assert.For(d.link.Type() == object.SHORTINT, 20)
  46. d.val = x
  47. case LONGINT:
  48. assert.For(d.link.Type() == object.LONGINT, 20)
  49. d.val = x
  50. case BYTE:
  51. assert.For(d.link.Type() == object.BYTE, 20)
  52. d.val = x
  53. case SET:
  54. assert.For(d.link.Type() == object.SET, 20)
  55. d.val = x
  56. case REAL:
  57. assert.For(d.link.Type() == object.REAL, 20)
  58. d.val = x
  59. case SHORTREAL:
  60. assert.For(d.link.Type() == object.SHORTREAL, 20)
  61. d.val = x
  62. default:
  63. panic(fmt.Sprintln(reflect.TypeOf(x)))
  64. }
  65. }
  66. func (d *data) String() string {
  67. return fmt.Sprint(d.link.Name(), "=", d.val)
  68. }
  69. type INTEGER int32
  70. type BOOLEAN bool
  71. type BYTE int8
  72. type SHORTINT int16
  73. type LONGINT int64
  74. type SET struct {
  75. bits *big.Int
  76. }
  77. type CHAR rune
  78. type REAL float64
  79. type SHORTREAL float32
  80. type SHORTCHAR rune
  81. func (x SHORTCHAR) String() string { return fmt.Sprint(rune(x)) }
  82. func (x SHORTREAL) String() string { return fmt.Sprint(float32(x)) }
  83. func (x REAL) String() string { return fmt.Sprint(float64(x)) }
  84. func (x CHAR) String() string { return fmt.Sprint(rune(x)) }
  85. func (x SET) String() string { return fmt.Sprint(x.bits) }
  86. func (x LONGINT) String() string { return fmt.Sprint(int64(x)) }
  87. func (x SHORTINT) String() string { return fmt.Sprint(int16(x)) }
  88. func (x BYTE) String() string { return fmt.Sprint(int8(x)) }
  89. func (x INTEGER) String() string { return fmt.Sprint(int32(x)) }
  90. func (x BOOLEAN) String() string { return fmt.Sprint(bool(x)) }
  91. func NewData(o object.Object) (ret scope.Variable) {
  92. switch o.Type() {
  93. case object.INTEGER:
  94. ret = &data{link: o, val: INTEGER(0)}
  95. case object.BOOLEAN:
  96. ret = &data{link: o, val: BOOLEAN(false)}
  97. case object.BYTE:
  98. ret = &data{link: o, val: BYTE(0)}
  99. case object.CHAR:
  100. ret = &data{link: o, val: CHAR(0)}
  101. case object.LONGINT:
  102. ret = &data{link: o, val: LONGINT(0)}
  103. case object.SHORTINT:
  104. ret = &data{link: o, val: SHORTINT(0)}
  105. case object.SET:
  106. ret = &data{link: o, val: SET{bits: big.NewInt(0)}}
  107. case object.REAL:
  108. ret = &data{link: o, val: REAL(0)}
  109. case object.SHORTREAL:
  110. ret = &data{link: o, val: SHORTREAL(0)}
  111. case object.SHORTCHAR:
  112. ret = &data{link: o, val: SHORTCHAR(0)}
  113. default:
  114. panic(fmt.Sprintln("unsupported type", o.Type()))
  115. }
  116. return ret
  117. }
  118. func fromg(x interface{}) scope.Value {
  119. switch x := x.(type) {
  120. case int32:
  121. return INTEGER(x)
  122. case bool:
  123. return BOOLEAN(x)
  124. default:
  125. halt.As(100, reflect.TypeOf(x))
  126. }
  127. panic(100)
  128. }
  129. func NewConst(n node.Node) scope.Value {
  130. switch x := n.(type) {
  131. case node.ConstantNode:
  132. switch x.Type() {
  133. case object.INTEGER:
  134. return INTEGER(x.Data().(int32))
  135. case object.REAL:
  136. return REAL(x.Data().(float64))
  137. case object.BOOLEAN:
  138. return BOOLEAN(x.Data().(bool))
  139. case object.SHORTCHAR:
  140. return SHORTCHAR(x.Data().(rune))
  141. case object.LONGINT:
  142. return LONGINT(x.Data().(int64))
  143. case object.SHORTINT:
  144. return SHORTINT(x.Data().(int16))
  145. case object.SHORTREAL:
  146. return SHORTREAL(x.Data().(float32))
  147. case object.BYTE:
  148. return BYTE(x.Data().(int8))
  149. case object.SET:
  150. return SET{bits: x.Data().(*big.Int)}
  151. case object.CHAR:
  152. return CHAR(x.Data().(rune))
  153. default:
  154. panic(fmt.Sprintln(x.Type()))
  155. }
  156. }
  157. panic(0)
  158. }
  159. func vfrom(v scope.Value) scope.Value {
  160. switch n := v.(type) {
  161. case *data:
  162. switch n.link.Type() {
  163. case object.INTEGER:
  164. return n.val.(INTEGER)
  165. case object.BYTE:
  166. return n.val.(BYTE)
  167. default:
  168. halt.As(100, n.link.Type())
  169. }
  170. case INTEGER:
  171. return n
  172. default:
  173. halt.As(100, reflect.TypeOf(n))
  174. }
  175. return nil
  176. }
  177. func gfrom(v scope.Value) interface{} {
  178. switch n := v.(type) {
  179. case *data:
  180. return gfrom(n.val.(scope.Value))
  181. case INTEGER:
  182. return int32(n)
  183. case BOOLEAN:
  184. return bool(n)
  185. default:
  186. halt.As(100, reflect.TypeOf(n))
  187. }
  188. return nil
  189. }
  190. type ops struct{}
  191. func (o *ops) Sum(a, b scope.Value) scope.Value {
  192. switch a.(type) {
  193. case *data:
  194. return o.Sum(vfrom(a), b)
  195. default:
  196. switch b.(type) {
  197. case *data:
  198. return o.Sum(a, vfrom(b))
  199. default:
  200. switch x := a.(type) {
  201. case INTEGER:
  202. switch y := b.(type) {
  203. case INTEGER:
  204. return INTEGER(int32(x) + int32(y))
  205. default:
  206. panic(fmt.Sprintln(reflect.TypeOf(y)))
  207. }
  208. default:
  209. panic(fmt.Sprintln(reflect.TypeOf(x)))
  210. }
  211. }
  212. }
  213. panic(0)
  214. }
  215. func (o *ops) Sub(a, b scope.Value) scope.Value {
  216. switch a.(type) {
  217. case *data:
  218. return o.Sub(vfrom(a), b)
  219. default:
  220. switch b.(type) {
  221. case *data:
  222. return o.Sub(a, vfrom(b))
  223. default:
  224. switch x := a.(type) {
  225. case INTEGER:
  226. switch y := b.(type) {
  227. case INTEGER:
  228. return INTEGER(int32(x) - int32(y))
  229. default:
  230. panic(fmt.Sprintln(reflect.TypeOf(y)))
  231. }
  232. default:
  233. panic(fmt.Sprintln(reflect.TypeOf(x)))
  234. }
  235. }
  236. }
  237. panic(0)
  238. }
  239. func (o *ops) Conv(a scope.Value, typ object.Type) scope.Value {
  240. switch typ {
  241. case object.INTEGER:
  242. switch x := a.(type) {
  243. case *data:
  244. return o.Conv(vfrom(x), typ)
  245. case BYTE:
  246. return INTEGER(x)
  247. default:
  248. halt.As(100, reflect.TypeOf(x))
  249. }
  250. default:
  251. halt.As(100, typ)
  252. }
  253. panic(100)
  254. }
  255. func (o *ops) Eq(a, b scope.Value) scope.Value {
  256. switch a.(type) {
  257. case *data:
  258. return o.Eq(vfrom(a), b)
  259. default:
  260. switch b.(type) {
  261. case *data:
  262. return o.Eq(a, vfrom(b))
  263. default:
  264. switch x := a.(type) {
  265. case INTEGER:
  266. switch y := b.(type) {
  267. case INTEGER:
  268. return BOOLEAN(x == y)
  269. default:
  270. panic(fmt.Sprintln(reflect.TypeOf(y)))
  271. }
  272. default:
  273. panic(fmt.Sprintln(reflect.TypeOf(x)))
  274. }
  275. }
  276. }
  277. panic(0)
  278. }
  279. func (o *ops) Lss(a, b scope.Value) scope.Value {
  280. switch a.(type) {
  281. case *data:
  282. return o.Lss(vfrom(a), b)
  283. default:
  284. switch b.(type) {
  285. case *data:
  286. return o.Lss(a, vfrom(b))
  287. default:
  288. switch x := a.(type) {
  289. case INTEGER:
  290. switch y := b.(type) {
  291. case INTEGER:
  292. return BOOLEAN(x < y)
  293. default:
  294. panic(fmt.Sprintln(reflect.TypeOf(y)))
  295. }
  296. default:
  297. panic(fmt.Sprintln(reflect.TypeOf(x)))
  298. }
  299. }
  300. }
  301. panic(0)
  302. }
  303. func (o *ops) Leq(a, b scope.Value) scope.Value {
  304. switch a.(type) {
  305. case *data:
  306. return o.Leq(vfrom(a), b)
  307. default:
  308. switch b.(type) {
  309. case *data:
  310. return o.Leq(a, vfrom(b))
  311. default:
  312. switch x := a.(type) {
  313. case INTEGER:
  314. switch y := b.(type) {
  315. case INTEGER:
  316. return BOOLEAN(x <= y)
  317. default:
  318. panic(fmt.Sprintln(reflect.TypeOf(y)))
  319. }
  320. default:
  321. panic(fmt.Sprintln(reflect.TypeOf(x)))
  322. }
  323. }
  324. }
  325. panic(0)
  326. }
  327. func init() {
  328. scope.ValueFrom = vfrom
  329. scope.GoTypeFrom = gfrom
  330. scope.TypeFromGo = fromg
  331. scope.Ops = &ops{}
  332. }