Browse Source

загрузка CASE

kpmy 10 years ago
parent
commit
53c8edd71c
5 changed files with 99 additions and 2 deletions
  1. 1 0
      .gitignore
  2. 3 0
      cp/constant/nodeclass.go
  3. 48 2
      cp/node/class.go
  4. 23 0
      cp/node/node.go
  5. 24 0
      xev/converter.go

+ 1 - 0
.gitignore

@@ -22,3 +22,4 @@ _testmain.go
 *.exe
 *.test
 *.prof
+*.wiki

+ 3 - 0
cp/constant/nodeclass.go

@@ -25,4 +25,7 @@ const (
 	TRAP
 	WITH
 	GUARD
+	CASE
+	ELSE
+	DO
 )

+ 48 - 2
cp/node/class.go

@@ -63,6 +63,12 @@ func New(class constant.Class) Node {
 		return new(withNode)
 	case constant.GUARD:
 		return new(guardNode)
+	case constant.CASE:
+		return new(caseNode)
+	case constant.ELSE:
+		return new(elseNode)
+	case constant.DO:
+		return new(doNode)
 	default:
 		panic("no such class")
 	}
@@ -99,8 +105,9 @@ func (e *enterNode) Enter() enter.Enter         { return e.enter }
 
 type constantNode struct {
 	nodeFields
-	typ  object.Type
-	data interface{}
+	typ      object.Type
+	data     interface{}
+	min, max int
 }
 
 func (c *constantNode) SetType(t object.Type) { c.typ = t }
@@ -111,6 +118,11 @@ func (c *constantNode) Data() interface{} { return c.data }
 
 func (c *constantNode) Type() object.Type { return c.typ }
 
+func (c *constantNode) SetMax(x int) { c.max = x }
+func (c *constantNode) Max() int     { return c.max }
+func (c *constantNode) SetMin(x int) { c.min = x }
+func (c *constantNode) Min() int     { return c.min }
+
 type dyadicNode struct {
 	nodeFields
 	operation operation.Operation
@@ -258,3 +270,37 @@ type guardNode struct {
 func (v *guardNode) self() GuardNode              { return v }
 func (v *guardNode) SetType(t object.ComplexType) { v.typ = t }
 func (v *guardNode) Type() object.ComplexType     { return v.typ }
+
+type caseNode struct {
+	nodeFields
+}
+
+func (v *caseNode) self() CaseNode { return v }
+
+type elseNode struct {
+	nodeFields
+	min, max int
+}
+
+func (v *elseNode) Min(x ...int) int {
+	if len(x) > 0 {
+		v.min = x[0]
+	}
+	return v.min
+}
+
+//func (c *elseNode) SetMax(x int) { c.max = x }
+func (c *elseNode) Max(x ...int) int {
+	if len(x) > 0 {
+		c.max = x[0]
+	}
+	return c.max
+}
+
+//func (c *elseNode) SetMin(x int) { c.min = x }
+
+type doNode struct {
+	nodeFields
+}
+
+func (v *doNode) self() DoNode { return v }

+ 23 - 0
cp/node/node.go

@@ -36,6 +36,10 @@ type ConstantNode interface {
 	SetData(data interface{})
 	Data() interface{}
 	Type() object.Type
+	Min() int
+	Max() int
+	SetMin(int)
+	SetMax(int)
 	Node
 }
 
@@ -145,3 +149,22 @@ type GuardNode interface {
 	Type() object.ComplexType
 	SetType(object.ComplexType)
 }
+
+type CaseNode interface {
+	self() CaseNode
+	Node
+}
+
+type ElseNode interface {
+	Node
+	Min(...int) int
+	Max(...int) int
+	//	SetMin(int)
+	//	SetMax(int)
+
+}
+
+type DoNode interface {
+	self() DoNode
+	Node
+}

+ 24 - 0
xev/converter.go

@@ -345,6 +345,15 @@ func (r *Result) buildNode(n *Node) (ret node.Node) {
 			ret = node.New(constant.CONSTANT)
 			convertData(n.Data.Nod.Typ, n.Data.Nod.Value, ret.(node.ConstantNode))
 			//fmt.Println(ret.(node.ConstantNode).Data())
+			x := ret.(node.ConstantNode)
+			if n.Data.Nod.Min != nil {
+				val, _ := strconv.Atoi(n.Data.Nod.Min.X)
+				x.SetMin(val)
+			}
+			if n.Data.Nod.Max != nil {
+				val, _ := strconv.Atoi(n.Data.Nod.Max.X)
+				x.SetMax(val)
+			}
 		case "assign":
 			ret = node.New(constant.ASSIGN)
 			switch n.Data.Nod.Statement {
@@ -410,6 +419,21 @@ func (r *Result) buildNode(n *Node) (ret node.Node) {
 					panic("error in node")
 				}
 			}
+		case "case":
+			ret = node.New(constant.CASE)
+		case "else":
+			ret = node.New(constant.ELSE)
+			x := ret.(node.ElseNode)
+			if n.Data.Nod.Min != nil {
+				val, _ := strconv.Atoi(n.Data.Nod.Min.X)
+				x.Min(val)
+			}
+			if n.Data.Nod.Max != nil {
+				val, _ := strconv.Atoi(n.Data.Nod.Max.X)
+				x.Max(val)
+			}
+		case "do":
+			ret = node.New(constant.DO)
 		default:
 			fmt.Println(n.Data.Nod.Class)
 			panic("no such node type")