val.go 11 KB

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