Esempio 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)
}
Esempio 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)
}
Esempio 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)
	}
}
Esempio n. 4
0
File: os.go Progetto: 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)
}
Esempio n. 5
0
File: os.go Progetto: 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...))
}
Esempio n. 6
0
File: lens.go Progetto: 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)
		}
	}
}
Esempio n. 7
0
File: merge.go Progetto: 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())
}
Esempio n. 8
0
File: gates.go Progetto: 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)
}
Esempio n. 9
0
File: proc.go Progetto: 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)
	}
}
Esempio n. 10
0
File: parse.go Progetto: 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)
	}
}
Esempio n. 11
0
File: chunk.go Progetto: 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)
	}
}
Esempio 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)
	}
}
Esempio n. 13
0
File: star.go Progetto: 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)
	}
}
Esempio n. 14
0
File: index.go Progetto: 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
}
Esempio 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
}
Esempio n. 16
0
File: form.go Progetto: 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())
}
Esempio n. 17
0
File: yield.go Progetto: 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...)))
		}
	}
}
Esempio 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")
	}
}
Esempio 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)
}
Esempio n. 20
0
File: flows.go Progetto: 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)
}
Esempio n. 21
0
File: proc.go Progetto: 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
}
Esempio n. 22
0
File: sum.go Progetto: 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)
	}()
}
Esempio n. 23
0
File: match.go Progetto: 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
	}
}
Esempio n. 24
0
File: exec.go Progetto: 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)
}
Esempio 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)
}
Esempio n. 26
0
File: grow.go Progetto: 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")))
}
Esempio n. 27
0
File: polar.go Progetto: mrG7/escher
func (Polar) CognizePolar(eye *be.Eye, v interface{}) {
	x := v.(Circuit)
	eye.Show("Complex", cmplx.Rect(x.FloatAt("R"), x.FloatAt("Theta")))
}
Esempio n. 28
0
File: polar.go Progetto: 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))
}
Esempio n. 29
0
func (Planar) CognizePlanar(eye *be.Eye, v interface{}) {
	x := v.(Circuit)
	eye.Show("Complex", complex(x.FloatAt("X"), x.FloatAt("Y")))
}
Esempio 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))))
}