val.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. type arr struct {
  18. link object.Object
  19. val []interface{}
  20. length int64
  21. }
  22. type dynarr struct {
  23. link object.Object
  24. val []interface{}
  25. }
  26. type proc struct {
  27. link object.Object
  28. }
  29. type rec struct {
  30. link object.Object
  31. scope.Record
  32. l *level
  33. }
  34. type idx struct {
  35. arr *arr
  36. idx int
  37. }
  38. func (r *rec) String() string {
  39. return r.link.Name()
  40. }
  41. func (r *rec) Id() cp.ID {
  42. return r.link.Adr()
  43. }
  44. func (r *rec) Set(v scope.Value) {
  45. }
  46. func (r *rec) Get(id cp.ID) scope.Value {
  47. k := r.l.k[id]
  48. if r.l.v[k] == nil { //ref
  49. return r.l.r[k]
  50. } else {
  51. return r.l.v[k]
  52. }
  53. }
  54. func newRec(o object.Object) *rec {
  55. _, ok := o.Complex().(object.RecordType)
  56. assert.For(ok, 20)
  57. return &rec{link: o}
  58. }
  59. func (p *proc) String() string {
  60. return fmt.Sprint(p.link.Adr(), p.link.Name())
  61. }
  62. func (x *data) Id() cp.ID {
  63. return x.link.Adr()
  64. }
  65. func (x *arr) Id() cp.ID {
  66. return x.link.Adr()
  67. }
  68. func (x *dynarr) Id() cp.ID {
  69. return x.link.Adr()
  70. }
  71. func (a *arr) Set(v scope.Value) {
  72. switch x := v.(type) {
  73. case STRING:
  74. v := make([]interface{}, int(a.length))
  75. for i := 0; i < int(a.length) && i < len(x); i++ {
  76. v[i] = CHAR(x[i])
  77. }
  78. a.val = v
  79. default:
  80. halt.As(100, reflect.TypeOf(x))
  81. }
  82. }
  83. func (a *dynarr) Set(v scope.Value) {
  84. switch x := v.(type) {
  85. case *arr:
  86. a.val = x.val
  87. case STRING:
  88. v := make([]interface{}, len(x))
  89. for i := 0; i < len(x); i++ {
  90. v[i] = CHAR(x[i])
  91. }
  92. a.val = v
  93. case SHORTSTRING:
  94. v := make([]interface{}, len(x))
  95. for i := 0; i < len(x); i++ {
  96. v[i] = SHORTCHAR(x[i])
  97. }
  98. a.val = v
  99. default:
  100. halt.As(100, reflect.TypeOf(x))
  101. }
  102. }
  103. func (a *arr) String() (ret string) {
  104. ret = fmt.Sprint("array", "[", a.length, "]")
  105. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  106. switch x := a.val[i].(type) {
  107. case CHAR:
  108. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  109. case SHORTCHAR:
  110. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  111. default:
  112. halt.As(100, reflect.TypeOf(x))
  113. }
  114. }
  115. return ret
  116. }
  117. func (a *arr) Get(id scope.Value) scope.Value {
  118. switch i := id.(type) {
  119. case *data:
  120. return a.Get(i.val.(scope.Value))
  121. case INTEGER:
  122. assert.For(int64(i) < a.length, 20)
  123. if len(a.val) == 0 {
  124. a.val = make([]interface{}, int(a.length))
  125. }
  126. return &idx{arr: a, idx: int(i)}
  127. default:
  128. halt.As(100, reflect.TypeOf(i))
  129. }
  130. panic(0)
  131. }
  132. func (i *idx) Id() cp.ID {
  133. return i.arr.Id()
  134. }
  135. func (i *idx) String() string {
  136. return fmt.Sprint("@", i.Id(), "[", i.idx, "]")
  137. }
  138. func (i *idx) Set(v scope.Value) {
  139. fmt.Println(i, len(i.arr.val))
  140. switch x := v.(type) {
  141. case *idx:
  142. i.arr.val[i.idx] = x.arr.val[x.idx]
  143. case CHAR:
  144. i.arr.val[i.idx] = x
  145. default:
  146. halt.As(100, reflect.TypeOf(x))
  147. }
  148. }
  149. func (a *dynarr) String() (ret string) {
  150. ret = fmt.Sprint("dyn array")
  151. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  152. switch x := a.val[i].(type) {
  153. case CHAR:
  154. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  155. case SHORTCHAR:
  156. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  157. default:
  158. halt.As(100, reflect.TypeOf(x))
  159. }
  160. }
  161. return ret
  162. }
  163. func (d *data) Set(v scope.Value) {
  164. fmt.Println("set data")
  165. switch x := v.(type) {
  166. case *data:
  167. assert.For(d.link.Type() == x.link.Type(), 20)
  168. d.val = x.val
  169. case *proc:
  170. assert.For(d.link.Type() == object.COMPLEX, 20)
  171. t, ok := d.link.Complex().(object.BasicType)
  172. assert.For(ok, 21, reflect.TypeOf(d.link.Complex()))
  173. assert.For(t.Type() == object.PROCEDURE, 22)
  174. d.val = x
  175. case INTEGER:
  176. switch d.link.Type() {
  177. case object.INTEGER:
  178. d.val = x
  179. case object.LONGINT:
  180. d.val = LONGINT(x)
  181. default:
  182. halt.As(20, d.link.Type())
  183. }
  184. case BOOLEAN:
  185. assert.For(d.link.Type() == object.BOOLEAN, 20)
  186. d.val = x
  187. case SHORTCHAR:
  188. assert.For(d.link.Type() == object.SHORTCHAR, 20)
  189. d.val = x
  190. case CHAR:
  191. assert.For(d.link.Type() == object.CHAR, 20)
  192. d.val = x
  193. case SHORTINT:
  194. assert.For(d.link.Type() == object.SHORTINT, 20)
  195. d.val = x
  196. case LONGINT:
  197. assert.For(d.link.Type() == object.LONGINT, 20)
  198. d.val = x
  199. case BYTE:
  200. assert.For(d.link.Type() == object.BYTE, 20)
  201. d.val = x
  202. case SET:
  203. assert.For(d.link.Type() == object.SET, 20)
  204. d.val = x
  205. case REAL:
  206. assert.For(d.link.Type() == object.REAL, 20)
  207. d.val = x
  208. case SHORTREAL:
  209. assert.For(d.link.Type() == object.SHORTREAL, 20)
  210. d.val = x
  211. default:
  212. panic(fmt.Sprintln(reflect.TypeOf(x)))
  213. }
  214. }
  215. func (d *data) String() string {
  216. return fmt.Sprint(d.link.Name(), "=", d.val)
  217. }
  218. type INTEGER int32
  219. type BOOLEAN bool
  220. type BYTE int8
  221. type SHORTINT int16
  222. type LONGINT int64
  223. type SET struct {
  224. bits *big.Int
  225. }
  226. type CHAR rune
  227. type REAL float64
  228. type SHORTREAL float32
  229. type SHORTCHAR rune
  230. type STRING string
  231. type SHORTSTRING string
  232. func (x SHORTSTRING) String() string { return string(x) }
  233. func (x STRING) String() string { return string(x) }
  234. func (x SHORTCHAR) String() string { return fmt.Sprint(rune(x)) }
  235. func (x SHORTREAL) String() string { return fmt.Sprint(float32(x)) }
  236. func (x REAL) String() string { return fmt.Sprint(float64(x)) }
  237. func (x CHAR) String() string { return fmt.Sprint(rune(x)) }
  238. func (x SET) String() string { return fmt.Sprint(x.bits) }
  239. func (x LONGINT) String() string { return fmt.Sprint(int64(x)) }
  240. func (x SHORTINT) String() string { return fmt.Sprint(int16(x)) }
  241. func (x BYTE) String() string { return fmt.Sprint(int8(x)) }
  242. func (x INTEGER) String() string { return fmt.Sprint(int32(x)) }
  243. func (x BOOLEAN) String() string { return fmt.Sprint(bool(x)) }
  244. func NewData(o object.Object) (ret scope.Variable) {
  245. switch o.Type() {
  246. case object.INTEGER:
  247. ret = &data{link: o, val: INTEGER(0)}
  248. case object.BOOLEAN:
  249. ret = &data{link: o, val: BOOLEAN(false)}
  250. case object.BYTE:
  251. ret = &data{link: o, val: BYTE(0)}
  252. case object.CHAR:
  253. ret = &data{link: o, val: CHAR(0)}
  254. case object.LONGINT:
  255. ret = &data{link: o, val: LONGINT(0)}
  256. case object.SHORTINT:
  257. ret = &data{link: o, val: SHORTINT(0)}
  258. case object.SET:
  259. ret = &data{link: o, val: SET{bits: big.NewInt(0)}}
  260. case object.REAL:
  261. ret = &data{link: o, val: REAL(0)}
  262. case object.SHORTREAL:
  263. ret = &data{link: o, val: SHORTREAL(0)}
  264. case object.SHORTCHAR:
  265. ret = &data{link: o, val: SHORTCHAR(0)}
  266. case object.COMPLEX:
  267. switch t := o.Complex().(type) {
  268. case object.BasicType:
  269. switch t.Type() {
  270. case object.PROCEDURE:
  271. ret = &data{link: o, val: nil}
  272. default:
  273. halt.As(100, t.Type())
  274. }
  275. case object.ArrayType:
  276. ret = &arr{link: o, length: t.Len()}
  277. case object.DynArrayType:
  278. ret = &dynarr{link: o}
  279. default:
  280. halt.As(100, reflect.TypeOf(t))
  281. }
  282. default:
  283. panic(fmt.Sprintln("unsupported type", o.Type()))
  284. }
  285. return ret
  286. }
  287. func fromg(x interface{}) scope.Value {
  288. switch x := x.(type) {
  289. case int32:
  290. return INTEGER(x)
  291. case bool:
  292. return BOOLEAN(x)
  293. default:
  294. halt.As(100, reflect.TypeOf(x))
  295. }
  296. panic(100)
  297. }
  298. func NewProc(o object.Object) scope.Value {
  299. p, ok := o.(object.ProcedureObject)
  300. assert.For(ok, 20, reflect.TypeOf(o))
  301. return &proc{link: p}
  302. }
  303. func NewConst(n node.Node) scope.Value {
  304. switch x := n.(type) {
  305. case node.ConstantNode:
  306. switch x.Type() {
  307. case object.INTEGER:
  308. return INTEGER(x.Data().(int32))
  309. case object.REAL:
  310. return REAL(x.Data().(float64))
  311. case object.BOOLEAN:
  312. return BOOLEAN(x.Data().(bool))
  313. case object.SHORTCHAR:
  314. return SHORTCHAR(x.Data().(rune))
  315. case object.LONGINT:
  316. return LONGINT(x.Data().(int64))
  317. case object.SHORTINT:
  318. return SHORTINT(x.Data().(int16))
  319. case object.SHORTREAL:
  320. return SHORTREAL(x.Data().(float32))
  321. case object.BYTE:
  322. return BYTE(x.Data().(int8))
  323. case object.SET:
  324. return SET{bits: x.Data().(*big.Int)}
  325. case object.CHAR:
  326. return CHAR(x.Data().(rune))
  327. case object.STRING:
  328. return STRING(x.Data().(string))
  329. case object.SHORTSTRING:
  330. return SHORTSTRING(x.Data().(string))
  331. default:
  332. panic(fmt.Sprintln(x.Type()))
  333. }
  334. }
  335. panic(0)
  336. }
  337. func vfrom(v scope.Value) scope.Value {
  338. switch n := v.(type) {
  339. case *data:
  340. switch n.link.Type() {
  341. case object.INTEGER:
  342. return n.val.(INTEGER)
  343. case object.BYTE:
  344. return n.val.(BYTE)
  345. default:
  346. halt.As(100, n.link.Type())
  347. }
  348. case INTEGER:
  349. return n
  350. default:
  351. halt.As(100, reflect.TypeOf(n))
  352. }
  353. return nil
  354. }
  355. func gfrom(v scope.Value) interface{} {
  356. switch n := v.(type) {
  357. case *data:
  358. if n.val == nil {
  359. return nil
  360. } else {
  361. return gfrom(n.val.(scope.Value))
  362. }
  363. case *proc:
  364. return n.link
  365. case INTEGER:
  366. return int32(n)
  367. case BOOLEAN:
  368. return bool(n)
  369. default:
  370. halt.As(100, reflect.TypeOf(n))
  371. }
  372. return nil
  373. }
  374. type ops struct{}
  375. func (o *ops) Sum(a, b scope.Value) scope.Value {
  376. switch a.(type) {
  377. case *data:
  378. return o.Sum(vfrom(a), b)
  379. default:
  380. switch b.(type) {
  381. case *data:
  382. return o.Sum(a, vfrom(b))
  383. default:
  384. switch x := a.(type) {
  385. case INTEGER:
  386. switch y := b.(type) {
  387. case INTEGER:
  388. return INTEGER(int32(x) + int32(y))
  389. default:
  390. panic(fmt.Sprintln(reflect.TypeOf(y)))
  391. }
  392. default:
  393. panic(fmt.Sprintln(reflect.TypeOf(x)))
  394. }
  395. }
  396. }
  397. panic(0)
  398. }
  399. func (o *ops) Sub(a, b scope.Value) scope.Value {
  400. switch a.(type) {
  401. case *data:
  402. return o.Sub(vfrom(a), b)
  403. default:
  404. switch b.(type) {
  405. case *data:
  406. return o.Sub(a, vfrom(b))
  407. default:
  408. switch x := a.(type) {
  409. case INTEGER:
  410. switch y := b.(type) {
  411. case INTEGER:
  412. return INTEGER(int32(x) - int32(y))
  413. default:
  414. panic(fmt.Sprintln(reflect.TypeOf(y)))
  415. }
  416. default:
  417. panic(fmt.Sprintln(reflect.TypeOf(x)))
  418. }
  419. }
  420. }
  421. panic(0)
  422. }
  423. func (o *ops) Len(a object.Object, _a, _b scope.Value) (ret scope.Value) {
  424. //assert.For(a != nil, 20)
  425. assert.For(_b != nil, 21)
  426. var b int32 = gfrom(_b).(int32)
  427. assert.For(b == 0, 22)
  428. if a != nil {
  429. assert.For(a.Type() == object.COMPLEX, 23)
  430. switch typ := a.Complex().(type) {
  431. case object.ArrayType:
  432. ret = INTEGER(int32(typ.Len()))
  433. case object.DynArrayType:
  434. switch t := _a.(type) {
  435. //case string:
  436. // ret = int64(utf8.RuneCountInString(_a.(string)))
  437. default:
  438. ret = INTEGER(0)
  439. fmt.Sprintln("unsupported", reflect.TypeOf(t))
  440. }
  441. default:
  442. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
  443. }
  444. } else {
  445. switch a := _a.(type) {
  446. // case string:
  447. // ret = int64(utf8.RuneCountInString(_a.(string)))
  448. // case []interface{}:
  449. // ret = int64(len(_a.([]interface{})))
  450. case *arr:
  451. ret = INTEGER(int32(a.length))
  452. case *dynarr:
  453. ret = INTEGER(int32(len(a.val)))
  454. default:
  455. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a)))
  456. }
  457. }
  458. return ret
  459. }
  460. func (o *ops) Conv(a scope.Value, typ object.Type) scope.Value {
  461. switch typ {
  462. case object.INTEGER:
  463. switch x := a.(type) {
  464. case *data:
  465. return o.Conv(vfrom(x), typ)
  466. case BYTE:
  467. return INTEGER(x)
  468. default:
  469. halt.As(100, reflect.TypeOf(x))
  470. }
  471. default:
  472. halt.As(100, typ)
  473. }
  474. panic(100)
  475. }
  476. func (o *ops) Eq(a, b scope.Value) scope.Value {
  477. switch a.(type) {
  478. case *data:
  479. return o.Eq(vfrom(a), b)
  480. default:
  481. switch b.(type) {
  482. case *data:
  483. return o.Eq(a, vfrom(b))
  484. default:
  485. switch x := a.(type) {
  486. case INTEGER:
  487. switch y := b.(type) {
  488. case INTEGER:
  489. return BOOLEAN(x == y)
  490. default:
  491. panic(fmt.Sprintln(reflect.TypeOf(y)))
  492. }
  493. default:
  494. panic(fmt.Sprintln(reflect.TypeOf(x)))
  495. }
  496. }
  497. }
  498. panic(0)
  499. }
  500. func (o *ops) Lss(a, b scope.Value) scope.Value {
  501. switch a.(type) {
  502. case *data:
  503. return o.Lss(vfrom(a), b)
  504. default:
  505. switch b.(type) {
  506. case *data:
  507. return o.Lss(a, vfrom(b))
  508. default:
  509. switch x := a.(type) {
  510. case INTEGER:
  511. switch y := b.(type) {
  512. case INTEGER:
  513. return BOOLEAN(x < y)
  514. default:
  515. panic(fmt.Sprintln(reflect.TypeOf(y)))
  516. }
  517. default:
  518. panic(fmt.Sprintln(reflect.TypeOf(x)))
  519. }
  520. }
  521. }
  522. panic(0)
  523. }
  524. func (o *ops) Leq(a, b scope.Value) scope.Value {
  525. switch a.(type) {
  526. case *data:
  527. return o.Leq(vfrom(a), b)
  528. default:
  529. switch b.(type) {
  530. case *data:
  531. return o.Leq(a, vfrom(b))
  532. default:
  533. switch x := a.(type) {
  534. case INTEGER:
  535. switch y := b.(type) {
  536. case INTEGER:
  537. return BOOLEAN(x <= y)
  538. default:
  539. panic(fmt.Sprintln(reflect.TypeOf(y)))
  540. }
  541. default:
  542. panic(fmt.Sprintln(reflect.TypeOf(x)))
  543. }
  544. }
  545. }
  546. panic(0)
  547. }
  548. func init() {
  549. scope.ValueFrom = vfrom
  550. scope.GoTypeFrom = gfrom
  551. scope.TypeFromGo = fromg
  552. scope.Ops = &ops{}
  553. }