kpmy 10 年之前
父節點
當前提交
055f8c35d3
共有 10 個文件被更改,包括 488 次插入0 次删除
  1. 13 0
      LICENSE
  2. 28 0
      cp/node/class.go
  3. 100 0
      cp/node/node.go
  4. 51 0
      cp/object/object.go
  5. 1 0
      cp/tree.go
  6. 2 0
      fw/PrivDemo1.oxf
  7. 11 0
      fw/fw.go
  8. 18 0
      xev/cmds.go
  9. 174 0
      xev/converter.go
  10. 90 0
      xev/loader.go

+ 13 - 0
LICENSE

@@ -0,0 +1,13 @@
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+                   Version 2, December 2004
+
+Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.

+ 28 - 0
cp/node/class.go

@@ -0,0 +1,28 @@
+package node
+
+import "cp/object"
+
+type Enter int
+
+const (
+	MODULE Enter = iota
+)
+
+type Operation int
+
+const (
+	PLUS Operation = iota
+)
+
+type EnterNode interface {
+	SetEnter(enter Enter)
+}
+
+type OperationNode interface {
+	SetOperation(op Operation)
+}
+
+type ConstantNode interface {
+	SetType(typ object.Type)
+	SetData(data interface{})
+}

+ 100 - 0
cp/node/node.go

@@ -0,0 +1,100 @@
+package node
+
+import (
+	"cp/object"
+)
+
+type Class int
+
+const (
+	ENTER Class = iota
+	ASSIGN
+	VARIABLE
+	DYADIC
+	CONSTANT
+)
+
+type Node interface {
+	SetLeft(n Node)
+	SetRight(n Node)
+	SetLink(n Node)
+	SetObject(o object.Object)
+}
+
+func New(class Class) Node {
+	switch class {
+	case ENTER:
+		return new(enterNode)
+	case ASSIGN:
+		return new(assignNode)
+	case VARIABLE:
+		return new(variableNode)
+	case DYADIC:
+		return new(dyadicNode)
+	case CONSTANT:
+		return new(constantNode)
+	default:
+		panic("no such class")
+	}
+}
+
+type nodeFields struct {
+	left, right, link Node
+	obj               object.Object
+}
+
+func (nf nodeFields) SetLeft(n Node) {
+	nf.left = n
+}
+
+func (nf nodeFields) SetRight(n Node) {
+	nf.right = n
+}
+
+func (nf nodeFields) SetLink(n Node) {
+	nf.link = n
+}
+
+func (nf nodeFields) SetObject(o object.Object) {
+	nf.obj = o
+}
+
+type enterNode struct {
+	nodeFields
+	enter Enter
+}
+
+func (e enterNode) SetEnter(enter Enter) {
+	e.enter = enter
+}
+
+type constantNode struct {
+	nodeFields
+	typ  object.Type
+	data interface{}
+}
+
+func (c constantNode) SetType(t object.Type) {
+	c.typ = t
+}
+
+func (c constantNode) SetData(data interface{}) {
+	c.data = data
+}
+
+type variableNode struct {
+	nodeFields
+}
+
+type dyadicNode struct {
+	nodeFields
+	operation Operation
+}
+
+func (d dyadicNode) SetOperation(op Operation) {
+	d.operation = op
+}
+
+type assignNode struct {
+	nodeFields
+}

+ 51 - 0
cp/object/object.go

@@ -0,0 +1,51 @@
+package object
+
+type Mode int
+type Type int
+
+const (
+	HEAD Mode = iota
+	VARIABLE
+)
+
+const (
+	NOTYPE Type = iota
+	INTEGER
+)
+
+type Object interface {
+	SetName(name string)
+	SetType(typ Type)
+}
+
+func New(mode Mode) Object {
+	switch mode {
+	case HEAD:
+		return new(headObject)
+	case VARIABLE:
+		return new(variableObject)
+	default:
+		panic("no such object mode")
+	}
+}
+
+type objectFields struct {
+	name string
+	typ  Type
+}
+
+func (of objectFields) SetType(typ Type) {
+	of.typ = typ
+}
+
+func (of objectFields) SetName(name string) {
+	of.name = name
+}
+
+type variableObject struct {
+	objectFields
+}
+
+type headObject struct {
+	objectFields
+}

+ 1 - 0
cp/tree.go

@@ -0,0 +1 @@
+package cp

+ 2 - 0
fw/PrivDemo1.oxf

@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<graphml xmlns:cpt="urn:bbcb:component:dev:cpt" xmlns="http://graphml.graphdrawing.org/xmlns"><graph edgedefault="undirected" cpt:scope="PrivDemo1"><node id="18710000"><data><cpt:object cpt:name="PrivDemo1" cpt:mode="head"/></data></node><node id="18756640"><data><cpt:object cpt:name="i" cpt:mode="variable" cpt:type="INTEGER"/></data></node><node id="18757472"><data><cpt:object cpt:name="j" cpt:mode="variable" cpt:type="INTEGER"/></data></node></graph><graph edgedefault="undirected" cpt:proc="PrivDemo1"><node id="17217136"><data><cpt:node cpt:class="enter" cpt:enter="module"/></data></node><edge source="17217136" target="17188400" cpt:link="right"/><node id="17188400"><data><cpt:node cpt:class="assign"/></data></node><edge source="17188400" target="17183360" cpt:link="left"/><edge source="17188400" target="17204144" cpt:link="link"/><edge source="17188400" target="17184288" cpt:link="right"/><node id="17183360"><data><cpt:node cpt:class="variable"/></data></node><edge source="17183360" target="18756640" cpt:link="object"/><node id="17184288"><data><cpt:node cpt:class="constant" cpt:type="INTEGER">1</cpt:node></data></node><node id="17204144"><data><cpt:node cpt:class="assign"/></data></node><edge source="17204144" target="17189536" cpt:link="left"/><edge source="17204144" target="17203424" cpt:link="right"/><node id="17189536"><data><cpt:node cpt:class="variable"/></data></node><edge source="17189536" target="18757472" cpt:link="object"/><node id="17203424"><data><cpt:node cpt:class="dyadic" cpt:operation="plus"/></data></node><edge source="17203424" target="17191104" cpt:link="left"/><edge source="17203424" target="17191984" cpt:link="right"/><node id="17191104"><data><cpt:node cpt:class="variable"/></data></node><edge source="17191104" target="18756640" cpt:link="object"/><node id="17191984"><data><cpt:node cpt:class="constant" cpt:type="INTEGER">2</cpt:node></data></node></graph></graphml>

+ 11 - 0
fw/fw.go

@@ -0,0 +1,11 @@
+package main
+
+import (
+	"os"
+	"xev"
+)
+
+func main() {
+	path, _ := os.Getwd()
+	xev.Load(path, "PrivDemo1.oxf")
+}

+ 18 - 0
xev/cmds.go

@@ -0,0 +1,18 @@
+package xev
+
+import (
+	"fmt"
+	"io/ioutil"
+	"path/filepath"
+)
+
+func Load(path, name string) {
+	fmt.Println(path + ` ` + name)
+	var data []byte
+	data, _ = ioutil.ReadFile(filepath.Join(path, name))
+	fmt.Println(len(data))
+	if data != nil {
+		result := LoadOXF(data)
+		DoAST(result)
+	}
+}

+ 174 - 0
xev/converter.go

@@ -0,0 +1,174 @@
+package xev
+
+import (
+	"cp/node"
+	"cp/object"
+	"fmt"
+	"strconv"
+)
+
+func (r *Result) findNode(id string) *Node {
+	var ret *Node
+	for i := 0; i < len(r.GraphList) && (ret == nil); i++ {
+		for j := 0; j < len(r.GraphList[i].NodeList) && (ret == nil); j++ {
+			if id == r.GraphList[i].NodeList[j].Id {
+				ret = &r.GraphList[i].NodeList[j]
+			}
+		}
+	}
+	return ret
+}
+
+func (r *Result) findLink(n *Node, link string) *Node {
+	target := ""
+	for i := range r.GraphList {
+		for j := range r.GraphList[i].EdgeList {
+			if (r.GraphList[i].EdgeList[j].Source == n.Id) && (r.GraphList[i].EdgeList[j].CptLink == link) {
+				target = r.GraphList[i].EdgeList[j].Target
+			}
+		}
+	}
+	var ret *Node
+	if target != "" {
+		ret = r.findNode(target)
+	}
+	return ret
+}
+
+var nodeMap map[string]node.Node
+var objectMap map[string]object.Object
+
+func (r *Result) buildObject(n *Node) object.Object {
+	if n == nil {
+		panic("n is nil")
+	}
+	var ret object.Object
+	ret = objectMap[n.Id]
+	if ret == nil {
+		switch n.Data.Obj.Mode {
+		case "head":
+			ret = object.New(object.HEAD)
+		case "variable":
+			ret = object.New(object.VARIABLE)
+		default:
+			panic("no such object mode")
+		}
+	}
+	if ret != nil {
+		objectMap[n.Id] = ret
+		ret.SetName(n.Data.Obj.Name)
+		switch n.Data.Obj.Typ {
+		case "":
+			ret.SetType(object.NOTYPE)
+		case "INTEGER":
+			ret.SetType(object.INTEGER)
+		default:
+			panic("no such object type")
+		}
+	}
+	return ret
+}
+
+func (r *Result) buildObjectList(list []Node) []object.Object {
+	if list == nil {
+		panic("list is nil")
+	}
+	ret := make([]object.Object, 0)
+	for i := range list {
+		obj := r.buildObject(&list[i])
+		if obj != nil {
+			ret = append(ret, obj)
+		}
+	}
+
+	return ret
+}
+
+func (r *Result) buildNode(n *Node) node.Node {
+	if n == nil {
+		panic("n is nil")
+	}
+	var ret node.Node
+	ret = nodeMap[n.Id]
+	if ret == nil {
+		switch n.Data.Nod.Class {
+		case "enter":
+			ret = node.New(node.ENTER)
+			switch n.Data.Nod.Enter {
+			case "module":
+				ret.(node.EnterNode).SetEnter(node.MODULE)
+			default:
+				panic("no such enter type")
+			}
+		case "variable":
+			ret = node.New(node.VARIABLE)
+		case "dyadic":
+			ret = node.New(node.DYADIC)
+			switch n.Data.Nod.Operation {
+			case "plus":
+				ret.(node.OperationNode).SetOperation(node.PLUS)
+			default:
+				panic("no such operation")
+			}
+		case "constant":
+			ret = node.New(node.CONSTANT)
+			switch n.Data.Nod.Typ {
+			case "INTEGER":
+				ret.(node.ConstantNode).SetType(object.INTEGER)
+				x, _ := strconv.Atoi(n.Data.Nod.Value)
+				ret.(node.ConstantNode).SetData(x)
+			default:
+				panic("no such constant type")
+			}
+		case "assign":
+			ret = node.New(node.ASSIGN)
+		default:
+			panic("no such node type")
+		}
+	}
+	if ret != nil {
+		nodeMap[n.Id] = ret
+		left := r.findLink(n, "left")
+		if left != nil {
+			ret.SetLeft(r.buildNode(left))
+		}
+		right := r.findLink(n, "right")
+		if right != nil {
+			ret.SetRight(r.buildNode(right))
+		}
+		link := r.findLink(n, "link")
+		if link != nil {
+			ret.SetLink(r.buildNode(link))
+		}
+		object := r.findLink(n, "object")
+		if object != nil {
+			ret.SetObject(r.buildObject(object))
+		}
+	}
+	return ret
+}
+
+func buildMod(r *Result) (node.Node, []object.Object) {
+	var root node.Node
+	var list []object.Object
+	for i := range r.GraphList {
+		if r.GraphList[i].CptScope != "" {
+			list = r.buildObjectList(r.GraphList[i].NodeList)
+		}
+		if r.GraphList[i].CptProc != "" {
+			for j := range r.GraphList[i].NodeList {
+				root = r.buildNode(&r.GraphList[i].NodeList[j])
+			}
+		}
+	}
+	return root, list
+}
+
+func DoAST(r *Result) {
+	nodeMap = make(map[string]node.Node)
+	objectMap = make(map[string]object.Object)
+	_, _ = buildMod(r)
+	fmt.Println(len(nodeMap), len(objectMap))
+	nodeMap = nil
+	objectMap = nil
+}

+ 90 - 0
xev/loader.go

@@ -0,0 +1,90 @@
+package xev
+
+import "encoding/xml"
+
+type CptObject struct {
+	Name string `xml:"urn:bbcb:component:dev:cpt name,attr"`
+	Mode string `xml:"urn:bbcb:component:dev:cpt mode,attr"`
+	Typ  string `xml:"urn:bbcb:component:dev:cpt type,attr"`
+}
+
+type CptNode struct {
+	Class string `xml:"urn:bbcb:component:dev:cpt class,attr"`
+	//опциональные параметры
+	Typ       string `xml:"urn:bbcb:component:dev:cpt type,attr"`
+	Enter     string `xml:"urn:bbcb:component:dev:cpt enter,attr"`
+	Operation string `xml:"urn:bbcb:component:dev:cpt operation,attr"`
+	Value     string `xml:",chardata"`
+}
+
+type NodeData struct {
+	Obj *CptObject `xml:"urn:bbcb:component:dev:cpt object"`
+	Nod *CptNode   `xml:"urn:bbcb:component:dev:cpt node"`
+}
+
+type Node struct {
+	Id   string    `xml:"id,attr"`
+	Data *NodeData `xml:"data"`
+}
+
+type Edge struct {
+	Target  string `xml:"target,attr"`
+	Source  string `xml:"source,attr"`
+	CptLink string `xml:"urn:bbcb:component:dev:cpt link,attr"`
+}
+
+type GraphData struct {
+	CptScope string `xml:"urn:bbcb:component:dev:cpt scope,attr"`
+	CptProc  string `xml:"urn:bbcb:component:dev:cpt proc,attr"`
+}
+
+type Graph struct {
+	NodeList []Node `xml:"node"`
+	EdgeList []Edge `xml:"edge"`
+	GraphData
+}
+
+type Result struct {
+	GraphList []Graph `xml:"graph"`
+}
+
+/*
+func traverseNode(n *Node) {
+	fmt.Println(n.Id, n.Data)
+}
+
+func traverseEdge(e *Edge) {
+	fmt.Println(e.Source, e.Target, e.CptLink)
+}
+
+func traverseGraph(g *Graph) {
+	fmt.Println("scope", g.CptScope)
+	fmt.Println("proc", g.CptProc)
+	for n := range g.NodeList {
+		fmt.Println("node", n)
+		traverseNode(&g.NodeList[n])
+	}
+	for e := range g.EdgeList {
+		fmt.Println("edge", e)
+		traverseEdge(&g.EdgeList[e])
+	}
+}
+
+func traverse(r *Result) {
+	for g := range r.GraphList {
+		fmt.Println("graph", g)
+		traverseGraph(&r.GraphList[g])
+	}
+}
+*/
+func LoadOXF(data []byte) *Result {
+	r := new(Result)
+	err := xml.Unmarshal(data, r)
+	if err == nil {
+		//fmt.Println(len(r.GraphList))
+		//traverse(r)
+	} else {
+		panic("xml parse error")
+	}
+	return r
+}