Changeset 2e06a9e52d352e2a3171540c93bc1712ad6b1799
Commiter: Charles Childers
Author: Charles Childers
Parent: 36877ecffd
(2010/03/13 23:09) Almost 2 years ago
sync against gonga hg repo
Commiter: Charles Childers
Author: Charles Childers
Parent: 36877ecffd
(2010/03/13 23:09) Almost 2 years ago
sync against gonga hg repo
func (vm *NgaroVM) wait(port *[nports]int, tos int, depth int) (spdec int) {func (vm *NgaroVM) wait(port *[nports]int, tos, depth, rdepth int) (spdec int) {switch {switch {port[5] = stackDepthport[5] = rdepthallows to launch new cores writing 1 to the port 13. Theadds concurrency, floating point and direct IO support.new core will start running the same (shared) imagefrom the address at the top of the stack. Cores communicatewriting and id to port 1 (to receive) and 2 (to send). Toremove a channel write its id to ports 1 and 2 and wait.Some useful words:Concurrency vocabulary:Example:Floating poing vocabulary:-2000dup : fpush [ 5 + , ] ; dup : fpop [ 6 + , ] ;dup : f>jump [ 10 + , ] ; dup : f<jump [ 11 + , ] ;dup : f!jump [ 12 + , ] ; dup : f=jump [ 13 + , ] ;dup : f+ [ 16 + , ] ; dup : f- [ 17 + , ] ;dup : f* [ 18 + , ] ; dup : f/ [ 19 + , ] ;dropDirect IO vocabulary:-1100 dup : >int? ( a-xf ) [ 28 + , ] ; : . ( x- ) [ 29 + , ] ;-1200 dup : >float? ( a-xf ) [ 28 + , ] ; : f. ( x- ) [ 29 + , ] ;-1300 dup : <accept ( ca- ) [ 28 + , ] ; : $. ( a- ) [ 29 + , ] ;Direct IO usage:: tib<accept ( c- )dup 32 = whitespace @ and if drop -1 then tib <accept ;: (accept)dup tib<accept32 =if ( ugly way to know if this is the listen loop )tib >int? if .data rdrop listen ;then droptib >float? if .data rdrop listen ;then dropthen ;' (accept) is acceptExample of a concurrent program:Input *Inputfunc ftoi(f float) int { return *(*int)(unsafe.Pointer(&f)) }func itof(i int) float { return *(*float)(unsafe.Pointer(&i)) }func (vm *NgaroVM) core(ip int) {func (vm *NgaroVM) core(ip int) {sp -= vm.wait(&port, tos, sp-2, rsp)case FP + GtJump:ip++if itof(data[sp-1]) > itof(tos) {ip = vm.img[ip] - 1}sp = sp - 2case FP + LtJump:ip++if itof(data[sp-1]) < itof(tos) {ip = vm.img[ip] - 1}sp = sp - 2case FP + Add:data[sp-1] = ftoi(itof(data[sp-1]) + itof(tos))sp--case FP + Sub:data[sp-1] = ftoi(itof(data[sp-1]) - itof(tos))sp--case FP + Mul:data[sp-1] = ftoi(itof(data[sp-1]) * itof(tos))sp--case FP + Dinod:data[sp-1] = ftoi(itof(data[sp-1]) / itof(tos))sp--case IOint + In:a := readA(vm.img, tos)sp++if i, err := strconv.Atoi(a); err != nil {data[sp] = 0} else {data[sp-1] = idata[sp] = -1}case IOint + Out:fmt.Fprint(vm.Output, tos)sp--case IOfloat + In:a := readA(vm.img, tos)sp++if f, err := strconv.Atof(a); err != nil {data[sp] = 0} else {data[sp-1] = ftoi(f)data[sp] = -1}case IOfloat + Out:fmt.Fprint(vm.Output, itof(tos))sp--case IOstring + In: // Acceptvar c [1]bytevar cont func() boolif data[sp-1] == -1 {cont = func() bool { return c[0] != ' ' && c[0] != '\n' && c[0] != '\t' }} else {cont = func() bool { return c[0] != byte(data[sp-1]) }}_, err := vm.Input.Read(c[0:])// Skip leading white-spacefor data[sp-1] == -1 && (c[0] == ' ' || c[0] == '\n' || c[0] == '\t') {vm.Input.Read(c[0:])}a := tosfor ; err == nil && cont(); _, err = vm.Input.Read(c[0:]) {vm.img[a] = int(c[0])a++}vm.img[a] = 0sp -= 2case IOstring + Out:for i := tos; i < len(vm.img); i++ {if vm.img[i] == 0 {break}fmt.Fprintf(vm.Output, "%c", byte(vm.img[i]))}sp--