Jelajahi Sumber

абстрагировал операции от типов данных

kpmy 10 tahun lalu
induk
melakukan
724f45aca2
6 mengubah file dengan 71 tambahan dan 137 penghapusan
  1. 11 25
      cp/node/node.go
  2. 0 1
      fw/fw.go
  3. 24 6
      rt/heap.go
  4. 28 0
      rt/op.go
  5. 0 1
      rt/proc.go
  6. 8 104
      rt/seq.go

+ 11 - 25
cp/node/node.go

@@ -46,37 +46,21 @@ type nodeFields struct {
 	obj               object.Object
 }
 
-func (nf *nodeFields) SetLeft(n Node) {
-	nf.left = n
-}
+func (nf *nodeFields) SetLeft(n Node) { nf.left = n }
 
-func (nf *nodeFields) SetRight(n Node) {
-	nf.right = n
-}
+func (nf *nodeFields) SetRight(n Node) { nf.right = n }
 
-func (nf *nodeFields) SetLink(n Node) {
-	nf.link = n
-}
+func (nf *nodeFields) SetLink(n Node) { nf.link = n }
 
-func (nf *nodeFields) SetObject(o object.Object) {
-	nf.obj = o
-}
+func (nf *nodeFields) SetObject(o object.Object) { nf.obj = o }
 
-func (nf *nodeFields) Left() Node {
-	return nf.left
-}
+func (nf *nodeFields) Left() Node { return nf.left }
 
-func (nf *nodeFields) Right() Node {
-	return nf.right
-}
+func (nf *nodeFields) Right() Node { return nf.right }
 
-func (nf *nodeFields) Link() Node {
-	return nf.link
-}
+func (nf *nodeFields) Link() Node { return nf.link }
 
-func (nf *nodeFields) Object() object.Object {
-	return nf.obj
-}
+func (nf *nodeFields) Object() object.Object { return nf.obj }
 
 type enterNode struct {
 	nodeFields
@@ -134,4 +118,6 @@ type variableNode struct {
 	nodeFields
 }
 
-func (v *variableNode) Self() VariableNode { return v }
+func (v *variableNode) Self() VariableNode {
+	return v
+}

+ 0 - 1
fw/fw.go

@@ -14,7 +14,6 @@ func main() {
 		p := rt.NewProcessor()
 		p.ConnectTo(ret)
 		for {
-			fmt.Print(".")
 			res, _ := p.Do()
 			if res != rt.OK {
 				break

+ 24 - 6
rt/heap.go

@@ -7,8 +7,25 @@ import (
 
 type INTEGER int
 
+type Variable interface {
+	Set(interface{})
+}
+
+func (i *INTEGER) Set(val interface{}) {
+	if val == nil {
+		panic("cannot be nil")
+	}
+	switch val.(type) {
+	case int:
+		*i = INTEGER(val.(int))
+	default:
+		panic("wrong type for INTEGER")
+	}
+	fmt.Println("set", int(*i))
+}
+
 type Heap interface {
-	ThisVariable(obj object.Object) interface{}
+	This(obj object.Object) Variable
 }
 
 type stdHeap struct {
@@ -24,18 +41,19 @@ func (h *stdHeap) Init() *stdHeap {
 	return h
 }
 
-func (h *stdHeap) ThisVariable(obj object.Object) (ptr interface{}) {
-	fmt.Println(obj)
-	ptr = h.inner[obj]
-	if ptr == nil {
+func (h *stdHeap) This(obj object.Object) (ptr Variable) {
+	p := h.inner[obj]
+	if p == nil {
 		switch obj.Type() {
 		case object.INTEGER:
-			ptr = new(int)
+			ptr = new(INTEGER)
 			h.inner[obj] = ptr
 		default:
 			fmt.Println(obj.Type())
 			panic("unknown object type")
 		}
+	} else {
+		ptr = p.(Variable)
 	}
 	return ptr
 }

+ 28 - 0
rt/op.go

@@ -0,0 +1,28 @@
+package rt
+
+import (
+	"fmt"
+	"reflect"
+)
+
+func intOf(x interface{}) (a int) {
+	fmt.Println(reflect.TypeOf(x))
+	switch x.(type) {
+	case *INTEGER:
+		z := *x.(*INTEGER)
+		a = int(z)
+	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
+}

+ 0 - 1
rt/proc.go

@@ -107,7 +107,6 @@ func (p *procImpl) ConnectTo(head node.Node) (err error) {
 
 func (p *procImpl) Do() (res Result, err error) {
 	if p.stack.Top() != nil {
-		fmt.Println(p.cycle)
 		p.cycle++
 		f := p.stack.Top()
 		//цикл дейкстры

+ 8 - 104
rt/seq.go

@@ -2,8 +2,7 @@ package rt
 
 import (
 	"cp/node"
-	"fmt"
-	"reflect"
+	"cp/object"
 )
 
 type assignSeq struct {
@@ -23,8 +22,8 @@ func (s *assignSeq) Do(f *frame) (ret Wait) {
 					case node.ConstantNode:
 						f.ret[f.ir.Right()] = f.ir.Right().(node.ConstantNode).Data()
 						s.step = func() Wait {
-							fmt.Println(reflect.TypeOf(f.ret[f.ir.Right()]))
-							fmt.Println("присвоение константы")
+							a := f.p.heap.This(f.ret[f.ir.Left()].(object.Object))
+							a.Set(f.ret[f.ir.Right()])
 							return STOP
 						}
 						return DO
@@ -32,8 +31,8 @@ func (s *assignSeq) Do(f *frame) (ret Wait) {
 						nf := NewFrame(f.p, f.ir.Right()).(*frame)
 						f.push(nf)
 						s.step = func() Wait {
-							fmt.Println(reflect.TypeOf(f.ret[f.ir.Right()]))
-							fmt.Println("присвоение результата операции")
+							a := f.p.heap.This(f.ret[f.ir.Left()].(object.Object))
+							a.Set(f.ret[f.ir.Right()])
 							return STOP
 						}
 						return SKIP
@@ -63,9 +62,7 @@ func (s *opSeq) Do(f *frame) (ret Wait) {
 		case node.PLUS:
 			a := f.ret[f.ir.Left()]
 			b := f.ret[f.ir.Right()]
-			f.ret[f.ir] = 0 //a + b
-			fmt.Println("сложение")
-			fmt.Println(reflect.TypeOf(a), reflect.TypeOf(b))
+			f.ret[f.ir] = Sum(a, b)
 			return STOP
 		default:
 			panic("unknown operation")
@@ -79,7 +76,7 @@ func (s *opSeq) Do(f *frame) (ret Wait) {
 			return DO
 		case node.VariableNode:
 			s.step = func() Wait {
-				f.ret[f.ir.Right()] = f.p.heap.ThisVariable(f.ir.Right().Object())
+				f.ret[f.ir.Right()] = f.p.heap.This(f.ir.Right().Object())
 				s.step = op
 				return DO
 			}
@@ -96,7 +93,7 @@ func (s *opSeq) Do(f *frame) (ret Wait) {
 			return DO
 		case node.VariableNode:
 			s.step = func() Wait {
-				f.ret[f.ir.Left()] = f.p.heap.ThisVariable(f.ir.Left().Object())
+				f.ret[f.ir.Left()] = f.p.heap.This(f.ir.Left().Object())
 				s.step = right
 				return DO
 			}
@@ -113,96 +110,3 @@ func (s *opSeq) Do(f *frame) (ret Wait) {
 	}
 	return ret
 }
-
-/*
-func (p *procImpl) doStat() {
-	f := p.stack.Top().(*frame)
-	fmt.Println(reflect.TypeOf(f.ir))
-	switch f.ir.(type) {
-	//assign works like .left := .right
-	case node.AssignNode:
-		if f.prologue != nil {
-			f.ret = f.ir.Link()
-			switch f.ir.Left().(type) {
-			case node.VariableNode: //nothing to do
-			default:
-				panic("left is not variable")
-			}
-			switch f.ir.Right().(type) {
-			case node.ConstantNode:
-				x := p.heap.ThisVariable(f.ir.Left().Object())
-				_ = x
-				*x.(*int) = f.ir.Right().(node.ConstantNode).Data().(int)
-				fmt.Println(x)
-				x = p.heap.ThisVariable(f.ir.Left().Object())
-				fmt.Println(*x.(*int))
-			case node.OperationNode:
-				nf := new(frame)
-				nf.ir = f.ir.Right()
-				nf.ret = f.ir
-				nf.prologue = prologue
-				nf.epilogue = node.New(node.CONSTANT)
-				f.epilogue = nf.epilogue
-				p.stack.Push(nf)
-			default:
-				panic("unknown right assign")
-			}
-			f.prologue = nil
-		} else if f.epilogue != nil {
-			switch f.epilogue.(type) {
-			case node.ConstantNode:
-				x := p.heap.ThisVariable(f.ir.Left().Object())
-				_ = x
-				*x.(*int) = f.epilogue.(node.ConstantNode).Data().(int)
-				fmt.Println(*x.(*int))
-			default:
-				fmt.Println("no custom epilogue")
-			}
-			p.stack.Pop()
-			if f.ret != nil {
-				nf := new(frame)
-				nf.ir = f.ret
-				nf.prologue = prologue
-				nf.epilogue = prologue
-				p.stack.Push(nf)
-			}
-
-		}
-	case node.OperationNode:
-		switch f.ir.(node.OperationNode).Operation() {
-		case node.PLUS:
-			var a, b int
-			switch f.ir.Left().(type) {
-			case node.ConstantNode:
-				a = f.ir.Left().(node.ConstantNode).Data().(int)
-			case node.VariableNode:
-				x := p.heap.ThisVariable(f.ir.Left().Object())
-				a = *x.(*int)
-			default:
-				panic("unknown left operand")
-			}
-			switch f.ir.Right().(type) {
-			case node.ConstantNode:
-				b = f.ir.Right().(node.ConstantNode).Data().(int)
-			case node.VariableNode:
-				x := p.heap.ThisVariable(f.ir.Right().Object())
-				b = *x.(*int)
-			default:
-				panic("unknown right operand")
-			}
-			switch f.epilogue.(type) {
-			case node.ConstantNode:
-				fmt.Println(a, b)
-				f.epilogue.(node.ConstantNode).SetData(a + b)
-				p.stack.Pop()
-			default:
-				panic("unknown epilogue")
-			}
-		default:
-			panic("unknown operation")
-		}
-	default:
-		panic("ooops")
-	}
-}
-*/