Exemplo n.º 1
0
func (IntString) CognizeString(eye *be.Eye, v interface{}) {
	i, err := strconv.Atoi(v.(string))
	if err != nil {
		panic(err)
	}
	eye.Show("Int", i)
}
Exemplo n.º 2
0
func (IgnoreValves) CognizeCircuit(eye *be.Eye, v interface{}) {
	u := v.(Circuit).Copy()
	n := u.Unify("ignoreValves")
	u.Gate[n] = NewVerbAddress("*", "Ignore")
	u.Reflow(Super, n)
	eye.Show(DefaultValve, u)
}
Exemplo n.º 3
0
func depthFirst(eye *be.Eye, walk []Name, v interface{}) {
	x, ok := v.(Circuit)
	if !ok {
		return
	}
	for _, n := range x.SortedNames() {
		switch n.(type) { // skip non alpha-numeric names
		case int, string:
			v := x.At(n)
			depthFirst(eye, append(walk, n), v)
		}
	}

	var nm Name = "" // The root circuit is shown with the empty name
	if len(walk) > 0 {
		nm = walk[len(walk)-1]
	}

	frame := New().
		Grow("Address", Circuit(NewAddress(walk...))).
		Grow("Name", nm).
		Grow("View", x)

	eye.Show("Frame", frame)
	if len(walk) == 0 {
		eye.Show("End", v)
	}
}
Exemplo n.º 4
0
Arquivo: os.go Projeto: mrG7/escher
func (LookPath) CognizeName(eye *be.Eye, value interface{}) {
	p, err := exec.LookPath(value.(string))
	if err != nil {
		log.Fatalf("no file path to %s", value.(string))
	}
	eye.Show(DefaultValve, p)
}
Exemplo n.º 5
0
Arquivo: os.go Projeto: mrG7/escher
func (Join) CognizeView(eye *be.Eye, v interface{}) {
	u := v.(Circuit)
	var s []string
	for _, n := range u.SortedNames() {
		s = append(s, u.Gate[n].(string))
	}
	eye.Show(DefaultValve, path.Join(s...))
}
Exemplo n.º 6
0
Arquivo: lens.go Projeto: mrG7/escher
func (g *Lens) OverCognize(eye *be.Eye, valve Name, value interface{}) {
	g.remember(valve, value)
	for _, v := range g.valve {
		if v != valve {
			eye.Show(v, value)
		}
	}
}
Exemplo n.º 7
0
Arquivo: merge.go Projeto: mrG7/escher
func (Merge) CognizeIn(eye *be.Eye, v interface{}) {
	var w bytes.Buffer
	x := v.(Circuit)
	for _, name := range x.SortedLetters() {
		w.WriteString(flatten(x.StringAt(name)))
	}
	eye.Show("Out", w.String())
}
Exemplo n.º 8
0
Arquivo: gates.go Projeto: mrG7/escher
func (Gates) Cognize(eye *be.Eye, value interface{}) {
	u := value.(Circuit)
	for name, _ := range u.SortedNames() {
		frame := New()
		frame.Grow("Name", name).Grow("Value", u.At(name))
		eye.Show("Frame", frame)
	}
	eye.Show("End", value)
}
Exemplo n.º 9
0
Arquivo: proc.go Projeto: mrG7/escher
func (Process) CognizeCommand(eye *be.Eye, dvalue interface{}) {
	x := New()
	if exit := spawnProcess(eye, cognizeCommand(dvalue)); exit != nil {
		x.Grow("Exit", 1)
		eye.Show("Exit", x)
	} else {
		x.Grow("Exit", 0)
		eye.Show("Exit", x)
	}
}
Exemplo n.º 10
0
Arquivo: parse.go Projeto: mrG7/escher
func (Parse) CognizeText(eye *be.Eye, v interface{}) {
	src := a.NewSrcString(plumb.AsString(v))
	for {
		v := see.SeeChamber(src)
		if v == nil {
			break
		}
		eye.Show("Value", v)
	}
}
Exemplo n.º 11
0
Arquivo: chunk.go Projeto: mrG7/escher
func (Chunk) CognizeReader(eye *be.Eye, v interface{}) {
	r := kio.NewChunkReader(v.(io.Reader))
	for {
		chunk, err := r.Read()
		if err != nil {
			return
		}
		eye.Show("Chunk", chunk)
	}
}
Exemplo n.º 12
0
func (Lookup) CognizeView(eye *be.Eye, v interface{}) {
	u := v.(Circuit)
	x := u.CircuitAt("Index")
	addr := u.VerbAt("Address")
	r := be.AsIndex(x).Recall(addr.Address()...)
	if r == nil {
		eye.Show("NotFound", New().Grow("NotFound", Circuit(addr)).Grow("In", x))
	} else {
		eye.Show("Found", r)
	}
}
Exemplo n.º 13
0
Arquivo: star.go Projeto: mrG7/escher
func (s *Star) OverCognize(eye *be.Eye, name Name, value interface{}) {
	if s.f != nil {
		s.f(name, value)
	}
	for gn_, _ := range s.view.Gate {
		gn := gn_
		if gn == name {
			continue
		}
		go eye.Show(gn, value)
	}
}
Exemplo n.º 14
0
Arquivo: index.go Projeto: mrG7/escher
func (Index) Spark(eye *be.Eye, matter Circuit, aux ...interface{}) Value {
	index, view := matter.CircuitAt("Index"), matter.CircuitAt("View")
	go func() {
		for vlv, _ := range view.Gate {
			eye.Show(vlv, index)
		}
	}()
	if view.Len() == 0 {
		return index
	}
	return nil
}
Exemplo n.º 15
0
func (SourceFile) Spark(eye *be.Eye, _ Circuit, aux ...interface{}) Value {
	go func() {
		name := aux[0].(string)
		file, err := os.Open(name)
		if err != nil {
			log.Printf("Problem opening file %q (%v)", name, err)
			panic("open file")
		}
		eye.Show(DefaultValve, file)
	}()
	return nil
}
Exemplo n.º 16
0
Arquivo: form.go Projeto: mrG7/escher
func (Form) CognizeIn(eye *be.Eye, v interface{}) {
	td := v.(Circuit)
	t, err := template.New("").Parse(td.StringAt("Form"))
	if err != nil {
		panic(err)
	}
	var w bytes.Buffer
	if err = t.Execute(&w, td.CircuitAt("Data")); err != nil {
		panic(err)
	}
	eye.Show("Out", w.String())
}
Exemplo n.º 17
0
Arquivo: yield.go Projeto: mrG7/escher
func yieldIndex(eye *be.Eye, x Circuit, path []Name) {
	for _, n := range x.SortedNames() {
		switch t := x.At(n).(type) {
		case Circuit:
			if t.Vol() == 0 { // circuits without flow are treated as indices and recursed into
				yieldIndex(eye, t, append(path, n))
			} else {
				eye.Show(DefaultValve, New().Grow("Value", t).Grow("Address", NewAddress(path...)))
			}
		default:
			eye.Show(DefaultValve, New().Grow("Value", t).Grow("Address", NewAddress(path...)))
		}
	}
}
Exemplo n.º 18
0
func (a *Alternate) OverCognize(eye *be.Eye, valve Name, value interface{}) {
	switch valve.(string) {
	case "SX":
		a.flow[0] <- struct{}{} // obtain token to send
		eye.Show("TX", value)
		<-a.flow[1] // grant token to other side
	case "SY":
		a.flow[1] <- struct{}{} // obtain token to send
		eye.Show("TY", value)
		<-a.flow[0] // grant token to other side
	case "TX", "TY":
	default:
		panic("invalid valve name on alternation gate")
	}
}
Exemplo n.º 19
0
func (h *WriteFile) CognizeContent(eye *be.Eye, v interface{}) {
	<-h.named
	switch t := v.(type) {
	case string:
		ioutil.WriteFile(h.name, []byte(t), 0644)
	case []byte:
		ioutil.WriteFile(h.name, t, 0644)
	case io.Reader:
		var w bytes.Buffer
		io.Copy(&w, t)
		ioutil.WriteFile(h.name, w.Bytes(), 0644)
	default:
		panic("eh?")
	}
	eye.Show("Ready", 1)
}
Exemplo n.º 20
0
Arquivo: flows.go Projeto: mrG7/escher
func (Flows) Cognize(eye *be.Eye, value interface{}) {
	u := value.(Circuit)
	for xname, xview := range u.Flow {
		for xvalve, xvec := range xview {
			yname, yvalve := xvec.Gate, xvec.Valve
			//
			xvalue, yvalue := sanitizeValue(u, xname), sanitizeValue(u, yname)
			//
			frame := New()
			xy := New().Grow("Name", xname).Grow("Value", xvalue).Grow("Valve", xvalve)
			yx := New().Grow("Name", xname).Grow("Value", yvalue).Grow("Valve", yvalve)
			frame.Grow(0, xy).Grow(1, yx)
			eye.Show("Frame", frame)
		}
	}
	eye.Show("End", value)
}
Exemplo n.º 21
0
Arquivo: proc.go Projeto: mrG7/escher
func spawnProcess(eye *be.Eye, cmd *exec.Cmd) (err error) {
	var stdin io.WriteCloser
	var stdout io.ReadCloser
	var stderr io.ReadCloser
	stdin, err = cmd.StdinPipe()
	if err != nil {
		panic(err)
	}
	stdout, err = cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}
	stderr, err = cmd.StderrPipe()
	if err != nil {
		panic(err)
	}
	if err = cmd.Start(); err != nil {
		log.Fatalf("Problem starting %s (%v)", cmd.Path, err)
		return err
	}
	// We cannot call cmd.Wait before all std streams have been closed.
	stdClose := make(chan struct{}, 3)
	stdin = kio.RunOnCloseWriter(stdin, func() { stdClose <- struct{}{} })
	stdout = kio.RunOnCloseReader(stdout, func() { stdClose <- struct{}{} })
	stderr = kio.RunOnCloseReader(stderr, func() { stdClose <- struct{}{} })
	g := New().
		Grow("Stdin", stdin).
		Grow("Stdout", stdout).
		Grow("Stderr", stderr)
	// log.Printf("os process io (%v)", Linearize(fmt.Sprintf("%v", when)))
	eye.Show("IO", g)
	<-stdClose
	<-stdClose
	<-stdClose
	// log.Printf("os process waiting (%v)", Linearize(fmt.Sprintf("%v", when)))
	err = cmd.Wait()
	switch err.(type) {
	case nil, *exec.ExitError:
	default:
		panic(err)
	}
	// log.Printf("os process exit (%v)", Linearize(fmt.Sprintf("%v", when)))
	return err
}
Exemplo n.º 22
0
Arquivo: sum.go Projeto: mrG7/escher
func (s *IntSum) fire(eye *be.Eye, v1, v2 string, u1, u2 int) {
	var wg sync.WaitGroup
	defer wg.Wait()
	wg.Add(2)
	go func() {
		defer func() {
			recover()
		}()
		defer wg.Done()
		eye.Show(v1, u1)
	}()
	go func() {
		defer func() {
			recover()
		}()
		defer wg.Done()
		eye.Show(v2, u2)
	}()
}
Exemplo n.º 23
0
Arquivo: match.go Projeto: mrG7/escher
func (m *Match) OverCognize(eye *be.Eye, name Name, v interface{}) {
	// compute valve index
	var i int
	for j, n := range m.name {
		if Same(n, name) {
			i = j
			break
		}
	}
	// match
	select {
	case u := <-m.flow[1-i]: // if the opposing channel is ready
		if !Same(u, v) {
			log.Fatalf("mismatch %v vs %v: %v vs %v\n", m.name[1-i], name, u, v)
		}
		eye.Show(DefaultValve, v) // emit the matched object
	default: // otherwise, offer our value
		m.flow[i] <- v
	}
}
Exemplo n.º 24
0
Arquivo: exec.go Projeto: mrG7/escher
func (Exec) CognizeIn(eye *be.Eye, v interface{}) {
	x := v.(Circuit)
	//
	addr := Verb(x.CircuitAt("Address").Copy())
	addr.Gate[""] = "*"
	cmd := exec.Command(os.Args[0], "-src", srcDir, addr.String())

	var success bool
	if err := cmd.Run(); err != nil {
		fmt.Printf("- Test %v (%v)\n", addr, err)
		success = false
	} else {
		fmt.Printf("+ Test %v (ok)\n", addr)
		success = true
	}
	r := New().
		Grow("Verb", Circuit(addr)).
		Grow("Result", success)
	eye.Show("Out", r)
}
Exemplo n.º 25
0
func (Filter) CognizeIn(eye *be.Eye, v interface{}) {
	x := v.(Circuit)
	//
	name_, view := x.NameAt("Name"), x.CircuitAt("View")
	name, ok := name_.(string)
	if !ok {
		return
	}
	if !strings.HasPrefix(name, "Test") {
		return
	}
	sfx := name[len("Test"):]
	if len(sfx) == 0 || !unicode.IsUpper(rune(sfx[0])) {
		return
	}
	y := New().
		Grow("Address", x.CircuitAt("Address")).
		Grow("Name", name).
		Grow("View", view)
	eye.Show("Out", y)
}
Exemplo n.º 26
0
Arquivo: grow.go Projeto: mrG7/escher
func (g *Grow) fire(eye *be.Eye) {
	if g.u.Len() != 3 {
		return
	}
	eye.Show("", g.u.CircuitAt("Img").Copy().ReGrow(g.u.At("Key"), g.u.At("Value")))
}
Exemplo n.º 27
0
Arquivo: polar.go Projeto: mrG7/escher
func (Polar) CognizePolar(eye *be.Eye, v interface{}) {
	x := v.(Circuit)
	eye.Show("Complex", cmplx.Rect(x.FloatAt("R"), x.FloatAt("Theta")))
}
Exemplo n.º 28
0
Arquivo: polar.go Projeto: mrG7/escher
func (Polar) CognizeComplex(eye *be.Eye, v interface{}) {
	r, theta := cmplx.Polar(v.(complex128))
	eye.Show("Polar", New().Grow("R", r).Grow("Theta", theta))
}
Exemplo n.º 29
0
func (Planar) CognizePlanar(eye *be.Eye, v interface{}) {
	x := v.(Circuit)
	eye.Show("Complex", complex(x.FloatAt("X"), x.FloatAt("Y")))
}
Exemplo n.º 30
0
func (Planar) CognizeComplex(eye *be.Eye, v interface{}) {
	eye.Show("Planar", New().Grow("X", real(v.(complex128))).Grow("Y", imag(v.(complex128))))
}