Esempio n. 1
0
File: model.go Progetto: kpmy/ot
func RootOf(o Object) Object {
	assert.For(!fn.IsNil(o), 20)
	if fn.IsNil(o.Parent()) {
		return o
	} else {
		return RootOf(o.Parent())
	}
}
Esempio n. 2
0
File: g.go Progetto: kpmy/ot
func (f *fm) Write(o otm.Object) {
	if !fn.IsNil(o) {
		f.object(o)
	} else {
		f.Trinary(tri.NIL)
	}
}
Esempio n. 3
0
File: target.go Progetto: kpmy/ot
func (t *target) emit(_s ir.Statement) {
	assert.For(!fn.IsNil(_s), 20)
	switch s := _s.(type) {
	default:
		t.tpl.Stmt = append(t.tpl.Stmt, s)
	}
}
Esempio n. 4
0
File: obj.go Progetto: kpmy/lomo
func (d *direct) init(v *ir.Variable, ctrl chan bool, def ...interface{}) {
	d.c = make(chan interface{})
	d.s = make(chan interface{})
	d.v = v
	d.ctrl = ctrl
	go func() {
		d.ctrl <- true
		var x interface{}
		for stop := false; !stop; {
			if fn.IsNil(x) {
				select {
				case x = <-d.s:
					log.Println("rd", x)
				case stop = <-d.ctrl:
				}
			} else {
				select {
				case d.c <- x:
					log.Println("wr", x)
				case stop = <-d.ctrl:
				}
			}
		}
		d.ctrl <- true
	}()
}
Esempio n. 5
0
func prettyPrintObject(o otm.Object, log *testing.T) {
	parent := ""
	for x := o.Parent(); !fn.IsNil(x); x = x.Parent() {
		parent = fmt.Sprint(x.Qualident(), "<-", parent)
	}
	log.Log(parent, o.Qualident())
	if o.ChildrenCount() > 0 {
		log.Log(":")
		for _x := range o.Children() {
			switch x := _x.(type) {
			case otm.Object:
				prettyPrintObject(x, log)
			case otm.Link:
				log.Log("@", x.Object().Qualident())
			case string, float64, int64, rune, tri.Trit:
				log.Log(_x)
			case []uint8:
				s := zbase32.EncodeToString(x)
				log.Log("zbase32(", s, ")", x)
			default:
				halt.As(100, reflect.TypeOf(x))
			}
		}
		log.Log(";")
	}
}
Esempio n. 6
0
File: i2m.go Progetto: kpmy/ot
func (l *futureLink) Object() otm.Object {
	if l.o == nil {
		assert.For(!fn.IsNil(l.up), 20)
		l.o = otm.RootOf(l.up).FindById(l.to).(*object)
		assert.For(l.o != nil, 60, "object not found ", l.to)
	}
	return l.o
}
Esempio n. 7
0
File: build.go Progetto: kpmy/ot
func (b *builder) Child(prod otm.Producer, mod ...otm.Modifier) otm.Builder {
	assert.For(b.root != nil, 20)
	n := prod(mod...).(*object)
	if n.id != "" {
		old := b.root.FindById(n.id)
		assert.For(fn.IsNil(old), 40, "non unique id")
	}
	b.root.vl = append(b.root.vl, n)
	n.up = b.root
	return b
}
Esempio n. 8
0
File: imp.go Progetto: kpmy/ot
func Resolve(o otm.Object) (err error) {
	assert.For(!fn.IsNil(o), 20)

	switch tpl := o.Qualident().Template; tpl {
	case Core.TemplateName:
		err = resolve(Core, o)
	default:
		err = errors.New("nothing to resolve")
	}
	return
}
Esempio n. 9
0
File: imp.go Progetto: kpmy/ot
func ResolveContext(o otm.Object, resolver ResolverFunc, data map[string]interface{}) (err error) {
	assert.For(!fn.IsNil(o), 20)

	switch tpl := o.Qualident().Template; tpl {
	case Core.TemplateName:
		err = resolveContext(o.(*object), resolver, data)
	default:
		err = errors.New("nothing to resolve")
	}
	return
}
Esempio n. 10
0
File: imp.go Progetto: kpmy/ot
func initCore() {
	Core = &ForeignTemplate{TemplateName: "core"}
	Core.Classes = make(map[string]*ForeignClass)
	Core.Classes["template"] = &ForeignClass{
		Template: Core,
		Class:    "template",
		Applicator: func(c *ForeignClass, o otm.Object) (_ func(*ForeignClass, otm.Object) error, err error) {
			c.Entity = &TemplateEntity{}
			x := otm.RootOf(o)
			if x != o {
				err = errors.New("core.template must be root")
			}
			return
		}}
	Core.Classes["import"] = &ForeignClass{
		Template: Core,
		Class:    "import",
		Applicator: func(c *ForeignClass, o otm.Object) (post func(*ForeignClass, otm.Object) error, err error) {
			e := &ImportEntity{Imports: make(map[otm.Qualident]otm.Object), Ref: o}
			c.Entity = e
			for imp := range o.ChildrenObjects() {
				e.Imports[imp.Qualident()] = imp
			}
			for k, _ := range e.Imports {
				if k.Template != "" || k.Identifier != "" || k.Class == Core.TemplateName {
					err = errors.New(fmt.Sprintln("cannot import ", k))
				}
			}
			post = func(c *ForeignClass, o otm.Object) (err error) {
				e := c.Entity.(*ImportEntity)
				for k, _ := range e.Imports {
					if t, ok := tm[k.Class]; ok {
						if p := o.Parent(); !fn.IsNil(p) {
							resolve(t, p)
						} else {
							halt.As(100, "cannot apply import to nil")
						}
					} else {
						err = errors.New(fmt.Sprintln("unknown template ", k))
					}
				}
				return
			}
			return
		}}
}
Esempio n. 11
0
File: proc.go Progetto: kpmy/gone
func Transfer(l *model.Layer) {
	assert.For(l != nil && l.Next != nil, 20)
	wg := new(sync.WaitGroup)
	for _, n := range l.Next.Nodes {
		wg.Add(1)
		go func(n *model.Node) {
			tmp := make(map[model.Link]interface{})
			for k, _ := range n.In {
				v := l.Nodes[k.NodeId].Out[k.LinkId]
				if fn.IsNil(v) {
					halt.As(100, k)
				}
				tmp[k] = v
			}
			n.In = tmp
			wg.Done()
		}(n)
	}
	wg.Wait()
}
Esempio n. 12
0
File: obj.go Progetto: kpmy/lomo
func (m *mem) init(v *ir.Variable, ctrl chan bool, def ...interface{}) {
	m.c = make(chan interface{})
	m.s = make(chan interface{})
	m.v = v
	m.ctrl = ctrl
	go func() {
		x := <-m.s
		m.ctrl <- true
		for stop := false; !stop; {
			select {
			case n := <-m.s:
				m.f = n
			case m.c <- x:
			case stop = <-m.ctrl:
			}
		}
		m.ctrl <- true
	}()
	if len(def) != 0 && !fn.IsNil(def[0]) {
		m.s <- def[0]
	} else {
		m.s <- m.defaults(v).val
	}
}
Esempio n. 13
0
func (i *Iq) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
	i.Id = getAttr(&start, "id")
	i.Type = IqType(getAttr(&start, "type"))
	var _t xml.Token
	for stop := false; !stop && err == nil; {
		_t, err = d.Token()
		switch t := _t.(type) {
		case xml.StartElement:
			if fact, ok := us[t.Name]; ok {
				i.Inner = fact()
			}
			if !fn.IsNil(i.Inner) {
				d.DecodeElement(i.Inner, &t)
			} else {
				//halt.As(100, t.Name)
			}
		case xml.EndElement:
			stop = t.Name == start.Name
		default:
			//halt.As(100, reflect.TypeOf(t))
		}
	}
	return
}
Esempio n. 14
0
File: imp.go Progetto: kpmy/ot
func resolve(t *ForeignTemplate, o otm.Object) (err error) {
	assert.For(!fn.IsNil(o), 20)
	var processList []pfunc

	var upd func(t *ForeignTemplate, o otm.Object)
	upd = func(t *ForeignTemplate, o otm.Object) {
		if clazz, ok := t.Classes[o.Qualident().Class]; ok && (o.Qualident().Template == t.TemplateName || o.Qualident().Template == "") {
			inst := clazz.copyOf()
			o.InstanceOf(inst)
			if fn := inst.apply(o); fn != nil {
				processList = append(processList, fn)
			}
			//log.Println("class updated for", o.Qualident(), " set ", clazz.Qualident())
		}
		for x := range o.ChildrenObjects() {
			upd(t, x)
		}
	}

	upd(t, o)

	for tmp := processList; len(tmp) > 0; {
		var _tmp []pfunc
		for _, f := range tmp {
			var p pfunc
			if p, err = f(); err == nil && p != nil {
				_tmp = append(_tmp, p)
			} else if err != nil {
				_tmp = nil
				break
			}
		}
		tmp = _tmp
	}
	return
}
Esempio n. 15
0
File: do2.go Progetto: kpmy/lomo
func (u *Unit) expr(e ir.Expression) *value {
	stack := &exprStack{}
	stack.init()
	var expr func(ir.Expression)
	expr = func(_e ir.Expression) {
		switch e := _e.(type) {
		case ir.WrappedExpression:
			expr(e.Process())
		case *ir.ConstExpr:
			stack.push(cval(e))
		case *ir.AtomExpr:
			stack.push(&value{typ: types.ATOM, val: Atom(e.Value)})
		case *ir.SelectExpr:
			if e.Var != nil {
				e.Var = u.code.Variables[e.Var.Name]
				var o object
				if e.Foreign == nil {
					o = u.objects[e.Var.Name]
				} else {
					if imp := u.imps[imp(e.Var)]; imp != nil {
						o = imp.objects[e.Foreign.Name]
					}
				}
				stack.push(get(o))
			} else if e.Const != nil {
				if c := u.code.Const[e.Const.Name]; c != nil {
					expr(c.Expr)
				} else {
					halt.As(100, "wrong constant name", e.Const.Name)
				}
			} else {
				halt.As(100)
			}
			if e.Inner != mods.NONE {
				base := stack.pop()
				switch {
				case e.Inner == mods.LIST && len(e.ExprList) == 1: //single item
					expr(e.ExprList[0])
					_i := stack.pop()

					switch base.typ {
					case types.STRING:
						idx := int(_i.toInt().Int64())
						s := []rune(base.toStr())
						assert.For(idx >= 0 && idx < len(s), 40)
						stack.push(&value{typ: types.CHAR, val: s[idx]})
					case types.MAP:
						idx := ThisAny(_i)
						m := base.asMap()
						data := m.Get(idx)
						stack.push(&value{typ: types.ANY, val: data})
					case types.LIST:
						idx := int(_i.toInt().Int64())
						l := base.asList()
						data := l.Get(idx)
						stack.push(&value{typ: types.ANY, val: data})
					default:
						halt.As(100, "not indexable ", base.typ)
					}
				case e.Inner == mods.LIST && len(e.ExprList) > 1: //some items
					switch base.typ {
					case types.STRING:
						s := []rune(base.toStr())
						var ret []rune

						for _, _e := range e.ExprList {
							expr(_e)
							_i := stack.pop()
							i := int(_i.toInt().Int64())
							assert.For(i >= 0 && i < len(s), 40)
							ret = append(ret, s[i])
						}
						stack.push(&value{typ: types.STRING, val: string(ret)})
					default:
						halt.As(100, "not indexable ")
					}
				case e.Inner == mods.RANGE && len(e.ExprList) == 2: //range min (from, to) .. max(from, to) with reverse
					expr(e.ExprList[0])
					_f := stack.pop()
					expr(e.ExprList[1])
					_t := stack.pop()
					from := _f.toInt().Int64()
					to := _t.toInt().Int64()

					if int64(math.Max(float64(from), float64(to))) == to { //forward
						switch base.typ {
						case types.STRING:
							s := []rune(base.toStr())
							var ret []rune
							for i := int(from); i <= int(to); i++ {
								assert.For(i >= 0 && i < len(s), 40)
								ret = append(ret, s[i])
							}
							stack.push(&value{typ: types.STRING, val: string(ret)})
						default:
							halt.As(100, "not indexable ", base.typ)
						}
					} else {
						switch base.typ {
						case types.STRING:
							s := []rune(base.toStr())
							var ret []rune
							for i := int(to); i >= int(from); i-- {
								assert.For(i >= 0 && i < len(s), 40)
								ret = append(ret, s[i])
							}
							stack.push(&value{typ: types.STRING, val: string(ret)})
						default:
							halt.As(100, "not indexable ", base.typ)
						}
					}
				case e.Inner == mods.RANGE && len(e.ExprList) == 1: //open range from `from` to the end of smth
					expr(e.ExprList[0])
					_i := stack.pop()
					idx := int(_i.toInt().Int64())

					switch base.typ {
					case types.STRING:
						s := []rune(base.toStr())
						var ret []rune
						for i := int(idx); i < len(s); i++ {
							assert.For(i >= 0 && i < len(s), 40)
							ret = append(ret, s[i])
						}
						stack.push(&value{typ: types.STRING, val: string(ret)})
					default:
						halt.As(100, "not indexable ")
					}
				case e.Inner == mods.DEREF:
					switch base.typ {
					case types.ANY:
						a := base.toAny()
						if !fn.IsNil(a.x) {
							stack.push(&value{typ: a.typ, val: a.x})
						} else {
							halt.As(100, "undef dereference read")
						}
					default:
						halt.As(101, "not a ref ", base.typ)
					}
				default:
					halt.As(100, "unexpected selector ", base, e.Inner)
				}
			}
		case *ir.Monadic:
			expr(e.Expr)
			v := stack.pop()
			switch e.Op {
			case ops.Neg:
				switch v.typ {
				case types.INTEGER:
					i := v.toInt()
					i = i.Neg(i)
					v = &value{typ: v.typ, val: ThisInt(i)}
				case types.REAL:
					i := v.toReal()
					i = i.Neg(i)
					v = &value{typ: v.typ, val: ThisRat(i)}
				default:
					halt.As(100, v.typ)
				}
			case ops.Not:
				switch v.typ {
				case types.BOOLEAN:
					b := v.toBool()
					v = &value{typ: v.typ, val: !b}
				case types.TRILEAN:
					t := v.toTril()
					v = &value{typ: v.typ, val: tri.Not(t)}
				case types.SET:
					s := v.toSet()
					ns := ThisSet(s)
					ns.inv = !ns.inv
					v = &value{typ: v.typ, val: ns}
				default:
					halt.As(100, "unexpected logical type")
				}
			case ops.Im:
				switch v.typ {
				case types.INTEGER:
					i := v.toInt()
					im := big.NewRat(0, 1)
					im.SetInt(i)
					re := big.NewRat(0, 1)
					c := &Cmp{}
					c.re = re
					c.im = im
					v = &value{typ: types.COMPLEX, val: c}
				case types.REAL:
					im := v.toReal()
					re := big.NewRat(0, 1)
					c := &Cmp{}
					c.re = re
					c.im = im
					v = &value{typ: types.COMPLEX, val: c}
				default:
					halt.As(100, "unexpected operand type ", v.typ)
				}
			default:
				halt.As(100, e.Op)
			}
			stack.push(v)
		case *ir.Dyadic:
			var l, r *value
			if !(e.Op == ops.Or || e.Op == ops.And) {
				expr(e.Left)
				l = stack.pop()
				if e.Op == ops.In {
					l = &value{typ: types.ANY, val: ThisAny(l)}
				}
				expr(e.Right)
				r = stack.pop()
				v := calcDyadic(l, e.Op, r)
				stack.push(v)
			} else {
				expr(e.Left)
				l = stack.pop()
				switch e.Op {
				case ops.And:
					switch l.typ {
					case types.BOOLEAN:
						lb := l.toBool()
						if lb {
							expr(e.Right)
							r = stack.pop()
							rb := r.toBool()
							lb = lb && rb
						}
						stack.push(&value{typ: l.typ, val: lb})
					case types.TRILEAN:
						lt := l.toTril()
						if !tri.False(lt) {
							expr(e.Right)
							r = stack.pop()
							rt := r.toTril()
							lt = tri.And(lt, rt)
						}
						stack.push(&value{typ: l.typ, val: lt})
					default:
						halt.As(100, "unexpected logical type")
					}
				case ops.Or:
					switch l.typ {
					case types.BOOLEAN:
						lb := l.toBool()
						if !lb {
							expr(e.Right)
							r = stack.pop()
							rb := r.toBool()
							lb = lb || rb
						}
						stack.push(&value{typ: l.typ, val: lb})
					case types.TRILEAN:
						lt := l.toTril()
						if !tri.True(lt) {
							expr(e.Right)
							r = stack.pop()
							rt := r.toTril()
							lt = tri.Or(lt, rt)
						}
						stack.push(&value{typ: l.typ, val: lt})
					default:
						halt.As(100, "unexpected logical type")
					}
				default:
					halt.As(100, "unknown dyadic op ", e.Op)
				}
			}
		case *ir.Ternary:
			expr(e.If)
			c := stack.pop()
			if c.toBool() {
				expr(e.Then)
			} else {
				expr(e.Else)
			}
		case *ir.InfixExpr:
			var args []*value
			for _, a := range e.Args {
				expr(a)
				args = append(args, stack.pop())
			}
			cm := Init(e.Unit.Name(), u.loader)
			c := make(chan value, 1)
			Do(cm, func(cm Cluster) {
				inf := cm[e.Unit.Name()]
				assert.For(inf != nil, 40)
				for i := len(args) - 1; i >= 0; i-- {
					go func(i int, v *value) {
						o := inf.objects[inf.code.Infix[i].Name]
						set(o, v)
					}(i+1, args[i])
				}
				go func(c chan value) {
					o := inf.objects[inf.code.Infix[0].Name]
					v := get(o)
					log.Println("infix", v)
					c <- *v
				}(c)
			}).Wait()
			v := <-c
			Close(cm).Wait()
			stack.push(&v)
		case *ir.TypeTest:
			expr(e.Operand)
			v := stack.pop()
			var a *Any
			switch v.typ {
			case types.ANY:
				a = v.toAny()
			/*case types.PTR:
			p := v.toPtr()
			if p.adr != 0 {
				a = p.link.Get()
			} else {
				a = &Any{}
			} */
			default:
				halt.As(100, "unsupported ")
			}
			switch {
			case a.x == nil:
				stack.push(&value{typ: types.TRILEAN, val: tri.NIL})
			case a.x != nil && a.typ == e.Typ.Builtin.Code:
				stack.push(&value{typ: types.TRILEAN, val: tri.TRUE})
			case a.x != nil && a.typ != e.Typ.Builtin.Code:
				stack.push(&value{typ: types.TRILEAN, val: tri.FALSE})
			default:
				halt.As(100, "unhandled type testing for ", v.typ, v.val)
			}
		case *ir.ListExpr:
			var tmp []*value
			for _, x := range e.Expr {
				expr(x)
				v := stack.pop()
				tmp = append(tmp, v)
			}
			stack.push(&value{typ: types.LIST, val: NewList(tmp...)})
		case *ir.SetExpr:
			var tmp []*value
			for _, x := range e.Expr {
				expr(x)
				v := stack.pop()
				tmp = append(tmp, v)
			}
			stack.push(&value{typ: types.SET, val: NewSet(tmp...)})
		case *ir.MapExpr:
			var k []*value
			for _, x := range e.Key {
				expr(x)
				v := stack.pop()
				k = append(k, v)
			}
			var v []*value
			for _, x := range e.Value {
				expr(x)
				n := stack.pop()
				v = append(v, n)
			}
			stack.push(&value{typ: types.MAP, val: NewMap(k, v)})
		default:
			halt.As(100, reflect.TypeOf(e))
		}
	}
	expr(e)
	return stack.pop()
}
Esempio n. 16
0
File: imp.go Progetto: kpmy/ot
func (c *ForeignClass) copyOf() *ForeignClass {
	assert.For(fn.IsNil(c.Entity), 20)
	ret := &ForeignClass{}
	*ret = *c
	return ret
}
Esempio n. 17
0
File: main.go Progetto: kpmy/gone
func main() {
	var s, r *model.Layer
	if _, err := os.Stat(name); os.IsNotExist(err) {
		s = generate()

		if f, err := os.Create(name); err == nil {
			if err := std.Store(f, s); err != nil {
				log.Fatal(err)
			}
			f.Close()
		}
	} else {
		log.Println("file exists")
	}

	if f, err := os.Open(name); err == nil {
		defer f.Close()
		if s, err = std.Load(f); err != nil {
			log.Fatal(err)
		}
	}
	white := color.RGBA{R: 0xff, G: 0xFF, B: 0xFF, A: 0xFF}
	if fi, err := os.Open("0.png"); err == nil {
		if i, _, err := image.Decode(fi); err == nil {
			j := 0
			log.Println("image", i.Bounds())
			for y := i.Bounds().Min.Y; y < i.Bounds().Max.Y; y++ {
				for x := i.Bounds().Min.X; x < i.Bounds().Max.X; x++ {
					c := i.At(x, y)
					if c == white {
						s.Nodes[j].In[model.Link{NodeId: 0, LinkId: 0}] = 1
					} else {
						s.Nodes[j].In[model.Link{NodeId: 0, LinkId: 0}] = 0
					}
					j++
				}
			}

		} else {
			log.Fatal(err)
		}
	} else {
		log.Fatal(err)
	}

	/* for _, n := range s.Nodes {
		n.In[model.Link{NodeId: 0, LinkId: 0}] = rand.Intn(2)
	} */
	t := make([]int, Nreact)
	for i := 0; i < len(t); i++ {
		t[i] = -1
	}
	t[0] = 1

	for stop := false; !stop; { //autolearn for t pattern
		for l := s; !fn.IsNil(l); l = l.Next {
			proc.Process(l)
			if l.Next != nil {
				proc.Transfer(l)
			} else {
				r = l
			}
		}
		a := make([]int, Nreact)
		for i, n := range r.Nodes {
			a[i] = n.Out[0].(int)
		}
		log.Println(t)
		log.Println(a)
		stop = true
		for i, _ := range t {
			switch {
			case t[i] == 1 && a[i] == 1: //ok
			case t[i] == 1 && a[i] != t[i]:
				std.Update(s.Next, r.Nodes[i].In, 1)
				stop = false
			case t[i] == -1 && a[i] != t[i]:
				std.Update(s.Next, r.Nodes[i].In, -1)
				stop = false
			}
		}
	}

	if f, err := os.Create(name + ".0"); err == nil {
		if err := std.Store(f, s); err != nil {
			log.Fatal(err)
		}
		f.Close()
	}
}