func This(typ Type, _x interface{}) (ret Value) { switch typ { case INTEGER: switch x := _x.(type) { case int64: ret.V = x case int: ret.V = int64(x) default: halt.As(101, reflect.TypeOf(x)) } case FLOAT: switch x := _x.(type) { case float64: ret.V = x default: halt.As(102, reflect.TypeOf(x)) } case COMPLEX: switch x := _x.(type) { case complex128: ret.V = x } default: halt.As(100, typ) } ret.T = typ return }
func (w *Words) Load() (err error) { w.Lock() const docId = "map" tmp := make(map[string]interface{}) if err = wordsDb.Get(docId, &tmp, nil); err == nil { for k, _v := range tmp { switch v := _v.(type) { case map[string]interface{}: mm := make(map[string]int) for k, _v := range v { switch v := _v.(type) { case float64: mm[k] = int(v) default: halt.As(100, k, " ", reflect.TypeOf(v)) } } w.wm[k] = mm case string: //do nothing default: halt.As(100, k, " ", reflect.TypeOf(v)) } } } else if couchdb.NotFound(err) { if _, err = wordsDb.Put(docId, tmp, ""); err == nil { err = w.Load() } } else { log.Println(err) } w.Unlock() return }
func prettyPrint(t *ir.Template, log *testing.T) { wr := bytes.NewBufferString("") depth := 0 tab := func() { for i := 0; i < depth; i++ { fmt.Fprint(wr, " ") } } tab() for _, _s := range t.Stmt { switch s := _s.(type) { case *ir.Emit: tab() fmt.Fprint(wr, fn.MaybeString(s.Template, "~"), s.Class, fn.MaybeString("(", s.Ident, ")")) if s.ChildCount == 0 { fmt.Fprintln(wr) } case *ir.Dive: if !s.Reuse { fmt.Fprintln(wr, ":") } else { fmt.Fprintln(wr, " ::") } depth++ case *ir.Rise: depth-- tab() fmt.Fprintln(wr, ";") case *ir.Put: tab() switch s.Type { case types.STRING: fmt.Fprint(wr, "`", s.Value, "`") case types.REAL, types.INTEGER, types.TRILEAN: fmt.Fprint(wr, s.Value) case types.CHAR: fmt.Fprint(wr, "0", strings.ToUpper(strconv.FormatUint(uint64(s.Value.(rune)), 16)), "U") case types.LINK: fmt.Fprint(wr, "@", s.Value) default: halt.As(100, s.Type) } fmt.Fprintln(wr) default: halt.As(100, reflect.TypeOf(s)) } } log.Log(wr.String()) }
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(";") } }
func (b *exprBuilder) push(_e ir.Expression) { b.init() switch e := _e.(type) { case *exprBuilder: b.stack.PushFront(e.final()) case *ir.ConstExpr, *ir.SelectExpr, *ir.ListExpr, *ir.MapExpr, *ir.SetExpr: b.stack.PushFront(e) case *ir.Monadic: e.Expr = b.pop() b.stack.PushFront(e) case *ir.TypeTest: e.Operand = b.pop() b.stack.PushFront(e) case *ir.Dyadic: e.Right = b.pop() e.Left = b.pop() b.stack.PushFront(e) case *ir.Ternary: e.Else = b.pop() e.Then = b.pop() e.If = b.pop() b.stack.PushFront(e) case *ir.InfixExpr: for i := 1; i < len(e.Unit.Infix()); i++ { e.Args = append(e.Args, b.pop()) } b.stack.PushFront(e) default: halt.As(100, reflect.TypeOf(e)) } //fmt.Println("push", _e) }
func calcDyadic(left *value, op ops.Operation, right *value) (ret *value) { if ml, ok := dyadic[left.typ].(tm); ml != nil && ok { if dml, ok := ml[right.typ].(df); dml != nil && ok { if df := dml[op]; df != nil { ret = df(left, right) } else { halt.As(102, "operation not found ", op, " for ", left.typ, " ", right.typ) } } else { halt.As(101, "unexpected right ", right.typ, " for ", left.typ, " ", op) } } else { halt.As(100, "unexpected left ", left.typ, " ", right.typ, " for ", op) } return }
func (u *extern) attr(start *xml.StartElement, name string, value interface{}) { str := func(value string) { assert.For(value != "", 20) a := xml.Attr{} a.Name.Local = name a.Value = value start.Attr = append(start.Attr, a) } switch v := value.(type) { case string: str(v) case bool: if v { str("true") } else { str("false") } case int: str(strconv.Itoa(v)) case types.Type: str(v.String()) default: halt.As(100, reflect.TypeOf(v), v) } }
func (i *intern) data(t types.Type, cd xml.CharData) (ret interface{}) { switch t { case types.INTEGER, types.REAL: ret = string(cd) case types.BOOLEAN: ret = string(cd) == "true" case types.TRILEAN: if s := string(cd); s == "null" { ret = tri.NIL } else if s == "true" { ret = tri.TRUE } else { ret = tri.FALSE } case types.CHAR: c, _ := strconv.ParseUint(string(cd), 16, 64) ret = rune(c) case types.STRING: data, err := base64.StdEncoding.DecodeString(string(cd)) assert.For(err == nil, 30) ret = strings.TrimPrefix(string(data), prefix) case types.ANY: assert.For(string(cd) == "null", 20) ret = nil default: halt.As(100, t) } return }
func init() { if client, err := couchdb.NewClient(dbUrl, nil); err == nil { statsDb, _ = client.CreateDB(dbStatsName) wordsDb, _ = client.CreateDB(dbWordsName) } else { halt.As(100, err) } }
func (r *stdLd) do(this *Unit, o object) { n := get(this.objects["name"]).toStr() if u := this.loader(n); u != nil { set(o, &value{typ: types.UNIT, val: NewRef(u)}) } else { halt.As(100, "unresolved unit ", n) } }
func init() { if client, err := couchdb.NewClient(dbUrl, nil); err == nil { db, _ = client.CreateDB(dbName) } else { halt.As(100, err) } }
func put(op Op, l Type, r Type, fn dfn) { key := key{op, l, r} if _, ok := om[key]; !ok { om[key] = dfs{f: fn, err: false} } else { halt.As(100, "op already exists ", key) } }
func (x *domm) Produce() (ret *bytes.Buffer) { if data, err := xml.Marshal(x); err == nil { ret = bytes.NewBuffer(data) } else { halt.As(100, ret) } return }
func (i *impl) NewCode(u *ir.Unit, wr io.Writer) { e := &extern{x: u} if data, err := xml.Marshal(e); err == nil { wr.Write([]byte(xml.Header)) io.Copy(wr, bytes.NewBuffer(data)) } else { halt.As(100, err) } }
func (s *exprStack) pop() (ret *value) { if s.vl.Len() > 0 { el := s.vl.Front() ret = s.vl.Remove(el).(*value) } else { halt.As(100, "pop on empty stack") } return }
func (v Value) ToInt() (ret int64) { switch v.T { case INTEGER: ret = v.V.(int64) default: halt.As(100, v.T) } return }
func (v Value) ToBool() (ret bool) { switch v.T { case BOOLEAN: ret = v.V.(bool) default: halt.As(100, v.T) } return }
func thisVal(x interface{}) (ret interface{}) { switch x.(type) { case int64, complex128, float64: ret = x default: halt.As(100, reflect.TypeOf(x)) } return }
func cval(e *ir.ConstExpr) (ret *value) { t := e.Type switch t { case types.INTEGER: b := big.NewInt(0) if err := b.UnmarshalText([]byte(e.Value.(string))); err == nil { v := ThisInt(b) ret = &value{typ: t, val: v} } else { halt.As(100, "wrong integer") } case types.REAL: r := big.NewRat(0, 1) if err := r.UnmarshalText([]byte(e.Value.(string))); err == nil { v := ThisRat(r) ret = &value{typ: t, val: v} } else { halt.As(100, "wrong real") } case types.BOOLEAN: ret = &value{typ: t, val: e.Value.(bool)} case types.TRILEAN: ret = &value{typ: t, val: tri.NIL} case types.CHAR: var v rune switch x := e.Value.(type) { case int32: v = rune(x) case int: v = rune(x) default: halt.As(100, "unsupported rune coding") } ret = &value{typ: t, val: v} case types.STRING: v := e.Value.(string) ret = &value{typ: t, val: v} case types.ANY: ret = &value{typ: t, val: &Any{}} default: halt.As(100, "unknown type ", t, " for ", e) } return }
func (v *value) toAny() (ret *Any) { assert.For(v.typ == types.ANY, 20) switch x := v.val.(type) { case *Any: ret = ThisAny(&value{typ: x.typ, val: x.x}) default: halt.As(100, "wrong any ", reflect.TypeOf(x)) } return }
func (v Value) ToCmp() (ret complex128) { switch v.T { case COMPLEX: ret = v.V.(complex128) default: halt.As(100, v.T) } return }
func (v *value) toCmp() (ret *Cmp) { assert.For(v.typ == types.COMPLEX, 20) switch x := v.val.(type) { case *Cmp: ret = ThisCmp(x) default: halt.As(100, "wrong complex ", reflect.TypeOf(x)) } return }
func (v *value) toBool() (ret bool) { assert.For(v.typ == types.BOOLEAN, 20) switch x := v.val.(type) { case bool: ret = x default: halt.As(100, "wrong boolean ", reflect.TypeOf(x)) } return }
func (v *value) toStr() (ret string) { assert.For(v.typ == types.STRING, 20) switch x := v.val.(type) { case string: ret = x default: halt.As(100, "wrong string ", reflect.TypeOf(x)) } return }
func (v *value) toRef() (ret *Ref) { assert.For(v.typ == types.UNIT, 20) switch x := v.val.(type) { case *Ref: ret = ThisRef(x) default: halt.As(100, "wrong list ", reflect.TypeOf(x)) } return }
func (v *value) asMap() (ret *Map) { assert.For(v.typ == types.MAP, 20) switch x := v.val.(type) { case *Map: ret = x default: halt.As(100, "wrong list ", reflect.TypeOf(x)) } return }
func (v *value) asList() (ret *List) { assert.For(v.typ == types.LIST, 20) switch x := v.val.(type) { case *List: ret = x default: halt.As(100, "wrong list ", reflect.TypeOf(x)) } return }
func (v *value) toRune() (ret rune) { assert.For(v.typ == types.CHAR, 20, v.typ) switch x := v.val.(type) { case rune: ret = x default: halt.As(100, "wrong rune ", reflect.TypeOf(x)) } return }
func (b *exprBuilder) pop() (ret ir.Expression) { b.init() if b.stack.Len() > 0 { ret = b.stack.Remove(b.stack.Front()).(ir.Expression) } else { halt.As(100, "pop on empty stack") } //fmt.Println("pop", ret) return }
func (v *value) toSet() (ret *Set) { assert.For(v.typ == types.SET, 20) switch x := v.val.(type) { case *Set: ret = ThisSet(x) default: halt.As(100, "wrong list ", reflect.TypeOf(x)) } return }