val.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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. default:
  586. halt.As(100, n.link.Complex().(object.ArrayType).Base())
  587. }
  588. panic(0)
  589. case *idx:
  590. switch n.arr.link.Complex().(object.ArrayType).Base() {
  591. case object.CHAR:
  592. return rune(n.Get().(CHAR))
  593. default:
  594. halt.As(100, n.arr.link.Complex().(object.ArrayType).Base())
  595. }
  596. panic(0)
  597. case PTR:
  598. assert.For(n == NIL, 40)
  599. return nil
  600. case INTEGER:
  601. return int32(n)
  602. case BOOLEAN:
  603. return bool(n)
  604. case STRING:
  605. return string(n)
  606. default:
  607. halt.As(100, reflect.TypeOf(n))
  608. }
  609. return nil
  610. }
  611. type ops struct{}
  612. func (o *ops) Sum(a, b scope.Value) scope.Value {
  613. switch a.(type) {
  614. case *data:
  615. return o.Sum(vfrom(a), b)
  616. default:
  617. switch b.(type) {
  618. case *data:
  619. return o.Sum(a, vfrom(b))
  620. default:
  621. switch x := a.(type) {
  622. case INTEGER:
  623. switch y := b.(type) {
  624. case INTEGER:
  625. return INTEGER(int32(x) + int32(y))
  626. default:
  627. panic(fmt.Sprintln(reflect.TypeOf(y)))
  628. }
  629. case SET:
  630. switch y := b.(type) {
  631. case SET:
  632. res := big.NewInt(0)
  633. for i := 0; i < 64; i++ {
  634. if x.bits.Bit(i) == 1 {
  635. res.SetBit(res, i, 1)
  636. }
  637. if y.bits.Bit(i) == 1 {
  638. res.SetBit(res, i, 1)
  639. }
  640. }
  641. return SET{bits: res}
  642. case INTEGER: // INCL(SET, INTEGER)
  643. return SET{bits: x.bits.SetBit(x.bits, int(y), 1)}
  644. default:
  645. panic(fmt.Sprintln(reflect.TypeOf(y)))
  646. }
  647. case *arr:
  648. switch y := b.(type) {
  649. case *arr:
  650. switch {
  651. case x.link.Type() == y.link.Type() && x.link.Complex().(object.ArrayType).Base() == object.CHAR:
  652. return STRING(x.tryString() + y.tryString())
  653. default:
  654. halt.As(100, x.link.Type(), y.link.Type())
  655. }
  656. case STRING:
  657. switch {
  658. case x.link.Complex().(object.ArrayType).Base() == object.CHAR:
  659. return STRING(x.tryString() + string(y))
  660. default:
  661. halt.As(100, x.link.Type())
  662. }
  663. default:
  664. panic(fmt.Sprintln(reflect.TypeOf(y)))
  665. }
  666. case *dynarr:
  667. switch y := b.(type) {
  668. case STRING:
  669. switch {
  670. case x.link.Complex().(object.DynArrayType).Base() == object.CHAR:
  671. return STRING(x.tryString() + string(y))
  672. default:
  673. halt.As(100, x.link.Type())
  674. }
  675. default:
  676. panic(fmt.Sprintln(reflect.TypeOf(y)))
  677. }
  678. case STRING:
  679. switch y := b.(type) {
  680. case STRING:
  681. return STRING(string(x) + string(y))
  682. default:
  683. halt.As(100, reflect.TypeOf(y))
  684. }
  685. case REAL:
  686. switch y := b.(type) {
  687. case REAL:
  688. return REAL(x + y)
  689. default:
  690. halt.As(100, reflect.TypeOf(y))
  691. }
  692. case LONGINT:
  693. switch y := b.(type) {
  694. case LONGINT:
  695. return LONGINT(x + y)
  696. default:
  697. halt.As(100, reflect.TypeOf(y))
  698. }
  699. default:
  700. panic(fmt.Sprintln(reflect.TypeOf(x)))
  701. }
  702. }
  703. }
  704. panic(0)
  705. }
  706. func (o *ops) Sub(a, b scope.Value) scope.Value {
  707. switch a.(type) {
  708. case *data:
  709. return o.Sub(vfrom(a), b)
  710. default:
  711. switch b.(type) {
  712. case *data:
  713. return o.Sub(a, vfrom(b))
  714. default:
  715. switch x := a.(type) {
  716. case INTEGER:
  717. switch y := b.(type) {
  718. case INTEGER:
  719. return INTEGER(int32(x) - int32(y))
  720. default:
  721. panic(fmt.Sprintln(reflect.TypeOf(y)))
  722. }
  723. case SET:
  724. switch y := b.(type) {
  725. case SET:
  726. res := big.NewInt(0).Set(x.bits)
  727. for i := 0; i < 64; i++ {
  728. if y.bits.Bit(i) == 1 {
  729. res.SetBit(res, i, 0)
  730. }
  731. }
  732. return SET{bits: res}
  733. case INTEGER:
  734. null := big.NewInt(0)
  735. res := null.SetBit(x.bits, int(y), 0)
  736. return SET{bits: res}
  737. default:
  738. panic(fmt.Sprintln(reflect.TypeOf(y)))
  739. }
  740. default:
  741. panic(fmt.Sprintln(reflect.TypeOf(x)))
  742. }
  743. }
  744. }
  745. panic(0)
  746. }
  747. func (o *ops) In(a, b scope.Value) scope.Value {
  748. switch a.(type) {
  749. case *data:
  750. return o.In(vfrom(a), b)
  751. default:
  752. switch b.(type) {
  753. case *data:
  754. return o.In(a, vfrom(b))
  755. default:
  756. switch x := a.(type) {
  757. case INTEGER:
  758. switch y := b.(type) {
  759. case SET:
  760. assert.For(int(x) < 64, 20)
  761. return BOOLEAN(y.bits.Bit(int(x)) == 1)
  762. default:
  763. panic(fmt.Sprintln(reflect.TypeOf(y)))
  764. }
  765. default:
  766. panic(fmt.Sprintln(reflect.TypeOf(x)))
  767. }
  768. }
  769. }
  770. panic(0)
  771. }
  772. func (o *ops) Min(a, b scope.Value) scope.Value {
  773. switch a.(type) {
  774. case *data:
  775. return o.Min(vfrom(a), b)
  776. default:
  777. switch b.(type) {
  778. case *data:
  779. return o.Min(a, vfrom(b))
  780. default:
  781. switch x := a.(type) {
  782. case INTEGER:
  783. switch y := b.(type) {
  784. case INTEGER:
  785. return INTEGER(int32(math.Min(float64(x), float64(y))))
  786. default:
  787. panic(fmt.Sprintln(reflect.TypeOf(y)))
  788. }
  789. default:
  790. panic(fmt.Sprintln(reflect.TypeOf(x)))
  791. }
  792. }
  793. }
  794. panic(0)
  795. }
  796. func (o *ops) Max(a, b scope.Value) scope.Value {
  797. switch a.(type) {
  798. case *data:
  799. return o.Max(vfrom(a), b)
  800. default:
  801. switch b.(type) {
  802. case *data:
  803. return o.Max(a, vfrom(b))
  804. default:
  805. switch x := a.(type) {
  806. case INTEGER:
  807. switch y := b.(type) {
  808. case INTEGER:
  809. return INTEGER(int32(math.Max(float64(x), float64(y))))
  810. default:
  811. panic(fmt.Sprintln(reflect.TypeOf(y)))
  812. }
  813. default:
  814. panic(fmt.Sprintln(reflect.TypeOf(x)))
  815. }
  816. }
  817. }
  818. panic(0)
  819. }
  820. func (o *ops) And(a, b scope.Value) scope.Value {
  821. switch a.(type) {
  822. case *data:
  823. return o.And(vfrom(a), b)
  824. default:
  825. switch b.(type) {
  826. case *data:
  827. return o.And(a, vfrom(b))
  828. default:
  829. switch x := a.(type) {
  830. case BOOLEAN:
  831. switch y := b.(type) {
  832. case BOOLEAN:
  833. return BOOLEAN(x && y)
  834. default:
  835. panic(fmt.Sprintln(reflect.TypeOf(y)))
  836. }
  837. default:
  838. panic(fmt.Sprintln(reflect.TypeOf(x)))
  839. }
  840. }
  841. }
  842. panic(0)
  843. }
  844. func (o *ops) Or(a, b scope.Value) scope.Value {
  845. switch a.(type) {
  846. case *data:
  847. return o.Or(vfrom(a), b)
  848. default:
  849. switch b.(type) {
  850. case *data:
  851. return o.Or(a, vfrom(b))
  852. default:
  853. switch x := a.(type) {
  854. case BOOLEAN:
  855. switch y := b.(type) {
  856. case BOOLEAN:
  857. return BOOLEAN(x || y)
  858. default:
  859. panic(fmt.Sprintln(reflect.TypeOf(y)))
  860. }
  861. default:
  862. panic(fmt.Sprintln(reflect.TypeOf(x)))
  863. }
  864. }
  865. }
  866. panic(0)
  867. }
  868. func (o *ops) Ash(a, b scope.Value) scope.Value {
  869. switch a.(type) {
  870. case *data:
  871. return o.Ash(vfrom(a), b)
  872. default:
  873. switch b.(type) {
  874. case *data:
  875. return o.Ash(a, vfrom(b))
  876. default:
  877. switch x := a.(type) {
  878. case INTEGER:
  879. switch y := b.(type) {
  880. case INTEGER:
  881. return INTEGER(x << uint(y))
  882. default:
  883. panic(fmt.Sprintln(reflect.TypeOf(y)))
  884. }
  885. default:
  886. panic(fmt.Sprintln(reflect.TypeOf(x)))
  887. }
  888. }
  889. }
  890. panic(0)
  891. }
  892. func (o *ops) Div(a, b scope.Value) scope.Value {
  893. switch a.(type) {
  894. case *data:
  895. return o.Div(vfrom(a), b)
  896. default:
  897. switch b.(type) {
  898. case *data:
  899. return o.Div(a, vfrom(b))
  900. default:
  901. switch x := a.(type) {
  902. case INTEGER:
  903. switch y := b.(type) {
  904. case INTEGER:
  905. return INTEGER(math.Floor(float64(x) / float64(y)))
  906. default:
  907. panic(fmt.Sprintln(reflect.TypeOf(y)))
  908. }
  909. case LONGINT:
  910. switch y := b.(type) {
  911. case LONGINT:
  912. return LONGINT(x / y)
  913. default:
  914. panic(fmt.Sprintln(reflect.TypeOf(y)))
  915. }
  916. default:
  917. panic(fmt.Sprintln(reflect.TypeOf(x)))
  918. }
  919. }
  920. }
  921. panic(0)
  922. }
  923. func (o *ops) Mod(a, b scope.Value) scope.Value {
  924. switch a.(type) {
  925. case *data:
  926. return o.Mod(vfrom(a), b)
  927. default:
  928. switch b.(type) {
  929. case *data:
  930. return o.Mod(a, vfrom(b))
  931. default:
  932. switch x := a.(type) {
  933. case INTEGER:
  934. switch y := b.(type) {
  935. case INTEGER:
  936. z := x % y
  937. switch {
  938. case (x < 0) != (y < 0):
  939. z = z + y
  940. }
  941. return INTEGER(z)
  942. default:
  943. panic(fmt.Sprintln(reflect.TypeOf(y)))
  944. }
  945. case LONGINT:
  946. switch y := b.(type) {
  947. case LONGINT:
  948. return LONGINT(x % y)
  949. default:
  950. panic(fmt.Sprintln(reflect.TypeOf(y)))
  951. }
  952. default:
  953. panic(fmt.Sprintln(reflect.TypeOf(x)))
  954. }
  955. }
  956. }
  957. panic(0)
  958. }
  959. func (o *ops) Mult(a, b scope.Value) scope.Value {
  960. switch a.(type) {
  961. case *data:
  962. return o.Mult(vfrom(a), b)
  963. default:
  964. switch b.(type) {
  965. case *data:
  966. return o.Mult(a, vfrom(b))
  967. default:
  968. switch x := a.(type) {
  969. case INTEGER:
  970. switch y := b.(type) {
  971. case INTEGER:
  972. return INTEGER(x * y)
  973. default:
  974. panic(fmt.Sprintln(reflect.TypeOf(y)))
  975. }
  976. case REAL:
  977. switch y := b.(type) {
  978. case REAL:
  979. return REAL(x * y)
  980. default:
  981. panic(fmt.Sprintln(reflect.TypeOf(y)))
  982. }
  983. case SET:
  984. switch y := b.(type) {
  985. case SET:
  986. res := big.NewInt(0)
  987. for i := 0; i < 64; i++ {
  988. if x.bits.Bit(i) == 1 && y.bits.Bit(i) == 1 {
  989. res.SetBit(res, i, 1)
  990. }
  991. }
  992. return SET{bits: res}
  993. default:
  994. panic(fmt.Sprintln(reflect.TypeOf(y)))
  995. }
  996. default:
  997. panic(fmt.Sprintln(reflect.TypeOf(x)))
  998. }
  999. }
  1000. }
  1001. panic(0)
  1002. }
  1003. func (o *ops) Divide(a, b scope.Value) scope.Value {
  1004. switch a.(type) {
  1005. case *data:
  1006. return o.Divide(vfrom(a), b)
  1007. default:
  1008. switch b.(type) {
  1009. case *data:
  1010. return o.Divide(a, vfrom(b))
  1011. default:
  1012. switch x := a.(type) {
  1013. case INTEGER:
  1014. switch y := b.(type) {
  1015. case INTEGER:
  1016. return REAL(float64(x) / float64(y))
  1017. default:
  1018. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1019. }
  1020. case REAL:
  1021. switch y := b.(type) {
  1022. case REAL:
  1023. return REAL(float64(x) / float64(y))
  1024. default:
  1025. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1026. }
  1027. case SET:
  1028. switch y := b.(type) {
  1029. case SET:
  1030. res := big.NewInt(0)
  1031. for i := 0; i < 64; i++ {
  1032. if x.bits.Bit(i) != y.bits.Bit(i) {
  1033. res.SetBit(res, i, 1)
  1034. }
  1035. }
  1036. return SET{bits: res}
  1037. default:
  1038. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1039. }
  1040. default:
  1041. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1042. }
  1043. }
  1044. }
  1045. panic(0)
  1046. }
  1047. func (o *ops) Len(a object.Object, _a, _b scope.Value) (ret scope.Value) {
  1048. //assert.For(a != nil, 20)
  1049. assert.For(_b != nil, 21)
  1050. var b int32 = gfrom(_b).(int32)
  1051. assert.For(b == 0, 22)
  1052. if a != nil {
  1053. assert.For(a.Type() == object.COMPLEX, 23)
  1054. switch typ := a.Complex().(type) {
  1055. case object.ArrayType:
  1056. ret = INTEGER(int32(typ.Len()))
  1057. case object.DynArrayType:
  1058. switch t := _a.(type) {
  1059. case *arr:
  1060. ret = INTEGER(t.length)
  1061. case *dynarr:
  1062. ret = INTEGER(len(t.val))
  1063. default:
  1064. halt.As(100, "unsupported", reflect.TypeOf(t))
  1065. }
  1066. default:
  1067. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
  1068. }
  1069. } else {
  1070. switch a := _a.(type) {
  1071. case *arr:
  1072. ret = INTEGER(int32(a.length))
  1073. case *dynarr:
  1074. ret = INTEGER(int32(len(a.val)))
  1075. case STRING:
  1076. rs := []rune(string(a))
  1077. ln := len(rs)
  1078. ret = INTEGER(0)
  1079. for l := 0; l < ln && rs[l] != 0; l++ {
  1080. ret = INTEGER(l + 1)
  1081. }
  1082. default:
  1083. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a)))
  1084. }
  1085. }
  1086. return ret
  1087. }
  1088. func (o *ops) Is(a scope.Value, typ object.ComplexType) scope.Value {
  1089. var compare func(x, a object.ComplexType) bool
  1090. compare = func(_x, _a object.ComplexType) bool {
  1091. switch x := _x.(type) {
  1092. case object.RecordType:
  1093. switch a := _a.(type) {
  1094. case object.RecordType:
  1095. switch {
  1096. case x.Name() == a.Name():
  1097. // fmt.Println("eq")
  1098. return true //опасно сравнивать имена конеш
  1099. case x.BaseType() != nil:
  1100. // fmt.Println("go base")
  1101. return compare(x.BaseType(), a)
  1102. default:
  1103. return false
  1104. }
  1105. default:
  1106. halt.As(100, reflect.TypeOf(a))
  1107. }
  1108. case object.PointerType:
  1109. switch a := _a.(type) {
  1110. case object.PointerType:
  1111. switch {
  1112. case x.Name() == a.Name():
  1113. // fmt.Println("eq")
  1114. return true //опасно сравнивать имена конеш
  1115. case x.Base() != nil:
  1116. // fmt.Println("go base")
  1117. return compare(x.Base(), a)
  1118. default:
  1119. return false
  1120. }
  1121. default:
  1122. halt.As(100, reflect.TypeOf(a))
  1123. }
  1124. default:
  1125. halt.As(100, reflect.TypeOf(a))
  1126. }
  1127. panic(0)
  1128. }
  1129. switch x := a.(type) {
  1130. case *rec:
  1131. z, a := x.link.Complex().(object.RecordType)
  1132. y, b := typ.(object.RecordType)
  1133. //fmt.Println("compare", x.link.Complex(), typ, a, b, a && b && compare(z, y))
  1134. return BOOLEAN(a && b && compare(z, y))
  1135. case *ptr:
  1136. z, a := x.link.Complex().(object.PointerType)
  1137. y, b := typ.(object.PointerType)
  1138. //fmt.Println("compare", x.link.Complex(), typ, a, b, a && b && compare(z, y))
  1139. return BOOLEAN(a && b && compare(z, y))
  1140. default:
  1141. halt.As(100, reflect.TypeOf(x))
  1142. }
  1143. panic(0)
  1144. }
  1145. func (o *ops) Conv(a scope.Value, typ object.Type, comp ...object.ComplexType) scope.Value {
  1146. switch typ {
  1147. case object.INTEGER:
  1148. switch x := a.(type) {
  1149. case *data:
  1150. return o.Conv(vfrom(x), typ)
  1151. case BYTE:
  1152. return INTEGER(x)
  1153. case SET:
  1154. return INTEGER(x.bits.Int64())
  1155. case REAL:
  1156. return INTEGER(x)
  1157. default:
  1158. halt.As(100, reflect.TypeOf(x))
  1159. }
  1160. case object.LONGINT:
  1161. switch x := a.(type) {
  1162. case *data:
  1163. return o.Conv(vfrom(x), typ)
  1164. case INTEGER:
  1165. return LONGINT(x)
  1166. case REAL:
  1167. return LONGINT(math.Floor(float64(x)))
  1168. default:
  1169. halt.As(100, reflect.TypeOf(x))
  1170. }
  1171. case object.SET:
  1172. switch x := a.(type) {
  1173. case *data:
  1174. return o.Conv(vfrom(x), typ)
  1175. case INTEGER:
  1176. return SET{bits: big.NewInt(int64(x))}
  1177. default:
  1178. halt.As(100, reflect.TypeOf(x))
  1179. }
  1180. case object.REAL:
  1181. switch x := a.(type) {
  1182. case *data:
  1183. return o.Conv(vfrom(x), typ)
  1184. case INTEGER:
  1185. return REAL(float64(x))
  1186. default:
  1187. halt.As(100, reflect.TypeOf(x))
  1188. }
  1189. case object.CHAR:
  1190. switch x := a.(type) {
  1191. case *data:
  1192. return o.Conv(vfrom(x), typ)
  1193. case LONGINT:
  1194. return CHAR(rune(x))
  1195. default:
  1196. halt.As(100, reflect.TypeOf(x))
  1197. }
  1198. case object.NOTYPE:
  1199. assert.For(len(comp) > 0, 20)
  1200. switch t := comp[0].(type) {
  1201. case object.BasicType:
  1202. switch t.Type() {
  1203. case object.SHORTSTRING:
  1204. switch x := a.(type) {
  1205. case *dynarr:
  1206. return SHORTSTRING(x.tryString())
  1207. case *arr:
  1208. return SHORTSTRING(x.tryString())
  1209. case STRING:
  1210. return SHORTSTRING(x)
  1211. default:
  1212. halt.As(100, reflect.TypeOf(x))
  1213. }
  1214. default:
  1215. halt.As(100, t.Type())
  1216. }
  1217. default:
  1218. halt.As(100, reflect.TypeOf(t))
  1219. }
  1220. default:
  1221. halt.As(100, typ)
  1222. }
  1223. panic(100)
  1224. }
  1225. func (o *ops) Not(a scope.Value) scope.Value {
  1226. switch x := a.(type) {
  1227. case *data:
  1228. return o.Not(vfrom(x))
  1229. case BOOLEAN:
  1230. return BOOLEAN(!x)
  1231. default:
  1232. halt.As(100, reflect.TypeOf(x))
  1233. }
  1234. panic(100)
  1235. }
  1236. func (o *ops) Abs(a scope.Value) scope.Value {
  1237. switch x := a.(type) {
  1238. case *data:
  1239. return o.Abs(vfrom(x))
  1240. case INTEGER:
  1241. return INTEGER(int32(math.Abs(float64(x))))
  1242. default:
  1243. halt.As(100, reflect.TypeOf(x))
  1244. }
  1245. panic(100)
  1246. }
  1247. func (o *ops) Minus(a scope.Value) scope.Value {
  1248. switch x := a.(type) {
  1249. case *data:
  1250. return o.Minus(vfrom(x))
  1251. case INTEGER:
  1252. return INTEGER(-x)
  1253. case LONGINT:
  1254. return LONGINT(-x)
  1255. default:
  1256. halt.As(100, reflect.TypeOf(x))
  1257. }
  1258. panic(100)
  1259. }
  1260. func (o *ops) Odd(a scope.Value) scope.Value {
  1261. switch x := a.(type) {
  1262. case *data:
  1263. return o.Odd(vfrom(x))
  1264. case INTEGER:
  1265. return BOOLEAN(int32(math.Abs(float64(x)))%2 == 1)
  1266. default:
  1267. halt.As(100, reflect.TypeOf(x))
  1268. }
  1269. panic(100)
  1270. }
  1271. func (o *ops) Cap(a scope.Value) scope.Value {
  1272. switch x := a.(type) {
  1273. case *data:
  1274. return o.Cap(vfrom(x))
  1275. case CHAR:
  1276. return CHAR([]rune(strings.ToUpper(string(x)))[0])
  1277. default:
  1278. halt.As(100, reflect.TypeOf(x))
  1279. }
  1280. panic(100)
  1281. }
  1282. func (o *ops) Bits(a scope.Value) scope.Value {
  1283. switch x := a.(type) {
  1284. case *data:
  1285. return o.Bits(vfrom(x))
  1286. case INTEGER:
  1287. return SET{bits: big.NewInt(0).SetBit(big.NewInt(0), int(x), 1)}
  1288. default:
  1289. halt.As(100, reflect.TypeOf(x))
  1290. }
  1291. panic(100)
  1292. }
  1293. func (o *ops) Eq(a, b scope.Value) scope.Value {
  1294. switch a.(type) {
  1295. case *data:
  1296. return o.Eq(vfrom(a), b)
  1297. default:
  1298. switch b.(type) {
  1299. case *data:
  1300. return o.Eq(a, vfrom(b))
  1301. default:
  1302. switch x := a.(type) {
  1303. case INTEGER:
  1304. switch y := b.(type) {
  1305. case INTEGER:
  1306. return BOOLEAN(x == y)
  1307. default:
  1308. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1309. }
  1310. case LONGINT:
  1311. switch y := b.(type) {
  1312. case LONGINT:
  1313. return BOOLEAN(x == y)
  1314. default:
  1315. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1316. }
  1317. default:
  1318. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1319. }
  1320. }
  1321. }
  1322. panic(0)
  1323. }
  1324. func (o *ops) Neq(a, b scope.Value) scope.Value {
  1325. switch i := a.(type) {
  1326. case *data:
  1327. return o.Neq(vfrom(a), b)
  1328. case *idx:
  1329. return o.Neq(vfrom(i.Get()), b)
  1330. default:
  1331. switch b.(type) {
  1332. case *data:
  1333. return o.Neq(a, vfrom(b))
  1334. default:
  1335. switch x := a.(type) {
  1336. case *ptr:
  1337. switch y := b.(type) {
  1338. case PTR:
  1339. assert.For(y == NIL, 40)
  1340. return BOOLEAN(x.val != nil && x.val.id != 0)
  1341. default:
  1342. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1343. }
  1344. case INTEGER:
  1345. switch y := b.(type) {
  1346. case INTEGER:
  1347. return BOOLEAN(x != y)
  1348. default:
  1349. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1350. }
  1351. case LONGINT:
  1352. switch y := b.(type) {
  1353. case LONGINT:
  1354. return BOOLEAN(x != y)
  1355. default:
  1356. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1357. }
  1358. case SET:
  1359. switch y := b.(type) {
  1360. case SET:
  1361. return BOOLEAN(x.bits.Cmp(y.bits) != 0)
  1362. default:
  1363. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1364. }
  1365. case BOOLEAN:
  1366. switch y := b.(type) {
  1367. case BOOLEAN:
  1368. return BOOLEAN(x != y)
  1369. default:
  1370. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1371. }
  1372. case CHAR:
  1373. switch y := b.(type) {
  1374. case CHAR:
  1375. return BOOLEAN(x != y)
  1376. default:
  1377. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1378. }
  1379. default:
  1380. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1381. }
  1382. }
  1383. }
  1384. panic(0)
  1385. }
  1386. func (o *ops) Lss(a, b scope.Value) scope.Value {
  1387. switch a.(type) {
  1388. case *data:
  1389. return o.Lss(vfrom(a), b)
  1390. default:
  1391. switch b.(type) {
  1392. case *data:
  1393. return o.Lss(a, vfrom(b))
  1394. default:
  1395. switch x := a.(type) {
  1396. case INTEGER:
  1397. switch y := b.(type) {
  1398. case INTEGER:
  1399. return BOOLEAN(x < y)
  1400. default:
  1401. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1402. }
  1403. case LONGINT:
  1404. switch y := b.(type) {
  1405. case LONGINT:
  1406. return BOOLEAN(x < y)
  1407. default:
  1408. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1409. }
  1410. default:
  1411. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1412. }
  1413. }
  1414. }
  1415. panic(0)
  1416. }
  1417. func (o *ops) Gtr(a, b scope.Value) scope.Value {
  1418. switch a.(type) {
  1419. case *data:
  1420. return o.Gtr(vfrom(a), b)
  1421. default:
  1422. switch b.(type) {
  1423. case *data:
  1424. return o.Gtr(a, vfrom(b))
  1425. default:
  1426. switch x := a.(type) {
  1427. case INTEGER:
  1428. switch y := b.(type) {
  1429. case INTEGER:
  1430. return BOOLEAN(x > y)
  1431. default:
  1432. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1433. }
  1434. case STRING:
  1435. switch y := b.(type) {
  1436. case STRING:
  1437. //fmt.Println(x, y, x > y)
  1438. return BOOLEAN(x > y)
  1439. default:
  1440. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1441. }
  1442. case REAL:
  1443. switch y := b.(type) {
  1444. case REAL:
  1445. return BOOLEAN(x > y)
  1446. default:
  1447. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1448. }
  1449. default:
  1450. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1451. }
  1452. }
  1453. }
  1454. panic(0)
  1455. }
  1456. func (o *ops) Leq(a, b scope.Value) scope.Value {
  1457. switch a.(type) {
  1458. case *data:
  1459. return o.Leq(vfrom(a), b)
  1460. default:
  1461. switch b.(type) {
  1462. case *data:
  1463. return o.Leq(a, vfrom(b))
  1464. default:
  1465. switch x := a.(type) {
  1466. case INTEGER:
  1467. switch y := b.(type) {
  1468. case INTEGER:
  1469. return BOOLEAN(x <= y)
  1470. default:
  1471. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1472. }
  1473. default:
  1474. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1475. }
  1476. }
  1477. }
  1478. panic(0)
  1479. }
  1480. func (o *ops) Geq(a, b scope.Value) scope.Value {
  1481. switch a.(type) {
  1482. case *data:
  1483. return o.Geq(vfrom(a), b)
  1484. default:
  1485. switch b.(type) {
  1486. case *data:
  1487. return o.Geq(a, vfrom(b))
  1488. default:
  1489. switch x := a.(type) {
  1490. case INTEGER:
  1491. switch y := b.(type) {
  1492. case INTEGER:
  1493. return BOOLEAN(x >= y)
  1494. default:
  1495. panic(fmt.Sprintln(reflect.TypeOf(y)))
  1496. }
  1497. default:
  1498. panic(fmt.Sprintln(reflect.TypeOf(x)))
  1499. }
  1500. }
  1501. }
  1502. panic(0)
  1503. }
  1504. func (o *ops) TypeOf(x scope.Value) (object.Type, object.ComplexType) {
  1505. switch v := x.(type) {
  1506. case *ptr:
  1507. if v.val != nil {
  1508. return v.val.link.Type(), v.val.link.Complex()
  1509. }
  1510. case *rec:
  1511. return v.link.Type(), v.link.Complex()
  1512. case *dynarr:
  1513. return v.link.Type(), v.link.Complex()
  1514. case *arr:
  1515. return v.link.Type(), v.link.Complex()
  1516. case *data:
  1517. return v.link.Type(), v.link.Complex()
  1518. default:
  1519. halt.As(100, reflect.TypeOf(v))
  1520. }
  1521. return object.NOTYPE, nil
  1522. }
  1523. func init() {
  1524. scope.ValueFrom = vfrom
  1525. scope.GoTypeFrom = gfrom
  1526. scope.TypeFromGo = fromg
  1527. scope.Ops = &ops{}
  1528. }