val.go 27 KB

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