瀏覽代碼

все демки снова работают

kpmy 10 年之前
父節點
當前提交
081e34439d

+ 2 - 2
fw.go

@@ -36,8 +36,8 @@ func main() {
 	flag.Parse()
 	utils.Debug(debug)
 	if name == "" {
-		name = "XevDemo19"
-		utils.Debug(false)
+		name = "XevDemo13"
+		utils.Debug(true)
 	}
 	global := &stdDomain{god: true}
 	global.global = global

+ 24 - 6
rt2/rules2/wrap/data/items/items.go

@@ -39,6 +39,7 @@ type Item interface {
 type Data interface {
 	Set(Key, Item)
 	Get(Key, ...Opts) Item
+	Remove(Key)
 
 	Hold(Key)
 	Link(Key, ID)
@@ -50,11 +51,11 @@ type Data interface {
 	Begin()
 	End()
 	Drop()
-	Check()
+	Check(...Opts)
 }
 
 func New() Data {
-	return &data{x: list.New()}
+	return &data{x: list.New(), check: true}
 }
 
 type dummy struct {
@@ -66,7 +67,8 @@ func (d *dummy) String() string {
 }
 
 type data struct {
-	x *list.List
+	x     *list.List
+	check bool
 }
 
 func (d *data) find(k Key, from *list.Element) (ret Value, elem *list.Element) {
@@ -130,7 +132,7 @@ func (d *data) Get(k Key, opts ...Opts) (ret Item) {
 			if to.In == d {
 				x, e = d.find(to.This, e)
 			} else {
-				ret = to.In.Get(to.This)
+				ret = to.In.Get(to.This, opts...)
 			}
 		}
 	}
@@ -138,6 +140,19 @@ func (d *data) Get(k Key, opts ...Opts) (ret Item) {
 	return
 }
 
+func (d *data) Remove(key Key) {
+	ok := false
+	for x := d.x.Front(); x != nil && !ok; x = x.Next() {
+		switch v := x.Value.(type) {
+		case Value:
+			if v.KeyOf().EqualTo(key) == 0 {
+				d.x.Remove(x)
+				ok = true
+			}
+		}
+	}
+}
+
 func (d *data) Hold(key Key) {
 	assert.For(key != nil, 20)
 	d.x.PushFront(&dummy{k: key})
@@ -209,9 +224,12 @@ type limit_key struct{}
 func (l *limit_key) EqualTo(Key) int { return -1 }
 func (l *limit) KeyOf(...Key) Key    { return &limit_key{} }
 
-func (d *data) Check() {
+func (d *data) Check(o ...Opts) {
+	if len(o) == 1 {
+		d.check = false
+	}
 	t := d.x.Front()
-	if t != nil {
+	if d.check && t != nil {
 		_, ok := t.Value.(*limit)
 		assert.For(ok, 30, "data not ready")
 	}

+ 119 - 26
rt2/rules2/wrap/data/stack.go

@@ -3,6 +3,7 @@ package data
 import (
 	"fmt"
 	"fw/cp"
+	"fw/cp/constant"
 	"fw/cp/node"
 	"fw/cp/object"
 	"fw/rt2"
@@ -14,21 +15,26 @@ import (
 	"fw/rt2/scope"
 	"fw/utils"
 	"reflect"
+	"runtime"
 	"ypk/assert"
 	"ypk/halt"
 )
 
 type area struct {
-	d    context.Domain
-	all  scope.Allocator
-	il   items.Data
-	init bool
+	d      context.Domain
+	all    scope.Allocator
+	il     items.Data
+	unsafe bool
 }
 
 type salloc struct {
 	area *area
 }
 
+type halloc struct {
+	area *area
+}
+
 type key struct {
 	items.Key
 	id cp.ID
@@ -76,7 +82,7 @@ func (i *item) Value() scope.Value {
 func (a *area) Select(this cp.ID, val scope.ValueOf) {
 	utils.PrintScope("SELECT", this)
 	opts := make([]items.Opts, 0)
-	if a.init {
+	if a.unsafe {
 		opts = append(opts, items.INIT)
 	}
 	d, ok := a.il.Get(&key{id: this}, opts...).(*item)
@@ -92,23 +98,16 @@ func (a *area) Exists(this cp.ID) bool {
 func push(dom context.Domain, il items.Data, _o object.Object) {
 	switch o := _o.(type) {
 	case object.VariableObject, object.FieldObject:
+		var x interface{}
 		switch t := o.Complex().(type) {
 		case nil, object.BasicType:
-			x := newData(o)
-			d := &item{}
-			d.Data(x)
-			il.Set(&key{id: o.Adr()}, d)
+			x = newData(o)
 		case object.ArrayType, object.DynArrayType:
-			x := newData(o)
-			d := &item{}
-			d.Data(x)
-			il.Set(&key{id: o.Adr()}, d)
+			x = newData(o)
 		case object.RecordType:
 			ml := dom.Global().Discover(context.MOD).(rtm.List)
-			x := newRec(o)
-			d := &item{}
-			d.Data(x)
-			il.Set(&key{id: o.Adr()}, d)
+			r := newRec(o)
+			x = r
 			fl := make([]object.Object, 0)
 			for rec := t; rec != nil; {
 				for x := rec.Link(); x != nil; x = x.Link() {
@@ -132,15 +131,22 @@ func push(dom context.Domain, il items.Data, _o object.Object) {
 					rec = rec.BaseRec()
 				}
 			}
-			x.fi = items.New()
-			x.fi.Begin()
+			r.fi = items.New()
+			r.fi.Begin()
 			for _, f := range fl {
-				push(dom, x.fi, f)
+				push(dom, r.fi, f)
 			}
-			x.fi.End()
+			r.fi.End()
+		case object.PointerType:
+			x = newPtr(o)
 		default:
 			halt.As(100, reflect.TypeOf(t))
 		}
+		assert.For(x != nil, 40)
+		//fmt.Println(_o.Name(), x)
+		d := &item{}
+		d.Data(x)
+		il.Set(&key{id: o.Adr()}, d)
 	case object.ParameterObject:
 		il.Hold(&key{id: o.Adr()})
 	default:
@@ -148,6 +154,90 @@ func push(dom context.Domain, il items.Data, _o object.Object) {
 	}
 }
 
+func fin(x interface{}) {
+	switch p := x.(type) {
+	case *ptrValue:
+		defer func() {
+			mod := rtm.ModuleOfType(p.scope.Domain(), p.link.Complex())
+			ol := mod.Objects[mod.Enter]
+			var fn object.ProcedureObject
+			for _, _po := range ol {
+				switch po := _po.(type) {
+				case object.ProcedureObject:
+					if po.Name() == "FINALIZE" && po.Link().Complex().Equals(p.link.Complex()) {
+						fn = po
+						break
+					}
+				}
+			}
+			if fn != nil {
+				global := p.scope.Domain().Discover(context.UNIVERSE).(context.Domain)
+				root := global.Discover(context.STACK).(frame.Stack)
+				cn := node.New(constant.CALL, cp.Some())
+				ol := mod.NodeByObject(fn)
+				assert.For(len(ol) <= 1, 40)
+				cn.SetLeft(ol[0])
+				cc := node.New(constant.CONSTANT, cp.Some()).(node.ConstantNode)
+				cc.SetData(p)
+				cc.SetType(object.COMPLEX)
+				cn.SetRight(cc)
+				nf := rt2.New(cn)
+				nf.Init(global.Discover(mod.Name).(context.Domain))
+				root.Queue(nf)
+			}
+			p.scope.Target().(scope.HeapAllocator).Dispose(p.id)
+		}()
+	}
+}
+
+func (h *halloc) Allocate(o object.Object, t object.PointerType, par ...interface{}) scope.Value {
+	utils.PrintScope("HEAP ALLOCATE")
+	//mod := rtm.ModuleOfType(h.area.d, t)
+	assert.For(t != nil, 20)
+	assert.For(o != nil, 21)
+	var res scope.Value
+	var talloc func(t object.PointerType)
+	talloc = func(t object.PointerType) {
+		switch bt := t.Complex().(type) {
+		case object.RecordType:
+			fake := object.New(object.VARIABLE, cp.Some())
+			fake.SetComplex(bt)
+			fake.SetType(object.COMPLEX)
+			fake.SetName("{" + o.Name() + "}")
+			push(h.area.d, h.area.il, fake)
+			res = &ptrValue{scope: h.area, id: fake.Adr(), link: o}
+		case object.DynArrayType:
+			assert.For(len(par) > 0, 20)
+			fake := object.New(object.VARIABLE, cp.Some())
+			fake.SetComplex(bt)
+			fake.SetType(object.COMPLEX)
+			fake.SetName("[]")
+			push(h.area.d, h.area.il, fake)
+			h.area.Select(fake.Adr(), func(v scope.Value) {
+				arr, ok := v.(*dynarr)
+				assert.For(ok, 60)
+				arr.Set(par[0].(scope.Value))
+			})
+			res = &ptrValue{scope: h.area, id: fake.Adr(), link: o}
+		default:
+			halt.As(100, fmt.Sprintln("cannot allocate", reflect.TypeOf(bt)))
+		}
+	}
+	talloc(t)
+	assert.For(res != nil, 60)
+	runtime.SetFinalizer(res, fin)
+	return res
+}
+
+func (h *halloc) Dispose(i cp.ID) {
+	h.area.Select(i, func(v scope.Value) {
+		utils.PrintScope("dispose", v)
+		h.area.il.Remove(&key{id: i})
+	})
+}
+
+func (a *halloc) Join(m scope.Manager) { a.area = m.(*area) }
+
 func (a *salloc) Allocate(n node.Node, final bool) {
 	mod := rtm.ModuleOfNode(a.area.d, n)
 	utils.PrintScope("ALLOCATE FOR", mod.Name, n.Adr())
@@ -189,10 +279,12 @@ func (a *salloc) Allocate(n node.Node, final bool) {
 			skip[o.Adr()] = o
 		case object.ConstantObject:
 			skip[o.Adr()] = o
+		case object.Module:
+			skip[o.Adr()] = o
 		}
 	}
 	a.area.il.Begin()
-	a.area.init = true
+	a.area.unsafe = true
 	for _, o := range ol {
 		if skip[o.Adr()] == nil {
 			utils.PrintScope(o.Adr(), o.Name())
@@ -201,7 +293,7 @@ func (a *salloc) Allocate(n node.Node, final bool) {
 	}
 	if final {
 		a.area.il.End()
-		a.area.init = false
+		a.area.unsafe = false
 	}
 }
 
@@ -215,7 +307,7 @@ func (a *salloc) proper_init(root node.Node, _val node.Node, _par object.Object,
 	const link = "initialize:par"
 	end := func(in eval.IN) eval.OUT {
 		a.area.il.End()
-		a.area.init = false
+		a.area.unsafe = false
 		return eval.Later(tail)
 	}
 	var next eval.Do
@@ -327,8 +419,9 @@ func nn(role string) scope.Manager {
 	case context.SCOPE, context.CALL:
 		return &area{all: &salloc{}, il: items.New()}
 	case context.HEAP:
-		return &area{all: nil}
-		//return &area{all: &halloc{}}
+		ret := &area{all: &halloc{}, il: items.New(), unsafe: true}
+		ret.il.Check(items.INIT)
+		return ret
 	default:
 		panic(0)
 	}

+ 5 - 3
rt2/rules2/wrap/data/val.go

@@ -461,13 +461,15 @@ func (p *ptr) Set(v scope.Value) {
 	}
 }
 
-func (p *ptr) Get() scope.Value {
+func (p *ptr) Get() (ret scope.Value) {
 	if p.val == nil {
 		return NIL
 	} else {
-		panic(0)
-		//return p.val.scope.Select(p.val.id)
+		p.val.scope.Select(p.val.id, func(v scope.Value) {
+			ret = v
+		})
 	}
+	return
 }
 
 func newPtr(o object.Object) scope.Variable {

+ 2 - 0
rt2/rules2/wrap/eval/do.go

@@ -110,6 +110,8 @@ func GetDesignator(in IN, key interface{}, design node.Node, next Do) OUT {
 		m := rt2.RegOf(f)[context.META]
 		do(v != nil, 1957, " no data for ", key)
 		do(m != nil, 1480, " no meta")
+		meta := m.(*Meta)
+		do(meta.Scope != nil || meta.Rec != nil || meta.Arr != nil, 1380, " wrong meta")
 	})
 	return Later(func(IN) OUT {
 		return Now(next)

+ 21 - 10
rt2/rules2/wrap/eval/expr.go

@@ -37,11 +37,12 @@ func getVar(in IN) OUT {
 
 func getVarPar(in IN) OUT {
 	v := in.IR.(node.ParameterNode)
-	rt2.ScopeFor(in.Frame, v.Object().Adr(), func(val scope.Value) {
+	sc := rt2.ScopeFor(in.Frame, v.Object().Adr(), func(val scope.Value) {
 		rt2.ValueOf(in.Parent)[v.Adr()] = val
 		rt2.RegOf(in.Parent)[in.Key] = v.Adr()
 		rt2.RegOf(in.Parent)[context.META] = &Meta{}
 	})
+	rt2.RegOf(in.Parent)[context.META] = &Meta{Scope: sc, Id: v.Object().Adr()}
 	return End()
 }
 
@@ -52,10 +53,10 @@ func getField(in IN) OUT {
 		_v := rt2.ValueOf(in.Frame)[KeyOf(in, left)]
 		switch v := _v.(type) {
 		case scope.Record:
-			fld := v.Get(f.Object().Adr())
+			fld := v.Get(f.Object().Adr()).(scope.Variable)
 			rt2.ValueOf(in.Parent)[f.Adr()] = fld
 			rt2.RegOf(in.Parent)[in.Key] = f.Adr()
-			rt2.RegOf(in.Parent)[context.META] = &Meta{}
+			rt2.RegOf(in.Parent)[context.META] = &Meta{Scope: nil, Rec: v, Id: fld.Id()}
 			return End()
 		default:
 			halt.As(100, reflect.TypeOf(v))
@@ -80,7 +81,7 @@ func getIndex(in IN) OUT {
 			case scope.Array:
 				rt2.ValueOf(in.Parent)[i.Adr()] = a.Get(idx)
 				rt2.RegOf(in.Parent)[in.Key] = i.Adr()
-				rt2.RegOf(in.Parent)[context.META] = &Meta{}
+				rt2.RegOf(in.Parent)[context.META] = &Meta{Arr: a, Id: a.Id()}
 				return End()
 			default:
 				halt.As(100, reflect.TypeOf(a))
@@ -128,14 +129,24 @@ func getDeref(in IN) OUT {
 			}
 		case scope.Pointer:
 			assert.For(d.Ptr(), 41)
-			rec := v.Get()
-			rt2.ValueOf(in.Parent)[d.Adr()] = rec
-			rt2.RegOf(in.Parent)[in.Key] = d.Adr()
-			rt2.RegOf(in.Parent)[context.META] = &Meta{}
-			if scope.GoTypeFrom(rec) == nil {
+			if r := v.Get(); scope.GoTypeFrom(r) == nil {
 				out = makeTrap(in.Frame, traps.NILderef)
 			} else {
 				out = End()
+				switch r.(type) {
+				case scope.Record:
+					rec := r.(scope.Record)
+					rt2.ValueOf(in.Parent)[d.Adr()] = rec
+					rt2.RegOf(in.Parent)[in.Key] = d.Adr()
+					rt2.RegOf(in.Parent)[context.META] = &Meta{Scope: rt2.ScopeFor(in.Frame, rec.Id()), Id: rec.Id()}
+				case scope.Array:
+					arr := r.(scope.Array)
+					rt2.ValueOf(in.Parent)[d.Adr()] = arr
+					rt2.RegOf(in.Parent)[in.Key] = d.Adr()
+					rt2.RegOf(in.Parent)[context.META] = &Meta{Scope: rt2.ScopeFor(in.Frame, arr.Id()), Id: arr.Id()}
+				default:
+					halt.As(100, reflect.TypeOf(r))
+				}
 			}
 			return out
 		default:
@@ -290,7 +301,7 @@ func getGuard(in IN) OUT {
 		if scope.GoTypeFrom(scope.Ops.Is(v, g.Type())).(bool) {
 			rt2.ValueOf(in.Parent)[g.Adr()] = v
 			rt2.RegOf(in.Parent)[in.Key] = g.Adr()
-			rt2.RegOf(in.Parent)[context.META] = &Meta{}
+			rt2.RegOf(in.Parent)[context.META] = rt2.RegOf(in.Frame)[context.META] //&Meta{Id: vv.Id(), }
 			return End()
 		} else {
 			return makeTrap(in.Frame, 0)

+ 1 - 0
rt2/rules2/wrap/eval/stmt.go

@@ -25,6 +25,7 @@ import (
 func makeTrap(f frame.Frame, err traps.TRAP) (out OUT) {
 	trap := node.New(constant.TRAP, cp.Some()).(node.TrapNode)
 	code := node.New(constant.CONSTANT, cp.Some()).(node.ConstantNode)
+	code.SetType(object.INTEGER)
 	code.SetData(int32(err))
 	trap.SetLeft(code)
 	rt2.Push(rt2.New(trap), f)

+ 2 - 0
rt2/rules2/wrap/eval/x.go

@@ -21,6 +21,8 @@ const (
 type Meta struct {
 	Id    cp.ID
 	Scope scope.Manager
+	Rec   scope.Record
+	Arr   scope.Array
 }
 
 var Propose func(Do) frame.Sequence