Преглед изворни кода

переделал схему работы со скоупами, стало проще искать нужный для запроса данных объекта
добился работоспособности парсера json

kpmy пре 10 година
родитељ
комит
4c4346aecf

BIN
code/JSonParser.oz


+ 4 - 1
domain.go

@@ -25,8 +25,11 @@ func (d *stdDomain) Attach(name string, x context.ContextAware) {
 	d.list[name] = x
 }
 
-func (d *stdDomain) Discover(name string) (ret context.ContextAware) {
+func (d *stdDomain) Discover(name string, opts ...interface{}) (ret context.ContextAware) {
 	assert.For(name != "", 20)
+	if name == context.VSCOPE {
+		assert.For(len(opts) != 0, 20)
+	}
 	if d.list != nil {
 		ret = d.list[name]
 	}

+ 2 - 2
rt2/context/ctx.go

@@ -2,7 +2,7 @@ package context
 
 const (
 	STACK    = "fw/rt2/frame"
-	SCOPE    = "fw/rt2/scope"
+	VSCOPE   = "fw/rt2/scope"
 	MOD      = "fw/rt2/module"
 	UNIVERSE = "fw/rt2/ctx"
 	HEAP     = "fw/rt2/scope,heap"
@@ -16,7 +16,7 @@ type Factory interface {
 
 type Domain interface {
 	Attach(name string, c ContextAware)
-	Discover(name string) ContextAware
+	Discover(name string, opts ...interface{}) ContextAware
 	Id(c ContextAware) string
 	ContextAware
 }

+ 1 - 0
rt2/decision/table.go

@@ -11,4 +11,5 @@ var (
 	Run         func(global context.Domain, init []*module.Module)
 	PrologueFor func(n node.Node) frame.Sequence
 	EpilogueFor func(n node.Node) frame.Sequence
+	AssertFor   func(n node.Node) (bool, int)
 )

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

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

+ 13 - 9
rt2/nodeframe/frame.go

@@ -19,11 +19,15 @@ type NodeFrameUtils struct{}
 
 func (fu NodeFrameUtils) New(n node.Node) (f frame.Frame) {
 	assert.For(n != nil, 20)
-	f = new(nodeFrame)
-	f.(*nodeFrame).ir = n
-	f.(*nodeFrame).data = make(map[interface{}]interface{})
-	f.(*nodeFrame).value = make(map[cp.ID]scope.Value)
-	utils.PrintFrame("_", "NEW", reflect.TypeOf(n), n.Adr())
+	ok, _ := decision.AssertFor(n)
+	//assert.For(ok, 21, "wrong node", reflect.TypeOf(n), code)
+	if ok {
+		f = new(nodeFrame)
+		f.(*nodeFrame).ir = n
+		f.(*nodeFrame).data = make(map[interface{}]interface{})
+		f.(*nodeFrame).value = make(map[cp.ID]scope.Value)
+		utils.PrintFrame("_", "NEW", reflect.TypeOf(n), n.Adr())
+	}
 	return f
 }
 
@@ -74,11 +78,11 @@ type nodeFrame struct {
 }
 
 func done(f frame.Frame) {
-	utils.PrintFrame("____")
-	utils.PrintFrame(f.Domain().Discover(context.SCOPE))
+	//utils.PrintFrame("____")
+	//utils.PrintFrame(f.Domain().Discover(context.SCOPE))
 	//	utils.PrintFrame("--")
 	//	utils.PrintFrame(f.Domain().Discover(context.HEAP))
-	utils.PrintFrame("^^^^")
+	//utils.PrintFrame("^^^^")
 }
 
 func (f *nodeFrame) Do() frame.WAIT {
@@ -95,7 +99,7 @@ func (f *nodeFrame) Do() frame.WAIT {
 			var fu NodeFrameUtils
 			utils.PrintTrap()
 			utils.PrintTrap("something WRONG")
-			utils.PrintTrap(f.Domain().Discover(context.SCOPE))
+			utils.PrintTrap(f.Domain().Discover(context.VSCOPE, 0))
 			depth := 10
 			f.root.ForEach(func(old frame.Frame) bool {
 				n := fu.NodeOf(old)

+ 9 - 7
rt2/rules/assign.go

@@ -62,14 +62,14 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		switch r := a.Right().(type) {
 		case node.ConstantNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+				sc := rt2.ThisScope(f)
 				vleft.Set(sc.Provide(a.Right())(nil)) //scope.Simple(a.Right().(node.ConstantNode).Data()))
 				return frame.End()
 			}
 			ret = frame.NOW
 		case node.VariableNode, node.ParameterNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+				sc := rt2.ScopeFor(f, a.Right().Object().Adr())
 				vleft.Set(sc.Select(a.Right().Object().Adr()))
 				return frame.End()
 			}
@@ -96,10 +96,11 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				return rt2.ValueOf(f)[rightId] != nil, 62
 			})
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+
 				right := rt2.ValueOf(f)[r.Adr()]
 				switch z := r.Left().(type) {
 				case node.VariableNode, node.ParameterNode:
+					sc := rt2.ScopeFor(f, z.Object().Adr())
 					arr := sc.Select(z.Object().Adr()).(scope.Array)
 					right = arr.Get(right)
 					vleft.Set(right)
@@ -118,7 +119,7 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			}
 			ret = frame.LATER
 		case node.ProcedureNode:
-			sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+			sc := rt2.ThisScope(f)
 			vleft.Set(sc.Provide(a.Right().Object())(nil))
 			return frame.End()
 		default:
@@ -132,7 +133,7 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	case statement.ASSIGN:
 		switch l := a.Left().(type) {
 		case node.VariableNode, node.ParameterNode:
-			sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+			sc := rt2.ScopeFor(f, a.Left().Object().Adr())
 			left = sc.Select(a.Left().Object().Adr())
 			seq, ret = right(f)
 		case node.FieldNode:
@@ -151,10 +152,10 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				return rt2.ValueOf(f)[l.Adr()] != nil, 64
 			})
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
 				left = rt2.ValueOf(f)[l.Adr()]
 				switch z := l.Left().(type) {
 				case node.VariableNode, node.ParameterNode:
+					sc := rt2.ScopeFor(f, l.Left().Object().Adr())
 					arr := sc.Select(l.Left().Object().Adr()).(scope.Array)
 					left = arr.Get(left)
 					return right(f)
@@ -203,13 +204,13 @@ 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 := f.Domain().Discover(context.HEAP).(scope.Manager).Target().(scope.HeapAllocator)
 		if a.Right() != nil {
 			seq, ret = This(expectExpr(f, a.Right(), func(in ...IN) (out OUT) {
 				//fmt.Println("NEW", rt2.ValueOf(f)[a.Right().Adr()], "here")
 				switch z := a.Left().(type) {
 				case node.VariableNode:
+					sc := rt2.ScopeFor(f, a.Left().Object().Adr())
 					fn := heap.Allocate(a.Left(), rt2.ValueOf(f)[a.Right().Adr()])
 					sc.Update(a.Left().Object().Adr(), fn)
 					return End()
@@ -234,6 +235,7 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		} else {
 			//fmt.Println("NEW here", a.Left().Adr())
 			fn := heap.Allocate(a.Left())
+			sc := rt2.ScopeFor(f, a.Left().Object().Adr())
 			sc.Update(a.Left().Object().Adr(), fn)
 			return frame.End()
 		}

+ 3 - 3
rt2/rules/call.go

@@ -59,7 +59,7 @@ func callHandler(f frame.Frame, obj object.Object, data interface{}) {
 
 func go_process(f frame.Frame, par node.Node) (frame.Sequence, frame.WAIT) {
 	assert.For(par != nil, 20)
-	sm := f.Domain().Discover(context.SCOPE).(scope.Manager)
+	sm := rt2.ThisScope(f)
 	do := func(val string) {
 		if val != "" {
 			msg := &Msg{}
@@ -132,7 +132,7 @@ func go_math(f frame.Frame, par node.Node) (seq frame.Sequence, ret frame.WAIT)
 		EXP  = 3.0
 	)
 	assert.For(par != nil, 20)
-	sm := f.Domain().Discover(context.SCOPE).(scope.Manager)
+	sm := rt2.ThisScope(f)
 	res := math.NaN()
 	switch p := par.(type) {
 	case node.VariableNode:
@@ -266,7 +266,7 @@ func callSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		}
 	case node.VariableNode:
 		m := rtm.DomainModule(f.Domain())
-		sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+		sc := rt2.ScopeFor(f, n.Left().Object().Adr())
 		obj := scope.GoTypeFrom(sc.Select(n.Left().Object().Adr()))
 
 		if obj, ok := obj.(object.Object); ok {

+ 4 - 2
rt2/rules/deref.go

@@ -5,7 +5,6 @@ import (
 	"fw/cp/object"
 	"fw/cp/traps"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
 	"fw/rt2/scope"
 	"reflect"
@@ -15,11 +14,12 @@ import (
 
 func derefSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	n := rt2.NodeOf(f).(node.DerefNode)
-	sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+
 	//fmt.Println("deref from ptr", n.Ptr())
 	if n.Ptr() {
 		switch l := n.Left().(type) {
 		case node.ParameterNode, node.VariableNode:
+			sc := rt2.ScopeFor(f, l.Object().Adr())
 			sc.Select(l.Object().Adr(), func(v scope.Value) {
 				ptr, ok := v.(scope.Pointer)
 				assert.For(ok, 60, reflect.TypeOf(v))
@@ -73,6 +73,7 @@ func derefSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		if n.Left().Object() != nil {
 			switch l := n.Left().Object().(type) {
 			case object.ParameterObject, object.VariableObject:
+				sc := rt2.ScopeFor(f, l.Adr())
 				val := sc.Select(l.Adr())
 				deref(val)
 				return frame.End()
@@ -98,6 +99,7 @@ func derefSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				})
 				seq = Propose(func(...IN) OUT {
 					val := rt2.ValueOf(f)[left.Adr()]
+					sc := rt2.ScopeFor(f, left.Left().Object().Adr())
 					arr := sc.Select(left.Left().Object().Adr()).(scope.Array)
 					deref(arr.Get(val).(scope.Pointer).Get())
 					//rt2.ValueOf(f.Parent())[n.Adr()] = rt2.ValueOf(f)[left.Adr()]

+ 1 - 2
rt2/rules/enter.go

@@ -3,7 +3,6 @@ package rules
 import (
 	"fw/cp/node"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
 	"fw/rt2/module"
 	"fw/rt2/scope"
@@ -43,7 +42,7 @@ func enterSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 
 		}
 	}
-	sm := f.Domain().Discover(context.SCOPE).(scope.Manager)
+	sm := rt2.ThisScope(f)
 	//fmt.Println(n.Object())
 	if n.Object() != nil {
 		par, ok := rt2.RegOf(f)[n.Object()].(node.Node)

+ 5 - 6
rt2/rules/expect.go

@@ -5,7 +5,6 @@ import (
 	"fw/cp/node"
 	"fw/cp/object"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
 	rtm "fw/rt2/module"
 	"fw/rt2/scope"
@@ -17,27 +16,27 @@ import (
 //функция вернет в данные родительского фрейма вычисленное значение expr
 func expectExpr(parent frame.Frame, expr node.Node, next Do) OUT {
 	assert.For(expr != nil, 20)
-	sm := rt2.ScopeOf(parent)
 	switch e := expr.(type) {
 	case node.ConstantNode:
-		rt2.ValueOf(parent)[expr.Adr()] = sm.Provide(e)(nil)
+		rt2.ValueOf(parent)[expr.Adr()] = rt2.ThisScope(parent).Provide(e)(nil)
 		return OUT{do: next, next: NOW}
 	case node.VariableNode, node.ParameterNode:
 		m := rtm.ModuleOfObject(parent.Domain(), expr.Object())
 		assert.For(m != nil, 40)
 		imp := m.ImportOf(expr.Object())
 		if imp != "" {
-			md := rtm.ModuleDomain(parent.Domain(), imp)
-			sm = md.Discover(context.SCOPE).(scope.Manager)
+			//			md := rtm.ModuleDomain(parent.Domain(), imp)
+			//sm = md.Discover(context.SCOPE).(scope.Manager)
 			fm := rtm.Module(parent.Domain(), imp)
 			ol := fm.ObjectByName(fm.Enter, expr.Object().Name())
 			for _, obj := range ol {
 				if _, ok := obj.(object.VariableObject); ok {
+					sm := rt2.ScopeFor(parent, obj.Adr())
 					rt2.ValueOf(parent)[expr.Adr()] = sm.Select(obj.Adr())
 				}
 			}
 		} else {
-			sm = rt2.ScopeOf(parent)
+			sm := rt2.ScopeFor(parent, expr.Object().Adr())
 			rt2.ValueOf(parent)[expr.Adr()] = sm.Select(expr.Object().Adr())
 		}
 		return OUT{do: next, next: NOW}

+ 1 - 2
rt2/rules/field.go

@@ -7,7 +7,6 @@ import (
 import (
 	"fw/cp/node"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
 	"fw/rt2/scope"
 	"ypk/halt"
@@ -16,7 +15,6 @@ import (
 func fieldSeq(in ...IN) (out OUT) {
 	f := in[0].frame
 	n := rt2.NodeOf(f)
-	sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
 	var left node.Node
 	left, _ = rt2.RegOf(f)[n.Left()].(node.Node)
 	if left == nil {
@@ -24,6 +22,7 @@ func fieldSeq(in ...IN) (out OUT) {
 	}
 	switch l := left.(type) {
 	case node.VariableNode, node.ParameterNode:
+		sc := rt2.ScopeFor(f, l.Object().Adr())
 		sc.Select(l.Object().Adr(), func(v scope.Value) {
 			rt2.ValueOf(f.Parent())[n.Adr()] = v.(scope.Record).Get(n.Object().Adr())
 			//fmt.Println(n.Object().Adr())

+ 1 - 2
rt2/rules/guard.go

@@ -4,18 +4,17 @@ import (
 	"fmt"
 	"fw/cp/node"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
 	"fw/rt2/scope"
 	"reflect"
 )
 
 func guardSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-	sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
 	n := rt2.NodeOf(f).(node.GuardNode)
 	var obj scope.Value
 	switch l := n.Left().(type) {
 	case node.VariableNode, node.ParameterNode:
+		sc := rt2.ScopeFor(f, l.Object().Adr())
 		obj = sc.Select(l.Object().Adr())
 	default:
 		panic(fmt.Sprintln("unsupported left", reflect.TypeOf(l)))

+ 3 - 3
rt2/rules/if.go

@@ -1,12 +1,12 @@
 package rules
 
 import (
-	"fmt"
 	"fw/cp/node"
 	"fw/rt2"
 	"fw/rt2/frame"
 	"fw/rt2/scope"
 	"reflect"
+	"ypk/halt"
 )
 
 func ifExpr(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
@@ -20,13 +20,13 @@ func ifExpr(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			return frame.End()
 		}
 		ret = frame.LATER
-	case node.ConstantNode, node.VariableNode, node.ParameterNode:
+	case node.ConstantNode, node.VariableNode, node.ParameterNode, node.FieldNode:
 		return This(expectExpr(f, l, func(...IN) OUT {
 			rt2.ValueOf(f.Parent())[n.Adr()] = rt2.ValueOf(f)[l.Adr()]
 			return End()
 		}))
 	default:
-		panic(fmt.Sprintf("unknown condition expression", reflect.TypeOf(n.Left())))
+		halt.As(100, reflect.TypeOf(l))
 	}
 	return seq, ret
 }

+ 2 - 5
rt2/rules/index.go

@@ -4,9 +4,7 @@ import (
 	"fmt"
 	"fw/cp/node"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
-	"fw/rt2/scope"
 	"reflect"
 )
 
@@ -16,14 +14,13 @@ func indexSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	switch i.Right().(type) {
 	case node.ConstantNode:
 		seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-			sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
-			rt2.ValueOf(f.Parent())[i.Adr()] = sc.Provide(i.Right())(nil)
+			rt2.ValueOf(f.Parent())[i.Adr()] = rt2.ThisScope(f).Provide(i.Right())(nil)
 			return frame.End()
 		}
 		ret = frame.NOW
 	case node.VariableNode:
 		seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-			sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+			sc := rt2.ScopeFor(f, i.Right().Object().Adr())
 			rt2.ValueOf(f.Parent())[i.Adr()] = sc.Select(i.Right().Object().Adr())
 			return frame.End()
 		}

+ 8 - 7
rt2/rules/op.go

@@ -6,7 +6,6 @@ import (
 	"fw/cp/node"
 	"fw/cp/object"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
 	"fw/rt2/scope"
 	"math/big"
@@ -31,7 +30,6 @@ func int32Of(x interface{}) (a int32) {
 }
 
 func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-	sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
 	n := rt2.NodeOf(f).(node.MonadicNode)
 
 	op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
@@ -41,6 +39,7 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Not(rt2.ValueOf(f)[n.Left().Adr()])
 			return frame.End()
 		case operation.IS:
+			sc := rt2.ScopeFor(f, n.Left().Object().Adr())
 			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Is(sc.Select(n.Left().Object().Adr()), n.Object().Complex())
 			return frame.End()
 		case operation.ABS:
@@ -75,6 +74,7 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		}
 		switch n.Left().(type) {
 		case node.VariableNode, node.ParameterNode:
+			sc := rt2.ScopeFor(f, n.Left().Object().Adr())
 			x := sc.Select(n.Left().Object().Adr())
 			assert.For(x != nil, 40)
 			conv(scope.ValueFrom(x))
@@ -90,10 +90,11 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	case operation.NOT:
 		switch n.Left().(type) {
 		case node.ConstantNode:
-			rt2.ValueOf(f)[n.Left().Adr()] = sc.Provide(n.Left())(nil)
+			rt2.ValueOf(f)[n.Left().Adr()] = rt2.ThisScope(f).Provide(n.Left())(nil)
 			return op, frame.NOW
 		case node.VariableNode, node.ParameterNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
+				sc := rt2.ScopeFor(f, n.Left().Object().Adr())
 				rt2.ValueOf(f)[n.Left().Adr()] = sc.Select(n.Left().Object().Adr())
 				return op, frame.NOW
 			}
@@ -132,8 +133,6 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 }
 
 func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-	sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
-
 	op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		n := rt2.NodeOf(f).(node.OperationNode)
 		switch n.Operation() {
@@ -203,10 +202,11 @@ func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		n := rt2.NodeOf(f)
 		switch r := n.Right().(type) {
 		case node.ConstantNode:
-			rt2.ValueOf(f)[n.Right().Adr()] = sc.Provide(n.Right())(nil)
+			rt2.ValueOf(f)[n.Right().Adr()] = rt2.ThisScope(f).Provide(n.Right())(nil)
 			return op, frame.NOW
 		case node.VariableNode, node.ParameterNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
+				sc := rt2.ScopeFor(f, n.Right().Object().Adr())
 				rt2.ValueOf(f)[n.Right().Adr()] = sc.Select(n.Right().Object().Adr())
 				return op, frame.NOW
 			}
@@ -267,10 +267,11 @@ func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		n := rt2.NodeOf(f)
 		switch l := n.Left().(type) {
 		case node.ConstantNode:
-			rt2.ValueOf(f)[n.Left().Adr()] = sc.Provide(n.Left())(nil)
+			rt2.ValueOf(f)[n.Left().Adr()] = rt2.ThisScope(f).Provide(n.Left())(nil)
 			return short, frame.NOW
 		case node.VariableNode, node.ParameterNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
+				sc := rt2.ScopeFor(f, n.Left().Object().Adr())
 				rt2.ValueOf(f)[n.Left().Adr()] = sc.Select(n.Left().Object().Adr())
 				return short, frame.NOW
 			}

+ 2 - 5
rt2/rules/return.go

@@ -4,9 +4,7 @@ import (
 	"fmt"
 	"fw/cp/node"
 	"fw/rt2"
-	"fw/rt2/context"
 	"fw/rt2/frame"
-	"fw/rt2/scope"
 	"reflect"
 )
 
@@ -17,16 +15,15 @@ func returnSeq(f frame.Frame) (frame.Sequence, frame.WAIT) {
 		switch a.Left().(type) {
 		case node.ConstantNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
 				//rt2.DataOf(f.Parent())[a.Object()] = a.Left().(node.ConstantNode).Data()
-				rt2.ValueOf(f.Parent())[a.Object().Adr()] = sc.Provide(a.Left())(nil)
+				rt2.ValueOf(f.Parent())[a.Object().Adr()] = rt2.ThisScope(f).Provide(a.Left())(nil)
 				rt2.RegOf(f.Parent())["RETURN"] = rt2.ValueOf(f.Parent())[a.Object().Adr()]
 				return frame.End()
 			}
 			ret = frame.NOW
 		case node.VariableNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+				sc := rt2.ScopeFor(f, a.Left().Object().Adr())
 				rt2.ValueOf(f.Parent())[a.Object().Adr()] = sc.Select(a.Left().Object().Adr())
 				rt2.RegOf(f.Parent())["RETURN"] = rt2.ValueOf(f.Parent())[a.Object().Adr()]
 				return frame.End()

+ 16 - 3
rt2/rules/table.go

@@ -20,6 +20,16 @@ import (
 	"ypk/assert"
 )
 
+func test(n node.Node) (bool, int) {
+	switch n.(type) {
+	case node.ConstantNode:
+		return false, -1
+	default:
+		return true, 0
+	}
+	panic(0)
+}
+
 func prologue(n node.Node) frame.Sequence {
 	//fmt.Println(reflect.TypeOf(n))
 	switch next := n.(type) {
@@ -120,7 +130,10 @@ func epilogue(n node.Node) frame.Sequence {
 			//fmt.Println("from", reflect.TypeOf(n))
 			//fmt.Println("next", reflect.TypeOf(next))
 			if next != nil {
-				f.Root().PushFor(rt2.New(next), f.Parent())
+				nf := rt2.New(next)
+				if nf != nil {
+					f.Root().PushFor(nf, f.Parent())
+				}
 			}
 			if _, ok := n.(node.CallNode); ok {
 				if f.Parent() != nil {
@@ -139,9 +152,8 @@ func epilogue(n node.Node) frame.Sequence {
 	case node.EnterNode:
 		return func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			//fmt.Println(rt_module.DomainModule(f.Domain()).Name)
-			sm := f.Domain().Discover(context.SCOPE).(scope.Manager)
 			if e.Enter() == enter.PROCEDURE {
-				sm.Target().(scope.ScopeAllocator).Dispose(n)
+				rt2.ThisScope(f).Target().(scope.ScopeAllocator).Dispose(n)
 			}
 			//возвращаем результаты вызова функции
 			if f.Parent() != nil {
@@ -267,4 +279,5 @@ func init() {
 	decision.PrologueFor = prologue
 	decision.EpilogueFor = epilogue
 	decision.Run = run
+	decision.AssertFor = test
 }

+ 1 - 0
rt2/scope/area.go

@@ -22,6 +22,7 @@ type Manager interface {
 	Update(id cp.ID, val ValueFor)
 	Select(cp.ID, ...ValueOf) Value
 	Target(...Allocator) Allocator
+	Exists(cp.ID) bool
 	Provide(interface{}) ValueFor
 	String() string
 }

+ 45 - 7
rt2/scope/modern/ms.go

@@ -73,6 +73,42 @@ func (a *area) top() *level {
 	return nil
 }
 
+func (a *area) Exists(id cp.ID) bool {
+	var ret scope.Value
+	var sel func(x int, id cp.ID)
+	sel = func(x int, id cp.ID) {
+		utils.PrintScope("SELECT", id)
+		for i := x - 1; i >= 0 && ret == nil; i-- {
+			l := a.data[i]
+			k := 0
+			if l.ready {
+				k = l.k[id]
+				if k != 0 {
+					ret = l.v[k]
+					if ret == nil { //ref?
+						r := l.r[k]
+						if r != nil {
+							if l.l[k] != nil { //rec
+								panic(0)
+							} else {
+								utils.PrintScope("ref")
+								if r.(*ref).sc == a {
+									sel(i, r.(*ref).id)
+								} else {
+									ret = r.(*ref).sc.Select(r.(*ref).id)
+								}
+							}
+							break
+						}
+					}
+				}
+			}
+		}
+	}
+	sel(len(a.data), id)
+	return ret != nil
+}
+
 func (a *area) Provide(x interface{}) scope.ValueFor {
 	return func(scope.Value) scope.Value {
 		switch z := x.(type) {
@@ -254,7 +290,7 @@ func (a *salloc) Initialize(n node.Node, par scope.PARAM) (frame.Sequence, frame
 			if mod != nil {
 				//fmt.Println(mod.Name)
 				global = global.Discover(mod.Name).(context.Domain)
-				sm = global.Discover(context.SCOPE).(scope.Manager)
+				sm = global.Discover(context.VSCOPE, 0).(scope.Manager)
 			} else { //для фиктивных узлов, которые созданы рантаймом, типа INC/DEC
 				sm = a.area
 			}
@@ -265,11 +301,13 @@ func (a *salloc) Initialize(n node.Node, par scope.PARAM) (frame.Sequence, frame
 					v := newConst(nv)
 					l.v[l.k[o.Adr()]].Set(v)
 				case node.VariableNode, node.ParameterNode:
-					if nv.Object().Imp() != "" {
+					/*if nv.Object().Imp() != "" {
 						md := rtm.ModuleDomain(f.Domain(), nv.Object().Imp())
-						sm = md.Discover(context.SCOPE).(scope.Manager)
-					}
-					v := sm.Select(rtm.MapImportObject(f.Domain(), nv.Object()).Adr())
+						sm = md.Discover(context.VSCOPE).(scope.Manager)
+					}*/
+					adr := rtm.MapImportObject(f.Domain(), nv.Object()).Adr()
+					sm = rt2.ScopeFor(f, adr)
+					v := sm.Select(adr)
 					l.v[l.k[o.Adr()]].Set(v)
 				case node.OperationNode:
 					nf := rt2.New(nv)
@@ -309,7 +347,7 @@ func (a *salloc) Initialize(n node.Node, par scope.PARAM) (frame.Sequence, frame
 						return rt2.ValueOf(f)[nv.Adr()] != nil, 64
 					})
 					seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-						sc := global.Discover(context.SCOPE).(scope.Manager)
+						sc := rt2.ScopeFor(f, nv.Left().Object().Adr())
 						left := rt2.ValueOf(f)[nv.Adr()]
 						arr := sc.Select(nv.Left().Object().Adr()).(scope.Array)
 						v := arr.Get(left)
@@ -537,7 +575,7 @@ func fn(mgr scope.Manager, name string) (ret object.Object) {
 }
 
 func nn(role string) scope.Manager {
-	if role == context.SCOPE {
+	if role == context.VSCOPE {
 		return &area{all: &salloc{}}
 	} else if role == context.HEAP {
 		return &area{all: &halloc{}}

+ 22 - 2
rt2/utils.go

@@ -5,8 +5,10 @@ import (
 	"fw/cp/node"
 	"fw/rt2/context"
 	"fw/rt2/frame"
+	rtm "fw/rt2/module"
 	"fw/rt2/nodeframe"
 	"fw/rt2/scope"
+	"ypk/assert"
 )
 
 var utils nodeframe.NodeFrameUtils
@@ -17,8 +19,26 @@ func ValueOf(f frame.Frame) map[cp.ID]scope.Value     { return utils.ValueOf(f)
 func NodeOf(f frame.Frame) node.Node                  { return utils.NodeOf(f) }
 func Push(f, p frame.Frame)                           { utils.Push(f, p) }
 func New(n node.Node) frame.Frame                     { return utils.New(n) }
-func ScopeOf(f frame.Frame) scope.Manager {
-	return f.Domain().Discover(context.SCOPE).(scope.Manager)
+
+func ThisScope(f frame.Frame) scope.Manager {
+	return f.Domain().Discover(context.VSCOPE, 0).(scope.Manager)
+}
+
+func ScopeFor(f frame.Frame, id cp.ID) (ret scope.Manager) {
+	glob := f.Domain().Discover(context.UNIVERSE).(context.Domain)
+	ml := glob.Discover(context.MOD).(rtm.List)
+	for _, m := range ml.AsList() {
+		md := glob.Discover(m.Name).(context.Domain)
+		sc := md.Discover(context.VSCOPE, 0).(scope.Manager)
+		//fmt.Println(m.Name, sc.Exists(id), id)
+		if sc.Exists(id) {
+			assert.For(ret == nil, 40) //только в одном скоупе!
+			ret = sc
+		}
+	}
+	assert.For(ret != nil, 60)
+	return
 }
+
 func ReplaceDomain(f frame.Frame, d context.Domain) { utils.ReplaceDomain(f, d) }
 func Assert(f frame.Frame, ok frame.Assert)         { utils.Assert(f, ok) }