val.go 25 KB

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