Jelajahi Sumber

начал работу над новым вариантом кучи

kpmy 10 tahun lalu
induk
melakukan
85ebed5b2e
9 mengubah file dengan 187 tambahan dan 88 penghapusan
  1. 2 2
      fw.go
  2. 1 1
      rt2/frame/std/sf.go
  3. 14 0
      rt2/module/ml.go
  4. 5 5
      rt2/rules/assign.go
  5. 4 2
      rt2/scope/area.go
  6. 4 0
      rt2/scope/data.go
  7. 41 0
      rt2/scope/modern/hp.go
  8. 88 65
      rt2/scope/modern/ms.go
  9. 28 13
      rt2/scope/modern/val.go

+ 2 - 2
fw.go

@@ -9,7 +9,6 @@ import (
 	rtmod "fw/rt2/module"
 	_ "fw/rt2/rules"
 	"fw/rt2/scope"
-	// "fw/rt2/scope/std"
 	_ "fw/rt2/scope/modern"
 	"time"
 	"ypk/assert"
@@ -31,9 +30,10 @@ func main() {
 		name = "XevDemo19"
 	}
 	global := &stdDomain{god: true}
+	global.global = global
 	modList := rtmod.New()
 	global.Attach(context.MOD, modList)
-	global.Attach(context.HEAP, scope.New())
+	global.Attach(context.HEAP, scope.New(context.HEAP))
 	t0 := time.Now()
 	var init []*mod.Module
 	_, err := modList.Load(name, func(m *mod.Module) {

+ 1 - 1
rt2/frame/std/sf.go

@@ -34,7 +34,7 @@ func (f *RootFrame) PushFor(fr, parent frame.Frame) {
 	f.inner.PushFront(fr)
 	if parent == nil {
 		domain := f.Domain().(context.Factory).New()
-		domain.Attach(context.SCOPE, scope.New())
+		domain.Attach(context.SCOPE, scope.New(context.SCOPE))
 		fr.Init(domain)
 	} else {
 		fr.Init(parent.Domain())

+ 14 - 0
rt2/module/ml.go

@@ -3,6 +3,7 @@ package module
 import (
 	"fmt"
 	mod "fw/cp/module"
+	"fw/cp/node"
 	"fw/rt2/context"
 	"fw/xev"
 	"os"
@@ -89,3 +90,16 @@ func DomainModule(d context.Domain) *mod.Module {
 	ml := uni.Discover(context.MOD).(List)
 	return ml.Loaded(name)
 }
+
+func ModuleOfNode(d context.Domain, x node.Node) *mod.Module {
+	uni := d.Discover(context.UNIVERSE).(context.Domain)
+	ml := uni.Discover(context.MOD).(List)
+	for _, m := range ml.AsList() {
+		for _, n := range m.Nodes {
+			if n == x {
+				return m
+			}
+		}
+	}
+	return nil
+}

+ 5 - 5
rt2/rules/assign.go

@@ -163,18 +163,18 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			panic(fmt.Sprintln("wrong left", reflect.TypeOf(a.Left())))
 		}
 	case statement.NEW:
-		//		sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
-		//heap := scope.This(f.Domain().Discover(context.HEAP))
-		panic(0)
+		//sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+		heap := f.Domain().Discover(context.HEAP).(scope.Manager)
 		if a.Right() != nil {
 			seq, ret = This(expectExpr(f, a.Right(), func(...IN) OUT {
-				fmt.Println("NEW", rt2.DataOf(f)[a.Right()], "here")
+				fmt.Println("NEW", rt2.ValueOf(f)[a.Right().Adr()], "here")
 				//				sc.Update(scope.Designator(a.Left()), heap.Target().(scope.HeapAllocator).Allocate(a.Left(), rt2.DataOf(f)[a.Right()]))
 				return End()
 			}))
 		} else {
 			fmt.Println("NEW here")
-			//			sc.Update(scope.Designator(a.Left()), heap.Target().(scope.HeapAllocator).Allocate(a.Left()))
+			heap.Target().(scope.HeapAllocator).Allocate(a.Left())
+			//sc.Update(scope.Designator(a.Left()), heap.Target().(scope.HeapAllocator).Allocate(a.Left()))
 			return frame.End()
 		}
 	default:

+ 4 - 2
rt2/scope/area.go

@@ -26,7 +26,9 @@ type Manager interface {
 	String() string
 }
 
-type Allocator interface{}
+type Allocator interface {
+	Join(Manager)
+}
 
 type ScopeAllocator interface {
 	Allocator
@@ -43,4 +45,4 @@ type HeapAllocator interface {
 
 var FindObjByName func(m Manager, name string) object.Object
 
-var New func() Manager
+var New func(role string) Manager

+ 4 - 0
rt2/scope/data.go

@@ -64,6 +64,10 @@ type Array interface {
 	Get(Value) Value
 }
 
+type Pointer interface {
+	Variable
+}
+
 //средство обновления значенияx
 type ValueFor func(in Value) (out Value)
 type ValueOf func(in Value)

+ 41 - 0
rt2/scope/modern/hp.go

@@ -0,0 +1,41 @@
+package modern
+
+import (
+	"fmt"
+	//cpm "fw/cp/module"
+	"fw/cp/node"
+	"fw/cp/object"
+	rtm "fw/rt2/module"
+	"fw/rt2/scope"
+	"reflect"
+	"ypk/halt"
+)
+
+type halloc struct {
+	area *area
+}
+
+func (h *halloc) Allocate(n node.Node, par ...interface{}) scope.ValueFor {
+	fmt.Println("HEAP ALLOCATE")
+	_ = rtm.ModuleOfNode(h.area.d, n)
+	if h.area.data == nil {
+		h.area.data = append(h.area.data, newlvl())
+	}
+	switch v := n.(type) {
+	case node.VariableNode:
+		switch t := v.Object().Complex().(type) {
+		case object.PointerType:
+			panic(0)
+			//h.area.data[0].alloc(mod, nil, )
+		default:
+			halt.As(100, reflect.TypeOf(t))
+		}
+	default:
+		halt.As(101, reflect.TypeOf(v))
+	}
+	return nil
+}
+
+func (h *halloc) Dispose(n node.Node) {}
+
+func (a *halloc) Join(m scope.Manager) { a.area = m.(*area) }

+ 88 - 65
rt2/scope/modern/ms.go

@@ -4,7 +4,7 @@ import (
 	"fmt"
 	"fw/cp"
 	"fw/cp/constant/enter"
-	//	cpm "fw/cp/module"
+	cpm "fw/cp/module"
 	"fw/cp/node"
 	"fw/cp/object"
 	"fw/rt2"
@@ -30,6 +30,7 @@ type level struct {
 type area struct {
 	d    context.Domain
 	data []*level
+	all  scope.Allocator
 }
 
 type salloc struct {
@@ -69,9 +70,9 @@ func (a *area) Provide(x interface{}) scope.ValueFor {
 	return func(scope.Value) scope.Value {
 		switch z := x.(type) {
 		case node.ConstantNode:
-			return NewConst(z)
+			return newConst(z)
 		case object.ProcedureObject:
-			return NewProc(z)
+			return newProc(z)
 		default:
 			halt.As(100, reflect.TypeOf(z))
 		}
@@ -79,6 +80,65 @@ func (a *area) Provide(x interface{}) scope.ValueFor {
 	}
 }
 
+//var alloc func(*level, []object.Object, map[cp.ID]interface{})
+func (l *level) alloc(mod *cpm.Module, root node.Node, ol []object.Object, skip map[cp.ID]interface{}) {
+	for _, o := range ol {
+		imp := mod.ImportOf(o)
+		fmt.Println(reflect.TypeOf(o), o.Adr())
+		_, field := o.(object.FieldObject)
+		if imp == "" && (skip[o.Adr()] == nil || (field && l.nested)) {
+			fmt.Println("next", l.next)
+			switch x := o.(type) {
+			case object.VariableObject, object.FieldObject:
+				switch t := o.Complex().(type) {
+				case nil, object.BasicType, object.ArrayType, object.DynArrayType:
+					l.v[l.next] = newData(x)
+					l.k[x.Adr()] = l.next
+					l.next++
+				case object.RecordType:
+					l.v[l.next] = newRec(x)
+					nl := newlvl()
+					nl.nested = true
+					l.l[l.next] = nl
+					l.k[x.Adr()] = l.next
+					fl := make([]object.Object, 0)
+					for rec := t; rec != nil; {
+						for x := rec.Link(); x != nil; x = x.Link() {
+							//fmt.Println(o.Name(), ".", x.Name(), x.Adr())
+							fl = append(fl, x)
+						}
+						rec = rec.BaseType()
+					}
+					//fmt.Println("record")
+					l.v[l.next].(*rec).l = nl
+					nl.alloc(mod, root, fl, skip)
+					l.next++
+				case object.PointerType:
+					l.v[l.next] = newPtr(x)
+					l.k[x.Adr()] = l.next
+					l.next++
+				default:
+					halt.As(20, reflect.TypeOf(t))
+				}
+			case object.TypeObject:
+				//do nothing
+			case object.ConstantObject:
+				//do nothing
+			case object.ProcedureObject:
+				//do nothing
+			case object.ParameterObject:
+				if root.(node.EnterNode).Enter() == enter.PROCEDURE {
+					l.r[l.next] = newRef(x)
+					l.k[x.Adr()] = l.next
+					l.next++
+				}
+			default:
+				halt.As(20, reflect.TypeOf(x))
+			}
+		}
+	}
+}
+
 func (a *salloc) Allocate(n node.Node, final bool) {
 	fmt.Println("ALLOCATE")
 	mod := rtm.DomainModule(a.area.d)
@@ -98,66 +158,10 @@ func (a *salloc) Allocate(n node.Node, final bool) {
 		}
 	}
 
-	var alloc func(*level, []object.Object)
-	alloc = func(l *level, ol []object.Object) {
-		for _, o := range ol {
-			imp := mod.ImportOf(o)
-			fmt.Println(reflect.TypeOf(o), o.Adr())
-			_, field := o.(object.FieldObject)
-			if imp == "" && (skip[o.Adr()] == nil || (field && l.nested)) {
-				fmt.Println("next", l.next)
-				switch x := o.(type) {
-				case object.VariableObject, object.FieldObject:
-					switch t := o.Complex().(type) {
-					case nil, object.BasicType, object.ArrayType, object.DynArrayType:
-						l.v[l.next] = NewData(x)
-						l.k[x.Adr()] = l.next
-						l.next++
-					case object.RecordType:
-						l.v[l.next] = newRec(x)
-						nl := newlvl()
-						nl.nested = true
-						l.l[l.next] = nl
-						l.k[x.Adr()] = l.next
-						fl := make([]object.Object, 0)
-						for rec := t; rec != nil; {
-							for x := rec.Link(); x != nil; x = x.Link() {
-								//fmt.Println(o.Name(), ".", x.Name(), x.Adr())
-								fl = append(fl, x)
-							}
-							rec = rec.BaseType()
-						}
-						//fmt.Println("record")
-						l.v[l.next].(*rec).l = nl
-						alloc(nl, fl)
-						l.next++
-					case object.PointerType:
-						panic(0)
-					default:
-						halt.As(20, reflect.TypeOf(t))
-					}
-				case object.TypeObject:
-					//do nothing
-				case object.ConstantObject:
-					//do nothing
-				case object.ProcedureObject:
-					//do nothing
-				case object.ParameterObject:
-					if n.(node.EnterNode).Enter() == enter.PROCEDURE {
-						l.r[l.next] = newRef(x)
-						l.k[x.Adr()] = l.next
-						l.next++
-					}
-				default:
-					halt.As(20, reflect.TypeOf(x))
-				}
-			}
-		}
-	}
 	nl := newlvl()
 	nl.ready = final
 	a.area.data = append(a.area.data, nl)
-	alloc(nl, mod.Objects[n])
+	nl.alloc(mod, n, mod.Objects[n], skip)
 }
 
 func (a *salloc) Dispose(n node.Node) {
@@ -189,7 +193,7 @@ func (a *salloc) Initialize(n node.Node, par scope.PARAM) (seq frame.Sequence, r
 		case object.VariableObject:
 			switch nv := val.(type) {
 			case node.ConstantNode:
-				v := NewConst(nv)
+				v := newConst(nv)
 				l.v[l.k[o.Adr()]].Set(v)
 			case node.VariableNode:
 				v := a.area.Select(nv.Object().Adr())
@@ -205,7 +209,7 @@ func (a *salloc) Initialize(n node.Node, par scope.PARAM) (seq frame.Sequence, r
 			case node.ConstantNode: //array :) заменяем ссылку на переменную
 				old := l.r[l.k[o.Adr()]].(*ref)
 				l.r[l.k[o.Adr()]] = nil
-				data := NewConst(nv)
+				data := newConst(nv)
 				switch data.(type) {
 				case STRING, SHORTSTRING:
 					val := &dynarr{link: old.link}
@@ -251,6 +255,8 @@ func (a *salloc) Initialize(n node.Node, par scope.PARAM) (seq frame.Sequence, r
 	return seq, ret
 }
 
+func (a *salloc) Join(m scope.Manager) { a.area = m.(*area) }
+
 func (a *area) Update(id cp.ID, fval scope.ValueFor) {
 	assert.For(id != 0, 20)
 	fmt.Println("UPDATE", id)
@@ -314,7 +320,18 @@ func (a *area) Select(id cp.ID, val ...scope.ValueOf) (ret scope.Value) {
 	return ret
 }
 
-func (a *area) Target(...scope.Allocator) scope.Allocator { return &salloc{area: a} }
+func (a *area) Target(all ...scope.Allocator) scope.Allocator {
+	if len(all) > 0 {
+		a.all = all[0]
+	}
+	if a.all == nil {
+		return &salloc{area: a}
+	} else {
+		a.all.Join(a)
+		return a.all
+	}
+}
+
 func (a *area) String() (ret string) {
 	for _, l := range a.data {
 		ret = fmt.Sprintln(ret, l)
@@ -365,8 +382,14 @@ func fn(mgr scope.Manager, name string) (ret object.Object) {
 	return ret
 }
 
-func nn() scope.Manager {
-	return &area{}
+func nn(role string) scope.Manager {
+	if role == context.SCOPE {
+		return &area{all: &salloc{}}
+	} else if role == context.HEAP {
+		return &area{all: &halloc{}}
+	} else {
+		panic(0)
+	}
 }
 
 func init() {

+ 28 - 13
rt2/scope/modern/val.go

@@ -40,6 +40,11 @@ type rec struct {
 	l *level
 }
 
+type ptr struct {
+	link object.Object
+	scope.Pointer
+}
+
 type idx struct {
 	arr *arr
 	idx int
@@ -54,7 +59,7 @@ func (r *rec) Id() cp.ID {
 }
 
 func (r *rec) Set(v scope.Value) {
-
+	panic(0)
 }
 
 func (r *rec) Get(id cp.ID) scope.Value {
@@ -76,17 +81,11 @@ 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 *data) Id() cp.ID { return x.link.Adr() }
 
-func (x *arr) 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 (x *dynarr) Id() cp.ID { return x.link.Adr() }
 
 func (a *arr) Set(v scope.Value) {
 	switch x := v.(type) {
@@ -247,6 +246,22 @@ func (d *data) String() string {
 	return fmt.Sprint(d.link.Name(), "=", d.val)
 }
 
+func (p *ptr) String() string {
+	return "pointer"
+}
+
+func (p *ptr) Id() cp.ID { return p.link.Adr() }
+
+func (p *ptr) Set(scope.Value) {
+	panic(0)
+}
+
+func newPtr(o object.Object) scope.Variable {
+	_, ok := o.Complex().(object.PointerType)
+	assert.For(ok, 20)
+	return &ptr{link: o}
+}
+
 type INTEGER int32
 type BOOLEAN bool
 type BYTE int8
@@ -275,7 +290,7 @@ 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) {
+func newData(o object.Object) (ret scope.Variable) {
 	switch o.Type() {
 	case object.INTEGER:
 		ret = &data{link: o, val: INTEGER(0)}
@@ -334,13 +349,13 @@ func fromg(x interface{}) scope.Value {
 	panic(100)
 }
 
-func NewProc(o object.Object) scope.Value {
+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 {
+func newConst(n node.Node) scope.Value {
 	switch x := n.(type) {
 	case node.ConstantNode:
 		switch x.Type() {