val.go 33 KB

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