Преглед на файлове

отрефакторил часть примеров

kpmy преди 10 години
родител
ревизия
8cd71f5ba0
променени са 14 файла, в които са добавени 225 реда и са изтрити 50 реда
  1. 1 1
      fw.go
  2. 2 2
      rt2/rules/assign.go
  3. 2 2
      rt2/rules/call.go
  4. 7 2
      rt2/rules/enter.go
  5. 8 6
      rt2/rules/if.go
  6. 2 2
      rt2/rules/loop.go
  7. 10 16
      rt2/rules/op.go
  8. 6 4
      rt2/rules/repeat.go
  9. 7 2
      rt2/rules/table.go
  10. 5 3
      rt2/rules/while.go
  11. 10 1
      rt2/scope/data.go
  12. 1 1
      rt2/scope/modern/ms.go
  13. 162 7
      rt2/scope/modern/val.go
  14. 2 1
      rt2/utils.go

+ 1 - 1
fw.go

@@ -28,7 +28,7 @@ func close() {
 func main() {
 	flag.Parse()
 	if name == "" {
-		name = "XevDemo1"
+		name = "XevDemo6"
 	}
 	global := &stdDomain{god: true}
 	modList := rtmod.New()

+ 2 - 2
rt2/rules/assign.go

@@ -23,7 +23,7 @@ func incSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	rt2.Push(rt2.New(op), f)
 	seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
-		sc.Update(n.Left().Adr(), nil) //scope.Simple(rt2.DataOf(f)[op]))
+		sc.Update(n.Left().Object().Adr(), scope.Simple(rt2.ValueOf(f)[op.Adr()]))
 		return frame.End()
 	}
 	ret = frame.LATER
@@ -39,7 +39,7 @@ func decSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	rt2.Push(rt2.New(op), f)
 	seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
-		sc.Update(n.Left().Adr(), nil) //scope.Simple(rt2.DataOf(f)[op]))
+		sc.Update(n.Left().Object().Adr(), scope.Simple(rt2.ValueOf(f)[op.Adr()]))
 		return frame.End()
 	}
 	ret = frame.LATER

+ 2 - 2
rt2/rules/call.go

@@ -91,12 +91,12 @@ func callSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		}
 		//передаем ссылку на цепочку значений параметров в данные фрейма входа в процедуру
 		if (n.Right() != nil) && (proc.Object() != nil) {
-			rt2.DataOf(nf)[proc.Object()] = n.Right()
+			rt2.RegOf(nf)[proc.Object()] = n.Right()
 		} else {
 			//fmt.Println("no data for call")
 		}
 		seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-			rt2.DataOf(f.Parent())[n] = rt2.DataOf(f)[n.Left().Object()]
+			//			rt2.DataOf(f.Parent())[n] = rt2.DataOf(f)[n.Left().Object()]
 			rt2.ValueOf(f.Parent())[n.Adr()] = rt2.ValueOf(f)[n.Left().Object().Adr()]
 			return frame.End()
 		}

+ 7 - 2
rt2/rules/enter.go

@@ -46,13 +46,18 @@ func enterSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	sm := f.Domain().Discover(context.SCOPE).(scope.Manager)
 	//fmt.Println(n.Object())
 	if n.Object() != nil {
-		par, ok := rt2.DataOf(f)[n.Object()].(node.Node)
+		par, ok := rt2.RegOf(f)[n.Object()].(node.Node)
 		//fmt.Println(rt2.DataOf(f)[n.Object()])
 		//fmt.Println(ok)
 		if ok {
 			sm.Target().(scope.ScopeAllocator).Allocate(n, false)
 			seq = func(f frame.Frame) (frame.Sequence, frame.WAIT) {
-				return sm.Target().(scope.ScopeAllocator).Initialize(n, scope.PARAM{Objects: n.Object().Link(), Values: par, Frame: f, Tail: tail})
+				return sm.Target().(scope.ScopeAllocator).Initialize(n,
+					scope.PARAM{
+						Objects: n.Object().Link(),
+						Values:  par,
+						Frame:   f,
+						Tail:    tail})
 			}
 		} else {
 			sm.Target().(scope.ScopeAllocator).Allocate(n, true)

+ 8 - 6
rt2/rules/if.go

@@ -5,6 +5,7 @@ import (
 	"fw/cp/node"
 	"fw/rt2"
 	"fw/rt2/frame"
+	"fw/rt2/scope"
 	"reflect"
 )
 
@@ -14,7 +15,8 @@ func ifExpr(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	case node.OperationNode:
 		rt2.Push(rt2.New(n.Left()), f)
 		seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-			rt2.DataOf(f.Parent())[n] = rt2.DataOf(f)[n.Left()]
+			rt2.RegOf(f.Parent())[n] = rt2.RegOf(f)[n.Left()]
+			rt2.ValueOf(f.Parent())[n.Adr()] = rt2.ValueOf(f)[n.Left().Adr()]
 			return frame.End()
 		}
 		ret = frame.LATER
@@ -26,19 +28,19 @@ func ifExpr(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 
 func ifSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	n := rt2.NodeOf(f)
-	rt2.DataOf(f)[0] = n.Left() //if
+	rt2.RegOf(f)[0] = n.Left() //if
 	rt2.Push(rt2.New(n.Left()), f)
 	seq = func(f frame.Frame) (frame.Sequence, frame.WAIT) {
-		last := rt2.DataOf(f)[0].(node.Node)
-		done := rt2.DataOf(f)[last].(bool)
-		rt2.DataOf(f)[last] = nil
+		last := rt2.RegOf(f)[0].(node.Node)
+		done := scope.GoTypeFrom(rt2.ValueOf(f)[last.Adr()]).(bool)
+		rt2.RegOf(f)[last] = nil
 		if done && last.Right() != nil {
 			rt2.Push(rt2.New(last.Right()), f)
 			return frame.Tail(frame.STOP), frame.LATER
 		} else if last.Right() == nil {
 			return frame.End()
 		} else if last.Link() != nil { //elsif
-			rt2.DataOf(f)[0] = last.Link()
+			rt2.RegOf(f)[0] = last.Link()
 			rt2.Push(rt2.New(last.Link()), f)
 			return seq, frame.LATER
 		} else if n.Right() != nil { //else

+ 2 - 2
rt2/rules/loop.go

@@ -13,7 +13,7 @@ func exitSeq(x frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		n := rt2.NodeOf(f)
 		_, ok = n.(node.LoopNode)
 		if ok {
-			rt2.DataOf(f)[flag] = true
+			rt2.RegOf(f)[flag] = true
 		}
 		ok = !ok
 		return ok
@@ -23,7 +23,7 @@ func exitSeq(x frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 
 func loopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	n := rt2.NodeOf(f)
-	exit, ok := rt2.DataOf(f)[flag].(bool)
+	exit, ok := rt2.RegOf(f)[flag].(bool)
 	if ok && exit {
 		return frame.End()
 	}

+ 10 - 16
rt2/rules/op.go

@@ -135,14 +135,6 @@ func sub(_a interface{}, _b interface{}) interface{} {
 	return a - b
 }
 
-func eq(_a interface{}, _b interface{}) bool {
-	assert.For(_a != nil, 20)
-	assert.For(_b != nil, 21)
-	var a int32 = int32Of(_a)
-	var b int32 = int32Of(_b)
-	return a == b
-}
-
 func and(_a interface{}, _b interface{}) bool {
 	assert.For(_a != nil, 20)
 	assert.For(_b != nil, 21)
@@ -317,8 +309,9 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 
 	switch n.Operation() {
 	case operation.ALIEN_CONV:
-		conv := func(x interface{}) {
-			switch n.Type() {
+		conv := func(x scope.Value) {
+			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Conv(x, n.Type())
+			/*switch n.Type() {
 			case object.INTEGER:
 				switch v := x.(type) {
 				case int8:
@@ -327,6 +320,7 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 					rt2.DataOf(f.Parent())[n] = int32(v.Int64())
 				case int32:
 					rt2.DataOf(f.Parent())[n] = v
+				case
 				default:
 					panic(fmt.Sprintln("ooops", reflect.TypeOf(x)))
 				}
@@ -346,7 +340,7 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				}
 			default:
 				panic(fmt.Sprintln("wrong type", n.Type()))
-			}
+			} */
 		}
 		switch n.Left().(type) {
 		case node.VariableNode, node.ParameterNode:
@@ -356,7 +350,7 @@ func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			return frame.End()
 		case node.OperationNode:
 			return This(expectExpr(f, n.Left(), func(...IN) OUT {
-				conv(rt2.DataOf(f)[n.Left()])
+				conv(rt2.ValueOf(f)[n.Left().Adr()])
 				return End()
 			}))
 		default:
@@ -423,16 +417,16 @@ func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Sum(rt2.ValueOf(f)[n.Left().Adr()], rt2.ValueOf(f)[n.Right().Adr()])
 			return frame.End()
 		case operation.MINUS:
-			rt2.DataOf(f.Parent())[n] = sub(rt2.DataOf(f)[n.Left()], rt2.DataOf(f)[n.Right()])
+			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Sub(rt2.ValueOf(f)[n.Left().Adr()], rt2.ValueOf(f)[n.Right().Adr()])
 			return frame.End()
 		case operation.EQUAL:
-			rt2.DataOf(f.Parent())[n] = eq(rt2.DataOf(f)[n.Left()], rt2.DataOf(f)[n.Right()])
+			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Eq(rt2.ValueOf(f)[n.Left().Adr()], rt2.ValueOf(f)[n.Right().Adr()])
 			return frame.End()
 		case operation.LESSER:
-			rt2.DataOf(f.Parent())[n] = lss(rt2.DataOf(f)[n.Left()], rt2.DataOf(f)[n.Right()])
+			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Lss(rt2.ValueOf(f)[n.Left().Adr()], rt2.ValueOf(f)[n.Right().Adr()])
 			return frame.End()
 		case operation.LESS_EQUAL:
-			rt2.DataOf(f.Parent())[n] = leq(rt2.DataOf(f)[n.Left()], rt2.DataOf(f)[n.Right()])
+			rt2.ValueOf(f.Parent())[n.Adr()] = scope.Ops.Leq(rt2.ValueOf(f)[n.Left().Adr()], rt2.ValueOf(f)[n.Right().Adr()])
 			return frame.End()
 		case operation.LEN:
 			rt2.DataOf(f.Parent())[n] = length(n.Left().Object(), rt2.DataOf(f)[n.Left()], rt2.DataOf(f)[n.Right()])

+ 6 - 4
rt2/rules/repeat.go

@@ -5,17 +5,18 @@ import (
 	"fw/cp/node"
 	"fw/rt2"
 	"fw/rt2/frame"
+	"fw/rt2/scope"
 	"reflect"
 )
 
 func repeatSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	n := rt2.NodeOf(f)
 
-	rt2.DataOf(f)[n.Right()] = false
+	rt2.ValueOf(f)[n.Right().Adr()] = scope.TypeFromGo(false)
 	var cond func(f frame.Frame) (frame.Sequence, frame.WAIT)
 	next := func(f frame.Frame) (frame.Sequence, frame.WAIT) {
-		done := rt2.DataOf(f)[n.Right()].(bool)
-		rt2.DataOf(f)[n.Right()] = nil
+		done := scope.GoTypeFrom(rt2.ValueOf(f)[n.Right().Adr()]).(bool)
+		rt2.ValueOf(f)[n.Right().Adr()] = nil
 		if !done && n.Left() != nil {
 			rt2.Push(rt2.New(n.Left()), f)
 			return cond, frame.LATER
@@ -33,7 +34,8 @@ func repeatSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		case node.OperationNode:
 			rt2.Push(rt2.New(n.Right()), f)
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				rt2.DataOf(f.Parent())[n] = rt2.DataOf(f)[n.Right()]
+				rt2.RegOf(f.Parent())[n] = rt2.RegOf(f)[n.Right()]
+				rt2.ValueOf(f.Parent())[n.Adr()] = rt2.ValueOf(f)[n.Right().Adr()]
 				return next, frame.LATER
 			}
 			ret = frame.LATER

+ 7 - 2
rt2/rules/table.go

@@ -117,10 +117,14 @@ func epilogue(n node.Node) frame.Sequence {
 			sm.Target().(scope.ScopeAllocator).Dispose(n)
 			//возвращаем результаты вызова функции
 			if f.Parent() != nil {
-				par := rt2.DataOf(f.Parent())
-				for k, v := range rt2.DataOf(f) {
+				par := rt2.RegOf(f.Parent())
+				for k, v := range rt2.RegOf(f) {
 					par[k] = v
 				}
+				val := rt2.ValueOf(f.Parent())
+				for k, v := range rt2.ValueOf(f) {
+					val[k] = v
+				}
 			}
 			return frame.End()
 		}
@@ -222,6 +226,7 @@ func run(global context.Domain, init []*module.Module) {
 		t0 := time.Now()
 		for x := frame.NOW; x == frame.NOW; x = root.Do() {
 			fmt.Println("STEP", i)
+			assert.For(i < 1000, 40)
 			i++
 		}
 		t1 := time.Now()

+ 5 - 3
rt2/rules/while.go

@@ -5,6 +5,7 @@ import (
 	"fw/cp/node"
 	"fw/rt2"
 	"fw/rt2/frame"
+	"fw/rt2/scope"
 	"reflect"
 )
 
@@ -13,8 +14,8 @@ func whileSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 
 	var cond func(f frame.Frame) (frame.Sequence, frame.WAIT)
 	next := func(f frame.Frame) (frame.Sequence, frame.WAIT) {
-		done := rt2.DataOf(f)[n.Left()].(bool)
-		rt2.DataOf(f)[n.Left()] = nil
+		done := scope.GoTypeFrom(rt2.ValueOf(f)[n.Left().Adr()]).(bool)
+		rt2.RegOf(f)[n.Left()] = nil
 		if done && n.Right() != nil {
 			rt2.Push(rt2.New(n.Right()), f)
 			return cond, frame.LATER
@@ -32,7 +33,8 @@ func whileSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		case node.OperationNode:
 			rt2.Push(rt2.New(n.Left()), f)
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-				rt2.DataOf(f.Parent())[n] = rt2.DataOf(f)[n.Left()]
+				rt2.RegOf(f.Parent())[n] = rt2.RegOf(f)[n.Left()]
+				rt2.ValueOf(f.Parent())[n.Adr()] = rt2.ValueOf(f)[n.Left().Adr()]
 				return next, frame.LATER
 			}
 			ret = frame.LATER

+ 10 - 1
rt2/scope/data.go

@@ -2,10 +2,18 @@ package scope
 
 import (
 	"fw/cp"
+	"fw/cp/object"
 )
 
 type Operations interface {
 	Sum(Value, Value) Value
+	Sub(Value, Value) Value
+
+	Eq(Value, Value) Value
+	Lss(Value, Value) Value
+	Leq(Value, Value) Value
+
+	Conv(Value, object.Type) Value
 }
 
 type Value interface {
@@ -36,5 +44,6 @@ func Simple(v Value) ValueFor {
 }
 
 var ValueFrom func(v Value) Value
-
+var GoTypeFrom func(v Value) interface{}
+var TypeFromGo func(v interface{}) Value
 var Ops Operations

+ 1 - 1
rt2/scope/modern/ms.go

@@ -133,7 +133,7 @@ func (a *salloc) Initialize(n node.Node, par scope.PARAM) (seq frame.Sequence, r
 				v := NewConst(nv)
 				l.v[l.k[o.Adr()]].Set(v)
 			case node.VariableNode:
-				v := a.area.Select(nv.Adr())
+				v := a.area.Select(nv.Object().Adr())
 				l.v[l.k[o.Adr()]].Set(v)
 			default:
 				halt.As(40, reflect.TypeOf(nv))

+ 162 - 7
rt2/scope/modern/val.go

@@ -124,6 +124,18 @@ func NewData(o object.Object) (ret scope.Variable) {
 	return ret
 }
 
+func fromg(x interface{}) scope.Value {
+	switch x := x.(type) {
+	case int32:
+		return INTEGER(x)
+	case bool:
+		return BOOLEAN(x)
+	default:
+		halt.As(100, reflect.TypeOf(x))
+	}
+	panic(100)
+}
+
 func NewConst(n node.Node) scope.Value {
 	switch x := n.(type) {
 	case node.ConstantNode:
@@ -161,6 +173,8 @@ func vfrom(v scope.Value) scope.Value {
 		switch n.link.Type() {
 		case object.INTEGER:
 			return n.val.(INTEGER)
+		case object.BYTE:
+			return n.val.(BYTE)
 		default:
 			halt.As(100, n.link.Type())
 		}
@@ -172,26 +186,167 @@ func vfrom(v scope.Value) scope.Value {
 	return nil
 }
 
+func gfrom(v scope.Value) interface{} {
+	switch n := v.(type) {
+	case *data:
+		return gfrom(n.val.(scope.Value))
+	case INTEGER:
+		return int32(n)
+	case BOOLEAN:
+		return bool(n)
+	default:
+		halt.As(100, reflect.TypeOf(n))
+	}
+	return nil
+}
+
 type ops struct{}
 
 func (o *ops) Sum(a, b scope.Value) scope.Value {
-	switch x := a.(type) {
+	switch a.(type) {
 	case *data:
 		return o.Sum(vfrom(a), b)
-	case INTEGER:
-		switch y := b.(type) {
+	default:
+		switch b.(type) {
 		case *data:
-			return o.Sum(x, vfrom(b))
-		case INTEGER:
-			return INTEGER(int32(x) + int32(y))
+			return o.Sum(a, vfrom(b))
+		default:
+			switch x := a.(type) {
+			case INTEGER:
+				switch y := b.(type) {
+				case INTEGER:
+					return INTEGER(int32(x) + int32(y))
+				default:
+					panic(fmt.Sprintln(reflect.TypeOf(y)))
+				}
+			default:
+				panic(fmt.Sprintln(reflect.TypeOf(x)))
+			}
 		}
+	}
+	panic(0)
+}
+
+func (o *ops) Sub(a, b scope.Value) scope.Value {
+	switch a.(type) {
+	case *data:
+		return o.Sub(vfrom(a), b)
 	default:
-		panic(fmt.Sprintln(reflect.TypeOf(x)))
+		switch b.(type) {
+		case *data:
+			return o.Sub(a, vfrom(b))
+		default:
+			switch x := a.(type) {
+			case INTEGER:
+				switch y := b.(type) {
+				case INTEGER:
+					return INTEGER(int32(x) - int32(y))
+				default:
+					panic(fmt.Sprintln(reflect.TypeOf(y)))
+				}
+			default:
+				panic(fmt.Sprintln(reflect.TypeOf(x)))
+			}
+		}
+	}
+	panic(0)
+
+}
+func (o *ops) Conv(a scope.Value, typ object.Type) scope.Value {
+	switch typ {
+	case object.INTEGER:
+		switch x := a.(type) {
+		case *data:
+			return o.Conv(vfrom(x), typ)
+		case BYTE:
+			return INTEGER(x)
+		default:
+			halt.As(100, reflect.TypeOf(x))
+		}
+	default:
+		halt.As(100, typ)
+	}
+	panic(100)
+}
+
+func (o *ops) Eq(a, b scope.Value) scope.Value {
+	switch a.(type) {
+	case *data:
+		return o.Eq(vfrom(a), b)
+	default:
+		switch b.(type) {
+		case *data:
+			return o.Eq(a, vfrom(b))
+		default:
+			switch x := a.(type) {
+			case INTEGER:
+				switch y := b.(type) {
+				case INTEGER:
+					return BOOLEAN(x == y)
+				default:
+					panic(fmt.Sprintln(reflect.TypeOf(y)))
+				}
+			default:
+				panic(fmt.Sprintln(reflect.TypeOf(x)))
+			}
+		}
+	}
+	panic(0)
+}
+
+func (o *ops) Lss(a, b scope.Value) scope.Value {
+	switch a.(type) {
+	case *data:
+		return o.Lss(vfrom(a), b)
+	default:
+		switch b.(type) {
+		case *data:
+			return o.Lss(a, vfrom(b))
+		default:
+			switch x := a.(type) {
+			case INTEGER:
+				switch y := b.(type) {
+				case INTEGER:
+					return BOOLEAN(x < y)
+				default:
+					panic(fmt.Sprintln(reflect.TypeOf(y)))
+				}
+			default:
+				panic(fmt.Sprintln(reflect.TypeOf(x)))
+			}
+		}
+	}
+	panic(0)
+}
+
+func (o *ops) Leq(a, b scope.Value) scope.Value {
+	switch a.(type) {
+	case *data:
+		return o.Leq(vfrom(a), b)
+	default:
+		switch b.(type) {
+		case *data:
+			return o.Leq(a, vfrom(b))
+		default:
+			switch x := a.(type) {
+			case INTEGER:
+				switch y := b.(type) {
+				case INTEGER:
+					return BOOLEAN(x <= y)
+				default:
+					panic(fmt.Sprintln(reflect.TypeOf(y)))
+				}
+			default:
+				panic(fmt.Sprintln(reflect.TypeOf(x)))
+			}
+		}
 	}
 	panic(0)
 }
 
 func init() {
 	scope.ValueFrom = vfrom
+	scope.GoTypeFrom = gfrom
+	scope.TypeFromGo = fromg
 	scope.Ops = &ops{}
 }

+ 2 - 1
rt2/utils.go

@@ -11,7 +11,8 @@ import (
 
 var utils nodeframe.NodeFrameUtils
 
-func DataOf(f frame.Frame) map[interface{}]interface{} { return utils.DataOf(f) }
+func DataOf(f frame.Frame) map[interface{}]interface{} { panic(100) }
+func RegOf(f frame.Frame) map[interface{}]interface{}  { return utils.DataOf(f) }
 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) }