val.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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. z := []rune(string(x))
  102. v := make([]interface{}, len(z))
  103. for i := 0; i < len(z); i++ {
  104. v[i] = CHAR(z[i])
  105. }
  106. a.val = v
  107. case SHORTSTRING:
  108. z := []rune(string(x))
  109. v := make([]interface{}, len(z))
  110. for i := 0; i < len(z); i++ {
  111. v[i] = SHORTCHAR(z[i])
  112. }
  113. a.val = v
  114. case INTEGER:
  115. a.val = make([]interface{}, int(x))
  116. default:
  117. halt.As(100, reflect.TypeOf(x))
  118. }
  119. }
  120. func (a *arr) tryString() (ret string) {
  121. stop := false
  122. for i := 0; !stop && i < len(a.val) && a.val[i] != nil; i++ {
  123. switch x := a.val[i].(type) {
  124. case CHAR:
  125. stop = int(x) == 0
  126. if !stop {
  127. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  128. }
  129. case SHORTCHAR:
  130. stop = int(x) == 0
  131. if !stop {
  132. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  133. }
  134. default:
  135. halt.As(100, reflect.TypeOf(x))
  136. }
  137. }
  138. return ret
  139. }
  140. func (a *arr) String() (ret string) {
  141. ret = fmt.Sprint("array", "[", a.length, "]")
  142. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  143. switch x := a.val[i].(type) {
  144. case CHAR:
  145. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  146. case SHORTCHAR:
  147. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  148. default:
  149. halt.As(100, reflect.TypeOf(x))
  150. }
  151. }
  152. return ret
  153. }
  154. //возвращает *idx
  155. func (a *arr) Get(id scope.Value) scope.Value {
  156. switch i := id.(type) {
  157. case *data:
  158. return a.Get(i.val.(scope.Value))
  159. case INTEGER:
  160. assert.For(int64(i) < a.length, 20)
  161. if len(a.val) == 0 {
  162. a.val = make([]interface{}, int(a.length))
  163. }
  164. return &idx{arr: a, idx: int(i)}
  165. default:
  166. halt.As(100, reflect.TypeOf(i))
  167. }
  168. panic(0)
  169. }
  170. func (i *idx) Id() cp.ID {
  171. return i.arr.Id()
  172. }
  173. func (i *idx) String() string {
  174. return fmt.Sprint("@", i.Id(), "[", i.idx, "]")
  175. }
  176. func (i *idx) Set(v scope.Value) {
  177. t := i.arr.link.Complex()
  178. switch x := v.(type) {
  179. case *idx:
  180. i.arr.val[i.idx] = x.arr.val[x.idx]
  181. case *data:
  182. i.Set(x.val.(scope.Value))
  183. case CHAR:
  184. i.arr.val[i.idx] = x
  185. case STRING:
  186. _ = t.(object.ArrayType)
  187. i.arr.val[i.idx].(*arr).Set(x)
  188. default:
  189. halt.As(100, reflect.TypeOf(x), x, t)
  190. }
  191. }
  192. func (i *idx) Get() scope.Value {
  193. x := i.arr.val[i.idx]
  194. switch z := x.(type) {
  195. case CHAR:
  196. return z
  197. case nil:
  198. b := i.arr.link.Complex().(object.ArrayType).Base()
  199. switch b {
  200. case object.CHAR:
  201. return CHAR(rune(0))
  202. default:
  203. halt.As(101, reflect.TypeOf(b))
  204. }
  205. default:
  206. halt.As(100, i.idx, reflect.TypeOf(z))
  207. }
  208. panic(0)
  209. }
  210. func (a *dynarr) tryString() (ret string) {
  211. stop := false
  212. for i := 0; !stop && i < len(a.val) && a.val[i] != nil; i++ {
  213. switch x := a.val[i].(type) {
  214. case CHAR:
  215. stop = int(x) == 0
  216. if !stop {
  217. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  218. }
  219. case SHORTCHAR:
  220. stop = int(x) == 0
  221. if !stop {
  222. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  223. }
  224. default:
  225. halt.As(100, reflect.TypeOf(x))
  226. }
  227. }
  228. return ret
  229. }
  230. func (a *dynarr) String() (ret string) {
  231. ret = fmt.Sprint("dyn array")
  232. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  233. switch x := a.val[i].(type) {
  234. case CHAR:
  235. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  236. case SHORTCHAR:
  237. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  238. default:
  239. halt.As(100, reflect.TypeOf(x))
  240. }
  241. }
  242. return ret
  243. }
  244. func (d *data) Set(v scope.Value) {
  245. utils.PrintScope("set data")
  246. switch x := v.(type) {
  247. case *data:
  248. if d.link.Type() == x.link.Type() {
  249. d.val = x.val
  250. } else {
  251. d.Set(x.val.(scope.Value))
  252. }
  253. case *proc:
  254. assert.For(d.link.Type() == object.COMPLEX, 20)
  255. t, ok := d.link.Complex().(object.BasicType)
  256. assert.For(ok, 21, reflect.TypeOf(d.link.Complex()))
  257. assert.For(t.Type() == object.PROCEDURE, 22)
  258. d.val = x
  259. case *idx:
  260. d.val = x.arr.val[x.idx]
  261. case INTEGER:
  262. switch d.link.Type() {
  263. case object.INTEGER:
  264. d.val = x
  265. case object.LONGINT:
  266. d.val = LONGINT(x)
  267. case object.REAL:
  268. d.val = REAL(x)
  269. default:
  270. halt.As(20, d.link.Type())
  271. }
  272. case BOOLEAN:
  273. assert.For(d.link.Type() == object.BOOLEAN, 20)
  274. d.val = x
  275. case SHORTCHAR:
  276. assert.For(d.link.Type() == object.SHORTCHAR, 20)
  277. d.val = x
  278. case CHAR:
  279. assert.For(d.link.Type() == object.CHAR, 20)
  280. d.val = x
  281. case SHORTINT:
  282. assert.For(d.link.Type() == object.SHORTINT, 20)
  283. d.val = x
  284. case LONGINT:
  285. assert.For(d.link.Type() == object.LONGINT, 20)
  286. d.val = x
  287. case BYTE:
  288. assert.For(d.link.Type() == object.BYTE, 20)
  289. d.val = x
  290. case SET:
  291. assert.For(d.link.Type() == object.SET, 20)
  292. d.val = x
  293. case REAL:
  294. assert.For(d.link.Type() == object.REAL, 20)
  295. d.val = x
  296. case SHORTREAL:
  297. assert.For(d.link.Type() == object.SHORTREAL, 20)
  298. d.val = x
  299. default:
  300. panic(fmt.Sprintln(reflect.TypeOf(x)))
  301. }
  302. }
  303. func (d *data) String() string {
  304. return fmt.Sprint(d.link.Name(), "=", d.val)
  305. }
  306. func (p *ptr) String() string {
  307. return fmt.Sprint("pointer ", p.link.Complex().(object.PointerType).Name(), "&", p.val)
  308. }
  309. func (p *ptr) Id() cp.ID { return p.link.Adr() }
  310. func (p *ptr) Set(v scope.Value) {
  311. switch x := v.(type) {
  312. case *ptr:
  313. p.Set(x.val)
  314. case *ptrValue:
  315. p.val = x
  316. case PTR:
  317. assert.For(x == NIL, 40)
  318. p.val = nil
  319. default:
  320. halt.As(100, reflect.TypeOf(x))
  321. }
  322. }
  323. func (p *ptr) Get() scope.Value {
  324. if p.val == nil {
  325. return NIL
  326. } else {
  327. return p.val.scope.Select(p.val.id)
  328. }
  329. }
  330. func newPtr(o object.Object) scope.Variable {
  331. _, ok := o.Complex().(object.PointerType)
  332. assert.For(ok, 20)
  333. return &ptr{link: o}
  334. }
  335. type ptrValue struct {
  336. scope *area
  337. id cp.ID
  338. link object.Object
  339. }
  340. func (p *ptrValue) String() string {
  341. return fmt.Sprint(p.id)
  342. }
  343. type PTR int
  344. func (p PTR) String() string {
  345. return "NIL"
  346. }
  347. const NIL PTR = 0
  348. type INTEGER int32
  349. type BOOLEAN bool
  350. type BYTE int8
  351. type SHORTINT int16
  352. type LONGINT int64
  353. type SET struct {
  354. bits *big.Int
  355. }
  356. type CHAR rune
  357. type REAL float64
  358. type SHORTREAL float32
  359. type SHORTCHAR rune
  360. type STRING string
  361. type SHORTSTRING string
  362. func (x SHORTSTRING) String() string { return string(x) }
  363. func (x STRING) String() string { return string(x) }
  364. func (x SHORTCHAR) String() string { return fmt.Sprint(rune(x)) }
  365. func (x SHORTREAL) String() string { return fmt.Sprint(float32(x)) }
  366. func (x REAL) String() string { return fmt.Sprint(float64(x)) }
  367. func (x CHAR) String() string { return fmt.Sprint(rune(x)) }
  368. func (x SET) String() string { return fmt.Sprint(x.bits) }
  369. func (x LONGINT) String() string { return fmt.Sprint(int64(x)) }
  370. func (x SHORTINT) String() string { return fmt.Sprint(int16(x)) }
  371. func (x BYTE) String() string { return fmt.Sprint(int8(x)) }
  372. func (x INTEGER) String() string { return fmt.Sprint(int32(x)) }
  373. func (x BOOLEAN) String() string { return fmt.Sprint(bool(x)) }
  374. func newData(o object.Object) (ret scope.Variable) {
  375. switch o.Type() {
  376. case object.INTEGER:
  377. ret = &data{link: o, val: INTEGER(0)}
  378. case object.BOOLEAN:
  379. ret = &data{link: o, val: BOOLEAN(false)}
  380. case object.BYTE:
  381. ret = &data{link: o, val: BYTE(0)}
  382. case object.CHAR:
  383. ret = &data{link: o, val: CHAR(0)}
  384. case object.LONGINT:
  385. ret = &data{link: o, val: LONGINT(0)}
  386. case object.SHORTINT:
  387. ret = &data{link: o, val: SHORTINT(0)}
  388. case object.SET:
  389. ret = &data{link: o, val: SET{bits: big.NewInt(0)}}
  390. case object.REAL:
  391. ret = &data{link: o, val: REAL(0)}
  392. case object.SHORTREAL:
  393. ret = &data{link: o, val: SHORTREAL(0)}
  394. case object.SHORTCHAR:
  395. ret = &data{link: o, val: SHORTCHAR(0)}
  396. case object.COMPLEX:
  397. switch t := o.Complex().(type) {
  398. case object.BasicType:
  399. switch t.Type() {
  400. case object.PROCEDURE:
  401. ret = &data{link: o, val: nil}
  402. default:
  403. halt.As(100, t.Type())
  404. }
  405. case object.ArrayType:
  406. ret = &arr{link: o, length: t.Len()}
  407. if a := ret.(*arr); t.Base() == object.COMPLEX {
  408. a.val = make([]interface{}, int(t.Len()))
  409. for i := 0; i < int(t.Len()); i++ {
  410. fake := object.New(object.VARIABLE, int(cp.SomeAdr()))
  411. fake.SetName("[?]")
  412. fake.SetType(object.COMPLEX)
  413. fake.SetComplex(t.Complex())
  414. a.val[i] = newData(fake)
  415. }
  416. }
  417. case object.DynArrayType:
  418. ret = &dynarr{link: o}
  419. default:
  420. halt.As(100, reflect.TypeOf(t))
  421. }
  422. default:
  423. panic(fmt.Sprintln("unsupported type", o.Type()))
  424. }
  425. return ret
  426. }
  427. func fromg(x interface{}) scope.Value {
  428. switch x := x.(type) {
  429. case int32:
  430. return INTEGER(x)
  431. case bool:
  432. return BOOLEAN(x)
  433. case *big.Int:
  434. return SET{bits: x}
  435. case string:
  436. return STRING(x)
  437. default:
  438. halt.As(100, reflect.TypeOf(x))
  439. }
  440. panic(100)
  441. }
  442. func newProc(o object.Object) scope.Value {
  443. p, ok := o.(object.ProcedureObject)
  444. assert.For(ok, 20, reflect.TypeOf(o))
  445. return &proc{link: p}
  446. }
  447. func newConst(n node.Node) scope.Value {
  448. switch x := n.(type) {
  449. case node.ConstantNode:
  450. switch x.Type() {
  451. case object.INTEGER:
  452. return INTEGER(x.Data().(int32))
  453. case object.REAL:
  454. return REAL(x.Data().(float64))
  455. case object.BOOLEAN:
  456. return BOOLEAN(x.Data().(bool))
  457. case object.SHORTCHAR:
  458. return SHORTCHAR(x.Data().(rune))
  459. case object.LONGINT:
  460. return LONGINT(x.Data().(int64))
  461. case object.SHORTINT:
  462. return SHORTINT(x.Data().(int16))
  463. case object.SHORTREAL:
  464. return SHORTREAL(x.Data().(float32))
  465. case object.BYTE:
  466. return BYTE(x.Data().(int8))
  467. case object.SET:
  468. return SET{bits: x.Data().(*big.Int)}
  469. case object.CHAR:
  470. return CHAR(x.Data().(rune))
  471. case object.STRING:
  472. return STRING(x.Data().(string))
  473. case object.SHORTSTRING:
  474. return SHORTSTRING(x.Data().(string))
  475. case object.NIL:
  476. return NIL
  477. case object.COMPLEX: //не может существовать в реальности, используется для передачи параметров от рантайма
  478. switch d := x.Data().(type) {
  479. case *ptrValue:
  480. return d
  481. default:
  482. halt.As(100, reflect.TypeOf(d))
  483. }
  484. default:
  485. panic(fmt.Sprintln(x.Type()))
  486. }
  487. }
  488. panic(0)
  489. }
  490. func vfrom(v scope.Value) scope.Value {
  491. switch n := v.(type) {
  492. case *data:
  493. switch n.link.Type() {
  494. case object.INTEGER:
  495. return n.val.(INTEGER)
  496. case object.BYTE:
  497. return n.val.(BYTE)
  498. case object.CHAR:
  499. return n.val.(CHAR)
  500. case object.SET:
  501. return n.val.(SET)
  502. case object.BOOLEAN:
  503. return n.val.(BOOLEAN)
  504. case object.REAL:
  505. return n.val.(REAL)
  506. case object.LONGINT:
  507. return n.val.(LONGINT)
  508. default:
  509. halt.As(100, n.link.Type())
  510. }
  511. case INTEGER, CHAR:
  512. return n
  513. default:
  514. halt.As(100, reflect.TypeOf(n))
  515. }
  516. return nil
  517. }
  518. func gfrom(v scope.Value) interface{} {
  519. switch n := v.(type) {
  520. case *data:
  521. if n.val == nil {
  522. return nil
  523. } else {
  524. return gfrom(n.val.(scope.Value))
  525. }
  526. case *rec:
  527. return n
  528. case *proc:
  529. return n.link
  530. case *dynarr:
  531. switch n.link.Complex().(object.DynArrayType).Base() {
  532. case object.SHORTCHAR:
  533. if n.val != nil {
  534. return n.tryString()
  535. } else {
  536. return ""
  537. }
  538. case object.CHAR:
  539. if n.val != nil {
  540. return n.tryString()
  541. } else {
  542. return ""
  543. }
  544. default:
  545. halt.As(100, n.link.Complex().(object.DynArrayType).Base())
  546. }
  547. panic(0)
  548. case *arr:
  549. switch n.link.Complex().(object.ArrayType).Base() {
  550. case object.SHORTCHAR:
  551. if n.val != nil {
  552. return n.tryString()
  553. } else {
  554. return ""
  555. }
  556. case object.CHAR:
  557. if n.val != nil {
  558. return n.tryString()
  559. } else {
  560. return ""
  561. }
  562. default:
  563. halt.As(100, n.link.Complex().(object.ArrayType).Base())
  564. }
  565. panic(0)
  566. case *idx:
  567. switch n.arr.link.Complex().(object.ArrayType).Base() {
  568. case object.CHAR:
  569. return rune(n.Get().(CHAR))
  570. default:
  571. halt.As(100, n.arr.link.Complex().(object.ArrayType).Base())
  572. }
  573. panic(0)
  574. case PTR:
  575. assert.For(n == NIL, 40)
  576. return nil
  577. case INTEGER:
  578. return int32(n)
  579. case BOOLEAN:
  580. return bool(n)
  581. case STRING:
  582. return string(n)
  583. default:
  584. halt.As(100, reflect.TypeOf(n))
  585. }
  586. return nil
  587. }
  588. type ops struct{}
  589. func (o *ops) Sum(a, b scope.Value) scope.Value {
  590. switch a.(type) {
  591. case *data:
  592. return o.Sum(vfrom(a), b)
  593. default:
  594. switch b.(type) {
  595. case *data:
  596. return o.Sum(a, vfrom(b))
  597. default:
  598. switch x := a.(type) {
  599. case INTEGER:
  600. switch y := b.(type) {
  601. case INTEGER:
  602. return INTEGER(int32(x) + int32(y))
  603. default:
  604. panic(fmt.Sprintln(reflect.TypeOf(y)))
  605. }
  606. case SET:
  607. switch y := b.(type) {
  608. case SET:
  609. res := big.NewInt(0)
  610. for i := 0; i < 64; i++ {
  611. if x.bits.Bit(i) == 1 {
  612. res.SetBit(res, i, 1)
  613. }
  614. if y.bits.Bit(i) == 1 {
  615. res.SetBit(res, i, 1)
  616. }
  617. }
  618. return SET{bits: res}
  619. case INTEGER: // INCL(SET, INTEGER)
  620. return SET{bits: x.bits.SetBit(x.bits, int(y), 1)}
  621. default:
  622. panic(fmt.Sprintln(reflect.TypeOf(y)))
  623. }
  624. case *arr:
  625. switch y := b.(type) {
  626. case *arr:
  627. switch {
  628. case x.link.Type() == y.link.Type() && x.link.Complex().(object.ArrayType).Base() == object.CHAR:
  629. return STRING(x.tryString() + y.tryString())
  630. default:
  631. halt.As(100, x.link.Type(), y.link.Type())
  632. }
  633. case STRING:
  634. switch {
  635. case x.link.Complex().(object.ArrayType).Base() == object.CHAR:
  636. return STRING(x.tryString() + string(y))
  637. default:
  638. halt.As(100, x.link.Type())
  639. }
  640. default:
  641. panic(fmt.Sprintln(reflect.TypeOf(y)))
  642. }
  643. case *dynarr:
  644. switch y := b.(type) {
  645. case STRING:
  646. switch {
  647. case x.link.Complex().(object.DynArrayType).Base() == object.CHAR:
  648. return STRING(x.tryString() + string(y))
  649. default:
  650. halt.As(100, x.link.Type())
  651. }
  652. default:
  653. panic(fmt.Sprintln(reflect.TypeOf(y)))
  654. }
  655. case STRING:
  656. switch y := b.(type) {
  657. case STRING:
  658. return STRING(string(x) + string(y))
  659. default:
  660. halt.As(100, reflect.TypeOf(y))
  661. }
  662. case REAL:
  663. switch y := b.(type) {
  664. case REAL:
  665. return REAL(x + y)
  666. default:
  667. halt.As(100, reflect.TypeOf(y))
  668. }
  669. case LONGINT:
  670. switch y := b.(type) {
  671. case LONGINT:
  672. return LONGINT(x + y)
  673. default:
  674. halt.As(100, reflect.TypeOf(y))
  675. }
  676. default:
  677. panic(fmt.Sprintln(reflect.TypeOf(x)))
  678. }
  679. }
  680. }
  681. panic(0)
  682. }
  683. func (o *ops) Sub(a, b scope.Value) scope.Value {
  684. switch a.(type) {
  685. case *data:
  686. return o.Sub(vfrom(a), b)
  687. default:
  688. switch b.(type) {
  689. case *data:
  690. return o.Sub(a, vfrom(b))
  691. default:
  692. switch x := a.(type) {
  693. case INTEGER:
  694. switch y := b.(type) {
  695. case INTEGER:
  696. return INTEGER(int32(x) - int32(y))
  697. default:
  698. panic(fmt.Sprintln(reflect.TypeOf(y)))
  699. }
  700. case SET:
  701. switch y := b.(type) {
  702. case SET:
  703. res := big.NewInt(0).Set(x.bits)
  704. for i := 0; i < 64; i++ {
  705. if y.bits.Bit(i) == 1 {
  706. res.SetBit(res, i, 0)
  707. }
  708. }
  709. return SET{bits: res}
  710. case INTEGER:
  711. null := big.NewInt(0)
  712. res := null.SetBit(x.bits, int(y), 0)
  713. return SET{bits: res}
  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) In(a, b scope.Value) scope.Value {
  725. switch a.(type) {
  726. case *data:
  727. return o.In(vfrom(a), b)
  728. default:
  729. switch b.(type) {
  730. case *data:
  731. return o.In(a, vfrom(b))
  732. default:
  733. switch x := a.(type) {
  734. case INTEGER:
  735. switch y := b.(type) {
  736. case SET:
  737. assert.For(int(x) < 64, 20)
  738. return BOOLEAN(y.bits.Bit(int(x)) == 1)
  739. default:
  740. panic(fmt.Sprintln(reflect.TypeOf(y)))
  741. }
  742. default:
  743. panic(fmt.Sprintln(reflect.TypeOf(x)))
  744. }
  745. }
  746. }
  747. panic(0)
  748. }
  749. func (o *ops) Min(a, b scope.Value) scope.Value {
  750. switch a.(type) {
  751. case *data:
  752. return o.Min(vfrom(a), b)
  753. default:
  754. switch b.(type) {
  755. case *data:
  756. return o.Min(a, vfrom(b))
  757. default:
  758. switch x := a.(type) {
  759. case INTEGER:
  760. switch y := b.(type) {
  761. case INTEGER:
  762. return INTEGER(int32(math.Min(float64(x), float64(y))))
  763. default:
  764. panic(fmt.Sprintln(reflect.TypeOf(y)))
  765. }
  766. default:
  767. panic(fmt.Sprintln(reflect.TypeOf(x)))
  768. }
  769. }
  770. }
  771. panic(0)
  772. }
  773. func (o *ops) Max(a, b scope.Value) scope.Value {
  774. switch a.(type) {
  775. case *data:
  776. return o.Max(vfrom(a), b)
  777. default:
  778. switch b.(type) {
  779. case *data:
  780. return o.Max(a, vfrom(b))
  781. default:
  782. switch x := a.(type) {
  783. case INTEGER:
  784. switch y := b.(type) {
  785. case INTEGER:
  786. return INTEGER(int32(math.Max(float64(x), float64(y))))
  787. default:
  788. panic(fmt.Sprintln(reflect.TypeOf(y)))
  789. }
  790. default:
  791. panic(fmt.Sprintln(reflect.TypeOf(x)))
  792. }
  793. }
  794. }
  795. panic(0)
  796. }
  797. func (o *ops) And(a, b scope.Value) scope.Value {
  798. switch a.(type) {
  799. case *data:
  800. return o.And(vfrom(a), b)
  801. default:
  802. switch b.(type) {
  803. case *data:
  804. return o.And(a, vfrom(b))
  805. default:
  806. switch x := a.(type) {
  807. case BOOLEAN:
  808. switch y := b.(type) {
  809. case BOOLEAN:
  810. return BOOLEAN(x && y)
  811. default:
  812. panic(fmt.Sprintln(reflect.TypeOf(y)))
  813. }
  814. default:
  815. panic(fmt.Sprintln(reflect.TypeOf(x)))
  816. }
  817. }
  818. }
  819. panic(0)
  820. }
  821. func (o *ops) Or(a, b scope.Value) scope.Value {
  822. switch a.(type) {
  823. case *data:
  824. return o.Or(vfrom(a), b)
  825. default:
  826. switch b.(type) {
  827. case *data:
  828. return o.Or(a, vfrom(b))
  829. default:
  830. switch x := a.(type) {
  831. case BOOLEAN:
  832. switch y := b.(type) {
  833. case BOOLEAN:
  834. return BOOLEAN(x || y)
  835. default:
  836. panic(fmt.Sprintln(reflect.TypeOf(y)))
  837. }
  838. default:
  839. panic(fmt.Sprintln(reflect.TypeOf(x)))
  840. }
  841. }
  842. }
  843. panic(0)
  844. }
  845. func (o *ops) Ash(a, b scope.Value) scope.Value {
  846. switch a.(type) {
  847. case *data:
  848. return o.Ash(vfrom(a), b)
  849. default:
  850. switch b.(type) {
  851. case *data:
  852. return o.Ash(a, vfrom(b))
  853. default:
  854. switch x := a.(type) {
  855. case INTEGER:
  856. switch y := b.(type) {
  857. case INTEGER:
  858. return INTEGER(x << uint(y))
  859. default:
  860. panic(fmt.Sprintln(reflect.TypeOf(y)))
  861. }
  862. default:
  863. panic(fmt.Sprintln(reflect.TypeOf(x)))
  864. }
  865. }
  866. }
  867. panic(0)
  868. }
  869. func (o *ops) Div(a, b scope.Value) scope.Value {
  870. switch a.(type) {
  871. case *data:
  872. return o.Div(vfrom(a), b)
  873. default:
  874. switch b.(type) {
  875. case *data:
  876. return o.Div(a, vfrom(b))
  877. default:
  878. switch x := a.(type) {
  879. case INTEGER:
  880. switch y := b.(type) {
  881. case INTEGER:
  882. return INTEGER(math.Floor(float64(x) / float64(y)))
  883. default:
  884. panic(fmt.Sprintln(reflect.TypeOf(y)))
  885. }
  886. case LONGINT:
  887. switch y := b.(type) {
  888. case LONGINT:
  889. return LONGINT(x / y)
  890. default:
  891. panic(fmt.Sprintln(reflect.TypeOf(y)))
  892. }
  893. default:
  894. panic(fmt.Sprintln(reflect.TypeOf(x)))
  895. }
  896. }
  897. }
  898. panic(0)
  899. }
  900. func (o *ops) Mod(a, b scope.Value) scope.Value {
  901. switch a.(type) {
  902. case *data:
  903. return o.Mod(vfrom(a), b)
  904. default:
  905. switch b.(type) {
  906. case *data:
  907. return o.Mod(a, vfrom(b))
  908. default:
  909. switch x := a.(type) {
  910. case INTEGER:
  911. switch y := b.(type) {
  912. case INTEGER:
  913. z := x % y
  914. switch {
  915. case (x < 0) != (y < 0):
  916. z = z + y
  917. }
  918. return INTEGER(z)
  919. default:
  920. panic(fmt.Sprintln(reflect.TypeOf(y)))
  921. }
  922. case LONGINT:
  923. switch y := b.(type) {
  924. case LONGINT:
  925. return LONGINT(x % y)
  926. default:
  927. panic(fmt.Sprintln(reflect.TypeOf(y)))
  928. }
  929. default:
  930. panic(fmt.Sprintln(reflect.TypeOf(x)))
  931. }
  932. }
  933. }
  934. panic(0)
  935. }
  936. func (o *ops) Mult(a, b scope.Value) scope.Value {
  937. switch a.(type) {
  938. case *data:
  939. return o.Mult(vfrom(a), b)
  940. default:
  941. switch b.(type) {
  942. case *data:
  943. return o.Mult(a, vfrom(b))
  944. default:
  945. switch x := a.(type) {
  946. case INTEGER:
  947. switch y := b.(type) {
  948. case INTEGER:
  949. return INTEGER(x * y)
  950. default:
  951. panic(fmt.Sprintln(reflect.TypeOf(y)))
  952. }
  953. case SET:
  954. switch y := b.(type) {
  955. case SET:
  956. res := big.NewInt(0)
  957. for i := 0; i < 64; i++ {
  958. if x.bits.Bit(i) == 1 && y.bits.Bit(i) == 1 {
  959. res.SetBit(res, i, 1)
  960. }
  961. }
  962. return SET{bits: res}
  963. default:
  964. panic(fmt.Sprintln(reflect.TypeOf(y)))
  965. }
  966. default:
  967. panic(fmt.Sprintln(reflect.TypeOf(x)))
  968. }
  969. }
  970. }
  971. panic(0)
  972. }
  973. func (o *ops) Divide(a, b scope.Value) scope.Value {
  974. switch a.(type) {
  975. case *data:
  976. return o.Divide(vfrom(a), b)
  977. default:
  978. switch b.(type) {
  979. case *data:
  980. return o.Divide(a, vfrom(b))
  981. default:
  982. switch x := a.(type) {
  983. case INTEGER:
  984. switch y := b.(type) {
  985. case INTEGER:
  986. return REAL(float64(x) / float64(y))
  987. default:
  988. panic(fmt.Sprintln(reflect.TypeOf(y)))
  989. }
  990. case REAL:
  991. switch y := b.(type) {
  992. case REAL:
  993. return REAL(float64(x) / float64(y))
  994. default:
  995. panic(fmt.Sprintln(reflect.TypeOf(y)))
  996. }
  997. case SET:
  998. switch y := b.(type) {
  999. case SET:
  1000. res := big.NewInt(0)
  1001. for i := 0; i < 64; i++ {
  1002. if x.bits.Bit(i) != y.bits.Bit(i) {
  1003. res.SetBit(res, i, 1)
  1004. }
  1005. }
  1006. return SET{bits: res}
  1007. default:
  1008. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1009. }
  1010. default:
  1011. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1012. }
  1013. }
  1014. }
  1015. panic(0)
  1016. }
  1017. func (o *ops) Len(a object.Object, _a, _b scope.Value) (ret scope.Value) {
  1018. //assert.For(a != nil, 20)
  1019. assert.For(_b != nil, 21)
  1020. var b int32 = gfrom(_b).(int32)
  1021. assert.For(b == 0, 22)
  1022. if a != nil {
  1023. assert.For(a.Type() == object.COMPLEX, 23)
  1024. switch typ := a.Complex().(type) {
  1025. case object.ArrayType:
  1026. ret = INTEGER(int32(typ.Len()))
  1027. case object.DynArrayType:
  1028. switch t := _a.(type) {
  1029. case *arr:
  1030. ret = INTEGER(t.length)
  1031. case *dynarr:
  1032. ret = INTEGER(len(t.val))
  1033. default:
  1034. halt.As(100, "unsupported", reflect.TypeOf(t))
  1035. }
  1036. default:
  1037. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
  1038. }
  1039. } else {
  1040. switch a := _a.(type) {
  1041. case *arr:
  1042. ret = INTEGER(int32(a.length))
  1043. case *dynarr:
  1044. ret = INTEGER(int32(len(a.val)))
  1045. case STRING:
  1046. rs := []rune(string(a))
  1047. ln := len(rs)
  1048. ret = INTEGER(0)
  1049. for l := 0; l < ln && rs[l] != 0; l++ {
  1050. ret = INTEGER(l + 1)
  1051. }
  1052. default:
  1053. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a)))
  1054. }
  1055. }
  1056. return ret
  1057. }
  1058. func (o *ops) Is(a scope.Value, typ object.ComplexType) scope.Value {
  1059. var compare func(x, a object.ComplexType) bool
  1060. compare = func(_x, _a object.ComplexType) bool {
  1061. switch x := _x.(type) {
  1062. case object.RecordType:
  1063. switch a := _a.(type) {
  1064. case object.RecordType:
  1065. switch {
  1066. case x.Name() == a.Name():
  1067. // fmt.Println("eq")
  1068. return true //опасно сравнивать имена конеш
  1069. case x.BaseType() != nil:
  1070. // fmt.Println("go base")
  1071. return compare(x.BaseType(), a)
  1072. default:
  1073. return false
  1074. }
  1075. default:
  1076. halt.As(100, reflect.TypeOf(a))
  1077. }
  1078. case object.PointerType:
  1079. switch a := _a.(type) {
  1080. case object.PointerType:
  1081. switch {
  1082. case x.Name() == a.Name():
  1083. // fmt.Println("eq")
  1084. return true //опасно сравнивать имена конеш
  1085. case x.Base() != nil:
  1086. // fmt.Println("go base")
  1087. return compare(x.Base(), a)
  1088. default:
  1089. return false
  1090. }
  1091. default:
  1092. halt.As(100, reflect.TypeOf(a))
  1093. }
  1094. default:
  1095. halt.As(100, reflect.TypeOf(a))
  1096. }
  1097. panic(0)
  1098. }
  1099. switch x := a.(type) {
  1100. case *rec:
  1101. z, a := x.link.Complex().(object.RecordType)
  1102. y, b := typ.(object.RecordType)
  1103. //fmt.Println("compare", x.link.Complex(), typ, a, b, a && b && compare(z, y))
  1104. return BOOLEAN(a && b && compare(z, y))
  1105. case *ptr:
  1106. z, a := x.link.Complex().(object.PointerType)
  1107. y, b := typ.(object.PointerType)
  1108. //fmt.Println("compare", x.link.Complex(), typ, a, b, a && b && compare(z, y))
  1109. return BOOLEAN(a && b && compare(z, y))
  1110. default:
  1111. halt.As(100, reflect.TypeOf(x))
  1112. }
  1113. panic(0)
  1114. }
  1115. func (o *ops) Conv(a scope.Value, typ object.Type, comp ...object.ComplexType) scope.Value {
  1116. switch typ {
  1117. case object.INTEGER:
  1118. switch x := a.(type) {
  1119. case *data:
  1120. return o.Conv(vfrom(x), typ)
  1121. case BYTE:
  1122. return INTEGER(x)
  1123. case SET:
  1124. return INTEGER(x.bits.Int64())
  1125. case REAL:
  1126. return INTEGER(x)
  1127. default:
  1128. halt.As(100, reflect.TypeOf(x))
  1129. }
  1130. case object.LONGINT:
  1131. switch x := a.(type) {
  1132. case *data:
  1133. return o.Conv(vfrom(x), typ)
  1134. case INTEGER:
  1135. return LONGINT(x)
  1136. case REAL:
  1137. return LONGINT(math.Floor(float64(x)))
  1138. default:
  1139. halt.As(100, reflect.TypeOf(x))
  1140. }
  1141. case object.SET:
  1142. switch x := a.(type) {
  1143. case *data:
  1144. return o.Conv(vfrom(x), typ)
  1145. case INTEGER:
  1146. return SET{bits: big.NewInt(int64(x))}
  1147. default:
  1148. halt.As(100, reflect.TypeOf(x))
  1149. }
  1150. case object.REAL:
  1151. switch x := a.(type) {
  1152. case *data:
  1153. return o.Conv(vfrom(x), typ)
  1154. case INTEGER:
  1155. return REAL(float64(x))
  1156. default:
  1157. halt.As(100, reflect.TypeOf(x))
  1158. }
  1159. case object.CHAR:
  1160. switch x := a.(type) {
  1161. case *data:
  1162. return o.Conv(vfrom(x), typ)
  1163. case LONGINT:
  1164. return CHAR(rune(x))
  1165. default:
  1166. halt.As(100, reflect.TypeOf(x))
  1167. }
  1168. case object.NOTYPE:
  1169. assert.For(len(comp) > 0, 20)
  1170. switch t := comp[0].(type) {
  1171. case object.BasicType:
  1172. switch t.Type() {
  1173. case object.SHORTSTRING:
  1174. switch x := a.(type) {
  1175. case *dynarr:
  1176. return SHORTSTRING(x.tryString())
  1177. case *arr:
  1178. return SHORTSTRING(x.tryString())
  1179. case STRING:
  1180. return SHORTSTRING(x)
  1181. default:
  1182. halt.As(100, reflect.TypeOf(x))
  1183. }
  1184. default:
  1185. halt.As(100, t.Type())
  1186. }
  1187. default:
  1188. halt.As(100, reflect.TypeOf(t))
  1189. }
  1190. default:
  1191. halt.As(100, typ)
  1192. }
  1193. panic(100)
  1194. }
  1195. func (o *ops) Not(a scope.Value) scope.Value {
  1196. switch x := a.(type) {
  1197. case *data:
  1198. return o.Not(vfrom(x))
  1199. case BOOLEAN:
  1200. return BOOLEAN(!x)
  1201. default:
  1202. halt.As(100, reflect.TypeOf(x))
  1203. }
  1204. panic(100)
  1205. }
  1206. func (o *ops) Abs(a scope.Value) scope.Value {
  1207. switch x := a.(type) {
  1208. case *data:
  1209. return o.Abs(vfrom(x))
  1210. case INTEGER:
  1211. return INTEGER(int32(math.Abs(float64(x))))
  1212. default:
  1213. halt.As(100, reflect.TypeOf(x))
  1214. }
  1215. panic(100)
  1216. }
  1217. func (o *ops) Minus(a scope.Value) scope.Value {
  1218. switch x := a.(type) {
  1219. case *data:
  1220. return o.Minus(vfrom(x))
  1221. case INTEGER:
  1222. return INTEGER(-x)
  1223. case LONGINT:
  1224. return LONGINT(-x)
  1225. default:
  1226. halt.As(100, reflect.TypeOf(x))
  1227. }
  1228. panic(100)
  1229. }
  1230. func (o *ops) Odd(a scope.Value) scope.Value {
  1231. switch x := a.(type) {
  1232. case *data:
  1233. return o.Odd(vfrom(x))
  1234. case INTEGER:
  1235. return BOOLEAN(int32(math.Abs(float64(x)))%2 == 1)
  1236. default:
  1237. halt.As(100, reflect.TypeOf(x))
  1238. }
  1239. panic(100)
  1240. }
  1241. func (o *ops) Cap(a scope.Value) scope.Value {
  1242. switch x := a.(type) {
  1243. case *data:
  1244. return o.Cap(vfrom(x))
  1245. case CHAR:
  1246. return CHAR([]rune(strings.ToUpper(string(x)))[0])
  1247. default:
  1248. halt.As(100, reflect.TypeOf(x))
  1249. }
  1250. panic(100)
  1251. }
  1252. func (o *ops) Bits(a scope.Value) scope.Value {
  1253. switch x := a.(type) {
  1254. case *data:
  1255. return o.Bits(vfrom(x))
  1256. case INTEGER:
  1257. return SET{bits: big.NewInt(0).SetBit(big.NewInt(0), int(x), 1)}
  1258. default:
  1259. halt.As(100, reflect.TypeOf(x))
  1260. }
  1261. panic(100)
  1262. }
  1263. func (o *ops) Eq(a, b scope.Value) scope.Value {
  1264. switch a.(type) {
  1265. case *data:
  1266. return o.Eq(vfrom(a), b)
  1267. default:
  1268. switch b.(type) {
  1269. case *data:
  1270. return o.Eq(a, vfrom(b))
  1271. default:
  1272. switch x := a.(type) {
  1273. case INTEGER:
  1274. switch y := b.(type) {
  1275. case INTEGER:
  1276. return BOOLEAN(x == y)
  1277. default:
  1278. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1279. }
  1280. case LONGINT:
  1281. switch y := b.(type) {
  1282. case LONGINT:
  1283. return BOOLEAN(x == y)
  1284. default:
  1285. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1286. }
  1287. default:
  1288. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1289. }
  1290. }
  1291. }
  1292. panic(0)
  1293. }
  1294. func (o *ops) Neq(a, b scope.Value) scope.Value {
  1295. switch i := a.(type) {
  1296. case *data:
  1297. return o.Neq(vfrom(a), b)
  1298. case *idx:
  1299. return o.Neq(vfrom(i.Get()), b)
  1300. default:
  1301. switch b.(type) {
  1302. case *data:
  1303. return o.Neq(a, vfrom(b))
  1304. default:
  1305. switch x := a.(type) {
  1306. case *ptr:
  1307. switch y := b.(type) {
  1308. case PTR:
  1309. assert.For(y == NIL, 40)
  1310. return BOOLEAN(x.val != nil && x.val.id != 0)
  1311. default:
  1312. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1313. }
  1314. case INTEGER:
  1315. switch y := b.(type) {
  1316. case INTEGER:
  1317. return BOOLEAN(x != y)
  1318. default:
  1319. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1320. }
  1321. case LONGINT:
  1322. switch y := b.(type) {
  1323. case LONGINT:
  1324. return BOOLEAN(x != y)
  1325. default:
  1326. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1327. }
  1328. case SET:
  1329. switch y := b.(type) {
  1330. case SET:
  1331. return BOOLEAN(x.bits.Cmp(y.bits) != 0)
  1332. default:
  1333. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1334. }
  1335. case BOOLEAN:
  1336. switch y := b.(type) {
  1337. case BOOLEAN:
  1338. return BOOLEAN(x != y)
  1339. default:
  1340. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1341. }
  1342. case CHAR:
  1343. switch y := b.(type) {
  1344. case CHAR:
  1345. return BOOLEAN(x != y)
  1346. default:
  1347. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1348. }
  1349. default:
  1350. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1351. }
  1352. }
  1353. }
  1354. panic(0)
  1355. }
  1356. func (o *ops) Lss(a, b scope.Value) scope.Value {
  1357. switch a.(type) {
  1358. case *data:
  1359. return o.Lss(vfrom(a), b)
  1360. default:
  1361. switch b.(type) {
  1362. case *data:
  1363. return o.Lss(a, vfrom(b))
  1364. default:
  1365. switch x := a.(type) {
  1366. case INTEGER:
  1367. switch y := b.(type) {
  1368. case INTEGER:
  1369. return BOOLEAN(x < y)
  1370. default:
  1371. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1372. }
  1373. case LONGINT:
  1374. switch y := b.(type) {
  1375. case LONGINT:
  1376. return BOOLEAN(x < y)
  1377. default:
  1378. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1379. }
  1380. default:
  1381. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1382. }
  1383. }
  1384. }
  1385. panic(0)
  1386. }
  1387. func (o *ops) Gtr(a, b scope.Value) scope.Value {
  1388. switch a.(type) {
  1389. case *data:
  1390. return o.Gtr(vfrom(a), b)
  1391. default:
  1392. switch b.(type) {
  1393. case *data:
  1394. return o.Gtr(a, vfrom(b))
  1395. default:
  1396. switch x := a.(type) {
  1397. case INTEGER:
  1398. switch y := b.(type) {
  1399. case INTEGER:
  1400. return BOOLEAN(x > y)
  1401. default:
  1402. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1403. }
  1404. default:
  1405. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1406. }
  1407. }
  1408. }
  1409. panic(0)
  1410. }
  1411. func (o *ops) Leq(a, b scope.Value) scope.Value {
  1412. switch a.(type) {
  1413. case *data:
  1414. return o.Leq(vfrom(a), b)
  1415. default:
  1416. switch b.(type) {
  1417. case *data:
  1418. return o.Leq(a, vfrom(b))
  1419. default:
  1420. switch x := a.(type) {
  1421. case INTEGER:
  1422. switch y := b.(type) {
  1423. case INTEGER:
  1424. return BOOLEAN(x <= y)
  1425. default:
  1426. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1427. }
  1428. default:
  1429. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1430. }
  1431. }
  1432. }
  1433. panic(0)
  1434. }
  1435. func (o *ops) Geq(a, b scope.Value) scope.Value {
  1436. switch a.(type) {
  1437. case *data:
  1438. return o.Geq(vfrom(a), b)
  1439. default:
  1440. switch b.(type) {
  1441. case *data:
  1442. return o.Geq(a, vfrom(b))
  1443. default:
  1444. switch x := a.(type) {
  1445. case INTEGER:
  1446. switch y := b.(type) {
  1447. case INTEGER:
  1448. return BOOLEAN(x >= y)
  1449. default:
  1450. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1451. }
  1452. default:
  1453. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1454. }
  1455. }
  1456. }
  1457. panic(0)
  1458. }
  1459. func (o *ops) TypeOf(x scope.Value) (object.Type, object.ComplexType) {
  1460. switch v := x.(type) {
  1461. case *ptr:
  1462. if v.val != nil {
  1463. return v.val.link.Type(), v.val.link.Complex()
  1464. }
  1465. case *rec:
  1466. return v.link.Type(), v.link.Complex()
  1467. case *dynarr:
  1468. return v.link.Type(), v.link.Complex()
  1469. case *arr:
  1470. return v.link.Type(), v.link.Complex()
  1471. default:
  1472. halt.As(100, reflect.TypeOf(v))
  1473. }
  1474. return object.NOTYPE, nil
  1475. }
  1476. func init() {
  1477. scope.ValueFrom = vfrom
  1478. scope.GoTypeFrom = gfrom
  1479. scope.TypeFromGo = fromg
  1480. scope.Ops = &ops{}
  1481. }