소스 검색

добавил контекст для связи различных частей рантайма

p.kushnir 10 년 전
부모
커밋
cee5a2459c
6개의 변경된 파일68개의 추가작업 그리고 7개의 파일을 삭제
  1. 12 1
      fw/fw.go
  2. 10 0
      rt2/context/ctx.go
  3. 5 0
      rt2/frame/frame.go
  4. 16 3
      rt2/frame/stdFrame.go
  5. 10 2
      rt2/nodeframe/frame.go
  6. 15 1
      ypk/assert/debug.go

+ 12 - 1
fw/fw.go

@@ -3,6 +3,7 @@ package main
 import (
 	"fmt"
 	"os"
+	"rt2/context"
 	"rt2/frame"
 	"rt2/nodeframe"
 	_ "rt2/rules"
@@ -10,11 +11,21 @@ import (
 	"ypk/assert"
 )
 
+type stdDomain struct {
+}
+
+func (d *stdDomain) ConnectTo(x context.ContextAware) {
+	assert.For(x != nil, 20)
+	x.Init(d)
+}
+
 func main() {
 	path, _ := os.Getwd()
 	ret := xev.Load(path, "PrivDemo1.oxf")
 	assert.For(ret != nil, 20)
-	root := new(frame.RootFrame).Init()
+	domain := new(stdDomain)
+	root := frame.NewRoot()
+	domain.ConnectTo(root)
 	var fu nodeframe.FrameUtils
 	root.Push(fu.New(ret.Enter))
 	i := 0

+ 10 - 0
rt2/context/ctx.go

@@ -0,0 +1,10 @@
+package context
+
+type Domain interface {
+	ConnectTo(c ContextAware)
+}
+
+type ContextAware interface {
+	Init(d Domain)
+	Domain() Domain
+}

+ 5 - 0
rt2/frame/frame.go

@@ -1,5 +1,9 @@
 package frame
 
+import (
+	"rt2/context"
+)
+
 type WAIT int
 
 const (
@@ -24,6 +28,7 @@ type Frame interface {
 	OnPop()
 	Parent() Frame
 	Root() Stack
+	context.ContextAware
 }
 
 //пользовательская функция, которую выполнит фрейм, может поставить на очередь выполнения себя или другую функцию

+ 16 - 3
rt2/frame/stdFrame.go

@@ -3,17 +3,24 @@ package frame
 import (
 	"container/list"
 	"fmt"
+	"rt2/context"
+	"ypk/assert"
 )
 
 type RootFrame struct {
-	inner list.List
+	inner  list.List
+	domain context.Domain
 }
 
-func (f *RootFrame) Init() *RootFrame {
+func (f *RootFrame) init() *RootFrame {
 	f.inner = *list.New()
 	return f
 }
 
+func NewRoot() *RootFrame {
+	return new(RootFrame).init()
+}
+
 func (f *RootFrame) Push(frame Frame) {
 	f.PushFor(frame, nil)
 }
@@ -24,6 +31,7 @@ func (f *RootFrame) PushFor(frame, parent Frame) {
 		panic("impossibru")
 	}
 	f.inner.PushFront(frame)
+	frame.Init(f.Domain())
 	frame.OnPush(f, parent)
 }
 
@@ -80,7 +88,12 @@ func (f *RootFrame) OnPush(a Stack, b Frame) {}
 func (f *RootFrame) OnPop()                  {}
 func (f *RootFrame) Parent() Frame           { return nil }
 func (f *RootFrame) Root() Stack             { return nil }
-
+func (f *RootFrame) Domain() context.Domain  { return f.domain }
+func (f *RootFrame) Init(d context.Domain) {
+	assert.For(f.domain == nil, 20)
+	assert.For(d != nil, 21)
+	f.domain = d
+}
 func (w WAIT) String() string {
 	switch w {
 	case DO:

+ 10 - 2
rt2/nodeframe/frame.go

@@ -2,6 +2,7 @@ package nodeframe
 
 import (
 	"cp/node"
+	"rt2/context"
 	"rt2/decision"
 	"rt2/frame"
 	"ypk/assert"
@@ -34,6 +35,7 @@ type nodeFrame struct {
 	parent frame.Frame
 	ir     node.Node
 	seq    frame.Sequence
+	domain context.Domain
 }
 
 func (f *nodeFrame) Do() frame.WAIT {
@@ -69,5 +71,11 @@ func (f *nodeFrame) OnPush(root frame.Stack, parent frame.Frame) {
 	f.onPush()
 }
 
-func (f *nodeFrame) Parent() frame.Frame { return f.parent }
-func (f *nodeFrame) Root() frame.Stack   { return f.root }
+func (f *nodeFrame) Parent() frame.Frame    { return f.parent }
+func (f *nodeFrame) Root() frame.Stack      { return f.root }
+func (f *nodeFrame) Domain() context.Domain { return f.domain }
+func (f *nodeFrame) Init(d context.Domain) {
+	assert.For(f.domain == nil, 20)
+	assert.For(d != nil, 21)
+	f.domain = d
+}

+ 15 - 1
ypk/assert/debug.go

@@ -1,7 +1,21 @@
 package assert
 
+import (
+	"fmt"
+)
+
 func For(cond bool, code int) {
+	e := fmt.Sprint(code)
 	if !cond {
-		panic(code)
+		switch {
+		case (code >= 20) && (code < 40):
+			e = fmt.Sprintln(code, "precondition violated")
+		case (code >= 40) && (code < 59):
+			e = fmt.Sprintln(code, "subcondition violated")
+		case (code >= 60) && (code < 80):
+			e = fmt.Sprintln(code, "postcondition violated")
+		default:
+		}
+		panic(e)
 	}
 }