val.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. package modern
  2. import (
  3. "fmt"
  4. "fw/cp"
  5. "fw/cp/node"
  6. "fw/cp/object"
  7. "fw/rt2/scope"
  8. "math"
  9. "math/big"
  10. "reflect"
  11. "strings"
  12. "ypk/assert"
  13. "ypk/halt"
  14. )
  15. type data struct {
  16. link object.Object
  17. val interface{}
  18. }
  19. type arr struct {
  20. link object.Object
  21. val []interface{}
  22. length int64
  23. }
  24. type dynarr struct {
  25. link object.Object
  26. val []interface{}
  27. }
  28. type proc struct {
  29. link object.Object
  30. }
  31. type rec struct {
  32. link object.Object
  33. scope.Record
  34. l *level
  35. }
  36. type ptr struct {
  37. link object.Object
  38. scope.Pointer
  39. }
  40. type idx struct {
  41. arr *arr
  42. idx int
  43. }
  44. func (r *rec) String() string {
  45. return r.link.Name()
  46. }
  47. func (r *rec) Id() cp.ID {
  48. return r.link.Adr()
  49. }
  50. func (r *rec) Set(v scope.Value) {
  51. panic(0)
  52. }
  53. func (r *rec) Get(id cp.ID) scope.Value {
  54. k := r.l.k[id]
  55. if r.l.v[k] == nil { //ref
  56. return r.l.r[k]
  57. } else {
  58. return r.l.v[k]
  59. }
  60. }
  61. func newRec(o object.Object) *rec {
  62. _, ok := o.Complex().(object.RecordType)
  63. assert.For(ok, 20)
  64. return &rec{link: o}
  65. }
  66. func (p *proc) String() string {
  67. return fmt.Sprint(p.link.Adr(), p.link.Name())
  68. }
  69. func (x *data) Id() cp.ID { return x.link.Adr() }
  70. func (x *arr) Id() cp.ID { return x.link.Adr() }
  71. func (x *dynarr) Id() cp.ID { return x.link.Adr() }
  72. func (a *arr) Set(v scope.Value) {
  73. switch x := v.(type) {
  74. case *arr:
  75. a.Set(STRING(x.String()))
  76. case STRING:
  77. v := make([]interface{}, int(a.length))
  78. for i := 0; i < int(a.length) && i < len(x); i++ {
  79. v[i] = CHAR(x[i])
  80. }
  81. a.val = v
  82. default:
  83. halt.As(100, reflect.TypeOf(x))
  84. }
  85. }
  86. func (a *dynarr) Set(v scope.Value) {
  87. switch x := v.(type) {
  88. case *arr:
  89. a.val = x.val
  90. case STRING:
  91. v := make([]interface{}, len(x))
  92. for i := 0; i < len(x); i++ {
  93. v[i] = CHAR(x[i])
  94. }
  95. a.val = v
  96. case SHORTSTRING:
  97. v := make([]interface{}, len(x))
  98. for i := 0; i < len(x); i++ {
  99. v[i] = SHORTCHAR(x[i])
  100. }
  101. a.val = v
  102. default:
  103. halt.As(100, reflect.TypeOf(x))
  104. }
  105. }
  106. func (a *arr) String() (ret string) {
  107. ret = fmt.Sprint("array", "[", a.length, "]")
  108. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  109. switch x := a.val[i].(type) {
  110. case CHAR:
  111. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  112. case SHORTCHAR:
  113. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  114. default:
  115. halt.As(100, reflect.TypeOf(x))
  116. }
  117. }
  118. return ret
  119. }
  120. func (a *arr) Get(id scope.Value) scope.Value {
  121. switch i := id.(type) {
  122. case *data:
  123. return a.Get(i.val.(scope.Value))
  124. case INTEGER:
  125. assert.For(int64(i) < a.length, 20)
  126. if len(a.val) == 0 {
  127. a.val = make([]interface{}, int(a.length))
  128. }
  129. return &idx{arr: a, idx: int(i)}
  130. default:
  131. halt.As(100, reflect.TypeOf(i))
  132. }
  133. panic(0)
  134. }
  135. func (i *idx) Id() cp.ID {
  136. return i.arr.Id()
  137. }
  138. func (i *idx) String() string {
  139. return fmt.Sprint("@", i.Id(), "[", i.idx, "]")
  140. }
  141. func (i *idx) Set(v scope.Value) {
  142. fmt.Println(i, len(i.arr.val))
  143. switch x := v.(type) {
  144. case *idx:
  145. i.arr.val[i.idx] = x.arr.val[x.idx]
  146. case CHAR:
  147. i.arr.val[i.idx] = x
  148. default:
  149. halt.As(100, reflect.TypeOf(x))
  150. }
  151. }
  152. func (a *dynarr) String() (ret string) {
  153. ret = fmt.Sprint("dyn array")
  154. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  155. switch x := a.val[i].(type) {
  156. case CHAR:
  157. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  158. case SHORTCHAR:
  159. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  160. default:
  161. halt.As(100, reflect.TypeOf(x))
  162. }
  163. }
  164. return ret
  165. }
  166. func (d *data) Set(v scope.Value) {
  167. fmt.Println("set data")
  168. switch x := v.(type) {
  169. case *data:
  170. assert.For(d.link.Type() == x.link.Type(), 20)
  171. d.val = x.val
  172. case *proc:
  173. assert.For(d.link.Type() == object.COMPLEX, 20)
  174. t, ok := d.link.Complex().(object.BasicType)
  175. assert.For(ok, 21, reflect.TypeOf(d.link.Complex()))
  176. assert.For(t.Type() == object.PROCEDURE, 22)
  177. d.val = x
  178. case INTEGER:
  179. switch d.link.Type() {
  180. case object.INTEGER:
  181. d.val = x
  182. case object.LONGINT:
  183. d.val = LONGINT(x)
  184. default:
  185. halt.As(20, d.link.Type())
  186. }
  187. case BOOLEAN:
  188. assert.For(d.link.Type() == object.BOOLEAN, 20)
  189. d.val = x
  190. case SHORTCHAR:
  191. assert.For(d.link.Type() == object.SHORTCHAR, 20)
  192. d.val = x
  193. case CHAR:
  194. assert.For(d.link.Type() == object.CHAR, 20)
  195. d.val = x
  196. case SHORTINT:
  197. assert.For(d.link.Type() == object.SHORTINT, 20)
  198. d.val = x
  199. case LONGINT:
  200. assert.For(d.link.Type() == object.LONGINT, 20)
  201. d.val = x
  202. case BYTE:
  203. assert.For(d.link.Type() == object.BYTE, 20)
  204. d.val = x
  205. case SET:
  206. assert.For(d.link.Type() == object.SET, 20)
  207. d.val = x
  208. case REAL:
  209. assert.For(d.link.Type() == object.REAL, 20)
  210. d.val = x
  211. case SHORTREAL:
  212. assert.For(d.link.Type() == object.SHORTREAL, 20)
  213. d.val = x
  214. default:
  215. panic(fmt.Sprintln(reflect.TypeOf(x)))
  216. }
  217. }
  218. func (d *data) String() string {
  219. return fmt.Sprint(d.link.Name(), "=", d.val)
  220. }
  221. func (p *ptr) String() string {
  222. return "pointer"
  223. }
  224. func (p *ptr) Id() cp.ID { return p.link.Adr() }
  225. func (p *ptr) Set(scope.Value) {
  226. panic(0)
  227. }
  228. func newPtr(o object.Object) scope.Variable {
  229. _, ok := o.Complex().(object.PointerType)
  230. assert.For(ok, 20)
  231. return &ptr{link: o}
  232. }
  233. type INTEGER int32
  234. type BOOLEAN bool
  235. type BYTE int8
  236. type SHORTINT int16
  237. type LONGINT int64
  238. type SET struct {
  239. bits *big.Int
  240. }
  241. type CHAR rune
  242. type REAL float64
  243. type SHORTREAL float32
  244. type SHORTCHAR rune
  245. type STRING string
  246. type SHORTSTRING string
  247. func (x SHORTSTRING) String() string { return string(x) }
  248. func (x STRING) String() string { return string(x) }
  249. func (x SHORTCHAR) String() string { return fmt.Sprint(rune(x)) }
  250. func (x SHORTREAL) String() string { return fmt.Sprint(float32(x)) }
  251. func (x REAL) String() string { return fmt.Sprint(float64(x)) }
  252. func (x CHAR) String() string { return fmt.Sprint(rune(x)) }
  253. func (x SET) String() string { return fmt.Sprint(x.bits) }
  254. func (x LONGINT) String() string { return fmt.Sprint(int64(x)) }
  255. func (x SHORTINT) String() string { return fmt.Sprint(int16(x)) }
  256. func (x BYTE) String() string { return fmt.Sprint(int8(x)) }
  257. func (x INTEGER) String() string { return fmt.Sprint(int32(x)) }
  258. func (x BOOLEAN) String() string { return fmt.Sprint(bool(x)) }
  259. func newData(o object.Object) (ret scope.Variable) {
  260. switch o.Type() {
  261. case object.INTEGER:
  262. ret = &data{link: o, val: INTEGER(0)}
  263. case object.BOOLEAN:
  264. ret = &data{link: o, val: BOOLEAN(false)}
  265. case object.BYTE:
  266. ret = &data{link: o, val: BYTE(0)}
  267. case object.CHAR:
  268. ret = &data{link: o, val: CHAR(0)}
  269. case object.LONGINT:
  270. ret = &data{link: o, val: LONGINT(0)}
  271. case object.SHORTINT:
  272. ret = &data{link: o, val: SHORTINT(0)}
  273. case object.SET:
  274. ret = &data{link: o, val: SET{bits: big.NewInt(0)}}
  275. case object.REAL:
  276. ret = &data{link: o, val: REAL(0)}
  277. case object.SHORTREAL:
  278. ret = &data{link: o, val: SHORTREAL(0)}
  279. case object.SHORTCHAR:
  280. ret = &data{link: o, val: SHORTCHAR(0)}
  281. case object.COMPLEX:
  282. switch t := o.Complex().(type) {
  283. case object.BasicType:
  284. switch t.Type() {
  285. case object.PROCEDURE:
  286. ret = &data{link: o, val: nil}
  287. default:
  288. halt.As(100, t.Type())
  289. }
  290. case object.ArrayType:
  291. ret = &arr{link: o, length: t.Len()}
  292. case object.DynArrayType:
  293. ret = &dynarr{link: o}
  294. default:
  295. halt.As(100, reflect.TypeOf(t))
  296. }
  297. default:
  298. panic(fmt.Sprintln("unsupported type", o.Type()))
  299. }
  300. return ret
  301. }
  302. func fromg(x interface{}) scope.Value {
  303. switch x := x.(type) {
  304. case int32:
  305. return INTEGER(x)
  306. case bool:
  307. return BOOLEAN(x)
  308. case *big.Int:
  309. return SET{bits: x}
  310. default:
  311. halt.As(100, reflect.TypeOf(x))
  312. }
  313. panic(100)
  314. }
  315. func newProc(o object.Object) scope.Value {
  316. p, ok := o.(object.ProcedureObject)
  317. assert.For(ok, 20, reflect.TypeOf(o))
  318. return &proc{link: p}
  319. }
  320. func newConst(n node.Node) scope.Value {
  321. switch x := n.(type) {
  322. case node.ConstantNode:
  323. switch x.Type() {
  324. case object.INTEGER:
  325. return INTEGER(x.Data().(int32))
  326. case object.REAL:
  327. return REAL(x.Data().(float64))
  328. case object.BOOLEAN:
  329. return BOOLEAN(x.Data().(bool))
  330. case object.SHORTCHAR:
  331. return SHORTCHAR(x.Data().(rune))
  332. case object.LONGINT:
  333. return LONGINT(x.Data().(int64))
  334. case object.SHORTINT:
  335. return SHORTINT(x.Data().(int16))
  336. case object.SHORTREAL:
  337. return SHORTREAL(x.Data().(float32))
  338. case object.BYTE:
  339. return BYTE(x.Data().(int8))
  340. case object.SET:
  341. return SET{bits: x.Data().(*big.Int)}
  342. case object.CHAR:
  343. return CHAR(x.Data().(rune))
  344. case object.STRING:
  345. return STRING(x.Data().(string))
  346. case object.SHORTSTRING:
  347. return SHORTSTRING(x.Data().(string))
  348. default:
  349. panic(fmt.Sprintln(x.Type()))
  350. }
  351. }
  352. panic(0)
  353. }
  354. func vfrom(v scope.Value) scope.Value {
  355. switch n := v.(type) {
  356. case *data:
  357. switch n.link.Type() {
  358. case object.INTEGER:
  359. return n.val.(INTEGER)
  360. case object.BYTE:
  361. return n.val.(BYTE)
  362. case object.CHAR:
  363. return n.val.(CHAR)
  364. case object.SET:
  365. return n.val.(SET)
  366. case object.BOOLEAN:
  367. return n.val.(BOOLEAN)
  368. default:
  369. halt.As(100, n.link.Type())
  370. }
  371. case INTEGER:
  372. return n
  373. default:
  374. halt.As(100, reflect.TypeOf(n))
  375. }
  376. return nil
  377. }
  378. func gfrom(v scope.Value) interface{} {
  379. switch n := v.(type) {
  380. case *data:
  381. if n.val == nil {
  382. return nil
  383. } else {
  384. return gfrom(n.val.(scope.Value))
  385. }
  386. case *proc:
  387. return n.link
  388. case INTEGER:
  389. return int32(n)
  390. case BOOLEAN:
  391. return bool(n)
  392. default:
  393. halt.As(100, reflect.TypeOf(n))
  394. }
  395. return nil
  396. }
  397. type ops struct{}
  398. func (o *ops) Sum(a, b scope.Value) scope.Value {
  399. switch a.(type) {
  400. case *data:
  401. return o.Sum(vfrom(a), b)
  402. default:
  403. switch b.(type) {
  404. case *data:
  405. return o.Sum(a, vfrom(b))
  406. default:
  407. switch x := a.(type) {
  408. case INTEGER:
  409. switch y := b.(type) {
  410. case INTEGER:
  411. return INTEGER(int32(x) + int32(y))
  412. default:
  413. panic(fmt.Sprintln(reflect.TypeOf(y)))
  414. }
  415. case SET:
  416. switch y := b.(type) {
  417. case SET:
  418. return SET{bits: x.bits.Add(x.bits, y.bits)}
  419. case INTEGER: // INCL(SET, INTEGER)
  420. return SET{bits: x.bits.SetBit(x.bits, int(y), 1)}
  421. default:
  422. panic(fmt.Sprintln(reflect.TypeOf(y)))
  423. }
  424. case *arr:
  425. switch y := b.(type) {
  426. case *arr:
  427. switch {
  428. case x.link.Type() == y.link.Type() && x.link.Complex().(object.ArrayType).Base() == object.CHAR:
  429. return STRING(x.String() + y.String())
  430. default:
  431. halt.As(100, x.link.Type(), y.link.Type())
  432. }
  433. case STRING:
  434. switch {
  435. case x.link.Complex().(object.ArrayType).Base() == object.CHAR:
  436. return STRING(x.String() + string(y))
  437. default:
  438. halt.As(100, x.link.Type())
  439. }
  440. default:
  441. panic(fmt.Sprintln(reflect.TypeOf(y)))
  442. }
  443. case STRING:
  444. switch y := b.(type) {
  445. case STRING:
  446. return STRING(string(x) + string(y))
  447. default:
  448. halt.As(100, reflect.TypeOf(y))
  449. }
  450. default:
  451. panic(fmt.Sprintln(reflect.TypeOf(x)))
  452. }
  453. }
  454. }
  455. panic(0)
  456. }
  457. func (o *ops) Sub(a, b scope.Value) scope.Value {
  458. switch a.(type) {
  459. case *data:
  460. return o.Sub(vfrom(a), b)
  461. default:
  462. switch b.(type) {
  463. case *data:
  464. return o.Sub(a, vfrom(b))
  465. default:
  466. switch x := a.(type) {
  467. case INTEGER:
  468. switch y := b.(type) {
  469. case INTEGER:
  470. return INTEGER(int32(x) - int32(y))
  471. default:
  472. panic(fmt.Sprintln(reflect.TypeOf(y)))
  473. }
  474. case SET:
  475. switch y := b.(type) {
  476. case SET:
  477. return SET{bits: x.bits.Sub(x.bits, y.bits)}
  478. case INTEGER:
  479. return SET{bits: x.bits.SetBit(x.bits, int(y), 0)}
  480. default:
  481. panic(fmt.Sprintln(reflect.TypeOf(y)))
  482. }
  483. default:
  484. panic(fmt.Sprintln(reflect.TypeOf(x)))
  485. }
  486. }
  487. }
  488. panic(0)
  489. }
  490. func (o *ops) In(a, b scope.Value) scope.Value {
  491. switch a.(type) {
  492. case *data:
  493. return o.In(vfrom(a), b)
  494. default:
  495. switch b.(type) {
  496. case *data:
  497. return o.In(a, vfrom(b))
  498. default:
  499. switch x := a.(type) {
  500. case INTEGER:
  501. switch y := b.(type) {
  502. case SET:
  503. fmt.Println("IN врет")
  504. return BOOLEAN(false)
  505. default:
  506. panic(fmt.Sprintln(reflect.TypeOf(y)))
  507. }
  508. default:
  509. panic(fmt.Sprintln(reflect.TypeOf(x)))
  510. }
  511. }
  512. }
  513. panic(0)
  514. }
  515. func (o *ops) Min(a, b scope.Value) scope.Value {
  516. switch a.(type) {
  517. case *data:
  518. return o.Min(vfrom(a), b)
  519. default:
  520. switch b.(type) {
  521. case *data:
  522. return o.Min(a, vfrom(b))
  523. default:
  524. switch x := a.(type) {
  525. case INTEGER:
  526. switch y := b.(type) {
  527. case INTEGER:
  528. return INTEGER(int32(math.Min(float64(x), float64(y))))
  529. default:
  530. panic(fmt.Sprintln(reflect.TypeOf(y)))
  531. }
  532. default:
  533. panic(fmt.Sprintln(reflect.TypeOf(x)))
  534. }
  535. }
  536. }
  537. panic(0)
  538. }
  539. func (o *ops) Max(a, b scope.Value) scope.Value {
  540. switch a.(type) {
  541. case *data:
  542. return o.Max(vfrom(a), b)
  543. default:
  544. switch b.(type) {
  545. case *data:
  546. return o.Max(a, vfrom(b))
  547. default:
  548. switch x := a.(type) {
  549. case INTEGER:
  550. switch y := b.(type) {
  551. case INTEGER:
  552. return INTEGER(int32(math.Max(float64(x), float64(y))))
  553. default:
  554. panic(fmt.Sprintln(reflect.TypeOf(y)))
  555. }
  556. default:
  557. panic(fmt.Sprintln(reflect.TypeOf(x)))
  558. }
  559. }
  560. }
  561. panic(0)
  562. }
  563. func (o *ops) And(a, b scope.Value) scope.Value {
  564. switch a.(type) {
  565. case *data:
  566. return o.And(vfrom(a), b)
  567. default:
  568. switch b.(type) {
  569. case *data:
  570. return o.And(a, vfrom(b))
  571. default:
  572. switch x := a.(type) {
  573. case BOOLEAN:
  574. switch y := b.(type) {
  575. case BOOLEAN:
  576. return BOOLEAN(x && y)
  577. default:
  578. panic(fmt.Sprintln(reflect.TypeOf(y)))
  579. }
  580. default:
  581. panic(fmt.Sprintln(reflect.TypeOf(x)))
  582. }
  583. }
  584. }
  585. panic(0)
  586. }
  587. func (o *ops) Or(a, b scope.Value) scope.Value {
  588. switch a.(type) {
  589. case *data:
  590. return o.Or(vfrom(a), b)
  591. default:
  592. switch b.(type) {
  593. case *data:
  594. return o.Or(a, vfrom(b))
  595. default:
  596. switch x := a.(type) {
  597. case BOOLEAN:
  598. switch y := b.(type) {
  599. case BOOLEAN:
  600. return BOOLEAN(x || y)
  601. default:
  602. panic(fmt.Sprintln(reflect.TypeOf(y)))
  603. }
  604. default:
  605. panic(fmt.Sprintln(reflect.TypeOf(x)))
  606. }
  607. }
  608. }
  609. panic(0)
  610. }
  611. func (o *ops) Ash(a, b scope.Value) scope.Value {
  612. switch a.(type) {
  613. case *data:
  614. return o.Max(vfrom(a), b)
  615. default:
  616. switch b.(type) {
  617. case *data:
  618. return o.Max(a, vfrom(b))
  619. default:
  620. switch x := a.(type) {
  621. case INTEGER:
  622. switch y := b.(type) {
  623. case INTEGER:
  624. return INTEGER(x << uint(y))
  625. default:
  626. panic(fmt.Sprintln(reflect.TypeOf(y)))
  627. }
  628. default:
  629. panic(fmt.Sprintln(reflect.TypeOf(x)))
  630. }
  631. }
  632. }
  633. panic(0)
  634. }
  635. func (o *ops) Div(a, b scope.Value) scope.Value {
  636. switch a.(type) {
  637. case *data:
  638. return o.Div(vfrom(a), b)
  639. default:
  640. switch b.(type) {
  641. case *data:
  642. return o.Div(a, vfrom(b))
  643. default:
  644. switch x := a.(type) {
  645. case INTEGER:
  646. switch y := b.(type) {
  647. case INTEGER:
  648. return INTEGER(x / y)
  649. default:
  650. panic(fmt.Sprintln(reflect.TypeOf(y)))
  651. }
  652. default:
  653. panic(fmt.Sprintln(reflect.TypeOf(x)))
  654. }
  655. }
  656. }
  657. panic(0)
  658. }
  659. func (o *ops) Mod(a, b scope.Value) scope.Value {
  660. switch a.(type) {
  661. case *data:
  662. return o.Mod(vfrom(a), b)
  663. default:
  664. switch b.(type) {
  665. case *data:
  666. return o.Mod(a, vfrom(b))
  667. default:
  668. switch x := a.(type) {
  669. case INTEGER:
  670. switch y := b.(type) {
  671. case INTEGER:
  672. return INTEGER(x % y)
  673. default:
  674. panic(fmt.Sprintln(reflect.TypeOf(y)))
  675. }
  676. default:
  677. panic(fmt.Sprintln(reflect.TypeOf(x)))
  678. }
  679. }
  680. }
  681. panic(0)
  682. }
  683. func (o *ops) Mult(a, b scope.Value) scope.Value {
  684. switch a.(type) {
  685. case *data:
  686. return o.Mult(vfrom(a), b)
  687. default:
  688. switch b.(type) {
  689. case *data:
  690. return o.Mult(a, vfrom(b))
  691. default:
  692. switch x := a.(type) {
  693. case INTEGER:
  694. switch y := b.(type) {
  695. case INTEGER:
  696. return INTEGER(x * y)
  697. default:
  698. panic(fmt.Sprintln(reflect.TypeOf(y)))
  699. }
  700. default:
  701. panic(fmt.Sprintln(reflect.TypeOf(x)))
  702. }
  703. }
  704. }
  705. panic(0)
  706. }
  707. func (o *ops) Divide(a, b scope.Value) scope.Value {
  708. switch a.(type) {
  709. case *data:
  710. return o.Divide(vfrom(a), b)
  711. default:
  712. switch b.(type) {
  713. case *data:
  714. return o.Divide(a, vfrom(b))
  715. default:
  716. switch x := a.(type) {
  717. case INTEGER:
  718. switch y := b.(type) {
  719. case INTEGER:
  720. return REAL(float64(x) / float64(y))
  721. default:
  722. panic(fmt.Sprintln(reflect.TypeOf(y)))
  723. }
  724. case REAL:
  725. switch y := b.(type) {
  726. case REAL:
  727. return REAL(float64(x) / float64(y))
  728. default:
  729. panic(fmt.Sprintln(reflect.TypeOf(y)))
  730. }
  731. default:
  732. panic(fmt.Sprintln(reflect.TypeOf(x)))
  733. }
  734. }
  735. }
  736. panic(0)
  737. }
  738. func (o *ops) Len(a object.Object, _a, _b scope.Value) (ret scope.Value) {
  739. //assert.For(a != nil, 20)
  740. assert.For(_b != nil, 21)
  741. var b int32 = gfrom(_b).(int32)
  742. assert.For(b == 0, 22)
  743. if a != nil {
  744. assert.For(a.Type() == object.COMPLEX, 23)
  745. switch typ := a.Complex().(type) {
  746. case object.ArrayType:
  747. ret = INTEGER(int32(typ.Len()))
  748. case object.DynArrayType:
  749. switch t := _a.(type) {
  750. //case string:
  751. // ret = int64(utf8.RuneCountInString(_a.(string)))
  752. default:
  753. ret = INTEGER(0)
  754. fmt.Sprintln("unsupported", reflect.TypeOf(t))
  755. }
  756. default:
  757. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
  758. }
  759. } else {
  760. switch a := _a.(type) {
  761. // case string:
  762. // ret = int64(utf8.RuneCountInString(_a.(string)))
  763. // case []interface{}:
  764. // ret = int64(len(_a.([]interface{})))
  765. case *arr:
  766. ret = INTEGER(int32(a.length))
  767. case *dynarr:
  768. ret = INTEGER(int32(len(a.val)))
  769. default:
  770. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a)))
  771. }
  772. }
  773. return ret
  774. }
  775. func (o *ops) Is(a scope.Value, typ object.ComplexType) scope.Value {
  776. var compare func(x, a object.RecordType) bool
  777. compare = func(x, a object.RecordType) bool {
  778. switch {
  779. case x.Name() == a.Name():
  780. // fmt.Println("eq")
  781. return true //опасно сравнивать имена конеш
  782. case x.BaseType() != nil:
  783. // fmt.Println("go base")
  784. return compare(x.BaseType(), a)
  785. default:
  786. return false
  787. }
  788. }
  789. switch x := a.(type) {
  790. case *rec:
  791. z, a := x.link.Complex().(object.RecordType)
  792. y, b := typ.(object.RecordType)
  793. fmt.Println("compare", x.link.Complex(), typ, a, b, a && b && compare(z, y))
  794. return BOOLEAN(a && b && compare(z, y))
  795. default:
  796. halt.As(100, reflect.TypeOf(x))
  797. }
  798. panic(0)
  799. }
  800. func (o *ops) Conv(a scope.Value, typ object.Type) scope.Value {
  801. switch typ {
  802. case object.INTEGER:
  803. switch x := a.(type) {
  804. case *data:
  805. return o.Conv(vfrom(x), typ)
  806. case BYTE:
  807. return INTEGER(x)
  808. case SET:
  809. return INTEGER(x.bits.Int64())
  810. default:
  811. halt.As(100, reflect.TypeOf(x))
  812. }
  813. case object.SET:
  814. switch x := a.(type) {
  815. case *data:
  816. return o.Conv(vfrom(x), typ)
  817. case INTEGER:
  818. return SET{bits: big.NewInt(int64(x))}
  819. default:
  820. halt.As(100, reflect.TypeOf(x))
  821. }
  822. case object.REAL:
  823. switch x := a.(type) {
  824. case *data:
  825. return o.Conv(vfrom(x), typ)
  826. case INTEGER:
  827. return REAL(float64(x))
  828. default:
  829. halt.As(100, reflect.TypeOf(x))
  830. }
  831. default:
  832. halt.As(100, typ)
  833. }
  834. panic(100)
  835. }
  836. func (o *ops) Not(a scope.Value) scope.Value {
  837. switch x := a.(type) {
  838. case *data:
  839. return o.Not(vfrom(x))
  840. case BOOLEAN:
  841. return BOOLEAN(!x)
  842. default:
  843. halt.As(100, reflect.TypeOf(x))
  844. }
  845. panic(100)
  846. }
  847. func (o *ops) Abs(a scope.Value) scope.Value {
  848. switch x := a.(type) {
  849. case *data:
  850. return o.Abs(vfrom(x))
  851. case INTEGER:
  852. return INTEGER(int32(math.Abs(float64(x))))
  853. default:
  854. halt.As(100, reflect.TypeOf(x))
  855. }
  856. panic(100)
  857. }
  858. func (o *ops) Odd(a scope.Value) scope.Value {
  859. switch x := a.(type) {
  860. case *data:
  861. return o.Odd(vfrom(x))
  862. case INTEGER:
  863. return BOOLEAN(int32(math.Abs(float64(x)))%2 == 1)
  864. default:
  865. halt.As(100, reflect.TypeOf(x))
  866. }
  867. panic(100)
  868. }
  869. func (o *ops) Cap(a scope.Value) scope.Value {
  870. switch x := a.(type) {
  871. case *data:
  872. return o.Cap(vfrom(x))
  873. case CHAR:
  874. return CHAR([]rune(strings.ToUpper(string(x)))[0])
  875. default:
  876. halt.As(100, reflect.TypeOf(x))
  877. }
  878. panic(100)
  879. }
  880. func (o *ops) Bits(a scope.Value) scope.Value {
  881. switch x := a.(type) {
  882. case *data:
  883. return o.Bits(vfrom(x))
  884. case INTEGER:
  885. return SET{bits: big.NewInt(int64(x))}
  886. default:
  887. halt.As(100, reflect.TypeOf(x))
  888. }
  889. panic(100)
  890. }
  891. func (o *ops) Eq(a, b scope.Value) scope.Value {
  892. switch a.(type) {
  893. case *data:
  894. return o.Eq(vfrom(a), b)
  895. default:
  896. switch b.(type) {
  897. case *data:
  898. return o.Eq(a, vfrom(b))
  899. default:
  900. switch x := a.(type) {
  901. case INTEGER:
  902. switch y := b.(type) {
  903. case INTEGER:
  904. return BOOLEAN(x == y)
  905. default:
  906. panic(fmt.Sprintln(reflect.TypeOf(y)))
  907. }
  908. default:
  909. panic(fmt.Sprintln(reflect.TypeOf(x)))
  910. }
  911. }
  912. }
  913. panic(0)
  914. }
  915. func (o *ops) Neq(a, b scope.Value) scope.Value {
  916. switch a.(type) {
  917. case *data:
  918. return o.Neq(vfrom(a), b)
  919. default:
  920. switch b.(type) {
  921. case *data:
  922. return o.Neq(a, vfrom(b))
  923. default:
  924. switch x := a.(type) {
  925. case INTEGER:
  926. switch y := b.(type) {
  927. case INTEGER:
  928. return BOOLEAN(x != y)
  929. default:
  930. panic(fmt.Sprintln(reflect.TypeOf(y)))
  931. }
  932. default:
  933. panic(fmt.Sprintln(reflect.TypeOf(x)))
  934. }
  935. }
  936. }
  937. panic(0)
  938. }
  939. func (o *ops) Lss(a, b scope.Value) scope.Value {
  940. switch a.(type) {
  941. case *data:
  942. return o.Lss(vfrom(a), b)
  943. default:
  944. switch b.(type) {
  945. case *data:
  946. return o.Lss(a, vfrom(b))
  947. default:
  948. switch x := a.(type) {
  949. case INTEGER:
  950. switch y := b.(type) {
  951. case INTEGER:
  952. return BOOLEAN(x < y)
  953. default:
  954. panic(fmt.Sprintln(reflect.TypeOf(y)))
  955. }
  956. default:
  957. panic(fmt.Sprintln(reflect.TypeOf(x)))
  958. }
  959. }
  960. }
  961. panic(0)
  962. }
  963. func (o *ops) Gtr(a, b scope.Value) scope.Value {
  964. switch a.(type) {
  965. case *data:
  966. return o.Gtr(vfrom(a), b)
  967. default:
  968. switch b.(type) {
  969. case *data:
  970. return o.Gtr(a, vfrom(b))
  971. default:
  972. switch x := a.(type) {
  973. case INTEGER:
  974. switch y := b.(type) {
  975. case INTEGER:
  976. return BOOLEAN(x > y)
  977. default:
  978. panic(fmt.Sprintln(reflect.TypeOf(y)))
  979. }
  980. default:
  981. panic(fmt.Sprintln(reflect.TypeOf(x)))
  982. }
  983. }
  984. }
  985. panic(0)
  986. }
  987. func (o *ops) Leq(a, b scope.Value) scope.Value {
  988. switch a.(type) {
  989. case *data:
  990. return o.Leq(vfrom(a), b)
  991. default:
  992. switch b.(type) {
  993. case *data:
  994. return o.Leq(a, vfrom(b))
  995. default:
  996. switch x := a.(type) {
  997. case INTEGER:
  998. switch y := b.(type) {
  999. case INTEGER:
  1000. return BOOLEAN(x <= y)
  1001. default:
  1002. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1003. }
  1004. default:
  1005. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1006. }
  1007. }
  1008. }
  1009. panic(0)
  1010. }
  1011. func (o *ops) Geq(a, b scope.Value) scope.Value {
  1012. switch a.(type) {
  1013. case *data:
  1014. return o.Geq(vfrom(a), b)
  1015. default:
  1016. switch b.(type) {
  1017. case *data:
  1018. return o.Geq(a, vfrom(b))
  1019. default:
  1020. switch x := a.(type) {
  1021. case INTEGER:
  1022. switch y := b.(type) {
  1023. case INTEGER:
  1024. return BOOLEAN(x >= y)
  1025. default:
  1026. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1027. }
  1028. default:
  1029. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1030. }
  1031. }
  1032. }
  1033. panic(0)
  1034. }
  1035. func init() {
  1036. scope.ValueFrom = vfrom
  1037. scope.GoTypeFrom = gfrom
  1038. scope.TypeFromGo = fromg
  1039. scope.Ops = &ops{}
  1040. }