Quellcode durchsuchen

разбираюсь с методами

kpmy vor 10 Jahren
Ursprung
Commit
39fa12eb5f

BIN
code/Cons.oz


BIN
code/Console.oz


BIN
code/Core.oz


BIN
code/JSonFormatter.oz


BIN
code/JSonGenerator.oz


BIN
code/JSonObxNoModel.oz


BIN
code/Mathe.oz


BIN
code/Out.oz


BIN
code/Start.oz


BIN
code/Start2.oz


BIN
code/Start3.oz


BIN
code/Str.oz


BIN
code/TestA0.oz


BIN
code/TestA1.oz


BIN
code/TestA2.oz


BIN
code/TestMath.oz


BIN
code/TestStrDyn.oz


BIN
code/TestStrings.oz


+ 27 - 4
cp/module/module.go

@@ -1,7 +1,6 @@
 package module
 
 import (
-	"fmt"
 	"fw/cp"
 	"fw/cp/constant/enter"
 	"fw/cp/node"
@@ -56,7 +55,7 @@ func (m *Module) ObjectByName(scope node.Node, name string) (rl []object.Object)
 func (m *Module) TypeByName(scope node.Node, name string) (ret object.ComplexType) {
 	assert.For(name != "", 20)
 	for _, typ := range m.Types[scope] {
-		fmt.Print(typ)
+		//fmt.Print(typ)
 		if v, ok := typ.(named); ok && v.Name() == name {
 			ret = typ
 			break //стыд какой
@@ -82,6 +81,23 @@ func (m *Module) ImportOf(obj object.Object) string {
 	return ""
 }
 
+func (m *Module) ImportOfType(obj object.ComplexType) string {
+	contains := func(v []object.ComplexType) bool {
+		for _, o := range v {
+			if o.Adr() == obj.Adr() {
+				return true
+			}
+		}
+		return false
+	}
+	for _, v := range m.Imports {
+		if contains(v.Types) {
+			return v.Name
+		}
+	}
+	return ""
+}
+
 func (m *Module) NodeByObject(obj object.Object) (ret []node.Node) {
 	assert.For(obj != nil, 20)
 	for i := 0; (i < len(m.Nodes)) && (ret == nil); i++ {
@@ -93,7 +109,7 @@ func (m *Module) NodeByObject(obj object.Object) (ret []node.Node) {
 	return ret
 }
 
-func (m *Module) Init() {
+func (m *Module) Init(inittd func(t object.ComplexType)) {
 	typeName := func(id cp.ID) string {
 		for _, s := range m.Objects {
 			for _, o := range s {
@@ -110,7 +126,7 @@ func (m *Module) Init() {
 				switch t := o.(type) {
 				case object.TypeObject:
 					if o.Complex().Adr() == id {
-						return t.Name()
+						return ">" + i.Name + "." + t.Name()
 					}
 				}
 			}
@@ -142,4 +158,11 @@ func (m *Module) Init() {
 			t.Qualident(q + "." + typeName(t.Adr()))
 		}
 	}
+	if inittd != nil {
+		for _, s := range m.Types {
+			for _, t := range s {
+				inittd(t)
+			}
+		}
+	}
 }

+ 11 - 7
cp/object/comp.go

@@ -1,7 +1,6 @@
 package object
 
 import (
-	"fmt"
 	"reflect"
 )
 
@@ -12,7 +11,7 @@ import (
 func (a *basic) Equals(x ComplexType) bool {
 	switch b := x.(type) {
 	case *basic:
-		fmt.Println("basic comp", a.Qualident(), ",", b.Qualident())
+		//fmt.Println("basic comp", a.Qualident(), ",", b.Qualident())
 		return a == b
 	default:
 		halt.As(100, reflect.TypeOf(b))
@@ -23,7 +22,7 @@ func (a *basic) Equals(x ComplexType) bool {
 func (a *arr) Equals(x ComplexType) bool {
 	switch b := x.(type) {
 	case *arr:
-		fmt.Println("arr comp", ":", a.Qualident(), ",", b.Qualident())
+		//fmt.Println("arr comp", ":", a.Qualident(), ",", b.Qualident())
 		return a == b
 	default:
 		halt.As(100, reflect.TypeOf(b))
@@ -34,7 +33,7 @@ func (a *arr) Equals(x ComplexType) bool {
 func (a *dyn) Equals(x ComplexType) bool {
 	switch b := x.(type) {
 	case *dyn:
-		fmt.Println("dyn comp", a.Qualident(), ",", b.Qualident())
+		//fmt.Println("dyn comp", a.Qualident(), ",", b.Qualident())
 		return a == b
 	default:
 		halt.As(100, reflect.TypeOf(b))
@@ -45,9 +44,10 @@ func (a *dyn) Equals(x ComplexType) bool {
 func (a *rec) Equals(x ComplexType) bool {
 	switch b := x.(type) {
 	case *rec:
-		fmt.Println("rec comp", a.Name(), ":", a.Qualident(), ",", b.Name(), ":", b.Qualident())
-		return a == b
+		//fmt.Println("rec:rec")
+		return a.Qualident() == b.Qualident()
 	case *ptr:
+		//fmt.Println("rec:ptr")
 		return false
 	default:
 		halt.As(100, reflect.TypeOf(b))
@@ -58,9 +58,13 @@ func (a *rec) Equals(x ComplexType) bool {
 func (a *ptr) Equals(x ComplexType) bool {
 	switch b := x.(type) {
 	case *ptr:
-		fmt.Println("pointer comp", a.Name(), ":", a.Qualident(), ",", b.Name(), ":", b.Qualident())
+		//fmt.Println("ptr:ptr")
+		//fmt.Println("pointer comp", a.Name(), ":", a.Qualident(), ",", b.Name(), ":", b.Qualident())
 		return a.Qualident() == b.Qualident()
 	case *rec:
+		//fmt.Println("ptr:rec")
+		return false
+	case nil:
 		return false
 	default:
 		halt.As(100, reflect.TypeOf(b))

+ 5 - 5
cp/object/type.go

@@ -127,8 +127,8 @@ type DynArrayType interface {
 
 type RecordType interface {
 	ComplexType
-	Base() string
-	BaseType() RecordType
+	BaseName() string
+	Base() RecordType
 	SetBase(ComplexType)
 	Name() string
 }
@@ -195,8 +195,8 @@ type rec struct {
 	basetyp    RecordType
 }
 
-func (r *rec) Name() string { return r.name }
-func (r *rec) Base() string { return r.base }
+func (r *rec) Name() string     { return r.name }
+func (r *rec) BaseName() string { return r.base }
 
 func NewRecordType(n string, id int, par ...string) (ret RecordType) {
 	if len(par) == 0 {
@@ -209,7 +209,7 @@ func NewRecordType(n string, id int, par ...string) (ret RecordType) {
 	return ret
 }
 
-func (r *rec) BaseType() RecordType { return r.basetyp }
+func (r *rec) Base() RecordType { return r.basetyp }
 func (r *rec) SetBase(t ComplexType) {
 	r.basetyp = t.(RecordType)
 }

+ 237 - 2
rt2/module/ml.go

@@ -1,13 +1,16 @@
 package module
 
 import (
+	"fmt"
 	mod "fw/cp/module"
 	"fw/cp/node"
 	"fw/cp/object"
 	"fw/rt2/context"
 	"fw/xev"
 	"os"
+	"reflect"
 	"ypk/assert"
+	"ypk/halt"
 )
 
 type Loader func(*mod.Module)
@@ -17,6 +20,7 @@ type List interface {
 	AsList() []*mod.Module
 	Load(string, ...Loader) (*mod.Module, error)
 	Loaded(string) *mod.Module
+	NewTypeCalc() TypeCalc
 }
 
 func New() List {
@@ -70,7 +74,52 @@ func (l *list) Load(name string, ldr ...Loader) (ret *mod.Module, err error) {
 			_, err = l.Load(imp.Name, loader)
 		}
 		if err == nil {
-			ret.Init()
+			ret.Init(func(t object.ComplexType) {
+				var base func(t object.ComplexType)
+				base = func(t object.ComplexType) {
+					switch i := t.(type) {
+					case object.PointerType:
+						if i.Base() != nil {
+							fmt.Print(i.Base().Qualident(), "(")
+							base(i.Base())
+							fmt.Print(")")
+						} else {
+							/*for _, n := range ret.Imports {
+								for _, _it := range n.Objects {
+									switch it := _it.(type) {
+									case object.TypeObject:
+										if it.Complex().Adr() == i.Adr() {
+											fmt.Print(it.Complex().Qualident(), "(")
+											fmt.Print(")")
+										}
+									}
+								}
+							}*/
+						}
+					case object.RecordType:
+						if i.Base() != nil {
+							fmt.Print(i.Base().Qualident(), "(")
+							base(i.Base())
+							fmt.Print(")")
+						} else {
+							/*for _, n := range ret.Imports {
+								for _, _it := range n.Objects {
+									switch it := _it.(type) {
+									case object.TypeObject:
+										if it.Complex().Adr() == i.Adr() {
+											fmt.Print(it.Complex().Qualident(), "(")
+											fmt.Print(")")
+										}
+									}
+								}
+							}*/
+						}
+					}
+				}
+				fmt.Print(t.Qualident(), "(")
+				base(t)
+				fmt.Println(")")
+			})
 			l.inner[name] = ret
 			loader(ret)
 			//fmt.Println("loaded", name)
@@ -84,6 +133,10 @@ func (l *list) Loaded(name string) *mod.Module {
 	return l.inner[name]
 }
 
+func (l *list) NewTypeCalc() TypeCalc {
+	return &tc{ml: l}
+}
+
 func DomainModule(d context.Domain) *mod.Module {
 	uni := d.Discover(context.UNIVERSE).(context.Domain)
 	name := uni.Id(d)
@@ -126,7 +179,7 @@ func ModuleOfType(d context.Domain, x object.ComplexType) *mod.Module {
 	for _, m := range ml.AsList() {
 		for _, v := range m.Types {
 			for _, o := range v {
-				if o == x {
+				if o.Adr() == x.Adr() { //сравнение по конкретному типу
 					return m
 				}
 			}
@@ -134,3 +187,185 @@ func ModuleOfType(d context.Domain, x object.ComplexType) *mod.Module {
 	}
 	return nil
 }
+
+func MapImportType(d context.Domain, imp string, t object.ComplexType) object.ComplexType {
+	uni := d.Discover(context.UNIVERSE).(context.Domain)
+	ml := uni.Discover(context.MOD).(List)
+	m := ml.Loaded(imp)
+	for _, v := range m.Types[m.Enter] {
+		if v.Equals(t) {
+			return v
+		}
+	}
+	return nil
+}
+
+type TypeCalc interface {
+	ConnectTo(interface{})
+	MethodList() map[int]Method
+}
+
+type Method struct {
+	Enter node.EnterNode
+	Obj   object.Object
+	Mod   *mod.Module
+}
+
+type tc struct {
+	ml  List
+	m   *mod.Module
+	typ object.ComplexType
+	TypeCalc
+}
+
+type inherited interface {
+	Base(...object.ComplexType) object.ComplexType
+}
+
+func (c *tc) ConnectTo(x interface{}) {
+	switch t := x.(type) {
+	case object.ComplexType:
+		c.typ = t
+	case object.TypeObject:
+		c.typ = t.Complex()
+	default:
+		halt.As(100, reflect.TypeOf(t))
+	}
+	c.m = ModuleOfType(c.ml.Domain(), c.typ)
+	assert.For(c.m != nil, 60)
+}
+
+func (c *tc) MethodList() (ret map[int]Method) {
+	ret = make(map[int]Method, 0)
+	//depth := 0
+	var deep func(*mod.Module, object.ComplexType)
+	list := func(m *mod.Module, t object.ComplexType) {
+		ol := m.Objects[c.m.Enter]
+		for _, _po := range ol {
+			switch po := _po.(type) {
+			case object.ProcedureObject:
+				proc := m.NodeByObject(po)
+				//local := false
+				for i := range proc {
+					if _, ok := proc[i].(node.EnterNode); ok {
+						//local = true
+					}
+				}
+				if po.Link() != nil {
+					pt := po.Link().Complex()
+					var pb object.ComplexType
+					if _, ok := pt.(inherited); ok {
+						pb = pt.(inherited).Base()
+					}
+					if t.Equals(pt) || t.Equals(pb) {
+						fmt.Println(po.Name())
+					}
+				}
+			}
+		}
+	}
+	foreign := func(t object.ComplexType) {
+		for _, n := range c.m.Imports {
+			for _, _it := range n.Objects {
+				switch it := _it.(type) {
+				case object.TypeObject:
+					if it.Complex().Adr() == t.Adr() {
+						nm := c.ml.Loaded(n.Name)
+						nt := nm.TypeByName(nm.Enter, it.Name())
+						deep(nm, nt)
+					}
+				}
+			}
+		}
+	}
+	deep = func(m *mod.Module, x object.ComplexType) {
+		for t := x; t != nil; {
+			list(m, t)
+			switch z := t.(type) {
+			case object.PointerType:
+				if z.Base() != nil {
+					t = z.Base()
+				} else {
+					foreign(t)
+					t = nil
+				}
+			case object.RecordType:
+				if z.Base() != nil {
+					t = z.Base()
+				} else {
+					foreign(t)
+					t = nil
+				}
+			default:
+				halt.As(0, reflect.TypeOf(t))
+			}
+		}
+		return
+	}
+	deep(c.m, c.typ)
+	return
+}
+
+func (c *tc) String() (ret string) {
+	foreign := func(t object.ComplexType) {
+		for _, n := range c.m.Imports {
+			for _, _it := range n.Objects {
+				switch it := _it.(type) {
+				case object.TypeObject:
+					if it.Complex().Adr() == t.Adr() {
+						nm := c.ml.Loaded(n.Name)
+						nt := nm.TypeByName(nm.Enter, it.Name())
+						other := c.ml.NewTypeCalc()
+						other.ConnectTo(nt)
+						ret = fmt.Sprintln(ret, "foreign", other)
+					}
+				}
+			}
+		}
+	}
+	for t := c.typ; t != nil; {
+		ret = fmt.Sprintln(ret, t.Qualident())
+		ol := c.m.Objects[c.m.Enter]
+		for _, _po := range ol {
+			switch po := _po.(type) {
+			case object.ProcedureObject:
+				proc := c.m.NodeByObject(po)
+				local := false
+				for i := range proc {
+					if _, ok := proc[i].(node.EnterNode); ok {
+						local = true
+					}
+				}
+				if po.Link() != nil {
+					pt := po.Link().Complex()
+					var pb object.ComplexType
+					if _, ok := pt.(inherited); ok {
+						pb = pt.(inherited).Base()
+					}
+					if t.Equals(pt) || t.Equals(pb) {
+						ret = fmt.Sprintln(ret, po.Name(), local)
+					}
+				}
+			}
+		}
+		switch z := t.(type) {
+		case object.PointerType:
+			if z.Base() != nil {
+				t = z.Base()
+			} else {
+				foreign(t)
+				t = nil
+			}
+		case object.RecordType:
+			if z.Base() != nil {
+				t = z.Base()
+			} else {
+				foreign(t)
+				t = nil
+			}
+		default:
+			halt.As(0, reflect.TypeOf(t))
+		}
+	}
+	return
+}

+ 9 - 1
rt2/rules/call.go

@@ -211,9 +211,11 @@ func callSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 					return This(expectExpr(f, n.Right(), func(...IN) (out OUT) {
 						var proc []node.Node
 						v := rt2.ValueOf(f)[n.Right().Adr()]
+						_, c := scope.Ops.TypeOf(v)
+
 						var dm context.Domain
 						var fn object.ProcedureObject
-						_, c := scope.Ops.TypeOf(v)
+						//fmt.Println(c.Qualident())
 						mod := rtm.ModuleOfType(f.Domain(), c)
 						dm = f.Domain().Discover(context.UNIVERSE).(context.Domain).Discover(mod.Name).(context.Domain)
 						ol := mod.Objects[mod.Enter]
@@ -228,6 +230,12 @@ func callSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 								}
 							}
 						}
+						if fn == nil {
+							x := ml.NewTypeCalc()
+							x.ConnectTo(c)
+							fmt.Println(x)
+							x.MethodList()
+						}
 						assert.For(fn != nil, 40, p.Object().Name())
 						proc = mod.NodeByObject(fn)
 						assert.For(proc != nil, 40)

+ 1 - 1
rt2/scope/modern/ms.go

@@ -117,7 +117,7 @@ func (l *level) alloc(mod *cpm.Module, root node.Node, ol []object.Object, skip
 							//fmt.Println(o.Name(), ".", x.Name(), x.Adr())
 							fl = append(fl, x)
 						}
-						rec = rec.BaseType()
+						rec = rec.Base()
 					}
 					//fmt.Println("record")
 					l.v[l.next].(*rec).l = nl

+ 2 - 2
rt2/scope/modern/val.go

@@ -1239,9 +1239,9 @@ func (o *ops) Is(a scope.Value, typ object.ComplexType) scope.Value {
 					//	fmt.Println("eq")
 					//fmt.Println("qid ", _x.Qualident(), _a.Qualident(), "names ", x.Name(), a.Name())
 					return true //опасно сравнивать имена конеш
-				case x.BaseType() != nil:
+				case x.Base() != nil:
 					//	fmt.Println("go base")
-					return compare(x.BaseType(), a)
+					return compare(x.Base(), a)
 				default:
 					return false
 				}

+ 1 - 1
xev/converter.go

@@ -273,7 +273,7 @@ func (r *Result) doType(n *Node) (ret object.ComplexType) {
 			base := r.findLink(n, "base")
 			if base != nil {
 				ret.(object.RecordType).SetBase(r.doType(base))
-				assert.For(ret.(object.RecordType).BaseType() != nil, 41)
+				assert.For(ret.(object.RecordType).Base() != nil, 41)
 			}
 		default:
 			panic(fmt.Sprintln("unknown form", n.Data.Typ.Form))