Browse Source

восстановил функциональность до уровня первого прототипа: присвоение, сумма и вызов функций.

kpmy 10 years ago
parent
commit
590fa8e775

+ 6 - 0
cp/object/object.go

@@ -20,6 +20,10 @@ type Object interface {
 	Type() Type
 	Type() Type
 }
 }
 
 
+type VariableObject interface {
+	This() VariableObject
+}
+
 func New(mode Mode) Object {
 func New(mode Mode) Object {
 	switch mode {
 	switch mode {
 	case HEAD:
 	case HEAD:
@@ -61,3 +65,5 @@ type headObject struct {
 type localProcedureObject struct {
 type localProcedureObject struct {
 	objectFields
 	objectFields
 }
 }
+
+func (v *variableObject) This() VariableObject { return v }

+ 2 - 0
fw/domain.go

@@ -37,6 +37,8 @@ func (d *stdDomain) Domain() context.Domain {
 	return d.parent
 	return d.parent
 }
 }
 
 
+func (d *stdDomain) Handle(msg interface{}) {}
+
 func (d *stdDomain) Init(dd context.Domain) {
 func (d *stdDomain) Init(dd context.Domain) {
 	glob := dd.(*stdDomain).global
 	glob := dd.(*stdDomain).global
 	assert.For(glob == nil, 20) //допустим только один уровень вложенности доменов пока
 	assert.For(glob == nil, 20) //допустим только один уровень вложенности доменов пока

+ 1 - 0
rt2/context/ctx.go

@@ -17,4 +17,5 @@ type Domain interface {
 type ContextAware interface {
 type ContextAware interface {
 	Init(d Domain)
 	Init(d Domain)
 	Domain() Domain
 	Domain() Domain
+	Handle(msg interface{})
 }
 }

+ 8 - 0
rt2/frame/frame.go

@@ -39,3 +39,11 @@ func Tail(x WAIT) (seq Sequence) {
 }
 }
 
 
 func End() (Sequence, WAIT) { return nil, STOP }
 func End() (Sequence, WAIT) { return nil, STOP }
+
+type SetDataMsg struct {
+	Data []interface{}
+}
+
+type GetDataMsg struct {
+	Data []interface{}
+}

+ 2 - 0
rt2/frame/stdFrame.go

@@ -94,6 +94,8 @@ func (f *RootFrame) Init(d context.Domain) {
 	assert.For(d != nil, 21)
 	assert.For(d != nil, 21)
 	f.domain = d
 	f.domain = d
 }
 }
+func (f *RootFrame) Handle(msg interface{}) {}
+
 func (w WAIT) String() string {
 func (w WAIT) String() string {
 	switch w {
 	switch w {
 	case DO:
 	case DO:

+ 1 - 0
rt2/module/ml.go

@@ -47,6 +47,7 @@ func (l *list) Init(d context.Domain) {
 	l.d = d
 	l.d = d
 }
 }
 
 
+func (l *list) Handle(msg interface{}) {}
 func (l *list) Load(name string) (*mod.Module, error) {
 func (l *list) Load(name string) (*mod.Module, error) {
 	assert.For(name != "", 20)
 	assert.For(name != "", 20)
 	ret := l.Loaded(name)
 	ret := l.Loaded(name)

+ 16 - 0
rt2/nodeframe/frame.go

@@ -30,12 +30,19 @@ func (fu FrameUtils) NodeOf(f frame.Frame) node.Node {
 	return ff.ir
 	return ff.ir
 }
 }
 
 
+func (fu FrameUtils) DataOf(f frame.Frame) []interface{} {
+	m := new(frame.GetDataMsg)
+	f.(context.ContextAware).Handle(m)
+	return m.Data
+}
+
 type nodeFrame struct {
 type nodeFrame struct {
 	root   frame.Stack
 	root   frame.Stack
 	parent frame.Frame
 	parent frame.Frame
 	ir     node.Node
 	ir     node.Node
 	seq    frame.Sequence
 	seq    frame.Sequence
 	domain context.Domain
 	domain context.Domain
+	data   []interface{}
 }
 }
 
 
 func (f *nodeFrame) Do() frame.WAIT {
 func (f *nodeFrame) Do() frame.WAIT {
@@ -79,3 +86,12 @@ func (f *nodeFrame) Init(d context.Domain) {
 	assert.For(d != nil, 21)
 	assert.For(d != nil, 21)
 	f.domain = d
 	f.domain = d
 }
 }
+func (f *nodeFrame) Handle(msg interface{}) {
+	assert.For(msg != nil, 20)
+	switch msg.(type) {
+	case *frame.SetDataMsg:
+		f.data = msg.(*frame.SetDataMsg).Data
+	case *frame.GetDataMsg:
+		msg.(*frame.GetDataMsg).Data = f.data
+	}
+}

+ 17 - 3
rt2/rules/assign.go

@@ -3,8 +3,10 @@ package rules
 import (
 import (
 	"cp/node"
 	"cp/node"
 	"cp/statement"
 	"cp/statement"
+	"rt2/context"
 	"rt2/frame"
 	"rt2/frame"
 	"rt2/nodeframe"
 	"rt2/nodeframe"
+	"rt2/scope"
 )
 )
 
 
 func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
@@ -12,25 +14,37 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	a := fu.NodeOf(f)
 	a := fu.NodeOf(f)
 	switch a.(node.AssignNode).Statement() {
 	switch a.(node.AssignNode).Statement() {
 	case statement.ASSIGN:
 	case statement.ASSIGN:
+		m := new(frame.SetDataMsg)
+		m.Data = make([]interface{}, 1)
+		f.(context.ContextAware).Handle(m)
 		switch a.Left().(type) {
 		switch a.Left().(type) {
 		case node.VariableNode:
 		case node.VariableNode:
 			switch a.Right().(type) {
 			switch a.Right().(type) {
 			case node.ConstantNode:
 			case node.ConstantNode:
 				seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-					//тут присвоение
+					sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+					sc.Update(a.Left().Object(), func(interface{}) interface{} {
+						return a.Right().(node.ConstantNode).Data()
+					})
 					return frame.End()
 					return frame.End()
 				}
 				}
 				ret = frame.DO
 				ret = frame.DO
 			case node.VariableNode:
 			case node.VariableNode:
 				seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-					//тут присвоение
+					sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+					sc.Update(a.Left().Object(), func(interface{}) interface{} {
+						return sc.Select(a.Right().Object())
+					})
 					return frame.End()
 					return frame.End()
 				}
 				}
 				ret = frame.DO
 				ret = frame.DO
 			case node.OperationNode:
 			case node.OperationNode:
 				fu.Push(fu.New(a.Right()), f)
 				fu.Push(fu.New(a.Right()), f)
 				seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-					//тут чтение результата операции
+					sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+					sc.Update(a.Left().Object(), func(interface{}) interface{} {
+						return fu.DataOf(f)[0]
+					})
 					return frame.End()
 					return frame.End()
 				}
 				}
 				ret = frame.SKIP
 				ret = frame.SKIP

+ 35 - 5
rt2/rules/op.go

@@ -3,18 +3,46 @@ package rules
 import (
 import (
 	"cp/constant/operation"
 	"cp/constant/operation"
 	"cp/node"
 	"cp/node"
+	"fmt"
+	"reflect"
+	"rt2/context"
 	"rt2/frame"
 	"rt2/frame"
 	"rt2/nodeframe"
 	"rt2/nodeframe"
+	"rt2/scope"
 )
 )
 
 
+func intOf(x interface{}) (a int) {
+	fmt.Println(reflect.TypeOf(x))
+	switch x.(type) {
+	case *int:
+		z := *x.(*int)
+		a = z
+	case int:
+		a = x.(int)
+	default:
+		panic("unsupported type")
+	}
+	return a
+}
+
+func sum(_a interface{}, _b interface{}) interface{} {
+	var a int = intOf(_a)
+	var b int = intOf(_b)
+	return a + b
+}
+
 func opSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 func opSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	var fu nodeframe.FrameUtils
 	var fu nodeframe.FrameUtils
 
 
+	m := new(frame.SetDataMsg)
+	m.Data = make([]interface{}, 2)
+	f.(context.ContextAware).Handle(m)
+
 	op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		n := fu.NodeOf(f)
 		n := fu.NodeOf(f)
 		switch n.(node.OperationNode).Operation() {
 		switch n.(node.OperationNode).Operation() {
 		case operation.PLUS:
 		case operation.PLUS:
-			//складываем
+			fu.DataOf(f.Parent())[0] = sum(fu.DataOf(f)[0], fu.DataOf(f)[1])
 			return frame.End()
 			return frame.End()
 		default:
 		default:
 			panic("unknown operation")
 			panic("unknown operation")
@@ -25,11 +53,12 @@ func opSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		n := fu.NodeOf(f)
 		n := fu.NodeOf(f)
 		switch n.Right().(type) {
 		switch n.Right().(type) {
 		case node.ConstantNode:
 		case node.ConstantNode:
-			//f.ret[f.ir.Right()] = f.ir.Right().(node.ConstantNode).Data()
+			fu.DataOf(f)[1] = n.Right().(node.ConstantNode).Data()
 			return op, frame.DO
 			return op, frame.DO
 		case node.VariableNode:
 		case node.VariableNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				//f.ret[f.ir.Right()] = f.p.heap.This(f.ir.Right().Object())
+				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+				fu.DataOf(f)[1] = sc.Select(n.Right().Object())
 				return op, frame.DO
 				return op, frame.DO
 			}
 			}
 			ret = frame.DO
 			ret = frame.DO
@@ -43,11 +72,12 @@ func opSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		n := fu.NodeOf(f)
 		n := fu.NodeOf(f)
 		switch n.Left().(type) {
 		switch n.Left().(type) {
 		case node.ConstantNode:
 		case node.ConstantNode:
-			//f.ret[f.ir.Left()] = f.ir.Left().(node.ConstantNode).Data()
+			fu.DataOf(f)[0] = n.Left().(node.ConstantNode).Data()
 			return right, frame.DO
 			return right, frame.DO
 		case node.VariableNode:
 		case node.VariableNode:
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				//f.ret[f.ir.Left()] = f.p.heap.This(f.ir.Left().Object())
+				sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
+				fu.DataOf(f)[0] = sc.Select(n.Left().Object())
 				return right, frame.DO
 				return right, frame.DO
 			}
 			}
 			ret = frame.DO
 			ret = frame.DO

+ 5 - 10
rt2/scope/area.go

@@ -6,19 +6,14 @@ import (
 	"rt2/context"
 	"rt2/context"
 )
 )
 
 
-//менеджер зон видимости
+//менеджер зон видимости, зоны видимости динамические, создаются в момент входа в EnterNode
 type Manager interface {
 type Manager interface {
 	context.ContextAware
 	context.ContextAware
-	Calculate(n node.Node) Area
+	Update(o object.Object, val Value)
+	Select(o object.Object) interface{}
 	Allocate(n node.Node)
 	Allocate(n node.Node)
 	Dispose(n node.Node)
 	Dispose(n node.Node)
 }
 }
 
 
-//зона видимости
-type Area interface {
-	Get(o object.Object) Object
-	Set(o Object)
-}
-
-//объект зоны видимости
-type Object interface{}
+//средство обновления значения
+type Value func(in interface{}) (out interface{})

+ 69 - 4
rt2/scope/stdScope.go

@@ -1,8 +1,11 @@
 package scope
 package scope
 
 
 import (
 import (
+	"container/list"
 	"cp/node"
 	"cp/node"
+	"cp/object"
 	"fmt"
 	"fmt"
+	"reflect"
 	"rt2/context"
 	"rt2/context"
 	rt_mod "rt2/module"
 	rt_mod "rt2/module"
 	"ypk/assert"
 	"ypk/assert"
@@ -18,24 +21,84 @@ func New() Manager {
 }
 }
 
 
 type manager struct {
 type manager struct {
-	d context.Domain
+	d     context.Domain
+	areas *list.List
 }
 }
 
 
+type heapObj struct {
+	heap map[object.Object]interface{}
+	root node.Node
+}
+
+type undefined struct{}
+
+var undef *undefined = new(undefined)
+
 func (m *manager) init() *manager {
 func (m *manager) init() *manager {
+	m.areas = list.New()
 	return m
 	return m
 }
 }
 
 
 func (m *manager) Allocate(n node.Node) {
 func (m *manager) Allocate(n node.Node) {
 	mod := rt_mod.DomainModule(m.Domain())
 	mod := rt_mod.DomainModule(m.Domain())
-	fmt.Println("allocate", len(mod.Objects[n]), "obj")
+	h := new(heapObj)
+	h.heap = make(map[object.Object]interface{})
+	h.root = n
+	for _, o := range mod.Objects[n] {
+		fmt.Println(reflect.TypeOf(o))
+		switch o.(type) {
+		case object.VariableObject:
+			h.heap[o] = undef
+		}
+	}
+	m.areas.PushFront(h)
+	fmt.Println("allocate", len(h.heap), "obj")
 }
 }
 
 
 func (m *manager) Dispose(n node.Node) {
 func (m *manager) Dispose(n node.Node) {
+	e := m.areas.Front()
+	assert.For(e != nil, 20)
+	h := e.Value.(*heapObj)
+	assert.For(h.root == n, 21)
+	m.areas.Remove(e)
 	fmt.Println("dispose")
 	fmt.Println("dispose")
 }
 }
 
 
-func (m *manager) Calculate(n node.Node) Area {
-	return nil
+func (m *manager) Select(o object.Object) (ret interface{}) {
+	assert.For(o != nil, 20)
+	for e := m.areas.Front(); (e != nil) && (ret == nil); e = e.Next() {
+		h := e.Value.(*heapObj)
+		ret = h.heap[o]
+	}
+	assert.For(ret != nil, 40)
+	if ret == undef {
+		ret = nil
+	}
+	return ret
+}
+
+func (m *manager) Update(o object.Object, val Value) {
+	assert.For(o != nil, 20)
+	assert.For(val != nil, 21)
+	var x *heapObj
+	for e := m.areas.Front(); (e != nil) && (x == nil); e = e.Next() {
+		h := e.Value.(*heapObj)
+		if h.heap[o] != nil {
+			x = h
+		}
+	}
+	assert.For(x != nil, 40)
+	tmp := x.heap[o]
+	if tmp == undef {
+		tmp = val(nil)
+	} else {
+		tmp = val(tmp)
+	}
+	if tmp == nil {
+		tmp = undef
+	}
+	x.heap[o] = tmp
+	fmt.Println("set", x.heap[o], reflect.TypeOf(x.heap[o]))
 }
 }
 
 
 func (m *manager) Init(d context.Domain) {
 func (m *manager) Init(d context.Domain) {
@@ -45,3 +108,5 @@ func (m *manager) Init(d context.Domain) {
 func (m *manager) Domain() context.Domain {
 func (m *manager) Domain() context.Domain {
 	return m.d
 	return m.d
 }
 }
+
+func (m *manager) Handle(msg interface{}) {}

+ 1 - 1
ypk/assert/debug.go

@@ -10,7 +10,7 @@ func For(cond bool, code int) {
 		switch {
 		switch {
 		case (code >= 20) && (code < 40):
 		case (code >= 20) && (code < 40):
 			e = fmt.Sprintln(code, "precondition violated")
 			e = fmt.Sprintln(code, "precondition violated")
-		case (code >= 40) && (code < 59):
+		case (code >= 40) && (code < 60):
 			e = fmt.Sprintln(code, "subcondition violated")
 			e = fmt.Sprintln(code, "subcondition violated")
 		case (code >= 60) && (code < 80):
 		case (code >= 60) && (code < 80):
 			e = fmt.Sprintln(code, "postcondition violated")
 			e = fmt.Sprintln(code, "postcondition violated")