1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351 |
- package modern
- import (
- "fmt"
- "fw/cp"
- "fw/cp/node"
- "fw/cp/object"
- "fw/rt2/scope"
- "fw/utils"
- "math"
- "math/big"
- "reflect"
- "strings"
- "ypk/assert"
- "ypk/halt"
- )
- type data struct {
- link object.Object
- val interface{}
- }
- type arr struct {
- link object.Object
- val []interface{}
- length int64
- }
- type dynarr struct {
- link object.Object
- val []interface{}
- }
- type proc struct {
- link object.Object
- }
- type rec struct {
- link object.Object
- scope.Record
- l *level
- }
- type ptr struct {
- link object.Object
- scope.Pointer
- val *ptrValue
- }
- type idx struct {
- arr *arr
- idx int
- }
- func (r *rec) String() string {
- return r.link.Name()
- }
- func (r *rec) Id() cp.ID {
- return r.link.Adr()
- }
- func (r *rec) Set(v scope.Value) {
- panic(0)
- }
- func (r *rec) Get(id cp.ID) scope.Value {
- k := r.l.k[id]
- if r.l.v[k] == nil { //ref
- return r.l.r[k]
- } else {
- return r.l.v[k]
- }
- }
- func newRec(o object.Object) *rec {
- _, ok := o.Complex().(object.RecordType)
- assert.For(ok, 20)
- return &rec{link: o}
- }
- func (p *proc) String() string {
- return fmt.Sprint(p.link.Adr(), p.link.Name())
- }
- func (x *data) Id() cp.ID { return x.link.Adr() }
- func (x *arr) Id() cp.ID { return x.link.Adr() }
- func (x *dynarr) Id() cp.ID { return x.link.Adr() }
- func (a *arr) Set(v scope.Value) {
- switch x := v.(type) {
- case *arr:
- a.Set(STRING(x.tryString()))
- case STRING:
- v := make([]interface{}, int(a.length))
- for i := 0; i < int(a.length) && i < len(x); i++ {
- v[i] = CHAR(x[i])
- }
- a.val = v
- case SHORTSTRING:
- v := make([]interface{}, int(a.length))
- for i := 0; i < int(a.length) && i < len(x); i++ {
- v[i] = SHORTCHAR(x[i])
- }
- a.val = v
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- func (a *dynarr) Set(v scope.Value) {
- switch x := v.(type) {
- case *arr:
- a.val = x.val
- case *dynarr:
- a.val = x.val
- case STRING:
- v := make([]interface{}, len(x))
- for i := 0; i < len(x); i++ {
- v[i] = CHAR(x[i])
- }
- a.val = v
- case SHORTSTRING:
- v := make([]interface{}, len(x))
- for i := 0; i < len(x); i++ {
- v[i] = SHORTCHAR(x[i])
- }
- a.val = v
- case INTEGER:
- a.val = make([]interface{}, int(x))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- func (a *arr) tryString() (ret string) {
- for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
- switch x := a.val[i].(type) {
- case CHAR:
- if int(x) == 0 {
- break
- }
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- case SHORTCHAR:
- if int(x) == 0 {
- break
- }
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- return ret
- }
- func (a *arr) String() (ret string) {
- ret = fmt.Sprint("array", "[", a.length, "]")
- for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
- switch x := a.val[i].(type) {
- case CHAR:
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- case SHORTCHAR:
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- return ret
- }
- func (a *arr) Get(id scope.Value) scope.Value {
- switch i := id.(type) {
- case *data:
- return a.Get(i.val.(scope.Value))
- case INTEGER:
- assert.For(int64(i) < a.length, 20)
- if len(a.val) == 0 {
- a.val = make([]interface{}, int(a.length))
- }
- return &idx{arr: a, idx: int(i)}
- default:
- halt.As(100, reflect.TypeOf(i))
- }
- panic(0)
- }
- func (i *idx) Id() cp.ID {
- return i.arr.Id()
- }
- func (i *idx) String() string {
- return fmt.Sprint("@", i.Id(), "[", i.idx, "]")
- }
- func (i *idx) Set(v scope.Value) {
- fmt.Println(i, len(i.arr.val))
- switch x := v.(type) {
- case *idx:
- i.arr.val[i.idx] = x.arr.val[x.idx]
- case *data:
- i.Set(x.val.(scope.Value))
- case CHAR:
- i.arr.val[i.idx] = x
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- func (a *dynarr) tryString() (ret string) {
- for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
- switch x := a.val[i].(type) {
- case CHAR:
- if int(x) == 0 {
- break
- }
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- case SHORTCHAR:
- if int(x) == 0 {
- break
- }
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- return ret
- }
- func (a *dynarr) String() (ret string) {
- ret = fmt.Sprint("dyn array")
- for i := 0; i < len(a.val) && a.val[i] != nil; i++ {
- switch x := a.val[i].(type) {
- case CHAR:
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- case SHORTCHAR:
- ret = fmt.Sprint(ret, string([]rune{rune(x)}))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- return ret
- }
- func (d *data) Set(v scope.Value) {
- utils.PrintScope("set data")
- switch x := v.(type) {
- case *data:
- if d.link.Type() == x.link.Type() {
- d.val = x.val
- } else {
- d.Set(x.val.(scope.Value))
- }
- case *proc:
- assert.For(d.link.Type() == object.COMPLEX, 20)
- t, ok := d.link.Complex().(object.BasicType)
- assert.For(ok, 21, reflect.TypeOf(d.link.Complex()))
- assert.For(t.Type() == object.PROCEDURE, 22)
- d.val = x
- case *idx:
- d.val = x.arr.val[x.idx]
- case INTEGER:
- switch d.link.Type() {
- case object.INTEGER:
- d.val = x
- case object.LONGINT:
- d.val = LONGINT(x)
- case object.REAL:
- d.val = REAL(x)
- default:
- halt.As(20, d.link.Type())
- }
- case BOOLEAN:
- assert.For(d.link.Type() == object.BOOLEAN, 20)
- d.val = x
- case SHORTCHAR:
- assert.For(d.link.Type() == object.SHORTCHAR, 20)
- d.val = x
- case CHAR:
- assert.For(d.link.Type() == object.CHAR, 20)
- d.val = x
- case SHORTINT:
- assert.For(d.link.Type() == object.SHORTINT, 20)
- d.val = x
- case LONGINT:
- assert.For(d.link.Type() == object.LONGINT, 20)
- d.val = x
- case BYTE:
- assert.For(d.link.Type() == object.BYTE, 20)
- d.val = x
- case SET:
- assert.For(d.link.Type() == object.SET, 20)
- d.val = x
- case REAL:
- assert.For(d.link.Type() == object.REAL, 20)
- d.val = x
- case SHORTREAL:
- assert.For(d.link.Type() == object.SHORTREAL, 20)
- d.val = x
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- func (d *data) String() string {
- return fmt.Sprint(d.link.Name(), "=", d.val)
- }
- func (p *ptr) String() string {
- return fmt.Sprint("pointer ", p.link.Complex().(object.PointerType).Name(), "&", p.val)
- }
- func (p *ptr) Id() cp.ID { return p.link.Adr() }
- func (p *ptr) Set(v scope.Value) {
- switch x := v.(type) {
- case *ptr:
- p.Set(x.val)
- case *ptrValue:
- p.val = x
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- }
- func (p *ptr) Get() scope.Value {
- if p.val == nil {
- return NIL
- } else {
- return p.val.scope.Select(p.val.id)
- }
- }
- func newPtr(o object.Object) scope.Variable {
- _, ok := o.Complex().(object.PointerType)
- assert.For(ok, 20)
- return &ptr{link: o}
- }
- type ptrValue struct {
- scope *area
- id cp.ID
- }
- func (p *ptrValue) String() string {
- return fmt.Sprint(p.id)
- }
- type PTR int
- func (p PTR) String() string { return "NIL" }
- const NIL PTR = 0
- type INTEGER int32
- type BOOLEAN bool
- type BYTE int8
- type SHORTINT int16
- type LONGINT int64
- type SET struct {
- bits *big.Int
- }
- type CHAR rune
- type REAL float64
- type SHORTREAL float32
- type SHORTCHAR rune
- type STRING string
- type SHORTSTRING string
- func (x SHORTSTRING) String() string { return string(x) }
- func (x STRING) String() string { return string(x) }
- func (x SHORTCHAR) String() string { return fmt.Sprint(rune(x)) }
- func (x SHORTREAL) String() string { return fmt.Sprint(float32(x)) }
- func (x REAL) String() string { return fmt.Sprint(float64(x)) }
- func (x CHAR) String() string { return fmt.Sprint(rune(x)) }
- func (x SET) String() string { return fmt.Sprint(x.bits) }
- func (x LONGINT) String() string { return fmt.Sprint(int64(x)) }
- func (x SHORTINT) String() string { return fmt.Sprint(int16(x)) }
- func (x BYTE) String() string { return fmt.Sprint(int8(x)) }
- func (x INTEGER) String() string { return fmt.Sprint(int32(x)) }
- func (x BOOLEAN) String() string { return fmt.Sprint(bool(x)) }
- func newData(o object.Object) (ret scope.Variable) {
- switch o.Type() {
- case object.INTEGER:
- ret = &data{link: o, val: INTEGER(0)}
- case object.BOOLEAN:
- ret = &data{link: o, val: BOOLEAN(false)}
- case object.BYTE:
- ret = &data{link: o, val: BYTE(0)}
- case object.CHAR:
- ret = &data{link: o, val: CHAR(0)}
- case object.LONGINT:
- ret = &data{link: o, val: LONGINT(0)}
- case object.SHORTINT:
- ret = &data{link: o, val: SHORTINT(0)}
- case object.SET:
- ret = &data{link: o, val: SET{bits: big.NewInt(0)}}
- case object.REAL:
- ret = &data{link: o, val: REAL(0)}
- case object.SHORTREAL:
- ret = &data{link: o, val: SHORTREAL(0)}
- case object.SHORTCHAR:
- ret = &data{link: o, val: SHORTCHAR(0)}
- case object.COMPLEX:
- switch t := o.Complex().(type) {
- case object.BasicType:
- switch t.Type() {
- case object.PROCEDURE:
- ret = &data{link: o, val: nil}
- default:
- halt.As(100, t.Type())
- }
- case object.ArrayType:
- ret = &arr{link: o, length: t.Len()}
- case object.DynArrayType:
- ret = &dynarr{link: o}
- default:
- halt.As(100, reflect.TypeOf(t))
- }
- default:
- panic(fmt.Sprintln("unsupported type", o.Type()))
- }
- return ret
- }
- func fromg(x interface{}) scope.Value {
- switch x := x.(type) {
- case int32:
- return INTEGER(x)
- case bool:
- return BOOLEAN(x)
- case *big.Int:
- return SET{bits: x}
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- panic(100)
- }
- func newProc(o object.Object) scope.Value {
- p, ok := o.(object.ProcedureObject)
- assert.For(ok, 20, reflect.TypeOf(o))
- return &proc{link: p}
- }
- func newConst(n node.Node) scope.Value {
- switch x := n.(type) {
- case node.ConstantNode:
- switch x.Type() {
- case object.INTEGER:
- return INTEGER(x.Data().(int32))
- case object.REAL:
- return REAL(x.Data().(float64))
- case object.BOOLEAN:
- return BOOLEAN(x.Data().(bool))
- case object.SHORTCHAR:
- return SHORTCHAR(x.Data().(rune))
- case object.LONGINT:
- return LONGINT(x.Data().(int64))
- case object.SHORTINT:
- return SHORTINT(x.Data().(int16))
- case object.SHORTREAL:
- return SHORTREAL(x.Data().(float32))
- case object.BYTE:
- return BYTE(x.Data().(int8))
- case object.SET:
- return SET{bits: x.Data().(*big.Int)}
- case object.CHAR:
- return CHAR(x.Data().(rune))
- case object.STRING:
- return STRING(x.Data().(string))
- case object.SHORTSTRING:
- return SHORTSTRING(x.Data().(string))
- default:
- panic(fmt.Sprintln(x.Type()))
- }
- }
- panic(0)
- }
- func vfrom(v scope.Value) scope.Value {
- switch n := v.(type) {
- case *data:
- switch n.link.Type() {
- case object.INTEGER:
- return n.val.(INTEGER)
- case object.BYTE:
- return n.val.(BYTE)
- case object.CHAR:
- return n.val.(CHAR)
- case object.SET:
- return n.val.(SET)
- case object.BOOLEAN:
- return n.val.(BOOLEAN)
- case object.REAL:
- return n.val.(REAL)
- case object.LONGINT:
- return n.val.(LONGINT)
- default:
- halt.As(100, n.link.Type())
- }
- case INTEGER:
- return n
- default:
- halt.As(100, reflect.TypeOf(n))
- }
- return nil
- }
- func gfrom(v scope.Value) interface{} {
- switch n := v.(type) {
- case *data:
- if n.val == nil {
- return nil
- } else {
- return gfrom(n.val.(scope.Value))
- }
- case *proc:
- return n.link
- case *dynarr:
- switch n.link.Complex().(object.DynArrayType).Base() {
- case object.SHORTCHAR:
- if n.val != nil {
- return n.tryString()
- } else {
- return ""
- }
- default:
- halt.As(100, n.link.Complex().(object.DynArrayType).Base())
- }
- panic(0)
- case *arr:
- switch n.link.Complex().(object.ArrayType).Base() {
- case object.SHORTCHAR:
- if n.val != nil {
- return n.tryString()
- } else {
- return ""
- }
- default:
- halt.As(100, n.link.Complex().(object.ArrayType).Base())
- }
- panic(0)
- case INTEGER:
- return int32(n)
- case BOOLEAN:
- return bool(n)
- default:
- halt.As(100, reflect.TypeOf(n))
- }
- return nil
- }
- type ops struct{}
- func (o *ops) Sum(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Sum(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Sum(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(int32(x) + int32(y))
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case SET:
- switch y := b.(type) {
- case SET:
- return SET{bits: x.bits.Add(x.bits, y.bits)}
- case INTEGER: // INCL(SET, INTEGER)
- return SET{bits: x.bits.SetBit(x.bits, int(y), 1)}
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case *arr:
- switch y := b.(type) {
- case *arr:
- switch {
- case x.link.Type() == y.link.Type() && x.link.Complex().(object.ArrayType).Base() == object.CHAR:
- return STRING(x.tryString() + y.tryString())
- default:
- halt.As(100, x.link.Type(), y.link.Type())
- }
- case STRING:
- switch {
- case x.link.Complex().(object.ArrayType).Base() == object.CHAR:
- return STRING(x.tryString() + string(y))
- default:
- halt.As(100, x.link.Type())
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case *dynarr:
- switch y := b.(type) {
- case STRING:
- switch {
- case x.link.Complex().(object.DynArrayType).Base() == object.CHAR:
- return STRING(x.tryString() + string(y))
- default:
- halt.As(100, x.link.Type())
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case STRING:
- switch y := b.(type) {
- case STRING:
- return STRING(string(x) + string(y))
- default:
- halt.As(100, reflect.TypeOf(y))
- }
- case REAL:
- switch y := b.(type) {
- case REAL:
- return REAL(x + y)
- default:
- halt.As(100, reflect.TypeOf(y))
- }
- case LONGINT:
- switch y := b.(type) {
- case LONGINT:
- return LONGINT(x + y)
- default:
- halt.As(100, reflect.TypeOf(y))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Sub(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Sub(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Sub(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(int32(x) - int32(y))
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case SET:
- switch y := b.(type) {
- case SET:
- return SET{bits: x.bits.Sub(x.bits, y.bits)}
- case INTEGER:
- return SET{bits: x.bits.SetBit(x.bits, int(y), 0)}
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) In(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.In(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.In(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case SET:
- fmt.Println("IN врет")
- return BOOLEAN(false)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Min(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Min(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Min(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(int32(math.Min(float64(x), float64(y))))
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Max(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Max(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Max(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(int32(math.Max(float64(x), float64(y))))
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) And(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.And(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.And(a, vfrom(b))
- default:
- switch x := a.(type) {
- case BOOLEAN:
- switch y := b.(type) {
- case BOOLEAN:
- return BOOLEAN(x && y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Or(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Or(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Or(a, vfrom(b))
- default:
- switch x := a.(type) {
- case BOOLEAN:
- switch y := b.(type) {
- case BOOLEAN:
- return BOOLEAN(x || y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Ash(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Max(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Max(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(x << uint(y))
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Div(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Div(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Div(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(x / y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case LONGINT:
- switch y := b.(type) {
- case LONGINT:
- return LONGINT(x / y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Mod(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Mod(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Mod(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(x % y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case LONGINT:
- switch y := b.(type) {
- case LONGINT:
- return LONGINT(x % y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Mult(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Mult(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Mult(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return INTEGER(x * y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Divide(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Divide(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Divide(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return REAL(float64(x) / float64(y))
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case REAL:
- switch y := b.(type) {
- case REAL:
- return REAL(float64(x) / float64(y))
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Len(a object.Object, _a, _b scope.Value) (ret scope.Value) {
- //assert.For(a != nil, 20)
- assert.For(_b != nil, 21)
- var b int32 = gfrom(_b).(int32)
- assert.For(b == 0, 22)
- if a != nil {
- assert.For(a.Type() == object.COMPLEX, 23)
- switch typ := a.Complex().(type) {
- case object.ArrayType:
- ret = INTEGER(int32(typ.Len()))
- case object.DynArrayType:
- switch t := _a.(type) {
- case *arr:
- ret = INTEGER(t.length)
- case *dynarr:
- ret = INTEGER(len(t.val))
- default:
- halt.As(100, "unsupported", reflect.TypeOf(t))
- }
- default:
- panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
- }
- } else {
- switch a := _a.(type) {
- // case string:
- // ret = int64(utf8.RuneCountInString(_a.(string)))
- // case []interface{}:
- // ret = int64(len(_a.([]interface{})))
- case *arr:
- ret = INTEGER(int32(a.length))
- case *dynarr:
- ret = INTEGER(int32(len(a.val)))
- default:
- panic(fmt.Sprintln("unsupported", reflect.TypeOf(a)))
- }
- }
- return ret
- }
- func (o *ops) Is(a scope.Value, typ object.ComplexType) scope.Value {
- var compare func(x, a object.ComplexType) bool
- compare = func(_x, _a object.ComplexType) bool {
- switch x := _x.(type) {
- case object.RecordType:
- switch a := _a.(type) {
- case object.RecordType:
- switch {
- case x.Name() == a.Name():
- // fmt.Println("eq")
- return true //опасно сравнивать имена конеш
- case x.BaseType() != nil:
- // fmt.Println("go base")
- return compare(x.BaseType(), a)
- default:
- return false
- }
- default:
- halt.As(100, reflect.TypeOf(a))
- }
- case object.PointerType:
- switch a := _a.(type) {
- case object.PointerType:
- switch {
- case x.Name() == a.Name():
- // fmt.Println("eq")
- return true //опасно сравнивать имена конеш
- case x.Base() != nil:
- // fmt.Println("go base")
- return compare(x.Base(), a)
- default:
- return false
- }
- default:
- halt.As(100, reflect.TypeOf(a))
- }
- default:
- halt.As(100, reflect.TypeOf(a))
- }
- panic(0)
- }
- switch x := a.(type) {
- case *rec:
- z, a := x.link.Complex().(object.RecordType)
- y, b := typ.(object.RecordType)
- fmt.Println("compare", x.link.Complex(), typ, a, b, a && b && compare(z, y))
- return BOOLEAN(a && b && compare(z, y))
- case *ptr:
- z, a := x.link.Complex().(object.PointerType)
- y, b := typ.(object.PointerType)
- fmt.Println("compare", x.link.Complex(), typ, a, b, a && b && compare(z, y))
- return BOOLEAN(a && b && compare(z, y))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- panic(0)
- }
- func (o *ops) Conv(a scope.Value, typ object.Type, comp ...object.ComplexType) scope.Value {
- switch typ {
- case object.INTEGER:
- switch x := a.(type) {
- case *data:
- return o.Conv(vfrom(x), typ)
- case BYTE:
- return INTEGER(x)
- case SET:
- return INTEGER(x.bits.Int64())
- case REAL:
- return INTEGER(x)
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- case object.SET:
- switch x := a.(type) {
- case *data:
- return o.Conv(vfrom(x), typ)
- case INTEGER:
- return SET{bits: big.NewInt(int64(x))}
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- case object.REAL:
- switch x := a.(type) {
- case *data:
- return o.Conv(vfrom(x), typ)
- case INTEGER:
- return REAL(float64(x))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- case object.CHAR:
- switch x := a.(type) {
- case *data:
- return o.Conv(vfrom(x), typ)
- case LONGINT:
- return CHAR(rune(x))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- case object.NOTYPE:
- assert.For(len(comp) > 0, 20)
- switch t := comp[0].(type) {
- case object.BasicType:
- switch t.Type() {
- case object.SHORTSTRING:
- switch x := a.(type) {
- case *dynarr:
- return SHORTSTRING(x.tryString())
- case *arr:
- return SHORTSTRING(x.tryString())
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- default:
- halt.As(100, t.Type())
- }
- default:
- halt.As(100, reflect.TypeOf(t))
- }
- default:
- halt.As(100, typ)
- }
- panic(100)
- }
- func (o *ops) Not(a scope.Value) scope.Value {
- switch x := a.(type) {
- case *data:
- return o.Not(vfrom(x))
- case BOOLEAN:
- return BOOLEAN(!x)
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- panic(100)
- }
- func (o *ops) Abs(a scope.Value) scope.Value {
- switch x := a.(type) {
- case *data:
- return o.Abs(vfrom(x))
- case INTEGER:
- return INTEGER(int32(math.Abs(float64(x))))
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- panic(100)
- }
- func (o *ops) Odd(a scope.Value) scope.Value {
- switch x := a.(type) {
- case *data:
- return o.Odd(vfrom(x))
- case INTEGER:
- return BOOLEAN(int32(math.Abs(float64(x)))%2 == 1)
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- panic(100)
- }
- func (o *ops) Cap(a scope.Value) scope.Value {
- switch x := a.(type) {
- case *data:
- return o.Cap(vfrom(x))
- case CHAR:
- return CHAR([]rune(strings.ToUpper(string(x)))[0])
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- panic(100)
- }
- func (o *ops) Bits(a scope.Value) scope.Value {
- switch x := a.(type) {
- case *data:
- return o.Bits(vfrom(x))
- case INTEGER:
- return SET{bits: big.NewInt(int64(x))}
- default:
- halt.As(100, reflect.TypeOf(x))
- }
- panic(100)
- }
- func (o *ops) Eq(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Eq(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Eq(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return BOOLEAN(x == y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case LONGINT:
- switch y := b.(type) {
- case LONGINT:
- return BOOLEAN(x == y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Neq(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Neq(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Neq(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return BOOLEAN(x != y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case LONGINT:
- switch y := b.(type) {
- case LONGINT:
- return BOOLEAN(x != y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Lss(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Lss(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Lss(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return BOOLEAN(x < y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- case LONGINT:
- switch y := b.(type) {
- case LONGINT:
- return BOOLEAN(x < y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Gtr(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Gtr(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Gtr(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return BOOLEAN(x > y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Leq(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Leq(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Leq(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return BOOLEAN(x <= y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func (o *ops) Geq(a, b scope.Value) scope.Value {
- switch a.(type) {
- case *data:
- return o.Geq(vfrom(a), b)
- default:
- switch b.(type) {
- case *data:
- return o.Geq(a, vfrom(b))
- default:
- switch x := a.(type) {
- case INTEGER:
- switch y := b.(type) {
- case INTEGER:
- return BOOLEAN(x >= y)
- default:
- panic(fmt.Sprintln(reflect.TypeOf(y)))
- }
- default:
- panic(fmt.Sprintln(reflect.TypeOf(x)))
- }
- }
- }
- panic(0)
- }
- func init() {
- scope.ValueFrom = vfrom
- scope.GoTypeFrom = gfrom
- scope.TypeFromGo = fromg
- scope.Ops = &ops{}
- }
|