Bläddra i källkod

реализованы операции INCL, EXCL, {...} и пр.

kpmy 10 år sedan
förälder
incheckning
1b7272b655

+ 1 - 0
cp/constant/nodeclass.go

@@ -28,4 +28,5 @@ const (
 	CASE
 	ELSE
 	DO
+	RANGE
 )

+ 43 - 0
cp/constant/operation/operation.go

@@ -18,4 +18,47 @@ const (
 	ODD
 	CAP
 	BITS
+	MIN
+	MAX
 )
+
+func (o Operation) String() string {
+	switch o {
+	case PLUS:
+		return "+"
+	case MINUS:
+		return "-"
+	case CONVERT:
+		return "CONVERT"
+	case EQUAL:
+		return "="
+	case LESSER:
+		return "<"
+	case LESS_EQUAL:
+		return "<="
+	case LEN:
+		return "LEN"
+	case NOT:
+		return "~"
+	case NOT_EQUAL:
+		return "#"
+	case IS:
+		return "IS"
+	case GREATER:
+		return ">"
+	case ABS:
+		return "ABS"
+	case ODD:
+		return "ODD"
+	case CAP:
+		return "CAP"
+	case BITS:
+		return "BITS"
+	case MIN:
+		return "MIN"
+	case MAX:
+		return "MAX"
+	default:
+		return "?"
+	}
+}

+ 23 - 0
cp/constant/statement/statement.go

@@ -1,9 +1,32 @@
 package statement
 
+import (
+	"strconv"
+)
+
 type Statement int
 
 const (
 	ASSIGN Statement = iota
 	INC
 	DEC
+	INCL
+	EXCL
 )
+
+func (s Statement) String() string {
+	switch s {
+	case ASSIGN:
+		return ":="
+	case INC:
+		return "INC"
+	case DEC:
+		return "DEC"
+	case INCL:
+		return "INCL"
+	case EXCL:
+		return "EXCL"
+	default:
+		return strconv.Itoa(int(s))
+	}
+}

+ 8 - 0
cp/node/class.go

@@ -69,6 +69,8 @@ func New(class constant.Class) Node {
 		return new(elseNode)
 	case constant.DO:
 		return new(doNode)
+	case constant.RANGE:
+		return new(rangeNode)
 	default:
 		panic("no such class")
 	}
@@ -304,3 +306,9 @@ type doNode struct {
 }
 
 func (v *doNode) self() DoNode { return v }
+
+type rangeNode struct {
+	nodeFields
+}
+
+func (v *rangeNode) self() RangeNode { return v }

+ 5 - 0
cp/node/node.go

@@ -168,3 +168,8 @@ type DoNode interface {
 	self() DoNode
 	Node
 }
+
+type RangeNode interface {
+	self() RangeNode
+	Node
+}

+ 1 - 1
fw.go

@@ -26,7 +26,7 @@ func close() {
 func main() {
 	flag.Parse()
 	if name == "" {
-		name = "XevDemo15"
+		name = "XevDemo16"
 	}
 	global := new(stdDomain)
 	modList := module.New()

+ 2 - 2
rt2/rules/assign.go

@@ -147,14 +147,14 @@ func assignSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			fmt.Println(reflect.TypeOf(a.Left()))
 			panic("wrong left")
 		}
-	case statement.INC:
+	case statement.INC, statement.INCL:
 		switch a.Left().(type) {
 		case node.VariableNode, node.ParameterNode:
 			seq, ret = incSeq(f)
 		default:
 			panic(fmt.Sprintln("wrong left", reflect.TypeOf(a.Left())))
 		}
-	case statement.DEC:
+	case statement.DEC, statement.EXCL:
 		switch a.Left().(type) {
 		case node.VariableNode, node.ParameterNode:
 			seq, ret = decSeq(f)

+ 26 - 4
rt2/rules/op.go

@@ -46,6 +46,22 @@ func int32Of(x interface{}) (a int32) {
 	return a
 }
 
+func min(_a interface{}, _b interface{}) interface{} {
+	assert.For(_a != nil, 20)
+	assert.For(_b != nil, 21)
+	var a int32 = int32Of(_a)
+	var b int32 = int32Of(_b)
+	return int32(math.Min(float64(a), float64(b)))
+}
+
+func max(_a interface{}, _b interface{}) interface{} {
+	assert.For(_a != nil, 20)
+	assert.For(_b != nil, 21)
+	var a int32 = int32Of(_a)
+	var b int32 = int32Of(_b)
+	return int32(math.Max(float64(a), float64(b)))
+}
+
 func sum(_a interface{}, _b interface{}) interface{} {
 	assert.For(_a != nil, 20)
 	assert.For(_b != nil, 21)
@@ -303,8 +319,8 @@ func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 	var fu nodeframe.FrameUtils
 
 	op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
-		n := fu.NodeOf(f)
-		switch n.(node.OperationNode).Operation() {
+		n := fu.NodeOf(f).(node.OperationNode)
+		switch n.Operation() {
 		case operation.PLUS:
 			fu.DataOf(f.Parent())[n] = sum(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
 			return frame.End()
@@ -329,8 +345,14 @@ func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 		case operation.GREATER:
 			fu.DataOf(f.Parent())[n] = gtr(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
 			return frame.End()
+		case operation.MAX:
+			fu.DataOf(f.Parent())[n] = max(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
+			return frame.End()
+		case operation.MIN:
+			fu.DataOf(f.Parent())[n] = min(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
+			return frame.End()
 		default:
-			panic("unknown operation")
+			panic(fmt.Sprintln("unknown operation", n.(node.OperationNode).Operation()))
 		}
 	}
 
@@ -385,7 +407,7 @@ func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 			}
 			ret = frame.NOW
 			return seq, ret
-		case node.OperationNode, node.DerefNode:
+		case node.OperationNode, node.DerefNode, node.RangeNode:
 			fu.Push(fu.New(n.Left()), f)
 			seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
 				return right, frame.NOW

+ 30 - 0
rt2/rules/range.go

@@ -0,0 +1,30 @@
+package rules
+
+import (
+	"math/big"
+)
+
+import (
+	"fw/rt2"
+	"fw/rt2/frame"
+)
+
+func bit_range(_f interface{}, _t interface{}) interface{} {
+	f := int32Of(_f)
+	t := int32Of(_t)
+	ret := big.NewInt(0)
+	for i := f; i <= t; i++ {
+		ret = ret.SetBit(ret, int(i), 1)
+	}
+	return ret
+}
+
+func rangeSeq(f frame.Frame) (frame.Sequence, frame.WAIT) {
+	n := rt2.NodeOf(f)
+	return expectExpr(f, n.Left(), func(f frame.Frame) (frame.Sequence, frame.WAIT) {
+		return expectExpr(f, n.Right(), func(f frame.Frame) (frame.Sequence, frame.WAIT) {
+			rt2.DataOf(f.Parent())[n] = bit_range(rt2.DataOf(f)[n.Left()], rt2.DataOf(f)[n.Right()])
+			return frame.End()
+		})
+	})
+}

+ 3 - 1
rt2/rules/table.go

@@ -67,6 +67,8 @@ func prologue(n node.Node) frame.Sequence {
 		return guardSeq
 	case node.CaseNode:
 		return caseSeq
+	case node.RangeNode:
+		return rangeSeq
 	default:
 		panic(fmt.Sprintln("unknown node", reflect.TypeOf(n)))
 	}
@@ -93,7 +95,7 @@ func epilogue(n node.Node) frame.Sequence {
 			return frame.End()
 		}
 	case node.OperationNode, node.ReturnNode, node.IfNode, node.LoopNode,
-		node.DerefNode, node.IndexNode, node.TrapNode, node.GuardNode:
+		node.DerefNode, node.IndexNode, node.TrapNode, node.GuardNode, node.RangeNode:
 		return nil
 	default:
 		fmt.Println(reflect.TypeOf(n))

+ 11 - 1
xev/converter.go

@@ -340,6 +340,10 @@ func (r *Result) buildNode(n *Node) (ret node.Node) {
 				ret.(node.OperationNode).SetOperation(operation.NOT_EQUAL)
 			case ">":
 				ret.(node.OperationNode).SetOperation(operation.GREATER)
+			case "max":
+				ret.(node.OperationNode).SetOperation(operation.MAX)
+			case "min":
+				ret.(node.OperationNode).SetOperation(operation.MIN)
 			default:
 				panic(fmt.Sprintln("no such operation", n.Data.Nod.Operation))
 			}
@@ -365,8 +369,12 @@ func (r *Result) buildNode(n *Node) (ret node.Node) {
 				ret.(node.AssignNode).SetStatement(statement.INC)
 			case "dec":
 				ret.(node.AssignNode).SetStatement(statement.DEC)
+			case "incl":
+				ret.(node.AssignNode).SetStatement(statement.INCL)
+			case "excl":
+				ret.(node.AssignNode).SetStatement(statement.EXCL)
 			default:
-				panic("unknown assign statement")
+				panic(fmt.Sprintln("unknown assign statement", n.Data.Nod.Statement))
 			}
 		case "call":
 			ret = node.New(constant.CALL)
@@ -446,6 +454,8 @@ func (r *Result) buildNode(n *Node) (ret node.Node) {
 			}
 		case "do":
 			ret = node.New(constant.DO)
+		case "range":
+			ret = node.New(constant.RANGE)
 		default:
 			fmt.Println(n.Data.Nod.Class)
 			panic("no such node type")