val.go 32 KB

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