val.go 24 KB

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