Example #1
0
File: val.go Project: kpmy/clac
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
}
Example #2
0
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
}
Example #3
0
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())
}
Example #4
0
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(";")
	}
}
Example #5
0
File: builder.go Project: kpmy/lomo
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)
}
Example #6
0
File: calc.go Project: kpmy/lomo
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
}
Example #7
0
File: st.go Project: kpmy/lomo
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)
	}
}
Example #8
0
File: st.go Project: kpmy/lomo
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
}
Example #9
0
File: db.go Project: golang-cjr/xep
func init() {
	if client, err := couchdb.NewClient(dbUrl, nil); err == nil {
		statsDb, _ = client.CreateDB(dbStatsName)
		wordsDb, _ = client.CreateDB(dbWordsName)
	} else {
		halt.As(100, err)
	}
}
Example #10
0
File: std.go Project: kpmy/lomo
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)
	}
}
Example #11
0
File: stat.go Project: postman0/xep
func init() {
	if client, err := couchdb.NewClient(dbUrl, nil); err == nil {
		db, _ = client.CreateDB(dbName)
	} else {
		halt.As(100, err)
	}

}
Example #12
0
File: clac.go Project: kpmy/clac
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)
	}
}
Example #13
0
File: entity.go Project: kpmy/xippo
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
}
Example #14
0
File: st.go Project: kpmy/lomo
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)
	}
}
Example #15
0
File: val.go Project: kpmy/lomo
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
}
Example #16
0
File: val.go Project: kpmy/clac
func (v Value) ToInt() (ret int64) {
	switch v.T {
	case INTEGER:
		ret = v.V.(int64)
	default:
		halt.As(100, v.T)
	}
	return
}
Example #17
0
File: val.go Project: kpmy/clac
func (v Value) ToBool() (ret bool) {
	switch v.T {
	case BOOLEAN:
		ret = v.V.(bool)
	default:
		halt.As(100, v.T)
	}
	return
}
Example #18
0
File: val.go Project: kpmy/clac
func thisVal(x interface{}) (ret interface{}) {
	switch x.(type) {
	case int64, complex128, float64:
		ret = x
	default:
		halt.As(100, reflect.TypeOf(x))
	}
	return
}
Example #19
0
File: val.go Project: kpmy/lomo
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
}
Example #20
0
File: val.go Project: kpmy/lomo
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
}
Example #21
0
File: val.go Project: kpmy/clac
func (v Value) ToCmp() (ret complex128) {
	switch v.T {
	case COMPLEX:
		ret = v.V.(complex128)
	default:
		halt.As(100, v.T)

	}
	return
}
Example #22
0
File: val.go Project: kpmy/lomo
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
}
Example #23
0
File: val.go Project: kpmy/lomo
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
}
Example #24
0
File: val.go Project: kpmy/lomo
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
}
Example #25
0
File: val.go Project: kpmy/lomo
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
}
Example #26
0
File: val.go Project: kpmy/lomo
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
}
Example #27
0
File: val.go Project: kpmy/lomo
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
}
Example #28
0
File: val.go Project: kpmy/lomo
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
}
Example #29
0
File: builder.go Project: kpmy/lomo
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
}
Example #30
0
File: val.go Project: kpmy/lomo
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
}