func populateServer(serv *server) []sfs.ReplicateChunkArgs { str := fmt.Sprintf("%s:%d", serv.addr.IP.String(), serv.addr.Port) log.Printf("master: PopulateServer: populating %s\n", str) log.Printf("master: PopulateServer: server heap state:\n%s\n", sHeap.printPresent()) if len(chunks) == 0 { return nil } thisVec := new(vector.Vector) for _, chunk := range chunks { //log.Printf("master: PopulateServer: examining chunk %+v, nservers %d\n", *chunk, chunk.servers.Len()) if chunk.servers.Len() < sfs.NREPLICAS { //populate chunk location list chunklist := make([]net.TCPAddr, chunk.servers.Len()) for cnt1 := 0; cnt1 < chunk.servers.Len(); cnt1++ { chunklist[cnt1] = chunk.servers.At(cnt1).(*server).addr } //send rpc call off thisVec.Push(sfs.ReplicateChunkArgs{chunk.chunkID, chunklist}) } } cnt := thisVec.Len() thisSlice := make([]sfs.ReplicateChunkArgs, cnt) for i := 0; i < cnt; i++ { thisSlice[i] = thisVec.Pop().(sfs.ReplicateChunkArgs) //horribly inefficient but what can you do... } return thisSlice }
func main() { re1 := new(vector.Vector) re1.Push(10) fmt.Print(re1.Pop()) infcheck(5) re1.Push(10) re1.Push(20) // fmt.Print(re1.Less(0,1)) fmt.Print("\n") i := 10 checkpoint(&i) checkTypeComp() // fmt.Print(int32(10).Less(5)); }
func renderSection(section *sectionElement, contextChain *vector.Vector, buf io.Writer) { value := lookup(contextChain, section.name) var context = contextChain.At(contextChain.Len() - 1).(reflect.Value) var contexts = new(vector.Vector) // if the value is nil, check if it's an inverted section isNil := isNil(value) if isNil && !section.inverted || !isNil && section.inverted { return } else { valueInd := indirect(value) switch val := valueInd; val.Kind() { case reflect.Slice: for i := 0; i < val.Len(); i++ { contexts.Push(val.Index(i)) } if val.Len() == 0 { contexts.Push(context) } case reflect.Array: for i := 0; i < val.Len(); i++ { contexts.Push(val.Index(i)) } if val.Len() == 0 { contexts.Push(context) } case reflect.Map, reflect.Struct: contexts.Push(value) default: contexts.Push(context) } } //by default we execute the section for j := 0; j < contexts.Len(); j++ { ctx := contexts.At(j).(reflect.Value) contextChain.Push(ctx) for i := 0; i < section.elems.Len(); i++ { renderElement(section.elems.At(i), contextChain, buf) } contextChain.Pop() } }
func run(ct Obj, stack *Frame, argstack vector.Vector) Obj { defer func() { if err := recover(); err != nil { fmt.Printf("Error in Scheme code: %v\n", err) stack_trace(stack) panic("no error recovery yet") } }() pc := stack.savedpc cycles := 0 for { cycles++ if cycles > SCHED_TICK { cycles = 0 runtime.Gosched() } i := stack.code.bc[pc] if false { name := "*unknown*" if stack.cc != nil { name = stack.cc.name } fmt.Printf("\nI=#x%x op=#b%b PC=#x%x procedure=%s\nregs: ", i, i>>I_SHIFT, pc, name) for i := range stack.regs { Write(stack.regs[i]) fmt.Printf(", ") } fmt.Printf("\nargstack: ") if argstack != nil { for i := 0; i < argstack.Len(); i++ { o := argstack.At(i) Write((o).(Obj)) fmt.Printf(", ") } } fmt.Printf("\n") } pc += 1 switch i >> I_SHIFT { // op1 format case FRAME: // TODO: use this to check that the required // number of arguments were passed continue case RETURN: v := stack.regs[i&OP1_R2] if stack.up == nil { return v } // stack_trace(stack) rreg := stack.rreg pc = stack.savedpc // fmt.Printf("will return to PC=#x%x in reg %d: ",pc,rreg) // Write(v) // fmt.Printf("\n") stack = stack.up stack.regs[rreg] = v case PUSH: //fmt.Printf("pushing reg %d on argument stack", i & OP1_R2) argstack.Push(stack.regs[i&OP1_R2]) case MOVE: src := (i & OP1_R1) >> OP1_R1_SHIFT dst := (i & OP1_R2) stack.regs[dst] = stack.regs[src] case MAKE_VOID: dst := (i & OP1_R2) stack.regs[dst] = Void case CLOSURE: f := make([]Obj, (i&OP1_N)>>OP1_N_SHIFT) stack.regs[i&OP1_R2] = wrap(&Procedure{apply: aprun, free: f, code: stack.code}) case CLOSURE_NAME: p := (*stack.regs[i&OP1_R2]).(*Procedure) name := stack.regs[(i&OP1_R1)>>OP1_R1_SHIFT] if is_immediate(name) { continue } p.name = (*name).(string) case CLOSURE_VAR: p := (*stack.regs[i&OP1_R2]).(*Procedure) value := stack.regs[(i&OP1_R1)>>OP1_R1_SHIFT] freevar := (i & OP1_N) >> OP1_N_SHIFT p.free[freevar] = value case CLOSURE_REF: p := stack.cc freevar := (i & OP1_N) >> OP1_N_SHIFT stack.regs[i&OP1_R2] = p.free[freevar] case FUNCALL: // stack_trace(stack) r := int(i & OP1_R2) argnum := int((i & OP1_N) >> OP1_N_SHIFT) _p := stack.regs[(i&OP1_R1)>>OP1_R1_SHIFT] if is_immediate(_p) { panic("Bad type to apply") } p := (*_p).(*Procedure) if p.apply != aprun { // This is a primitive. args := make([]Obj, argnum) for i := argnum - 1; i >= 0; i-- { args[i] = (argstack.Pop()).(Obj) } stack.regs[r] = p.apply(p, args, ct) continue } dst_i := p.code.bc[p.label] if (dst_i >> I_SHIFT) != FRAME { panic(fmt.Sprintf("Procedure %s at #x%x has no FRAME: #x%x", p.name, p.label, i)) } frame := call_frame(stack, r, pc, argnum+int((dst_i&OP1_R2))) for i := argnum - 1; i >= 0; i-- { frame.regs[i] = (argstack.Pop()).(Obj) } frame.cc = p frame.argnum = argnum frame.code = p.code stack = frame pc = p.label // stack_trace(stack) //fmt.Printf("funcall to %d (%s), new frame = %v\n",pc,p.name,stack) case TAILCALL: argnum := int((i & OP1_N) >> OP1_N_SHIFT) _p := stack.regs[i&OP1_R2] if is_immediate(_p) { panic("Bad type to apply") } p := (*_p).(*Procedure) dst_i := p.code.bc[p.label] if (dst_i >> I_SHIFT) != FRAME { panic(fmt.Sprintf("Procedure at #x%x has no FRAME: #x%x", p.label, i)) } if p.apply != aprun { panic("tail-call to primitive") } tail_frame(stack, argnum+int((dst_i&OP1_R2))) for i := argnum - 1; i >= 0; i-- { stack.regs[i] = (argstack.Pop()).(Obj) } stack.cc = p stack.argnum = argnum stack.code = p.code pc = p.label //fmt.Printf("tailcall to %d, new frame = %v\n",pc,stack) case CONSARGS: // Called at the very start of procedures with // rest arguments. n is how many variables are // in the formals of the procedure. n := int((i & OP1_N) >> OP1_N_SHIFT) rest := Eol for i := stack.argnum - 1; i >= n-1; i-- { rest = Cons(stack.regs[i], rest) stack.regs[i] = Void } stack.regs[n-1] = rest // op2 format case JUMP: disp := (i & OP2_N) >> OP2_N_SHIFT // fmt.Printf("JUMP DISPLACEMENT: %d = %d\n", disp, int17(disp)) // convert to signed: abs := pc - 1 + int17(disp) pc = abs case CONST_REF: stack.regs[i&OP2_R] = stack.code.consts[(i&OP2_N)>>OP2_N_SHIFT] case CLOSURE_LABEL: p := (*stack.regs[i&OP2_R]).(*Procedure) disp := (i & OP2_N) >> OP2_N_SHIFT // convert to signed: // fmt.Printf("CLOSURE LABEL DISPLACEMENT: %x = %x = %x\n", // disp, int17(disp), pc-1+int17(disp)) abs := pc - 1 + int17(disp) p.label = abs case BF: v := stack.regs[i&OP2_R] if v != False { continue } disp := (i & OP2_N) >> OP2_N_SHIFT // fmt.Printf("BRANCH DISPLACEMENT: %d = %d\n", // disp, int17(disp)) // convert to signed: abs := pc - 1 + int17(disp) pc = abs // op3 format case PRIMCALL: r := i & OP3_R primitive := (i & OP3_N2) >> OP3_N2_SHIFT argnum := int((i & OP3_N1) >> OP3_N1_SHIFT) args := make([]Obj, argnum) // fmt.Printf("primitive: %d, argnum: %d\nargs:",primitive,argnum) for i := argnum - 1; i >= 0; i-- { args[i] = (argstack.Pop()).(Obj) // Write(args[i]) // fmt.Printf(", ") } // fmt.Printf("\n") stack.regs[r] = evprimn(primitive, args, ct) case PRIMREF: r := i & OP3_R stack.regs[r] = primitive[(i&OP3_N2)>>OP3_N2_SHIFT] // unknown opcodes default: panic(fmt.Sprintf("Unimplemented bytecode op: #b%b (in #x%x)", i>>I_SHIFT, i)) } } // Appease Go panic("fell off the edge in run()") }
func (self *BasicState) AddRegex(re string, m RegexSet) (*BasicState, os.Error) { if m == nil { m = defaultMeta } // this is just the sort of horror show that lexical analysis avoids // stack machine start := self end := NewState() stack := new(vector.Vector) // state flags expr, esc, cs := false, false, false setstr := "" // go into a subexpression push := func() { rp := ®exPos{start, end} stack.Push(rp) end = NewState() start.AddEmptyTransition(end) } // come out of a subexpression pop := func() { rp := stack.Pop().(*regexPos) end.AddEmptyTransition(rp.end) start = rp.start end = rp.end } // move forward, for the purposes of concatenation move := func() { start = end end = NewState() } // the expression is inside an implicit ( ... ) push() // parse the expression for _, c := range re { // escaped characters if esc { esc = false // inside a charset jobby if cs { setstr += string(c) continue } // check out the metachar action if meta, ok := m[c]; ok { move() chars, err := Charset(meta, end) if err != nil { return nil, err } start.AddEmptyTransition(chars) expr = true continue } // nothing else going on? well you escaped it for a reason goto add } // charsets if cs { if c == '\\' { esc = true } else if c == ']' { chars, err := Charset(setstr, end) if err != nil { return nil, err } start.AddEmptyTransition(chars) setstr = "" cs = false expr = true } else { setstr += string(c) } continue } // everything else switch c { // charsets case '.': move() start.AddEmptyTransition(Any(end)) expr = true case '[': move() cs = true case ']': if !cs { return nil, os.ErrorString("trying to close unopened charset") } // grouping case '(': move() push() expr = false case ')': if stack.Len() <= 1 { return nil, os.ErrorString("trying to close unopened subexpr") } pop() expr = true // alternation case '|': pop() push() expr = false // modifiers case '?': start.AddEmptyTransition(end) goto check case '*': start.AddEmptyTransition(end) end.AddEmptyTransition(start) goto check case '+': end.AddEmptyTransition(start) goto check // escape character case '\\': esc = true expr = false // otherwise just add that char default: goto add } continue // make sure the modifier modified something check: if !expr { return nil, os.ErrorString("nothing to modify") } expr = false continue // add a character transition add: move() start.AddTransition(c, end) expr = true continue } // some final consistency checks if cs { return nil, os.ErrorString("unclosed charset") } if esc { return nil, os.ErrorString("invalid escape sequence") } if stack.Len() > 1 { return nil, os.ErrorString("unclosed subexpr") } // close the implicit brackets pop() return end, nil }
// Sets multiLine to true if the binary expression spans multiple lines. func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1 int, multiLine *bool) { prec := x.Op.Precedence() if prec < prec1 { // parenthesis needed // Note: The parser inserts an ast.ParenExpr node; thus this case // can only occur if the AST is created in a different way. p.print(token.LPAREN) p.expr(x, multiLine) p.print(token.RPAREN) return } // Traverse left, collect all operations at same precedence // and determine if blanks should be printed around operators. // // This algorithm assumes that the right-hand side of a binary // operation has a different (higher) precedence then the current // node, which is how the parser creates the AST. var list vector.Vector line := x.Y.Pos().Line printBlanks := prec <= token.EQL.Precedence() || needsBlanks(x.Y) for { list.Push(x) if t, ok := x.X.(*ast.BinaryExpr); ok && t.Op.Precedence() == prec { x = t prev := line line = x.Y.Pos().Line if needsBlanks(x.Y) || prev != line { printBlanks = true } } else { break } } prev := line line = x.X.Pos().Line if needsBlanks(x.X) || prev != line { printBlanks = true } // Print collected operations left-to-right, with blanks if necessary. ws := indent p.expr1(x.X, prec, multiLine) for list.Len() > 0 { x = list.Pop().(*ast.BinaryExpr) prev := line line = x.Y.Pos().Line if printBlanks { if prev != line { p.print(blank, x.OpPos, x.Op) // at least one line break, but respect an extra empty line // in the source if p.linebreak(line, 1, 2, ws, true) { ws = ignore *multiLine = true } } else { p.print(blank, x.OpPos, x.Op, blank) } } else { if prev != line { panic("internal error") } p.print(x.OpPos, x.Op) } p.expr1(x.Y, prec, multiLine) } if ws == ignore { p.print(unindent) } }
func parse_forth(dat string, DataStack *vector.Vector) { L := DataStack.Len() switch strings.TrimSpace(string(dat)) { case "": case "<cr>": return case "t": //check the DataStack size using the popped value // if it passes, then the program continues minimum := int(DataStack.Pop().(float32)) if DataStack.Len() < minimum { log.Println("DataStack has not enough minerals (values)") } case ".": log.Println(DataStack.Pop()) case "0SP": DataStack.Cut(0, L) case ".S": log.Println(DataStack) case "2/": DataStack.Push(DataStack.Pop().(float32) / 2) case "2*": DataStack.Push(DataStack.Pop().(float32) * 2) case "2-": DataStack.Push(DataStack.Pop().(float32) - 2) case "2+": DataStack.Push(DataStack.Pop().(float32) + 2) case "1-": DataStack.Push(DataStack.Pop().(float32) - 1) case "1+": DataStack.Push(DataStack.Pop().(float32) + 1) case "DUP": DataStack.Push(DataStack.Last()) case "?DUP": if DataStack.Last().(float32) != 0 { DataStack.Push(DataStack.Last().(float32)) } case "PICK": number := int(DataStack.Pop().(float32)) if number < L { DataStack.Push(DataStack.At(L - 1 - number).(float32)) } else { log.Fatal("picking out of stack not allowed. Stack Length: " + string(L) + ". Selecting: " + string(number) + ".") return } case "TUCK": DataStack.Insert(L-2, int(DataStack.Last().(float32))) case "NIP": DataStack.Delete(L - 2) case "2DROP": DataStack.Pop() DataStack.Pop() case "2DUP": DataStack.Push(DataStack.At(L - 2)) DataStack.Push(DataStack.At(DataStack.Len() - 2)) case "DROP": DataStack.Pop() case "OVER": DataStack.Push(DataStack.At(L - 2)) case "SWAP": l := DataStack.Len() DataStack.Swap(l-2, l-1) case "*": num1 := DataStack.Pop().(float32) num2 := DataStack.Pop().(float32) DataStack.Push(num1 * num2) case "+": num1 := DataStack.Pop().(float32) num2 := DataStack.Pop().(float32) DataStack.Push(num1 + num2) case "-": num1 := DataStack.Pop().(float32) num2 := DataStack.Pop().(float32) DataStack.Push(num2 - num1) case "/": num1 := DataStack.Pop().(float32) num2 := DataStack.Pop().(float32) DataStack.Push(num2 / num1) case "-ROT": DataStack.Swap(L-1, L-2) DataStack.Swap(L-2, L-3) case "ROT": DataStack.Swap(L-3, L-2) DataStack.Swap(L-2, L-1) case "2OVER": DataStack.Push(DataStack.At(L - 4)) DataStack.Push(DataStack.At(DataStack.Len() - 4)) case "2SWAP": DataStack.Swap(L-4, L-2) DataStack.Swap(L-3, L-1) case "EMIT": log.Println(string([]byte{uint8(DataStack.Last().(float32))})) default: val, ok := strconv.Atof32(dat) if ok == nil { DataStack.Push(val) } else { log.Println(ok) log.Fatalln("error, unknown token \"" + dat + "\"") } } }
// returns a topologically sorted vector of Node's func TopSort(dag vec.Vector, s byte) vec.Vector { sortDag := new(vec.Vector) tempDag := new(vec.Vector) destDag := new(vec.Vector) setVec := new(vec.Vector) for i := 0; i < dag.Len(); i++ { tempDag.Push((dag.At(i).(*par.Node)).Copy()) destDag.Push((dag.At(i).(*par.Node)).Copy()) } // t-level gets regular top sort if s == 't' { setVec.Push(tempDag.At(0)) destDag.Delete(0) for i := setVec.Len(); i > 0; i = setVec.Len() { n := (setVec.Pop().(*par.Node)).Copy() sortDag.Push(n) for j := 0; j < (n.Cl).Len(); j++ { c := ((n.Cl).At(j).(*par.Rel)).Id for k := 0; k < destDag.Len(); k++ { if (destDag.At(k).(*par.Node)).Id == c { for l := 0; l < (destDag.At(k).(*par.Node)).Pl.Len(); l++ { if (destDag.At(k).(*par.Node)).Pl.At(l).(*par.Rel).Id == n.Id { (destDag.At(k).(*par.Node)).Pl.Delete(l) break } } } } } for j := 0; j < destDag.Len(); j++ { if (destDag.At(j).(*par.Node)).Pl.Len() == 0 { c := (destDag.At(j).(*par.Node)).Id for k := 0; k < tempDag.Len(); k++ { if (tempDag.At(k).(*par.Node)).Id == c { setVec.Push(tempDag.At(k)) break } } destDag.Delete(j) j-- } } } // b-level gets reverse top sort } else if s == 'b' { setVec.Push(tempDag.At(tempDag.Len() - 1)) destDag.Delete(destDag.Len() - 1) for i := setVec.Len(); i > 0; i = setVec.Len() { n := (setVec.Pop().(*par.Node)).Copy() sortDag.Push(n) for j := 0; j < (n.Pl).Len(); j++ { c := ((n.Pl).At(j).(*par.Rel)).Id for k := 0; k < destDag.Len(); k++ { if (destDag.At(k).(*par.Node)).Id == c { for l := 0; l < (destDag.At(k).(*par.Node)).Cl.Len(); l++ { if (destDag.At(k).(*par.Node)).Cl.At(l).(*par.Rel).Id == n.Id { (destDag.At(k).(*par.Node)).Cl.Delete(l) break } } } } } for j := 0; j < destDag.Len(); j++ { if (destDag.At(j).(*par.Node)).Cl.Len() == 0 { c := (destDag.At(j).(*par.Node)).Id for k := 0; k < tempDag.Len(); k++ { if (tempDag.At(k).(*par.Node)).Id == c { setVec.Push(tempDag.At(k)) break } } destDag.Delete(j) j-- } } } } else { fmt.Printf("Error") } return sortDag.Copy() }