val.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. package data
  2. import (
  3. "fmt"
  4. "fw/cp"
  5. "fw/cp/node"
  6. "fw/cp/object"
  7. "fw/rt2/context"
  8. rtm "fw/rt2/module"
  9. "fw/rt2/rules2/wrap/data/items"
  10. "fw/rt2/scope"
  11. "fw/utils"
  12. "math"
  13. "math/big"
  14. "reflect"
  15. "strings"
  16. "ypk/assert"
  17. "ypk/halt"
  18. )
  19. type data struct {
  20. val interface{}
  21. typ object.Type
  22. comp object.ComplexType
  23. name string
  24. }
  25. type arr struct {
  26. val []interface{}
  27. length int64
  28. comp object.ComplexType
  29. }
  30. type dynarr struct {
  31. val []interface{}
  32. comp object.ComplexType
  33. }
  34. type proc struct {
  35. link object.Object
  36. }
  37. type rec struct {
  38. scope.Record
  39. fi items.Data
  40. name string
  41. comp object.ComplexType
  42. }
  43. type ptr struct {
  44. scope.Pointer
  45. val *ptrValue
  46. comp object.ComplexType
  47. typ object.Type
  48. }
  49. type idx struct {
  50. some scope.Array
  51. idx int
  52. }
  53. func (i *idx) comp() object.ComplexType {
  54. switch a := i.some.(type) {
  55. case *arr:
  56. return a.comp
  57. case *dynarr:
  58. return a.comp
  59. default:
  60. panic(0)
  61. }
  62. }
  63. func (i *idx) base() (object.Type, object.ComplexType) {
  64. switch a := i.comp().(type) {
  65. case object.ArrayType:
  66. return a.Base(), a.Complex()
  67. case object.DynArrayType:
  68. return a.Base(), a.Complex()
  69. default:
  70. panic(0)
  71. }
  72. }
  73. func (i *idx) val() []interface{} {
  74. switch a := i.some.(type) {
  75. case *arr:
  76. return a.val
  77. case *dynarr:
  78. return a.val
  79. default:
  80. panic(0)
  81. }
  82. }
  83. func (r *rec) String() string {
  84. return r.name
  85. }
  86. func (r *rec) Set(v scope.Value) {
  87. panic(0)
  88. }
  89. func (r *rec) Get(i cp.ID) scope.Value {
  90. x, ok := r.fi.Get(&key{id: i}).(*item)
  91. assert.For(ok, 40, i)
  92. return x.Value()
  93. }
  94. func newRec(o object.Object) *rec {
  95. _, ok := o.Complex().(object.RecordType)
  96. assert.For(ok, 20)
  97. return &rec{name: o.Name(), comp: o.Complex()}
  98. }
  99. func (p *proc) String() string {
  100. return fmt.Sprint(p.link.Adr(), p.link.Name())
  101. }
  102. //func (x *data) Id() cp.ID { return x.link.Adr() }
  103. //func (x *arr) Id() cp.ID { return x.link.Adr() }
  104. //func (x *dynarr) Id() cp.ID { return x.link.Adr() }
  105. func (a *arr) Set(v scope.Value) {
  106. switch x := v.(type) {
  107. case *arr:
  108. a.Set(STRING(x.tryString()))
  109. case STRING:
  110. v := make([]interface{}, int(a.length))
  111. for i := 0; i < int(a.length) && i < len(x); i++ {
  112. v[i] = CHAR(x[i])
  113. }
  114. a.val = v
  115. case SHORTSTRING:
  116. v := make([]interface{}, int(a.length))
  117. for i := 0; i < int(a.length) && i < len(x); i++ {
  118. v[i] = SHORTCHAR(x[i])
  119. }
  120. a.val = v
  121. default:
  122. halt.As(100, reflect.TypeOf(x))
  123. }
  124. }
  125. func (a *dynarr) Set(v scope.Value) {
  126. switch x := v.(type) {
  127. case *data:
  128. a.Set(vfrom(x))
  129. case *arr:
  130. a.val = x.val
  131. case *dynarr:
  132. a.val = x.val
  133. case *idx:
  134. a.Set(x.Get())
  135. case STRING:
  136. z := []rune(string(x))
  137. v := make([]interface{}, len(z)+1)
  138. for i := 0; i < len(z); i++ {
  139. v[i] = CHAR(z[i])
  140. }
  141. if len(v) > 1 {
  142. v[len(v)-1] = CHAR(0)
  143. }
  144. a.val = v
  145. case SHORTSTRING:
  146. z := []rune(string(x))
  147. v := make([]interface{}, len(z)+1)
  148. for i := 0; i < len(z); i++ {
  149. v[i] = SHORTCHAR(z[i])
  150. }
  151. if len(v) > 0 {
  152. v[len(v)-1] = SHORTCHAR(0)
  153. }
  154. a.val = v
  155. case INTEGER:
  156. a.val = make([]interface{}, int(x))
  157. default:
  158. halt.As(100, reflect.TypeOf(x))
  159. }
  160. }
  161. func (a *arr) tryString() (ret string) {
  162. stop := false
  163. for i := 0; !stop && i < len(a.val) && a.val[i] != nil; i++ {
  164. switch x := a.val[i].(type) {
  165. case CHAR:
  166. stop = int(x) == 0
  167. if !stop {
  168. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  169. }
  170. case SHORTCHAR:
  171. stop = int(x) == 0
  172. if !stop {
  173. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  174. }
  175. default:
  176. halt.As(100, reflect.TypeOf(x))
  177. }
  178. }
  179. return ret
  180. }
  181. func (a *arr) String() (ret string) {
  182. ret = fmt.Sprint("array", "[", a.length, "]")
  183. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  184. switch x := a.val[i].(type) {
  185. case CHAR:
  186. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  187. case SHORTCHAR:
  188. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  189. case REAL:
  190. ret = fmt.Sprint(ret, ", ", x)
  191. case *ptr:
  192. ret = fmt.Sprintln(ret, ", ", x)
  193. default:
  194. halt.As(100, reflect.TypeOf(x))
  195. }
  196. }
  197. return ret
  198. }
  199. //возвращает *idx
  200. func (a *arr) Get(id scope.Value) scope.Value {
  201. switch i := id.(type) {
  202. case *data:
  203. return a.Get(i.val.(scope.Value))
  204. case INTEGER:
  205. assert.For(int64(i) >= 0, 21)
  206. assert.For(int64(i) < a.length, 20)
  207. if len(a.val) == 0 {
  208. a.val = make([]interface{}, int(a.length))
  209. }
  210. return &idx{some: a, idx: int(i)}
  211. default:
  212. halt.As(100, reflect.TypeOf(i))
  213. }
  214. panic(0)
  215. }
  216. //возвращает *idx
  217. func (a *dynarr) Get(id scope.Value) scope.Value {
  218. switch i := id.(type) {
  219. case *data:
  220. return a.Get(i.val.(scope.Value))
  221. case INTEGER:
  222. assert.For(int64(i) >= 0, 21)
  223. assert.For(int(i) < len(a.val), 20)
  224. if len(a.val) == 0 {
  225. panic(0)
  226. }
  227. return &idx{some: a, idx: int(i)}
  228. default:
  229. halt.As(100, reflect.TypeOf(i))
  230. }
  231. panic(0)
  232. }
  233. //func (i *idx) Id() cp.ID { return i.some.Id()}
  234. func (i *idx) String() string {
  235. return fmt.Sprint("@", "[", i.idx, "]")
  236. }
  237. func (i *idx) Set(v scope.Value) {
  238. t := i.comp()
  239. switch x := v.(type) {
  240. case *idx:
  241. var comp object.Type = object.NOTYPE
  242. switch xt := x.comp().(type) {
  243. case object.ArrayType:
  244. comp = xt.Base()
  245. case object.DynArrayType:
  246. comp = xt.Base()
  247. default:
  248. halt.As(100, xt)
  249. }
  250. if comp != object.COMPLEX {
  251. i.val()[i.idx] = x.val()[x.idx]
  252. } else {
  253. switch z := x.val()[x.idx].(type) {
  254. case *arr:
  255. t := z.comp.(object.ArrayType).Base()
  256. switch t {
  257. case object.CHAR:
  258. i.val()[i.idx].(*arr).Set(STRING(z.tryString()))
  259. default:
  260. halt.As(100, t)
  261. }
  262. default:
  263. halt.As(100, reflect.TypeOf(z))
  264. }
  265. }
  266. case *data:
  267. i.Set(x.val.(scope.Value))
  268. case CHAR:
  269. i.val()[i.idx] = x
  270. case STRING:
  271. _ = t.(object.ArrayType)
  272. i.val()[i.idx].(*arr).Set(x)
  273. case REAL:
  274. i.val()[i.idx] = x
  275. case *ptrValue:
  276. switch tt := t.(type) {
  277. case object.DynArrayType:
  278. _, ok := tt.Complex().(object.PointerType)
  279. assert.For(ok, 20)
  280. i.val()[i.idx] = &ptr{val: x}
  281. default:
  282. halt.As(100, reflect.TypeOf(tt))
  283. }
  284. case *ptr:
  285. i.val()[i.idx] = x
  286. default:
  287. halt.As(100, reflect.TypeOf(x), x, t)
  288. }
  289. }
  290. func (i *idx) Get() scope.Value {
  291. x := i.val()[i.idx]
  292. switch z := x.(type) {
  293. case *arr:
  294. return z
  295. case *ptr:
  296. return z
  297. case CHAR:
  298. return z
  299. case nil:
  300. b := i.comp().(object.ArrayType).Base()
  301. switch b {
  302. case object.CHAR:
  303. return CHAR(rune(0))
  304. default:
  305. halt.As(101, reflect.TypeOf(b))
  306. }
  307. default:
  308. halt.As(100, i.idx, reflect.TypeOf(z))
  309. }
  310. panic(0)
  311. }
  312. func (a *dynarr) tryString() (ret string) {
  313. stop := false
  314. for i := 0; !stop && i < len(a.val) && a.val[i] != nil; i++ {
  315. switch x := a.val[i].(type) {
  316. case CHAR:
  317. stop = int(x) == 0
  318. if !stop {
  319. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  320. }
  321. case SHORTCHAR:
  322. stop = int(x) == 0
  323. if !stop {
  324. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  325. }
  326. default:
  327. halt.As(100, reflect.TypeOf(x))
  328. }
  329. }
  330. return ret
  331. }
  332. func (a *dynarr) String() (ret string) {
  333. ret = fmt.Sprint("dyn array")
  334. for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
  335. switch x := a.val[i].(type) {
  336. case CHAR:
  337. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  338. case SHORTCHAR:
  339. ret = fmt.Sprint(ret, string([]rune{rune(x)}))
  340. case *ptr:
  341. ret = fmt.Sprint(ret, ", ", x)
  342. default:
  343. halt.As(100, reflect.TypeOf(x))
  344. }
  345. }
  346. return ret
  347. }
  348. func (d *data) Set(v scope.Value) {
  349. utils.PrintScope("set data")
  350. switch x := v.(type) {
  351. case *data:
  352. if d.typ == x.typ {
  353. d.val = x.val
  354. } else {
  355. d.Set(x.val.(scope.Value))
  356. }
  357. case *proc:
  358. assert.For(d.typ == object.COMPLEX, 20)
  359. t, ok := d.comp.(object.BasicType)
  360. assert.For(ok, 21, reflect.TypeOf(d.comp))
  361. assert.For(t.Type() == object.PROCEDURE, 22)
  362. d.val = x
  363. case *idx:
  364. d.val = x.val()[x.idx]
  365. case INTEGER:
  366. switch d.typ {
  367. case object.INTEGER:
  368. d.val = x
  369. case object.LONGINT:
  370. d.val = LONGINT(x)
  371. case object.REAL:
  372. d.val = REAL(x)
  373. default:
  374. halt.As(20, d.typ)
  375. }
  376. case BOOLEAN:
  377. assert.For(d.typ == object.BOOLEAN, 20)
  378. d.val = x
  379. case SHORTCHAR:
  380. assert.For(d.typ == object.SHORTCHAR, 20)
  381. d.val = x
  382. case CHAR:
  383. assert.For(d.typ == object.CHAR, 20)
  384. d.val = x
  385. case SHORTINT:
  386. assert.For(d.typ == object.SHORTINT, 20)
  387. d.val = x
  388. case LONGINT:
  389. assert.For(d.typ == object.LONGINT, 20)
  390. d.val = x
  391. case BYTE:
  392. assert.For(d.typ == object.BYTE, 20)
  393. d.val = x
  394. case SET:
  395. assert.For(d.typ == object.SET, 20)
  396. d.val = x
  397. case REAL:
  398. assert.For(d.typ == object.REAL, 20)
  399. d.val = x
  400. case SHORTREAL:
  401. assert.For(d.typ == object.SHORTREAL, 20)
  402. d.val = x
  403. default:
  404. panic(fmt.Sprintln(reflect.TypeOf(x)))
  405. }
  406. }
  407. func (d *data) String() string {
  408. return fmt.Sprint(d.name, "=", d.val)
  409. }
  410. func (p *ptr) String() string {
  411. if p.comp != nil {
  412. return fmt.Sprint("pointer ", p.comp.(object.PointerType).Name(), "&", p.val)
  413. } else {
  414. return fmt.Sprint("pointer simple", p.typ)
  415. }
  416. }
  417. func (p *ptr) Set(v scope.Value) {
  418. switch x := v.(type) {
  419. case *ptr:
  420. p.Set(x.val)
  421. case *ptrValue:
  422. p.val = x
  423. case PTR:
  424. assert.For(x == NIL, 40)
  425. p.val = nil
  426. case *idx:
  427. p.Set(x.Get())
  428. default:
  429. halt.As(100, reflect.TypeOf(x))
  430. }
  431. }
  432. func (p *ptr) Get() (ret scope.Value) {
  433. if p.val == nil {
  434. return NIL
  435. } else {
  436. p.val.scope.Select(p.val.id, func(v scope.Value) {
  437. ret = v
  438. })
  439. }
  440. return
  441. }
  442. func (p *ptr) Copy() (ret scope.Pointer) {
  443. fake := object.New(object.VARIABLE, cp.Some())
  444. fake.SetComplex(p.comp)
  445. fake.SetType(object.COMPLEX)
  446. fake.SetName("<" + ">")
  447. push(p.val.scope.d, p.val.scope.il, fake)
  448. tmp := newPtr(fake)
  449. ret = tmp.(scope.Pointer)
  450. ret.Set(p)
  451. return
  452. }
  453. func newPtr(o object.Object) scope.Variable {
  454. _, ok := o.Complex().(object.PointerType)
  455. assert.For(ok, 20)
  456. return &ptr{typ: o.Type(), comp: o.Complex()}
  457. }
  458. type ptrValue struct {
  459. scope *area
  460. id cp.ID
  461. ct object.ComplexType
  462. //link object.Object
  463. }
  464. func (p *ptrValue) String() string {
  465. return fmt.Sprint(p.id)
  466. }
  467. type PTR int
  468. func (p PTR) String() string {
  469. return "NIL"
  470. }
  471. const NIL PTR = 0
  472. type INTEGER int32
  473. type BOOLEAN bool
  474. type BYTE int8
  475. type SHORTINT int16
  476. type LONGINT int64
  477. type SET struct {
  478. bits *big.Int
  479. }
  480. type CHAR rune
  481. type REAL float64
  482. type SHORTREAL float32
  483. type SHORTCHAR rune
  484. type STRING string
  485. type SHORTSTRING string
  486. func (x SHORTSTRING) String() string { return string(x) }
  487. func (x STRING) String() string { return string(x) }
  488. func (x SHORTCHAR) String() string { return fmt.Sprint(rune(x)) }
  489. func (x SHORTREAL) String() string { return fmt.Sprint(float32(x)) }
  490. func (x REAL) String() string { return fmt.Sprint(float64(x)) }
  491. func (x CHAR) String() string { return fmt.Sprint(rune(x)) }
  492. func (x SET) String() string { return fmt.Sprint(x.bits) }
  493. func (x LONGINT) String() string { return fmt.Sprint(int64(x)) }
  494. func (x SHORTINT) String() string { return fmt.Sprint(int16(x)) }
  495. func (x BYTE) String() string { return fmt.Sprint(int8(x)) }
  496. func (x INTEGER) String() string { return fmt.Sprint(int32(x)) }
  497. func (x BOOLEAN) String() string { return fmt.Sprint(bool(x)) }
  498. func newData(o object.Object) (ret scope.Variable) {
  499. switch o.Type() {
  500. case object.INTEGER:
  501. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: INTEGER(0)}
  502. case object.BOOLEAN:
  503. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: BOOLEAN(false)}
  504. case object.BYTE:
  505. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: BYTE(0)}
  506. case object.CHAR:
  507. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: CHAR(0)}
  508. case object.LONGINT:
  509. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: LONGINT(0)}
  510. case object.SHORTINT:
  511. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: SHORTINT(0)}
  512. case object.SET:
  513. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: SET{bits: big.NewInt(0)}}
  514. case object.REAL:
  515. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: REAL(0)}
  516. case object.SHORTREAL:
  517. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: SHORTREAL(0)}
  518. case object.SHORTCHAR:
  519. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: SHORTCHAR(0)}
  520. case object.COMPLEX:
  521. switch t := o.Complex().(type) {
  522. case object.BasicType:
  523. switch t.Type() {
  524. case object.PROCEDURE:
  525. ret = &data{typ: o.Type(), comp: o.Complex(), name: o.Name(), val: nil}
  526. default:
  527. halt.As(100, t.Type())
  528. }
  529. case object.ArrayType:
  530. ret = &arr{comp: o.Complex(), length: t.Len()}
  531. if a := ret.(*arr); t.Base() == object.COMPLEX {
  532. a.val = make([]interface{}, int(t.Len()))
  533. for i := 0; i < int(t.Len()); i++ {
  534. fake := object.New(object.VARIABLE, cp.Some())
  535. fake.SetName("[?]")
  536. fake.SetType(object.COMPLEX)
  537. fake.SetComplex(t.Complex())
  538. a.val[i] = newData(fake)
  539. }
  540. }
  541. case object.DynArrayType:
  542. ret = &dynarr{comp: o.Complex()}
  543. default:
  544. halt.As(100, reflect.TypeOf(t))
  545. }
  546. case object.NOTYPE:
  547. switch t := o.Complex().(type) {
  548. case nil:
  549. ret = &ptr{typ: o.Type(), comp: o.Complex()}
  550. default:
  551. halt.As(100, reflect.TypeOf(t))
  552. }
  553. default:
  554. panic(fmt.Sprintln("unsupported type", o, o.Type(), o.Complex()))
  555. }
  556. return ret
  557. }
  558. func fromg(x interface{}) scope.Value {
  559. switch x := x.(type) {
  560. case int32:
  561. return INTEGER(x)
  562. case bool:
  563. return BOOLEAN(x)
  564. case *big.Int:
  565. return SET{bits: x}
  566. case string:
  567. return STRING(x)
  568. case float64:
  569. return REAL(x)
  570. default:
  571. halt.As(100, reflect.TypeOf(x))
  572. }
  573. panic(100)
  574. }
  575. func newProc(o object.Object) scope.Value {
  576. p, ok := o.(object.ProcedureObject)
  577. assert.For(ok, 20, reflect.TypeOf(o))
  578. return &proc{link: p}
  579. }
  580. func newConst(n node.Node) scope.Value {
  581. switch x := n.(type) {
  582. case node.ConstantNode:
  583. switch x.Type() {
  584. case object.INTEGER:
  585. return INTEGER(x.Data().(int32))
  586. case object.REAL:
  587. return REAL(x.Data().(float64))
  588. case object.BOOLEAN:
  589. return BOOLEAN(x.Data().(bool))
  590. case object.SHORTCHAR:
  591. return SHORTCHAR(x.Data().(rune))
  592. case object.LONGINT:
  593. return LONGINT(x.Data().(int64))
  594. case object.SHORTINT:
  595. return SHORTINT(x.Data().(int16))
  596. case object.SHORTREAL:
  597. return SHORTREAL(x.Data().(float32))
  598. case object.BYTE:
  599. return BYTE(x.Data().(int8))
  600. case object.SET:
  601. return SET{bits: x.Data().(*big.Int)}
  602. case object.CHAR:
  603. return CHAR(x.Data().(rune))
  604. case object.STRING:
  605. return STRING(x.Data().(string))
  606. case object.SHORTSTRING:
  607. return SHORTSTRING(x.Data().(string))
  608. case object.NIL:
  609. return NIL
  610. case object.COMPLEX: //не может существовать в реальности, используется для передачи параметров от рантайма
  611. switch d := x.Data().(type) {
  612. case *ptrValue:
  613. return d
  614. default:
  615. halt.As(100, reflect.TypeOf(d))
  616. }
  617. default:
  618. panic(fmt.Sprintln(x.Type()))
  619. }
  620. }
  621. panic(0)
  622. }
  623. func vfrom(v scope.Value) scope.Value {
  624. switch n := v.(type) {
  625. case *data:
  626. switch n.typ {
  627. case object.INTEGER:
  628. return n.val.(INTEGER)
  629. case object.BYTE:
  630. return n.val.(BYTE)
  631. case object.CHAR:
  632. return n.val.(CHAR)
  633. case object.SET:
  634. return n.val.(SET)
  635. case object.BOOLEAN:
  636. return n.val.(BOOLEAN)
  637. case object.REAL:
  638. return n.val.(REAL)
  639. case object.LONGINT:
  640. return n.val.(LONGINT)
  641. default:
  642. halt.As(100, n.typ)
  643. }
  644. case INTEGER, CHAR:
  645. return n
  646. default:
  647. halt.As(100, reflect.TypeOf(n))
  648. }
  649. return nil
  650. }
  651. func gfrom(v scope.Value) interface{} {
  652. switch n := v.(type) {
  653. case *data:
  654. if n.val == nil {
  655. return nil
  656. } else {
  657. return gfrom(n.val.(scope.Value))
  658. }
  659. case *rec:
  660. return n
  661. case *ptr:
  662. return n
  663. case *proc:
  664. return n.link
  665. case *dynarr:
  666. switch n.comp.(object.DynArrayType).Base() {
  667. case object.SHORTCHAR:
  668. if n.val != nil {
  669. return n.tryString()
  670. } else {
  671. return ""
  672. }
  673. case object.CHAR:
  674. if n.val != nil {
  675. return n.tryString()
  676. } else {
  677. return ""
  678. }
  679. case object.COMPLEX:
  680. return n.val
  681. default:
  682. halt.As(100, n.comp.(object.DynArrayType).Base())
  683. }
  684. panic(0)
  685. case *arr:
  686. switch n.comp.(object.ArrayType).Base() {
  687. case object.SHORTCHAR:
  688. if n.val != nil {
  689. return n.tryString()
  690. } else {
  691. return ""
  692. }
  693. case object.CHAR:
  694. if n.val != nil {
  695. return n.tryString()
  696. } else {
  697. return ""
  698. }
  699. case object.REAL:
  700. ret := make([]float64, 0)
  701. for i := 0; i < len(n.val) && n.val[i] != nil; i++ {
  702. ret = append(ret, float64(n.val[i].(REAL)))
  703. }
  704. return ret
  705. default:
  706. halt.As(100, n.comp.(object.ArrayType).Base())
  707. }
  708. panic(0)
  709. case *idx:
  710. switch t := n.comp().(type) {
  711. case object.ArrayType:
  712. switch t.Base() {
  713. case object.CHAR:
  714. return rune(n.Get().(CHAR))
  715. default:
  716. halt.As(100, t.Base())
  717. }
  718. case object.DynArrayType:
  719. switch t.Base() {
  720. case object.CHAR:
  721. return rune(n.Get().(CHAR))
  722. default:
  723. halt.As(100, t.Base())
  724. }
  725. default:
  726. halt.As(100, reflect.TypeOf(t))
  727. }
  728. panic(0)
  729. case PTR:
  730. assert.For(n == NIL, 40)
  731. return nil
  732. case INTEGER:
  733. return int32(n)
  734. case BOOLEAN:
  735. return bool(n)
  736. case STRING:
  737. return string(n)
  738. case SHORTSTRING:
  739. return string(n)
  740. case CHAR:
  741. return rune(n)
  742. default:
  743. halt.As(100, reflect.TypeOf(n))
  744. }
  745. return nil
  746. }
  747. type ops struct {
  748. domain context.Domain
  749. }
  750. func (o *ops) Sum(a, b scope.Value) scope.Value {
  751. switch a.(type) {
  752. case *data:
  753. return o.Sum(vfrom(a), b)
  754. default:
  755. switch b.(type) {
  756. case *data:
  757. return o.Sum(a, vfrom(b))
  758. default:
  759. switch x := a.(type) {
  760. case INTEGER:
  761. switch y := b.(type) {
  762. case INTEGER:
  763. return INTEGER(int32(x) + int32(y))
  764. default:
  765. panic(fmt.Sprintln(reflect.TypeOf(y)))
  766. }
  767. case SET:
  768. switch y := b.(type) {
  769. case SET:
  770. res := big.NewInt(0)
  771. for i := 0; i < 64; i++ {
  772. if x.bits.Bit(i) == 1 {
  773. res.SetBit(res, i, 1)
  774. }
  775. if y.bits.Bit(i) == 1 {
  776. res.SetBit(res, i, 1)
  777. }
  778. }
  779. return SET{bits: res}
  780. case INTEGER: // INCL(SET, INTEGER)
  781. return SET{bits: x.bits.SetBit(x.bits, int(y), 1)}
  782. default:
  783. panic(fmt.Sprintln(reflect.TypeOf(y)))
  784. }
  785. case *arr:
  786. switch y := b.(type) {
  787. case *arr:
  788. switch {
  789. case x.comp.(object.ArrayType).Base() == object.CHAR:
  790. return STRING(x.tryString() + y.tryString())
  791. default:
  792. halt.As(100, x.comp, y.comp)
  793. }
  794. case STRING:
  795. switch {
  796. case x.comp.(object.ArrayType).Base() == object.CHAR:
  797. return STRING(x.tryString() + string(y))
  798. default:
  799. halt.As(100, x.comp)
  800. }
  801. default:
  802. panic(fmt.Sprintln(reflect.TypeOf(y)))
  803. }
  804. case *dynarr:
  805. switch y := b.(type) {
  806. case STRING:
  807. switch {
  808. case x.comp.(object.DynArrayType).Base() == object.CHAR:
  809. return STRING(x.tryString() + string(y))
  810. default:
  811. halt.As(100, x.comp)
  812. }
  813. default:
  814. panic(fmt.Sprintln(reflect.TypeOf(y)))
  815. }
  816. case STRING:
  817. switch y := b.(type) {
  818. case STRING:
  819. return STRING(string(x) + string(y))
  820. default:
  821. halt.As(100, reflect.TypeOf(y))
  822. }
  823. case REAL:
  824. switch y := b.(type) {
  825. case REAL:
  826. return REAL(x + y)
  827. default:
  828. halt.As(100, reflect.TypeOf(y))
  829. }
  830. case LONGINT:
  831. switch y := b.(type) {
  832. case LONGINT:
  833. return LONGINT(x + y)
  834. default:
  835. halt.As(100, reflect.TypeOf(y))
  836. }
  837. default:
  838. panic(fmt.Sprintln(reflect.TypeOf(x)))
  839. }
  840. }
  841. }
  842. panic(0)
  843. }
  844. func (o *ops) Sub(a, b scope.Value) scope.Value {
  845. switch a.(type) {
  846. case *data:
  847. return o.Sub(vfrom(a), b)
  848. default:
  849. switch b.(type) {
  850. case *data:
  851. return o.Sub(a, vfrom(b))
  852. default:
  853. switch x := a.(type) {
  854. case INTEGER:
  855. switch y := b.(type) {
  856. case INTEGER:
  857. return INTEGER(int32(x) - int32(y))
  858. default:
  859. panic(fmt.Sprintln(reflect.TypeOf(y)))
  860. }
  861. case REAL:
  862. switch y := b.(type) {
  863. case REAL:
  864. return REAL(x - y)
  865. default:
  866. panic(fmt.Sprintln(reflect.TypeOf(y)))
  867. }
  868. case LONGINT:
  869. switch y := b.(type) {
  870. case LONGINT:
  871. return INTEGER(int64(x) - int64(y))
  872. default:
  873. panic(fmt.Sprintln(reflect.TypeOf(y)))
  874. }
  875. case SET:
  876. switch y := b.(type) {
  877. case SET:
  878. res := big.NewInt(0).Set(x.bits)
  879. for i := 0; i < 64; i++ {
  880. if y.bits.Bit(i) == 1 {
  881. res.SetBit(res, i, 0)
  882. }
  883. }
  884. return SET{bits: res}
  885. case INTEGER:
  886. null := big.NewInt(0)
  887. res := null.SetBit(x.bits, int(y), 0)
  888. return SET{bits: res}
  889. default:
  890. panic(fmt.Sprintln(reflect.TypeOf(y)))
  891. }
  892. default:
  893. panic(fmt.Sprintln(reflect.TypeOf(x)))
  894. }
  895. }
  896. }
  897. panic(0)
  898. }
  899. func (o *ops) In(a, b scope.Value) scope.Value {
  900. switch a.(type) {
  901. case *data:
  902. return o.In(vfrom(a), b)
  903. default:
  904. switch b.(type) {
  905. case *data:
  906. return o.In(a, vfrom(b))
  907. default:
  908. switch x := a.(type) {
  909. case INTEGER:
  910. switch y := b.(type) {
  911. case SET:
  912. assert.For(int(x) < 64, 20)
  913. return BOOLEAN(y.bits.Bit(int(x)) == 1)
  914. default:
  915. panic(fmt.Sprintln(reflect.TypeOf(y)))
  916. }
  917. default:
  918. panic(fmt.Sprintln(reflect.TypeOf(x)))
  919. }
  920. }
  921. }
  922. panic(0)
  923. }
  924. func (o *ops) Min(a, b scope.Value) scope.Value {
  925. switch a.(type) {
  926. case *data:
  927. return o.Min(vfrom(a), b)
  928. default:
  929. switch b.(type) {
  930. case *data:
  931. return o.Min(a, vfrom(b))
  932. default:
  933. switch x := a.(type) {
  934. case INTEGER:
  935. switch y := b.(type) {
  936. case INTEGER:
  937. return INTEGER(int32(math.Min(float64(x), float64(y))))
  938. default:
  939. panic(fmt.Sprintln(reflect.TypeOf(y)))
  940. }
  941. default:
  942. panic(fmt.Sprintln(reflect.TypeOf(x)))
  943. }
  944. }
  945. }
  946. panic(0)
  947. }
  948. func (o *ops) Max(a, b scope.Value) scope.Value {
  949. switch a.(type) {
  950. case *data:
  951. return o.Max(vfrom(a), b)
  952. default:
  953. switch b.(type) {
  954. case *data:
  955. return o.Max(a, vfrom(b))
  956. default:
  957. switch x := a.(type) {
  958. case INTEGER:
  959. switch y := b.(type) {
  960. case INTEGER:
  961. return INTEGER(int32(math.Max(float64(x), float64(y))))
  962. default:
  963. panic(fmt.Sprintln(reflect.TypeOf(y)))
  964. }
  965. default:
  966. panic(fmt.Sprintln(reflect.TypeOf(x)))
  967. }
  968. }
  969. }
  970. panic(0)
  971. }
  972. func (o *ops) And(a, b scope.Value) scope.Value {
  973. switch a.(type) {
  974. case *data:
  975. return o.And(vfrom(a), b)
  976. default:
  977. switch b.(type) {
  978. case *data:
  979. return o.And(a, vfrom(b))
  980. default:
  981. switch x := a.(type) {
  982. case BOOLEAN:
  983. switch y := b.(type) {
  984. case BOOLEAN:
  985. return BOOLEAN(x && y)
  986. default:
  987. panic(fmt.Sprintln(reflect.TypeOf(y)))
  988. }
  989. default:
  990. panic(fmt.Sprintln(reflect.TypeOf(x)))
  991. }
  992. }
  993. }
  994. panic(0)
  995. }
  996. func (o *ops) Or(a, b scope.Value) scope.Value {
  997. switch a.(type) {
  998. case *data:
  999. return o.Or(vfrom(a), b)
  1000. default:
  1001. switch b.(type) {
  1002. case *data:
  1003. return o.Or(a, vfrom(b))
  1004. default:
  1005. switch x := a.(type) {
  1006. case BOOLEAN:
  1007. switch y := b.(type) {
  1008. case BOOLEAN:
  1009. return BOOLEAN(x || y)
  1010. default:
  1011. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1012. }
  1013. default:
  1014. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1015. }
  1016. }
  1017. }
  1018. panic(0)
  1019. }
  1020. func (o *ops) Ash(a, b scope.Value) scope.Value {
  1021. switch a.(type) {
  1022. case *data:
  1023. return o.Ash(vfrom(a), b)
  1024. default:
  1025. switch b.(type) {
  1026. case *data:
  1027. return o.Ash(a, vfrom(b))
  1028. default:
  1029. switch x := a.(type) {
  1030. case INTEGER:
  1031. switch y := b.(type) {
  1032. case INTEGER:
  1033. return INTEGER(x << uint(y))
  1034. default:
  1035. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1036. }
  1037. default:
  1038. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1039. }
  1040. }
  1041. }
  1042. panic(0)
  1043. }
  1044. func (o *ops) Div(a, b scope.Value) scope.Value {
  1045. switch a.(type) {
  1046. case *data:
  1047. return o.Div(vfrom(a), b)
  1048. default:
  1049. switch b.(type) {
  1050. case *data:
  1051. return o.Div(a, vfrom(b))
  1052. default:
  1053. switch x := a.(type) {
  1054. case INTEGER:
  1055. switch y := b.(type) {
  1056. case INTEGER:
  1057. return INTEGER(math.Floor(float64(x) / float64(y)))
  1058. default:
  1059. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1060. }
  1061. case LONGINT:
  1062. switch y := b.(type) {
  1063. case LONGINT:
  1064. return LONGINT(x / y)
  1065. default:
  1066. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1067. }
  1068. default:
  1069. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1070. }
  1071. }
  1072. }
  1073. panic(0)
  1074. }
  1075. func (o *ops) Mod(a, b scope.Value) scope.Value {
  1076. switch a.(type) {
  1077. case *data:
  1078. return o.Mod(vfrom(a), b)
  1079. default:
  1080. switch b.(type) {
  1081. case *data:
  1082. return o.Mod(a, vfrom(b))
  1083. default:
  1084. switch x := a.(type) {
  1085. case INTEGER:
  1086. switch y := b.(type) {
  1087. case INTEGER:
  1088. z := x % y
  1089. switch {
  1090. case (x < 0) != (y < 0):
  1091. z = z + y
  1092. }
  1093. return INTEGER(z)
  1094. default:
  1095. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1096. }
  1097. case LONGINT:
  1098. switch y := b.(type) {
  1099. case LONGINT:
  1100. z := x % y
  1101. switch {
  1102. case (x < 0) != (y < 0):
  1103. z = z + y
  1104. }
  1105. return LONGINT(z)
  1106. default:
  1107. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1108. }
  1109. default:
  1110. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1111. }
  1112. }
  1113. }
  1114. panic(0)
  1115. }
  1116. func (o *ops) Msk(a, b scope.Value) scope.Value {
  1117. switch a.(type) {
  1118. case *data:
  1119. return o.Msk(vfrom(a), b)
  1120. default:
  1121. switch b.(type) {
  1122. case *data:
  1123. return o.Msk(a, vfrom(b))
  1124. default:
  1125. switch x := a.(type) {
  1126. case INTEGER:
  1127. switch y := b.(type) {
  1128. case INTEGER:
  1129. y = -y
  1130. z := x % y
  1131. switch {
  1132. case (x < 0) != (y < 0):
  1133. z = z + y
  1134. }
  1135. return INTEGER(z)
  1136. default:
  1137. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1138. }
  1139. case LONGINT:
  1140. switch y := b.(type) {
  1141. case LONGINT:
  1142. y = -y
  1143. z := x % y
  1144. switch {
  1145. case (x < 0) != (y < 0):
  1146. z = z + y
  1147. }
  1148. return LONGINT(z)
  1149. default:
  1150. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1151. }
  1152. default:
  1153. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1154. }
  1155. }
  1156. }
  1157. panic(0)
  1158. }
  1159. func (o *ops) Mult(a, b scope.Value) scope.Value {
  1160. switch a.(type) {
  1161. case *data:
  1162. return o.Mult(vfrom(a), b)
  1163. default:
  1164. switch b.(type) {
  1165. case *data:
  1166. return o.Mult(a, vfrom(b))
  1167. default:
  1168. switch x := a.(type) {
  1169. case INTEGER:
  1170. switch y := b.(type) {
  1171. case INTEGER:
  1172. return INTEGER(x * y)
  1173. default:
  1174. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1175. }
  1176. case REAL:
  1177. switch y := b.(type) {
  1178. case REAL:
  1179. return REAL(x * y)
  1180. default:
  1181. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1182. }
  1183. case SET:
  1184. switch y := b.(type) {
  1185. case SET:
  1186. res := big.NewInt(0)
  1187. for i := 0; i < 64; i++ {
  1188. if x.bits.Bit(i) == 1 && y.bits.Bit(i) == 1 {
  1189. res.SetBit(res, i, 1)
  1190. }
  1191. }
  1192. return SET{bits: res}
  1193. default:
  1194. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1195. }
  1196. default:
  1197. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1198. }
  1199. }
  1200. }
  1201. panic(0)
  1202. }
  1203. func (o *ops) Divide(a, b scope.Value) scope.Value {
  1204. switch a.(type) {
  1205. case *data:
  1206. return o.Divide(vfrom(a), b)
  1207. default:
  1208. switch b.(type) {
  1209. case *data:
  1210. return o.Divide(a, vfrom(b))
  1211. default:
  1212. switch x := a.(type) {
  1213. case INTEGER:
  1214. switch y := b.(type) {
  1215. case INTEGER:
  1216. return REAL(float64(x) / float64(y))
  1217. default:
  1218. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1219. }
  1220. case REAL:
  1221. switch y := b.(type) {
  1222. case REAL:
  1223. return REAL(float64(x) / float64(y))
  1224. default:
  1225. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1226. }
  1227. case SET:
  1228. switch y := b.(type) {
  1229. case SET:
  1230. res := big.NewInt(0)
  1231. for i := 0; i < 64; i++ {
  1232. if x.bits.Bit(i) != y.bits.Bit(i) {
  1233. res.SetBit(res, i, 1)
  1234. }
  1235. }
  1236. return SET{bits: res}
  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) Len(a object.Object, _a, _b scope.Value) (ret scope.Value) {
  1248. //assert.For(a != nil, 20)
  1249. assert.For(_b != nil, 21)
  1250. var b int32 = gfrom(_b).(int32)
  1251. assert.For(b == 0, 22)
  1252. if a != nil {
  1253. assert.For(a.Type() == object.COMPLEX, 23)
  1254. switch typ := a.Complex().(type) {
  1255. case object.ArrayType:
  1256. ret = INTEGER(int32(typ.Len()))
  1257. case object.DynArrayType:
  1258. switch t := _a.(type) {
  1259. case *arr:
  1260. ret = INTEGER(t.length)
  1261. case *dynarr:
  1262. ret = INTEGER(len(t.val))
  1263. default:
  1264. halt.As(100, "unsupported", reflect.TypeOf(t))
  1265. }
  1266. default:
  1267. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
  1268. }
  1269. } else {
  1270. switch a := _a.(type) {
  1271. case *arr:
  1272. ret = INTEGER(int32(a.length))
  1273. case *dynarr:
  1274. ret = INTEGER(int32(len(a.val)))
  1275. case STRING:
  1276. rs := []rune(string(a))
  1277. ln := len(rs)
  1278. ret = INTEGER(0)
  1279. for l := 0; l < ln && rs[l] != 0; l++ {
  1280. ret = INTEGER(l + 1)
  1281. }
  1282. default:
  1283. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a)))
  1284. }
  1285. }
  1286. return ret
  1287. }
  1288. func (o *ops) Is(a scope.Value, typ object.ComplexType) scope.Value {
  1289. /* var compare func(x, a object.ComplexType) bool
  1290. compare = func(_x, _a object.ComplexType) bool {
  1291. switch x := _x.(type) {
  1292. case object.RecordType:
  1293. switch a := _a.(type) {
  1294. case object.RecordType:
  1295. tc := o.Domain().Global().Discover(context.MOD).(rtm.List).NewTypeCalc()
  1296. tc.ConnectTo(a)
  1297. _, fc := tc.ForeignBase()
  1298. //fmt.Println(a, fc)
  1299. switch {
  1300. case x.Name() == a.Name():
  1301. //fmt.Println("eq")
  1302. //fmt.Println("qid ", x.Qualident(), _a.Qualident(), "names ", x.Name(), a.Name())
  1303. return true //опасно сравнивать имена конеш
  1304. case x.Complex() != nil:
  1305. //fmt.Println("go base")
  1306. //fmt.Println("qid ", x.Complex().Qualident(), a.Qualident())
  1307. return compare(x.Complex(), a)
  1308. case fc != nil:
  1309. //fmt.Println("go foreign")
  1310. //fmt.Println("qid ", x.Qualident(), fc.Qualident())
  1311. return compare(x, fc)
  1312. default:
  1313. //fmt.Println("go here")
  1314. return false
  1315. }
  1316. case object.PointerType:
  1317. if a.Complex() != nil {
  1318. return compare(x, a.Complex())
  1319. } else {
  1320. //fmt.Println("to here")
  1321. return false
  1322. }
  1323. default:
  1324. halt.As(100, reflect.TypeOf(a))
  1325. }
  1326. case object.PointerType:
  1327. switch a := _a.(type) {
  1328. case object.PointerType:
  1329. switch {
  1330. case x.Name() == a.Name():
  1331. //fmt.Println("eq")
  1332. return true //опасно сравнивать имена конеш
  1333. case x.Complex() != nil:
  1334. //fmt.Println("go base")
  1335. return compare(x.Complex(), a)
  1336. default:
  1337. return false
  1338. }
  1339. default:
  1340. halt.As(100, reflect.TypeOf(a))
  1341. }
  1342. default:
  1343. halt.As(100, reflect.TypeOf(a))
  1344. }
  1345. panic(0)
  1346. } */
  1347. comp2 := func(this, with object.ComplexType) (ret bool) {
  1348. //fmt.Println("compare")
  1349. tc := o.Domain().Global().Discover(context.MOD).(rtm.List).NewTypeCalc()
  1350. other := with
  1351. dump := func(this object.ComplexType) {
  1352. //fmt.Println(this.Adr())
  1353. for x := this; x != nil && !ret; {
  1354. tc.ConnectTo(x)
  1355. //fmt.Println(x.Qualident(), other.Qualident(), x.Qualident() == other.Qualident())
  1356. ret = x.Qualident() == other.Qualident()
  1357. x = x.(rtm.Inherited).Complex()
  1358. if x == nil {
  1359. _, x = tc.ForeignBase()
  1360. }
  1361. }
  1362. }
  1363. tc.ConnectTo(with)
  1364. if _, foreign := tc.ForeignBase(); foreign != nil {
  1365. other = foreign
  1366. }
  1367. dump(this)
  1368. //dump(with)
  1369. //fmt.Println()
  1370. return
  1371. }
  1372. comp3 := func(this, with object.ComplexType) bool {
  1373. switch w := with.(type) {
  1374. case object.PointerType:
  1375. return comp2(this, w.Complex())
  1376. default:
  1377. return comp2(this, with)
  1378. }
  1379. }
  1380. var do func(scope.Value, object.ComplexType) bool
  1381. do = func(a scope.Value, typ object.ComplexType) (ret bool) {
  1382. switch x := a.(type) {
  1383. case *rec:
  1384. switch tt := typ.(type) {
  1385. case object.PointerType:
  1386. ret = comp2(x.comp, tt.Complex())
  1387. default:
  1388. ret = comp2(x.comp, typ)
  1389. }
  1390. case *ptr:
  1391. switch xt := x.comp.(type) {
  1392. case object.PointerType:
  1393. if xt.Name() == "ANYPTR" {
  1394. val := x.Get()
  1395. t, c := o.TypeOf(val)
  1396. assert.For(t == object.COMPLEX, 40)
  1397. ret = comp3(c, typ)
  1398. }
  1399. default:
  1400. ret = comp3(x.comp, typ)
  1401. }
  1402. case *idx:
  1403. ret = do(x.Get(), typ)
  1404. }
  1405. return
  1406. }
  1407. return BOOLEAN(do(a, typ))
  1408. /* switch x := a.(type) {
  1409. case *rec:
  1410. z, a := x.comp.(object.RecordType)
  1411. y, b := typ.(object.RecordType)
  1412. //fmt.Println("compare rec", x.comp, typ, a, b, a && b && compare(z, y))
  1413. return BOOLEAN(a && b && compare(z, y))
  1414. case *ptr:
  1415. var zz object.ComplexType
  1416. z, a := x.comp.(object.PointerType)
  1417. zz = z
  1418. if val := x.Get(); z.Name() == "ANYPTR" && val != NIL {
  1419. t, c := o.TypeOf(val)
  1420. assert.For(t == object.COMPLEX, 40)
  1421. zz, a = c.(object.RecordType)
  1422. }
  1423. y, b := typ.(object.PointerType)
  1424. //fmt.Println("compare ptr", z, typ, a, b, a && b && compare(z, y))
  1425. return BOOLEAN(a && b && compare(zz, y))
  1426. case *idx:
  1427. return o.Is(x.Get(), typ)
  1428. default:
  1429. halt.As(100, reflect.TypeOf(x))
  1430. }
  1431. panic(0)*/
  1432. }
  1433. func (o *ops) Conv(a scope.Value, typ object.Type, comp ...object.ComplexType) scope.Value {
  1434. switch typ {
  1435. case object.INTEGER:
  1436. switch x := a.(type) {
  1437. case *data:
  1438. return o.Conv(vfrom(x), typ, comp...)
  1439. case *idx:
  1440. //t, c := x.base()
  1441. return o.Conv(x.val()[x.idx].(scope.Value), typ, comp...)
  1442. case BYTE:
  1443. return INTEGER(x)
  1444. case SET:
  1445. return INTEGER(x.bits.Int64())
  1446. case REAL:
  1447. return INTEGER(x)
  1448. case LONGINT:
  1449. return INTEGER(x)
  1450. case CHAR:
  1451. return INTEGER(x)
  1452. default:
  1453. halt.As(100, reflect.TypeOf(x))
  1454. }
  1455. case object.LONGINT:
  1456. switch x := a.(type) {
  1457. case *data:
  1458. return o.Conv(vfrom(x), typ, comp...)
  1459. case INTEGER:
  1460. return LONGINT(x)
  1461. case REAL:
  1462. return LONGINT(math.Floor(float64(x)))
  1463. default:
  1464. halt.As(100, reflect.TypeOf(x))
  1465. }
  1466. case object.SET:
  1467. switch x := a.(type) {
  1468. case *data:
  1469. return o.Conv(vfrom(x), typ, comp...)
  1470. case INTEGER:
  1471. return SET{bits: big.NewInt(int64(x))}
  1472. default:
  1473. halt.As(100, reflect.TypeOf(x))
  1474. }
  1475. case object.REAL:
  1476. switch x := a.(type) {
  1477. case *data:
  1478. return o.Conv(vfrom(x), typ, comp...)
  1479. case INTEGER:
  1480. return REAL(float64(x))
  1481. default:
  1482. halt.As(100, reflect.TypeOf(x))
  1483. }
  1484. case object.CHAR:
  1485. switch x := a.(type) {
  1486. case *data:
  1487. return o.Conv(vfrom(x), typ, comp...)
  1488. case LONGINT:
  1489. return CHAR(rune(x))
  1490. case INTEGER:
  1491. return CHAR(rune(x))
  1492. case CHAR:
  1493. return x
  1494. default:
  1495. halt.As(100, reflect.TypeOf(x))
  1496. }
  1497. case object.NOTYPE:
  1498. assert.For(len(comp) > 0, 20)
  1499. switch t := comp[0].(type) {
  1500. case object.BasicType:
  1501. switch t.Type() {
  1502. case object.SHORTSTRING:
  1503. switch x := a.(type) {
  1504. case *dynarr:
  1505. return SHORTSTRING(x.tryString())
  1506. case *arr:
  1507. return SHORTSTRING(x.tryString())
  1508. case STRING:
  1509. return SHORTSTRING(x)
  1510. default:
  1511. halt.As(100, reflect.TypeOf(x))
  1512. }
  1513. default:
  1514. halt.As(100, t.Type())
  1515. }
  1516. default:
  1517. halt.As(100, reflect.TypeOf(t))
  1518. }
  1519. default:
  1520. halt.As(100, typ)
  1521. }
  1522. panic(100)
  1523. }
  1524. func (o *ops) Not(a scope.Value) scope.Value {
  1525. switch x := a.(type) {
  1526. case *data:
  1527. return o.Not(vfrom(x))
  1528. case BOOLEAN:
  1529. return BOOLEAN(!x)
  1530. default:
  1531. halt.As(100, reflect.TypeOf(x))
  1532. }
  1533. panic(100)
  1534. }
  1535. func (o *ops) Abs(a scope.Value) scope.Value {
  1536. switch x := a.(type) {
  1537. case *data:
  1538. return o.Abs(vfrom(x))
  1539. case INTEGER:
  1540. return INTEGER(int32(math.Abs(float64(x))))
  1541. default:
  1542. halt.As(100, reflect.TypeOf(x))
  1543. }
  1544. panic(100)
  1545. }
  1546. func (o *ops) Minus(a scope.Value) scope.Value {
  1547. switch x := a.(type) {
  1548. case *data:
  1549. return o.Minus(vfrom(x))
  1550. case INTEGER:
  1551. return INTEGER(-x)
  1552. case LONGINT:
  1553. return LONGINT(-x)
  1554. case REAL:
  1555. return REAL(-x)
  1556. default:
  1557. halt.As(100, reflect.TypeOf(x))
  1558. }
  1559. panic(100)
  1560. }
  1561. func (o *ops) Odd(a scope.Value) scope.Value {
  1562. switch x := a.(type) {
  1563. case *data:
  1564. return o.Odd(vfrom(x))
  1565. case INTEGER:
  1566. return BOOLEAN(int64(math.Abs(float64(x)))%2 == 1)
  1567. case LONGINT:
  1568. return BOOLEAN(int64(math.Abs(float64(x)))%2 == 1)
  1569. default:
  1570. halt.As(100, reflect.TypeOf(x))
  1571. }
  1572. panic(100)
  1573. }
  1574. func (o *ops) Cap(a scope.Value) scope.Value {
  1575. switch x := a.(type) {
  1576. case *data:
  1577. return o.Cap(vfrom(x))
  1578. case CHAR:
  1579. return CHAR([]rune(strings.ToUpper(string(x)))[0])
  1580. default:
  1581. halt.As(100, reflect.TypeOf(x))
  1582. }
  1583. panic(100)
  1584. }
  1585. func (o *ops) Bits(a scope.Value) scope.Value {
  1586. switch x := a.(type) {
  1587. case *data:
  1588. return o.Bits(vfrom(x))
  1589. case INTEGER:
  1590. return SET{bits: big.NewInt(0).SetBit(big.NewInt(0), int(x), 1)}
  1591. default:
  1592. halt.As(100, reflect.TypeOf(x))
  1593. }
  1594. panic(100)
  1595. }
  1596. func (o *ops) Eq(a, b scope.Value) scope.Value {
  1597. switch a.(type) {
  1598. case *data:
  1599. return o.Eq(vfrom(a), b)
  1600. default:
  1601. switch b.(type) {
  1602. case *data:
  1603. return o.Eq(a, vfrom(b))
  1604. default:
  1605. switch x := a.(type) {
  1606. case *ptr:
  1607. switch y := b.(type) {
  1608. case PTR:
  1609. assert.For(y == NIL, 40)
  1610. return BOOLEAN(x.val == nil || x.val.id == 0)
  1611. default:
  1612. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1613. }
  1614. case INTEGER:
  1615. switch y := b.(type) {
  1616. case INTEGER:
  1617. return BOOLEAN(x == y)
  1618. default:
  1619. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1620. }
  1621. case LONGINT:
  1622. switch y := b.(type) {
  1623. case LONGINT:
  1624. return BOOLEAN(x == y)
  1625. default:
  1626. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1627. }
  1628. case CHAR:
  1629. switch y := b.(type) {
  1630. case CHAR:
  1631. return BOOLEAN(x == y)
  1632. default:
  1633. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1634. }
  1635. default:
  1636. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1637. }
  1638. }
  1639. }
  1640. panic(0)
  1641. }
  1642. func (o *ops) Neq(a, b scope.Value) scope.Value {
  1643. switch i := a.(type) {
  1644. case *data:
  1645. return o.Neq(vfrom(a), b)
  1646. case *idx:
  1647. return o.Neq(vfrom(i.Get()), b)
  1648. default:
  1649. switch b.(type) {
  1650. case *data:
  1651. return o.Neq(a, vfrom(b))
  1652. default:
  1653. switch x := a.(type) {
  1654. case *ptr:
  1655. switch y := b.(type) {
  1656. case PTR:
  1657. assert.For(y == NIL, 40)
  1658. return BOOLEAN(x.val != nil && x.val.id != 0)
  1659. default:
  1660. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1661. }
  1662. case INTEGER:
  1663. switch y := b.(type) {
  1664. case INTEGER:
  1665. return BOOLEAN(x != y)
  1666. default:
  1667. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1668. }
  1669. case REAL:
  1670. switch y := b.(type) {
  1671. case REAL:
  1672. return BOOLEAN(x != y)
  1673. default:
  1674. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1675. }
  1676. case LONGINT:
  1677. switch y := b.(type) {
  1678. case LONGINT:
  1679. return BOOLEAN(x != y)
  1680. default:
  1681. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1682. }
  1683. case SET:
  1684. switch y := b.(type) {
  1685. case SET:
  1686. return BOOLEAN(x.bits.Cmp(y.bits) != 0)
  1687. default:
  1688. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1689. }
  1690. case BOOLEAN:
  1691. switch y := b.(type) {
  1692. case BOOLEAN:
  1693. return BOOLEAN(x != y)
  1694. default:
  1695. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1696. }
  1697. case CHAR:
  1698. switch y := b.(type) {
  1699. case CHAR:
  1700. return BOOLEAN(x != y)
  1701. default:
  1702. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1703. }
  1704. default:
  1705. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1706. }
  1707. }
  1708. }
  1709. panic(0)
  1710. }
  1711. func (o *ops) Lss(a, b scope.Value) scope.Value {
  1712. switch a.(type) {
  1713. case *data:
  1714. return o.Lss(vfrom(a), b)
  1715. default:
  1716. switch b.(type) {
  1717. case *data:
  1718. return o.Lss(a, vfrom(b))
  1719. default:
  1720. switch x := a.(type) {
  1721. case INTEGER:
  1722. switch y := b.(type) {
  1723. case INTEGER:
  1724. return BOOLEAN(x < y)
  1725. default:
  1726. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1727. }
  1728. case REAL:
  1729. switch y := b.(type) {
  1730. case REAL:
  1731. return BOOLEAN(x < y)
  1732. default:
  1733. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1734. }
  1735. case LONGINT:
  1736. switch y := b.(type) {
  1737. case LONGINT:
  1738. return BOOLEAN(x < y)
  1739. default:
  1740. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1741. }
  1742. case CHAR:
  1743. switch y := b.(type) {
  1744. case CHAR:
  1745. return BOOLEAN(x < y)
  1746. case INTEGER:
  1747. return BOOLEAN(uint(x) < uint(y))
  1748. default:
  1749. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1750. }
  1751. default:
  1752. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1753. }
  1754. }
  1755. }
  1756. panic(0)
  1757. }
  1758. func (o *ops) Gtr(a, b scope.Value) scope.Value {
  1759. switch a.(type) {
  1760. case *data:
  1761. return o.Gtr(vfrom(a), b)
  1762. default:
  1763. switch b.(type) {
  1764. case *data:
  1765. return o.Gtr(a, vfrom(b))
  1766. default:
  1767. switch x := a.(type) {
  1768. case INTEGER:
  1769. switch y := b.(type) {
  1770. case INTEGER:
  1771. return BOOLEAN(x > y)
  1772. default:
  1773. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1774. }
  1775. case STRING:
  1776. switch y := b.(type) {
  1777. case STRING:
  1778. //fmt.Println(x, y, x > y)
  1779. return BOOLEAN(x > y)
  1780. default:
  1781. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1782. }
  1783. case REAL:
  1784. switch y := b.(type) {
  1785. case REAL:
  1786. return BOOLEAN(x > y)
  1787. default:
  1788. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1789. }
  1790. case LONGINT:
  1791. switch y := b.(type) {
  1792. case LONGINT:
  1793. return BOOLEAN(x > y)
  1794. default:
  1795. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1796. }
  1797. default:
  1798. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1799. }
  1800. }
  1801. }
  1802. panic(0)
  1803. }
  1804. func (o *ops) Leq(a, b scope.Value) scope.Value {
  1805. switch a.(type) {
  1806. case *data:
  1807. return o.Leq(vfrom(a), b)
  1808. default:
  1809. switch b.(type) {
  1810. case *data:
  1811. return o.Leq(a, vfrom(b))
  1812. default:
  1813. switch x := a.(type) {
  1814. case INTEGER:
  1815. switch y := b.(type) {
  1816. case INTEGER:
  1817. return BOOLEAN(x <= y)
  1818. default:
  1819. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1820. }
  1821. default:
  1822. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1823. }
  1824. }
  1825. }
  1826. panic(0)
  1827. }
  1828. func (o *ops) Geq(a, b scope.Value) scope.Value {
  1829. switch a.(type) {
  1830. case *data:
  1831. return o.Geq(vfrom(a), b)
  1832. default:
  1833. switch b.(type) {
  1834. case *data:
  1835. return o.Geq(a, vfrom(b))
  1836. default:
  1837. switch x := a.(type) {
  1838. case INTEGER:
  1839. switch y := b.(type) {
  1840. case INTEGER:
  1841. return BOOLEAN(x >= y)
  1842. default:
  1843. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1844. }
  1845. case REAL:
  1846. switch y := b.(type) {
  1847. case REAL:
  1848. return BOOLEAN(x >= y)
  1849. default:
  1850. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1851. }
  1852. default:
  1853. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1854. }
  1855. }
  1856. }
  1857. panic(0)
  1858. }
  1859. func (o *ops) TypeOf(x scope.Value) (object.Type, object.ComplexType) {
  1860. switch v := x.(type) {
  1861. case *ptr:
  1862. //assert.For(v.val != nil, 20, v.Id())
  1863. if v.val != nil {
  1864. return object.COMPLEX, v.val.ct
  1865. } else {
  1866. return v.typ, v.comp
  1867. }
  1868. case *rec:
  1869. return object.COMPLEX, v.comp
  1870. case *dynarr:
  1871. return object.COMPLEX, v.comp
  1872. case *arr:
  1873. return object.COMPLEX, v.comp
  1874. case *data:
  1875. return v.typ, v.comp
  1876. case *idx:
  1877. return v.base()
  1878. default:
  1879. halt.As(100, reflect.TypeOf(v))
  1880. }
  1881. return object.NOTYPE, nil
  1882. }
  1883. func (o *ops) Domain(x ...context.Domain) context.Domain {
  1884. if len(x) == 1 {
  1885. o.domain = x[0]
  1886. }
  1887. return o.domain
  1888. }
  1889. func init() {
  1890. scope.ValueFrom = vfrom
  1891. scope.GoTypeFrom = gfrom
  1892. scope.TypeFromGo = fromg
  1893. scope.Ops = &ops{}
  1894. }