Ejemplo n.º 1
0
func (sp *SpatialPooler) avgColumnsPerInput() float64 {

	//TODO: extend to support different number of dimensions for inputs and
	// columns
	numDim := mathutil.Max(len(sp.ColumnDimensions), len(sp.InputDimensions))
	columnDims := sp.ColumnDimensions
	inputDims := sp.InputDimensions

	//overlay column dimensions across 1's matrix
	colDim := make([]int, numDim)
	inputDim := make([]int, numDim)

	for i := 0; i < numDim; i++ {
		if i < len(columnDims) {
			colDim[i] = columnDims[i]
		} else {
			colDim[i] = 1
		}

		if i < numDim {
			inputDim[i] = inputDims[i]
		} else {
			inputDim[i] = 1
		}

	}

	sum := 0.0
	for i := 0; i < len(inputDim); i++ {
		sum += float64(colDim[i]) / float64(inputDim[i])
	}
	return sum / float64(numDim)
}
Ejemplo n.º 2
0
Archivo: main.go Proyecto: cznic/yy
func symDocs(sym *y.Symbol) string {
	a := []string{"//\t" + sym.Name + ":"}
	pref := "        "
	w := 0
	for _, rule := range sym.Rules {
		s := ""
		for _, v := range rule.Body {
			switch x := v.(type) {
			case int:
				s += fmt.Sprintf("%s%q", pref, x)
			case string:
				s += pref + x
			default:
				continue
			}

			pref = " "
		}
		if s == "" {
			s = pref + "/* empty */"
		}
		w = mathutil.Max(w, len(s))
		a = append(a, s)
		pref = "|       "
	}
	for i := 2; i < len(a); i++ {
		s := a[i]
		s += fmt.Sprintf("%s// %s %d", strings.Repeat(" ", w+2-len(s)), *oKind, i-1)
		a[i] = s
	}
	return strings.Join(a, "\n//\t")
}
Ejemplo n.º 3
0
/*
 Maps a column to its input bits. This method encapsultes the topology of
the region. It takes the index of the column as an argument and determines
what are the indices of the input vector that are located within the
column's potential pool. The return value is a list containing the indices
of the input bits. The current implementation of the base class only
supports a 1 dimensional topology of columsn with a 1 dimensional topology
of inputs. To extend this class to support 2-D topology you will need to
override this method. Examples of the expected output of this method:
* If the potentialRadius is greater than or equal to the entire input
space, (global visibility), then this method returns an array filled with
all the indices
* If the topology is one dimensional, and the potentialRadius is 5, this
method will return an array containing 5 consecutive values centered on
the index of the column (wrapping around if necessary).
* If the topology is two dimensional (not implemented), and the
potentialRadius is 5, the method should return an array containing 25
'1's, where the exact indices are to be determined by the mapping from
1-D index to 2-D position.

Parameters:
----------------------------
index: The index identifying a column in the permanence, potential
and connectivity matrices.
wrapAround: A boolean value indicating that boundaries should be
region boundaries ignored.
*/
func (sp *SpatialPooler) mapPotential(index int, wrapAround bool) []bool {
	// Distribute column over inputs uniformly
	ratio := float64(index) / float64(mathutil.Max((sp.numColumns-1), 1))
	index = int(float64(sp.numInputs-1) * ratio)

	var indices []int
	indLen := 2*sp.PotentialRadius + 1

	for i := 0; i < indLen; i++ {
		temp := (i + index - sp.PotentialRadius)
		if wrapAround {
			temp = temp % sp.numInputs
			if temp < 0 {
				temp = sp.numInputs + temp
			}
		} else {
			if !(temp >= 0 && temp < sp.numInputs) {
				continue
			}
		}
		//no dupes
		if !utils.ContainsInt(temp, indices) {
			indices = append(indices, temp)
		}
	}

	// Select a subset of the receptive field to serve as the
	// the potential pool

	//shuffle indices
	for i := range indices {
		j := rand.Intn(i + 1)
		indices[i], indices[j] = indices[j], indices[i]
	}

	sampleLen := int(utils.RoundPrec(float64(len(indices))*sp.PotentialPct, 0))
	sample := indices[:sampleLen]
	//project indices onto input mask
	mask := make([]bool, sp.numInputs)
	for i, _ := range mask {
		mask[i] = utils.ContainsInt(i, sample)
	}

	return mask
}
Ejemplo n.º 4
0
Archivo: db.go Proyecto: cznic/xc
// PutBytes stores b in the DB and returns its id.  Zero length byte slices are
// guaranteed to return zero ID. PutBytes Locks the DB before updating it.
func (d *MemDB) PutBytes(b []byte) int {
	if len(b) == 0 {
		return 0
	}

	if len(b) == 1 {
		return int(b[0]) + 1
	}

	d.mu.Lock() // W+

	id := d.nextID
	pi := id >> dbPageShift
	if pi < len(d.pages) {
		p := d.pages[pi]
		off := id & dbPageMask
		if n := cap(p) - off - maxUvarint; n >= len(b) {
			p = p[:cap(p)]
			l := binary.PutUvarint(p[off:], uint64(len(b)))
			copy(p[off+l:], b)
			n = l + len(b)
			d.pages[pi] = p[:off+n]
			d.nextID += n

			d.mu.Unlock() // W-

			return id + 257
		}

		pi++
	}

	p := make([]byte, mathutil.Max(dbPageSize, maxUvarint+len(b)))
	p = p[:binary.PutUvarint(p, uint64(len(b)))]
	p = append(p, b...)
	d.pages = append(d.pages, p)
	id = pi << dbPageShift
	d.nextID = id + mathutil.Min(dbPageSize, len(p))

	d.mu.Unlock() // W-

	return id + 257
}
Ejemplo n.º 5
0
Archivo: main.go Proyecto: cznic/yy
func declareComponents(f io.Writer, spec *y.Parser, list []*y.Symbol, kind string) {
	m := map[string][]string{} // yacc type: []token name
	w := 0
	for _, v := range list {
		ytyp := v.Type
		m[ytyp] = append(m[ytyp], v.Name)
		s := ""
		if val := v.ExplicitValue; v.IsTerminal && val >= 0 && v.Name[0] != '\'' {
			s = fmt.Sprintf(" %d", val)
		}
		w = mathutil.Max(w, len(v.Name)+len(s))
	}
	var a []string
	for tn := range m {
		a = append(a, tn)
	}
	sort.Strings(a)
	for _, tn := range a {
		nms := m[tn]
		var a []string
		for _, nm := range nms {
			a = append(a, nm)
		}
		sort.Strings(a)
		fmt.Fprintf(f, "\n%s\t<%s>\n", kind, tn)
		for _, nm := range a {
			ls := ""
			sym := spec.Syms[nm]
			ns := ""
			if v := sym.ExplicitValue; sym.IsTerminal && v >= 0 && nm[0] != '\'' {
				ns = fmt.Sprintf(" %d", v)
			}
			if s := sym.LiteralString; s != "" {
				ls = strings.Repeat(" ", w-len(ns)+2-len(nm)) + s
			}
			fmt.Fprintf(f, "\t%s%s%s\n", nm, ns, ls)
		}
	}
}
Ejemplo n.º 6
0
func (f *truncFiler) WriteAt(b []byte, off int64) (n int, err error) {
	rq := len(b)
	n = f.totalWritten
	if lim := f.limit; lim >= 0 && n+rq > lim {
		over := n + rq - lim
		rq -= over
		rq = mathutil.Max(rq, 0)
	}

	if n, err = f.fake.WriteAt(b, off); err != nil {
		return
	}

	f.totalWritten += n
	if rq != 0 {
		n, err := f.f.WriteAt(b[:rq], off)
		if err != nil {
			return n, err
		}
		f.realWritten += n
	}
	return
}
Ejemplo n.º 7
0
func main1(in string) (err error) {
	var out io.Writer
	if nm := *oOut; nm != "" {
		var f *os.File
		var e error
		if f, err = os.Create(nm); err != nil {
			return err
		}

		defer func() {
			if e1 := f.Close(); e1 != nil && err == nil {
				err = e1
			}
		}()
		w := bufio.NewWriter(f)
		defer func() {
			if e1 := w.Flush(); e1 != nil && err == nil {
				err = e1
			}
		}()
		buf := bytes.NewBuffer(nil)
		out = buf
		defer func() {
			var dest []byte
			if dest, e = format.Source(buf.Bytes()); e != nil {
				dest = buf.Bytes()
			}

			if _, e = w.Write(dest); e != nil && err == nil {
				err = e
			}
		}()
	}

	var rep io.Writer
	if nm := *oReport; nm != "" {
		f, err1 := os.Create(nm)
		if err1 != nil {
			return err1
		}

		defer func() {
			if e := f.Close(); e != nil && err == nil {
				err = e
			}
		}()
		w := bufio.NewWriter(f)
		defer func() {
			if e := w.Flush(); e != nil && err == nil {
				err = e
			}
		}()
		rep = w
	}

	var xerrors []byte
	if nm := *oXErrors; nm != "" {
		b, err1 := ioutil.ReadFile(nm)
		if err1 != nil {
			return err1
		}

		xerrors = b
	}

	p, err := y.ProcessFile(token.NewFileSet(), in, &y.Options{
		//NoDefault:   *oNoDefault,
		AllowConflicts: true,
		Closures:       *oClosures,
		LA:             *oLA,
		Reducible:      *oReducible,
		Report:         rep,
		Resolved:       *oResolved,
		XErrorsName:    *oXErrors,
		XErrorsSrc:     xerrors,
	})
	if err != nil {
		return err
	}

	if fn := *oXErrorsGen; fn != "" {
		f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0666)
		if err != nil {
			return err
		}

		b := bufio.NewWriter(f)
		if err := p.SkeletonXErrors(b); err != nil {
			return err
		}

		if err := b.Flush(); err != nil {
			return err
		}

		if err := f.Close(); err != nil {
			return err
		}
	}

	msu := make(map[*y.Symbol]int, len(p.Syms)) // sym -> usage
	for nm, sym := range p.Syms {
		if nm == "" || nm == "ε" || nm == "$accept" || nm == "#" {
			continue
		}

		msu[sym] = 0
	}
	var minArg, maxArg int
	for _, state := range p.Table {
		for _, act := range state {
			msu[act.Sym]++
			k, arg := act.Kind()
			if k == 'a' {
				continue
			}

			if k == 'r' {
				arg = -arg
			}
			minArg, maxArg = mathutil.Min(minArg, arg), mathutil.Max(maxArg, arg)
		}
	}
	su := make(symsUsed, 0, len(msu))
	for sym, used := range msu {
		su = append(su, symUsed{sym, used})
	}
	sort.Sort(su)

	// ----------------------------------------------------------- Prologue
	f := strutil.IndentFormatter(out, "\t")
	f.Format("// CAUTION: Generated file - DO NOT EDIT.\n\n")
	f.Format("%s", injectImport(p.Prologue))
	f.Format(`
type %[1]sSymType %i%s%u

type %[1]sXError struct {
	state, xsym int
}
`, *oPref, p.UnionSrc)

	// ---------------------------------------------------------- Constants
	nsyms := map[string]*y.Symbol{}
	a := make([]string, 0, len(msu))
	maxTokName := 0
	for sym := range msu {
		nm := sym.Name
		if nm == "$default" || nm == "$end" || sym.IsTerminal && nm[0] != '\'' && sym.Value > 0 {
			maxTokName = mathutil.Max(maxTokName, len(nm))
			a = append(a, nm)
		}
		nsyms[nm] = sym
	}
	sort.Strings(a)
	f.Format("\nconst (%i\n")
	for _, v := range a {
		nm := v
		switch nm {
		case "error":
			nm = *oPref + "ErrCode"
		case "$default":
			nm = *oPref + "Default"
		case "$end":
			nm = *oPref + "EofCode"
		}
		f.Format("%s%s = %d\n", nm, strings.Repeat(" ", maxTokName-len(nm)+1), nsyms[v].Value)
	}
	minArg-- // eg: [-13, 42], minArg -14 maps -13 to 1 so zero cell values -> empty.
	f.Format("\n%sMaxDepth = 200\n", *oPref)
	f.Format("%sTabOfs   = %d\n", *oPref, minArg)
	f.Format("%u)")

	// ---------------------------------------------------------- Variables
	f.Format("\n\nvar (%i\n")

	// Lex translation table
	f.Format("%sXLAT = map[int]int{%i\n", *oPref)
	xlat := make(map[int]int, len(su))
	var errSym int
	for i, v := range su {
		if v.sym.Name == "error" {
			errSym = i
		}
		xlat[v.sym.Value] = i
		f.Format("%6d: %3d, // %s (%dx)\n", v.sym.Value, i, v.sym.Name, msu[v.sym])
	}
	f.Format("%u}\n")

	// Symbol names
	f.Format("\n%sSymNames = []string{%i\n", *oPref)
	for _, v := range su {
		f.Format("%q,\n", v.sym.Name)
	}
	f.Format("%u}\n")

	// Reduction table
	f.Format("\n%sReductions = []struct{xsym, components int}{%i\n", *oPref)
	for _, rule := range p.Rules {
		f.Format("{%d, %d},\n", xlat[rule.Sym.Value], len(rule.Components))
	}
	f.Format("%u}\n")

	// XError table
	f.Format("\n%[1]sXErrors = map[%[1]sXError]string{%i\n", *oPref)
	for _, xerr := range p.XErrors {
		state := xerr.Stack[len(xerr.Stack)-1]
		xsym := -1
		if xerr.Lookahead != nil {
			xsym = xlat[xerr.Lookahead.Value]
		}
		f.Format("%[1]sXError{%d, %d}: \"%s\",\n", *oPref, state, xsym, xerr.Msg)
	}
	f.Format("%u}\n\n")

	// Parse table
	tbits := 32
	switch n := mathutil.BitLen(maxArg - minArg + 1); {
	case n < 8:
		tbits = 8
	case n < 16:
		tbits = 16
	}
	f.Format("%sParseTab = [%d][]uint%d{%i\n", *oPref, len(p.Table), tbits)
	nCells := 0
	var tabRow sortutil.Uint64Slice
	for si, state := range p.Table {
		tabRow = tabRow[:0]
		max := 0
		for _, act := range state {
			sym := act.Sym
			xsym, ok := xlat[sym.Value]
			if !ok {
				panic("internal error 001")
			}

			max = mathutil.Max(max, xsym)
			kind, arg := act.Kind()
			switch kind {
			case 'a':
				arg = 0
			case 'r':
				arg *= -1
			}
			tabRow = append(tabRow, uint64(xsym)<<32|uint64(arg-minArg))
		}
		nCells += max
		tabRow.Sort()
		col := -1
		if si%5 == 0 {
			f.Format("// %d\n", si)
		}
		f.Format("{")
		for i, v := range tabRow {
			xsym := int(uint32(v >> 32))
			arg := int(uint32(v))
			if col+1 != xsym {
				f.Format("%d: ", xsym)
			}
			switch {
			case i == len(tabRow)-1:
				f.Format("%d", arg)
			default:
				f.Format("%d, ", arg)
			}
			col = xsym
		}
		f.Format("},\n")
	}
	f.Format("%u}\n")
	fmt.Fprintf(os.Stderr, "Parse table entries: %d of %d, x %d bits == %d bytes\n", nCells, len(p.Table)*len(msu), tbits, nCells*tbits/8)
	if n := p.ConflictsSR; n != 0 {
		fmt.Fprintf(os.Stderr, "conflicts: %d shift/reduce\n", n)
	}
	if n := p.ConflictsRR; n != 0 {
		fmt.Fprintf(os.Stderr, "conflicts: %d reduce/reduce\n", n)
	}

	f.Format(`%u)

var %[1]sDebug = 0

type %[1]sLexer interface {
	Lex(lval *%[1]sSymType) int
	Errorf(format string, a ...interface{})
	Errors() []error
}

type %[1]sLexerEx interface {
	%[1]sLexer
	Reduced(rule, state int, lval *%[1]sSymType) bool
}

func %[1]sSymName(c int) (s string) {
	x, ok := %[1]sXLAT[c]
	if ok {
		return %[1]sSymNames[x]
	}

	return __yyfmt__.Sprintf("%%d", c)
}

func %[1]slex1(yylex %[1]sLexer, lval *%[1]sSymType) (n int) {
	n = yylex.Lex(lval)
	if n <= 0 {
		n = %[1]sEofCode
	}
	if %[1]sDebug >= 3 {
		__yyfmt__.Printf("\nlex %%s(%%#x %%d), %[4]s: %[3]s\n", %[1]sSymName(n), n, n, %[4]s)
	}
	return n
}

func %[1]sParse(yylex %[1]sLexer, parser *Parser) int {
	const yyError = %[2]d

	yyEx, _ := yylex.(%[1]sLexerEx)
	var yyn int
	parser.yylval = %[1]sSymType{}
	parser.yyVAL = %[1]sSymType{}
	yyS := parser.cache

	Nerrs := 0   /* number of errors */
	Errflag := 0 /* error recovery flag */
	yyerrok := func() { 
		if %[1]sDebug >= 2 {
			__yyfmt__.Printf("yyerrok()\n")
		}
		Errflag = 0
	}
	_ = yyerrok
	yystate := 0
	yychar := -1
	var yyxchar int
	var yyshift int
	yyp := -1
	goto yystack

ret0:
	return 0

ret1:
	return 1

yystack:
	/* put a state and value onto the stack */
	yyp++
	if yyp >= len(yyS) {
		nyys := make([]%[1]sSymType, len(yyS)*2)
		copy(nyys, yyS)
		yyS = nyys
		parser.cache = yyS
	}
	yyS[yyp] = parser.yyVAL
	yyS[yyp].yys = yystate

yynewstate:
	if yychar < 0 {
		yychar = %[1]slex1(yylex, &parser.yylval)
		var ok bool
		if yyxchar, ok = %[1]sXLAT[yychar]; !ok {
			yyxchar = len(%[1]sSymNames) // > tab width
		}
	}
	if %[1]sDebug >= 4 {
		var a []int
		for _, v := range yyS[:yyp+1] {
			a = append(a, v.yys)
		}
		__yyfmt__.Printf("state stack %%v\n", a)
	}
	row := %[1]sParseTab[yystate]
	yyn = 0
	if yyxchar < len(row) {
		if yyn = int(row[yyxchar]); yyn != 0 {
			yyn += %[1]sTabOfs
		}
	}
	switch {
	case yyn > 0: // shift
		yychar = -1
		parser.yyVAL = parser.yylval
		yystate = yyn
		yyshift = yyn
		if %[1]sDebug >= 2 {
			__yyfmt__.Printf("shift, and goto state %%d\n", yystate)
		}
		if Errflag > 0 {
			Errflag--
		}
		goto yystack
	case yyn < 0: // reduce
	case yystate == 1: // accept
		if %[1]sDebug >= 2 {
			__yyfmt__.Println("accept")
		}
		goto ret0
	}

	if yyn == 0 {
		/* error ... attempt to resume parsing */
		switch Errflag {
		case 0: /* brand new error */
			if %[1]sDebug >= 1 {
				__yyfmt__.Printf("no action for %%s in state %%d\n", %[1]sSymName(yychar), yystate)
			}
			msg, ok := %[1]sXErrors[%[1]sXError{yystate, yyxchar}]
			if !ok {
				msg, ok = %[1]sXErrors[%[1]sXError{yystate, -1}]
			}
			if !ok && yyshift != 0 {
				msg, ok = %[1]sXErrors[%[1]sXError{yyshift, yyxchar}]
			}
			if !ok {
				msg, ok = %[1]sXErrors[%[1]sXError{yyshift, -1}]
			}
			if !ok || msg == "" {
				msg = "syntax error"
			}
			// ignore goyacc error message
			yylex.Errorf("")
			Nerrs++
			fallthrough

		case 1, 2: /* incompletely recovered error ... try again */
			Errflag = 3

			/* find a state where "error" is a legal shift action */
			for yyp >= 0 {
				row := %[1]sParseTab[yyS[yyp].yys]
				if yyError < len(row) {
					yyn = int(row[yyError])+%[1]sTabOfs
					if yyn > 0 { // hit
						if %[1]sDebug >= 2 {
							__yyfmt__.Printf("error recovery found error shift in state %%d\n", yyS[yyp].yys)
						}
						yystate = yyn /* simulate a shift of "error" */
						goto yystack
					}
				}

				/* the current p has no shift on "error", pop stack */
				if %[1]sDebug >= 2 {
					__yyfmt__.Printf("error recovery pops state %%d\n", yyS[yyp].yys)
				}
				yyp--
			}
			/* there is no state on the stack with an error shift ... abort */
			if %[1]sDebug >= 2 {
				__yyfmt__.Printf("error recovery failed\n")
			}
			goto ret1

		case 3: /* no shift yet; clobber input char */
			if %[1]sDebug >= 2 {
				__yyfmt__.Printf("error recovery discards %%s\n", %[1]sSymName(yychar))
			}
			if yychar == %[1]sEofCode {
				goto ret1
			}

			yychar = -1
			goto yynewstate /* try again in the same state */
		}
	}

	r := -yyn
	x0 := %[1]sReductions[r]
	x, n := x0.xsym, x0.components
	yypt := yyp
	_ = yypt // guard against "declared and not used"

	yyp -= n
	if yyp+1 >= len(yyS) {
		nyys := make([]%[1]sSymType, len(yyS)*2)
		copy(nyys, yyS)
		yyS = nyys
		parser.cache = yyS
	}
	parser.yyVAL = yyS[yyp+1]

	/* consult goto table to find next state */
	exState := yystate
	yystate = int(%[1]sParseTab[yyS[yyp].yys][x])+%[1]sTabOfs
	/* reduction by production r */
	if %[1]sDebug >= 2 {
		__yyfmt__.Printf("reduce using rule %%v (%%s), and goto state %%d\n", r, %[1]sSymNames[x], yystate)
	}

	switch r {%i
`,
		*oPref, errSym, *oDlvalf, *oDlval)
	for r, rule := range p.Rules {
		if rule.Action == nil {
			continue
		}

		action := rule.Action.Values
		if len(action) == 0 {
			continue
		}

		if len(action) == 1 {
			part := action[0]
			if part.Type == parser.ActionValueGo {
				src := part.Src
				src = src[1 : len(src)-1] // Remove lead '{' and trail '}'
				if strings.TrimSpace(src) == "" {
					continue
				}
			}
		}

		components := rule.Components
		typ := rule.Sym.Type
		max := len(components)
		if p1 := rule.Parent; p1 != nil {
			max = rule.MaxParentDlr
			components = p1.Components
		}
		f.Format("case %d: ", r)
		for _, part := range action {
			num := part.Num
			switch part.Type {
			case parser.ActionValueGo:
				f.Format("%s", part.Src)
			case parser.ActionValueDlrDlr:
				f.Format("parser.yyVAL.%s", typ)
				if typ == "" {
					panic("internal error 002")
				}
			case parser.ActionValueDlrNum:
				typ := p.Syms[components[num-1]].Type
				if typ == "" {
					panic("internal error 003")
				}
				f.Format("yyS[yypt-%d].%s", max-num, typ)
			case parser.ActionValueDlrTagDlr:
				f.Format("parser.yyVAL.%s", part.Tag)
			case parser.ActionValueDlrTagNum:
				f.Format("yyS[yypt-%d].%s", max-num, part.Tag)
			}
		}
		f.Format("\n")
	}
	f.Format(`%u
	}

	if yyEx != nil && yyEx.Reduced(r, exState, &parser.yyVAL) {
		return -1
	}
	goto yystack /* stack new state and value */
}

%[2]s
`, *oPref, p.Tail)
	_ = oNoLines //TODO Ignored for now
	return nil
}
Ejemplo n.º 8
0
Archivo: y.go Proyecto: postfix/y
func (y *y) report(w io.Writer) {
	f := strutil.IndentFormatter(w, "  ")
	if y.opts.debugSyms {
		var a []string
		max := 0
		for _, v := range y.syms {
			max = mathutil.Max(max, len(v.Name))
		}
		for _, v := range y.syms {
			a = append(a, fmt.Sprintf("%[2]*[1]s val %6[3]d, id %3[5]d, type %[4]q",
				v.Name, -max-1, v.Value, v.Type, v.id),
			)
		}
		sort.Strings(a)
		for _, v := range a {
			f.Format("%s\n", v)
		}
	}
	for si, state := range y.States {
		f.Format("state %d //", si)

		syms, la := state.Syms0()
		for _, s := range syms {
			switch {
			case s == nil:
				f.Format(" <?>")
			case !s.IsTerminal:
				f.Format(" <%s>", s)
			default:
				f.Format(" %s", s)
			}
		}
		if la != nil {
			f.Format(" [%s]", la)
		}
		f.Format("%i\n\n")

		switch {
		case y.opts.Closures:
			for _, item := range state.kernel.closure(y) {
				rule := y.Rules[item.rule()]
				f.Format("%v", item.dump(y))
				if y.opts.LA || item.next(y) == nil {
					switch i, ok := state.kernel.find(item); {
					case ok:
						f.Format("  [%s]", state.lookahead[i].dump(y))
					default:
						if i, ok := state.xitems.find(item); ok {
							f.Format("  [%s]", state.xla[i].dump(y))
						}
					}
				}
				if as := assocStr[rule.Associativity]; as != "" || rule.Precedence >= 0 {
					f.Format("  // assoc %s, prec %d", as, rule.Precedence)
				}
				f.Format("\n")
			}
		default:
			for i, item := range state.kernel {
				rule := y.Rules[item.rule()]
				f.Format("%v", item.dump(y))
				if y.opts.LA || item.dot() == len(rule.Components) {
					f.Format("  [%s]", state.lookahead[i].dump(y))
				}
				if as := assocStr[rule.Associativity]; as != "" || rule.Precedence >= 0 {
					f.Format("  // assoc %s, prec %d", as, rule.Precedence)
				}
				f.Format("\n")
			}
			for i, item := range state.xitems {
				rule := y.Rules[item.rule()]
				f.Format("%v  [%s]", item.dump(y), state.xla[i].dump(y))
				if as := assocStr[rule.Associativity]; as != "" || rule.Precedence >= 0 {
					f.Format(" // assoc %s, prec %d", as, rule.Precedence)
				}
				f.Format("\n")
			}
		}

		f.Format("%i\n")
		a := []string{}
		var w int
		for sym := range state.actions {
			w = mathutil.Max(w, len(sym.Name))
			a = append(a, sym.Name)
		}
		sort.Strings(a)
		type conflict struct {
			sym  *Symbol
			acts []action
		}
		var conflicts []conflict
		for _, nm := range a {
			sym := y.Syms[nm]
			acts := state.actions[sym]
			act := acts[0]
			f.Format("%-*s  %v", w, nm, act)
			if act.kind == 'r' {
				f.Format(" (%s)", y.Rules[act.arg].Sym.Name)
			}
			if len(acts) > 1 {
				conflicts = append(conflicts, conflict{sym, acts})
			}
			f.Format("\n")
		}
		a = a[:0]
		w = 0
		for sym := range state.gotos {
			w = mathutil.Max(w, len(sym.Name))
			a = append(a, sym.Name)
		}
		sort.Strings(a)
		for i, nm := range a {
			if i == 0 {
				f.Format("\n")
			}
			f.Format("%-*s  %v\n", w, nm, state.gotos[y.Syms[nm]])
		}
		for i, conflict := range conflicts {
			if i == 0 {
				if len(state.gotos) != 0 {
					f.Format("\n")
				}
			}
			sym := conflict.sym
			nm := sym.Name
			f.Format("conflict on %v", nm)
			for _, act := range conflict.acts {
				f.Format(", %s", act.String())
			}
			if as := assocStr[sym.Associativity]; as != "" || sym.Precedence >= 0 {
				f.Format(" // %v: assoc %s, prec %d", nm, assocStr[sym.Associativity], sym.Precedence)
			}
			f.Format("\n")
		}
		if len(state.resolved) != 0 {
			f.Format("\n")
		}
		for _, v := range state.resolved {
			f.Format("%s\n", v)
		}
		f.Format("%u%u\n")
	}
}
Ejemplo n.º 9
0
Archivo: main.go Proyecto: cznic/yy
func constructNode(kind int, spec *y.Parser, semanticAction bool, src string, clist []string, ids map[string]bool, sym *y.Symbol) string {
	ignored := isIgnored(sym)
	n := nodes[sym.Name]
	inj := ""
	final := ""
	i := strings.Index(src, "{")
	if src[i+1] != '\n' {
		final = "\n\t\t"
	}
	if ids[magicLx] {
		inj = fmt.Sprintf("\n\t\tlx := yylex.(%s)", *oYylex)
	}
	switch {
	case !semanticAction:
		// ok
	case len(clist) == 0 || len(clist) == 1 && clist[0] == "":
		if ignored {
			break
		}

		inj += fmt.Sprintf("\n\t\t$$ = (%s)(nil)", n.typ)
	default:
		var a []string
		if !ignored {
			if ids[magicLHS] {
				a = append(a, fmt.Sprintf("\n\t\tlhs := &%s{", n.typ[1:]))
			} else {
				a = append(a, fmt.Sprintf("\n\t\t$$ = &%s{", n.typ[1:]))
			}
		}

		m := nodeFieldMap0(sym)
		if !ignored {
			if kind != 0 && !isOpt(sym) {
				a = append(a, fmt.Sprintf("\t%v: %v,", *oKind, kind))
			}
		}
		if !ignored {
			for i, c := range clist {
				if c == "" || c == "error" {
					continue
				}

				csym := spec.Syms[c]
				fldname := c
				if csym.IsTerminal {
					fldname = *oToken
				}
				typ := ytypes[fldname]
				//TODO- if !csym.IsTerminal {
				//TODO- 	typ = fmt.Sprintf("*%s", c)
				//TODO- }
				if typ == "" {
					typ = fmt.Sprintf("*%s", c)
				}
				assert := fmt.Sprintf(".(%s)", typ)
				if !csym.IsTerminal {
					typ = nodes[c].typ
					if csym.Type != *oNode {
						assert = ""
					}
				}
				m[fldname]++
				j := m[fldname]
				if j != 1 {
					fldname = fmt.Sprintf("%s%v", fldname, j)
				}
				ta := ""
				if !csym.IsTerminal {
					re := ""
					if isList(csym) && csym != sym {
						re = ".reverse()"
					}
					ta = fmt.Sprintf("%s%s", assert, re)
				}
				a = append(a, fmt.Sprintf("\t%s: $%d%s,", fldname, i+1, ta))
			}
			w := 0
			for _, v := range a[1:] {
				i := strings.Index(v, ":")
				w = mathutil.Max(w, i)
			}
			for i, v := range a[1:] {
				x := strings.SplitAfterN(v, ":", 2)
				a[i+1] = x[0] + strings.Repeat(" ", w+2-len(x[0])) + x[1]
			}
			a = append(a, "}")
		}
		if ids[magicLHS] {
			a = append(a, "$$ = lhs")
		}
		inj += strings.Join(a, "\n\t\t")
	}
	return src[:i+1] + fmt.Sprintf("%s%s", inj, final) + src[i+1:]
}
Ejemplo n.º 10
0
Archivo: parser.go Proyecto: pkf/ql
func yyParse(yylex yyLexer) int {
	var yyn int
	var yylval yySymType
	var yyVAL yySymType
	yyS := make([]yySymType, yyMaxDepth)

	Nerrs := 0   /* number of errors */
	Errflag := 0 /* error recovery flag */
	yystate := 0
	yychar := -1
	yyp := -1
	goto yystack

ret0:
	return 0

ret1:
	return 1

yystack:
	/* put a state and value onto the stack */
	if yyDebug >= 4 {
		__yyfmt__.Printf("char %v in %v\n", yyTokname(yychar), yyStatname(yystate))
	}

	yyp++
	if yyp >= len(yyS) {
		nyys := make([]yySymType, len(yyS)*2)
		copy(nyys, yyS)
		yyS = nyys
	}
	yyS[yyp] = yyVAL
	yyS[yyp].yys = yystate

yynewstate:
	yyn = yyPact[yystate]
	if yyn <= yyFlag {
		goto yydefault /* simple state */
	}
	if yychar < 0 {
		yychar = yylex1(yylex, &yylval)
	}
	yyn += yychar
	if yyn < 0 || yyn >= yyLast {
		goto yydefault
	}
	yyn = yyAct[yyn]
	if yyChk[yyn] == yychar { /* valid shift */
		yychar = -1
		yyVAL = yylval
		yystate = yyn
		if Errflag > 0 {
			Errflag--
		}
		goto yystack
	}

yydefault:
	/* default state action */
	yyn = yyDef[yystate]
	if yyn == -2 {
		if yychar < 0 {
			yychar = yylex1(yylex, &yylval)
		}

		/* look through exception table */
		xi := 0
		for {
			if yyExca[xi+0] == -1 && yyExca[xi+1] == yystate {
				break
			}
			xi += 2
		}
		for xi += 2; ; xi += 2 {
			yyn = yyExca[xi+0]
			if yyn < 0 || yyn == yychar {
				break
			}
		}
		yyn = yyExca[xi+1]
		if yyn < 0 {
			goto ret0
		}
	}
	if yyn == 0 {
		/* error ... attempt to resume parsing */
		switch Errflag {
		case 0: /* brand new error */
			yylex.Error("syntax error")
			Nerrs++
			if yyDebug >= 1 {
				__yyfmt__.Printf("%s", yyStatname(yystate))
				__yyfmt__.Printf(" saw %s\n", yyTokname(yychar))
			}
			fallthrough

		case 1, 2: /* incompletely recovered error ... try again */
			Errflag = 3

			/* find a state where "error" is a legal shift action */
			for yyp >= 0 {
				yyn = yyPact[yyS[yyp].yys] + yyErrCode
				if yyn >= 0 && yyn < yyLast {
					yystate = yyAct[yyn] /* simulate a shift of "error" */
					if yyChk[yystate] == yyErrCode {
						goto yystack
					}
				}

				/* the current p has no shift on "error", pop stack */
				if yyDebug >= 2 {
					__yyfmt__.Printf("error recovery pops state %d\n", yyS[yyp].yys)
				}
				yyp--
			}
			/* there is no state on the stack with an error shift ... abort */
			goto ret1

		case 3: /* no shift yet; clobber input char */
			if yyDebug >= 2 {
				__yyfmt__.Printf("error recovery discards %s\n", yyTokname(yychar))
			}
			if yychar == yyEOFCode {
				goto ret1
			}
			yychar = -1
			goto yynewstate /* try again in the same state */
		}
	}

	/* reduction by production yyn */
	if yyDebug >= 2 {
		__yyfmt__.Printf("reduce %v in:\n\t%v\n", yyn, yyStatname(yystate))
	}

	yynt := yyn
	yypt := yyp
	_ = yypt // guard against "declared and not used"

	yyp -= yyR2[yyn]
	yyVAL = yyS[yyp+1]

	/* consult goto table to find next state */
	yyn = yyR1[yyn]
	yyg := yyPgo[yyn]
	yyj := yyg + yyS[yyp].yys + 1

	if yyj >= yyLast {
		yystate = yyAct[yyg]
	} else {
		yystate = yyAct[yyj]
		if yyChk[yystate] != -yyn {
			yystate = yyAct[yyg]
		}
	}
	// dummy call; replaced with literal code
	switch yynt {

	case 1:

		{
			yyVAL.item = &alterTableAddStmt{tableName: yyS[yypt-2].item.(string), c: yyS[yypt-0].item.(*col)}
		}
	case 2:

		{
			yyVAL.item = &alterTableDropColumnStmt{tableName: yyS[yypt-3].item.(string), colName: yyS[yypt-0].item.(string)}
		}
	case 3:

		{
			yyVAL.item = assignment{colName: yyS[yypt-2].item.(string), expr: yyS[yypt-0].item.(expression)}
		}
	case 4:

		{
			yyVAL.item = append([]assignment{yyS[yypt-2].item.(assignment)}, yyS[yypt-1].item.([]assignment)...)
		}
	case 5:

		{
			yyVAL.item = []assignment{}
		}
	case 6:

		{
			yyVAL.item = append(yyS[yypt-2].item.([]assignment), yyS[yypt-0].item.(assignment))
		}
	case 9:

		{
			yyVAL.item = beginTransactionStmt{}
		}
	case 10:

		{
			yyVAL.item = yyS[yypt-1].item
		}
	case 11:

		{
			yyVAL.item = []expression{}
		}
	case 12:
		yyVAL.item = yyS[yypt-0].item
	case 13:

		{
			yyVAL.item = &col{name: yyS[yypt-1].item.(string), typ: yyS[yypt-0].item.(int)}
		}
	case 14:
		yyVAL.item = yyS[yypt-0].item
	case 15:

		{
			yyVAL.item = append([]string{yyS[yypt-2].item.(string)}, yyS[yypt-1].item.([]string)...)
		}
	case 16:

		{
			yyVAL.item = []string{}
		}
	case 17:

		{
			yyVAL.item = append(yyS[yypt-2].item.([]string), yyS[yypt-0].item.(string))
		}
	case 20:

		{
			yyVAL.item = commitStmt{}
		}
	case 21:

		{
			yyVAL.item = &conversion{typ: yyS[yypt-3].item.(int), val: yyS[yypt-1].item.(expression)}
		}
	case 22:

		{
			indexName, tableName, columnName := yyS[yypt-5].item.(string), yyS[yypt-3].item.(string), yyS[yypt-1].item.(string)
			yyVAL.item = &createIndexStmt{unique: yyS[yypt-8].item.(bool), ifNotExists: yyS[yypt-6].item.(bool), indexName: indexName, tableName: tableName, colName: columnName}
			if indexName == tableName || indexName == columnName {
				yylex.(*lexer).err("index name collision: %s", indexName)
				return 1
			}

			if isSystemName[indexName] || isSystemName[tableName] {
				yylex.(*lexer).err("name is used for system tables: %s", indexName)
				return 1
			}
		}
	case 23:

		{
			indexName, tableName, columnName := yyS[yypt-7].item.(string), yyS[yypt-5].item.(string), yyS[yypt-3].item.(string)
			yyVAL.item = &createIndexStmt{unique: yyS[yypt-10].item.(bool), ifNotExists: yyS[yypt-8].item.(bool), indexName: indexName, tableName: tableName, colName: "id()"}
			if yyS[yypt-3].item.(string) != "id" {
				yylex.(*lexer).err("only the built-in function id() can be used in index: %s()", columnName)
				return 1
			}

			if indexName == tableName {
				yylex.(*lexer).err("index name collision: %s", indexName)
				return 1
			}

			if isSystemName[indexName] || isSystemName[tableName] {
				yylex.(*lexer).err("name is used for system tables: %s", indexName)
				return 1
			}
		}
	case 24:

		{
			yyVAL.item = false
		}
	case 25:

		{
			yyVAL.item = true
		}
	case 26:

		{
			yyVAL.item = false
		}
	case 27:

		{
			yyVAL.item = true
		}
	case 28:

		{
			nm := yyS[yypt-5].item.(string)
			yyVAL.item = &createTableStmt{tableName: nm, cols: append([]*col{yyS[yypt-3].item.(*col)}, yyS[yypt-2].item.([]*col)...)}
			if isSystemName[nm] {
				yylex.(*lexer).err("name is used for system tables: %s", nm)
				return 1
			}
		}
	case 29:

		{
			nm := yyS[yypt-5].item.(string)
			yyVAL.item = &createTableStmt{ifNotExists: true, tableName: nm, cols: append([]*col{yyS[yypt-3].item.(*col)}, yyS[yypt-2].item.([]*col)...)}
			if isSystemName[nm] {
				yylex.(*lexer).err("name is used for system tables: %s", nm)
				return 1
			}
		}
	case 30:

		{
			yyVAL.item = []*col{}
		}
	case 31:

		{
			yyVAL.item = append(yyS[yypt-2].item.([]*col), yyS[yypt-0].item.(*col))
		}
	case 34:

		{
			yyVAL.item = &truncateTableStmt{yyS[yypt-0].item.(string)}
		}
	case 35:

		{
			yyVAL.item = &deleteStmt{tableName: yyS[yypt-1].item.(string), where: yyS[yypt-0].item.(*whereRset).expr}
		}
	case 36:

		{
			yyVAL.item = &dropIndexStmt{ifExists: yyS[yypt-1].item.(bool), indexName: yyS[yypt-0].item.(string)}
		}
	case 37:

		{
			yyVAL.item = false
		}
	case 38:

		{
			yyVAL.item = true
		}
	case 39:

		{
			nm := yyS[yypt-0].item.(string)
			yyVAL.item = &dropTableStmt{tableName: nm}
			if isSystemName[nm] {
				yylex.(*lexer).err("name is used for system tables: %s", nm)
				return 1
			}
		}
	case 40:

		{
			nm := yyS[yypt-0].item.(string)
			yyVAL.item = &dropTableStmt{ifExists: true, tableName: nm}
			if isSystemName[nm] {
				yylex.(*lexer).err("name is used for system tables: %s", nm)
				return 1
			}
		}
	case 41:

		{
			yyVAL.item = nil
		}
	case 42:
		yyVAL.item = yyS[yypt-0].item
	case 43:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation(oror, yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 44:

		{
			yyVAL.item = append([]expression{yyS[yypt-2].item.(expression)}, yyS[yypt-1].item.([]expression)...)
		}
	case 45:

		{
			yyVAL.item = []expression(nil)
		}
	case 46:

		{
			yyVAL.item = append(yyS[yypt-2].item.([]expression), yyS[yypt-0].item.(expression))
		}
	case 49:
		yyVAL.item = yyS[yypt-0].item
	case 50:

		{
			yyVAL.item = &pIn{expr: yyS[yypt-4].item.(expression), list: yyS[yypt-1].item.([]expression)}
		}
	case 51:

		{
			yyVAL.item = &pIn{expr: yyS[yypt-5].item.(expression), not: true, list: yyS[yypt-1].item.([]expression)}
		}
	case 52:

		{
			yyVAL.item = &pBetween{expr: yyS[yypt-4].item.(expression), l: yyS[yypt-2].item.(expression), h: yyS[yypt-0].item.(expression)}
		}
	case 53:

		{
			yyVAL.item = &pBetween{expr: yyS[yypt-5].item.(expression), not: true, l: yyS[yypt-2].item.(expression), h: yyS[yypt-0].item.(expression)}
		}
	case 54:

		{
			yyVAL.item = &isNull{expr: yyS[yypt-2].item.(expression)}
		}
	case 55:

		{
			yyVAL.item = &isNull{expr: yyS[yypt-3].item.(expression), not: true}
		}
	case 56:
		yyVAL.item = yyS[yypt-0].item
	case 57:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation(ge, yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 58:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation('>', yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 59:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation(le, yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 60:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation('<', yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 61:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation(neq, yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 62:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation(eq, yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 63:

		{
			expr, name := yyS[yypt-1].item.(expression), yyS[yypt-0].item.(string)
			if name == "" {
				s, ok := expr.(*ident)
				if ok {
					name = s.s
				}
			}
			yyVAL.item = &fld{expr: expr, name: name}
		}
	case 64:

		{
			yyVAL.item = ""
		}
	case 65:

		{
			yyVAL.item = yyS[yypt-0].item
		}
	case 66:

		{
			yyVAL.item = []*fld{yyS[yypt-0].item.(*fld)}
		}
	case 67:

		{
			l, f := yyS[yypt-2].item.([]*fld), yyS[yypt-0].item.(*fld)
			if f.name != "" {
				if f := findFld(l, f.name); f != nil {
					yylex.(*lexer).err("duplicate field name %q", f.name)
					return 1
				}
			}

			yyVAL.item = append(yyS[yypt-2].item.([]*fld), yyS[yypt-0].item.(*fld))
		}
	case 68:

		{
			yyVAL.item = &groupByRset{colNames: yyS[yypt-0].item.([]string)}
		}
	case 69:

		{
			yyVAL.item = yyS[yypt-1].item
		}
	case 70:

		{
			yyVAL.item = &insertIntoStmt{tableName: yyS[yypt-7].item.(string), colNames: yyS[yypt-6].item.([]string), lists: append([][]expression{yyS[yypt-3].item.([]expression)}, yyS[yypt-1].item.([][]expression)...)}
		}
	case 71:

		{
			yyVAL.item = &insertIntoStmt{tableName: yyS[yypt-2].item.(string), colNames: yyS[yypt-1].item.([]string), sel: yyS[yypt-0].item.(*selectStmt)}
		}
	case 72:

		{
			yyVAL.item = []string{}
		}
	case 73:

		{
			yyVAL.item = yyS[yypt-1].item
		}
	case 74:

		{
			yyVAL.item = [][]expression{}
		}
	case 75:

		{
			yyVAL.item = append(yyS[yypt-4].item.([][]expression), yyS[yypt-1].item.([]expression))
		}
	case 78:
		yyVAL.item = yyS[yypt-0].item
	case 79:
		yyVAL.item = yyS[yypt-0].item
	case 80:
		yyVAL.item = yyS[yypt-0].item
	case 81:
		yyVAL.item = yyS[yypt-0].item
	case 82:
		yyVAL.item = yyS[yypt-0].item
	case 83:
		yyVAL.item = yyS[yypt-0].item
	case 84:
		yyVAL.item = yyS[yypt-0].item
	case 85:

		{
			yyVAL.item = value{yyS[yypt-0].item}
		}
	case 86:

		{
			n := yyS[yypt-0].item.(int)
			yyVAL.item = parameter{n}
			l := yylex.(*lexer)
			l.params = mathutil.Max(l.params, n)
			if n == 0 {
				l.err("parameter number must be non zero")
				return 1
			}
		}
	case 87:

		{
			yyVAL.item = &ident{yyS[yypt-0].item.(string)}
		}
	case 88:

		{
			yyVAL.item = &pexpr{expr: yyS[yypt-1].item.(expression)}
		}
	case 89:

		{
			yyVAL.item = &orderByRset{by: yyS[yypt-1].item.([]expression), asc: yyS[yypt-0].item.(bool)}
		}
	case 90:

		{
			yyVAL.item = true // ASC by default
		}
	case 91:

		{
			yyVAL.item = true
		}
	case 92:

		{
			yyVAL.item = false
		}
	case 93:
		yyVAL.item = yyS[yypt-0].item
	case 94:
		yyVAL.item = yyS[yypt-0].item
	case 95:

		{
			var err error
			if yyVAL.item, err = newIndex(yyS[yypt-1].item.(expression), yyS[yypt-0].item.(expression)); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 96:

		{
			var err error
			s := yyS[yypt-0].item.([2]*expression)
			if yyVAL.item, err = newSlice(yyS[yypt-1].item.(expression), s[0], s[1]); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 97:

		{
			x := yylex.(*lexer)
			f, ok := yyS[yypt-1].item.(*ident)
			if !ok {
				x.err("expected identifier or qualified identifier")
				return 1
			}

			var err error
			var agg bool
			if yyVAL.item, agg, err = newCall(f.s, yyS[yypt-0].item.([]expression)); err != nil {
				x.err("%v", err)
				return 1
			}
			if n := len(x.agg); n > 0 {
				x.agg[n-1] = x.agg[n-1] || agg
			}
		}
	case 98:
		yyVAL.item = yyS[yypt-0].item
	case 99:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation('^', yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 100:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation('|', yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 101:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation('-', yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 102:

		{
			var err error
			yyVAL.item, err = newBinaryOperation('+', yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 103:
		yyVAL.item = yyS[yypt-0].item
	case 104:

		{
			var err error
			yyVAL.item, err = newBinaryOperation(andnot, yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 105:

		{
			var err error
			yyVAL.item, err = newBinaryOperation('&', yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 106:

		{
			var err error
			yyVAL.item, err = newBinaryOperation(lsh, yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 107:

		{
			var err error
			yyVAL.item, err = newBinaryOperation(rsh, yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 108:

		{
			var err error
			yyVAL.item, err = newBinaryOperation('%', yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 109:

		{
			var err error
			yyVAL.item, err = newBinaryOperation('/', yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 110:

		{
			var err error
			yyVAL.item, err = newBinaryOperation('*', yyS[yypt-2].item, yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 111:
		yyVAL.item = yyS[yypt-0].item
	case 112:

		{
			yyVAL.item = fmt.Sprintf("%s.%s", yyS[yypt-2].item.(string), yyS[yypt-0].item.(string))
		}
	case 113:

		{
			yyVAL.item = []interface{}{yyS[yypt-1].item, yyS[yypt-0].item}
		}
	case 114:
		yyVAL.item = yyS[yypt-0].item
	case 115:

		{
			yyVAL.item = yyS[yypt-2].item
		}
	case 118:

		{
			yyVAL.item = ""
		}
	case 119:

		{
			yyVAL.item = yyS[yypt-0].item
		}
	case 120:

		{
			yyVAL.list = []interface{}{yyS[yypt-0].item}
		}
	case 121:

		{
			yyVAL.list = append(yyS[yypt-2].list, yyS[yypt-0].item)
		}
	case 122:

		{
			yyVAL.item = rollbackStmt{}
		}
	case 123:

		{
			x := yylex.(*lexer)
			n := len(x.agg)
			yyVAL.item = &selectStmt{
				distinct:      yyS[yypt-8].item.(bool),
				flds:          yyS[yypt-7].item.([]*fld),
				from:          &crossJoinRset{sources: yyS[yypt-5].list},
				hasAggregates: x.agg[n-1],
				where:         yyS[yypt-4].item.(*whereRset),
				group:         yyS[yypt-3].item.(*groupByRset),
				order:         yyS[yypt-2].item.(*orderByRset),
				limit:         yyS[yypt-1].item.(*limitRset),
				offset:        yyS[yypt-0].item.(*offsetRset),
			}
			x.agg = x.agg[:n-1]
		}
	case 124:

		{
			x := yylex.(*lexer)
			n := len(x.agg)
			yyVAL.item = &selectStmt{
				distinct:      yyS[yypt-9].item.(bool),
				flds:          yyS[yypt-8].item.([]*fld),
				from:          &crossJoinRset{sources: yyS[yypt-6].list},
				hasAggregates: x.agg[n-1],
				where:         yyS[yypt-4].item.(*whereRset),
				group:         yyS[yypt-3].item.(*groupByRset),
				order:         yyS[yypt-2].item.(*orderByRset),
				limit:         yyS[yypt-1].item.(*limitRset),
				offset:        yyS[yypt-0].item.(*offsetRset),
			}
			x.agg = x.agg[:n-1]
		}
	case 125:

		{
			yyVAL.item = (*limitRset)(nil)
		}
	case 126:

		{
			yyVAL.item = &limitRset{expr: yyS[yypt-0].item.(expression)}
		}
	case 127:

		{
			yyVAL.item = (*offsetRset)(nil)
		}
	case 128:

		{
			yyVAL.item = &offsetRset{expr: yyS[yypt-0].item.(expression)}
		}
	case 129:

		{
			yyVAL.item = false
		}
	case 130:

		{
			yyVAL.item = true
		}
	case 131:

		{
			yyVAL.item = []*fld{}
		}
	case 132:

		{
			yyVAL.item = yyS[yypt-0].item
		}
	case 133:

		{
			yyVAL.item = yyS[yypt-1].item
		}
	case 134:

		{
			yyVAL.item = (*whereRset)(nil)
		}
	case 135:
		yyVAL.item = yyS[yypt-0].item
	case 136:

		{
			yyVAL.item = (*groupByRset)(nil)
		}
	case 137:
		yyVAL.item = yyS[yypt-0].item
	case 138:

		{
			yyVAL.item = (*orderByRset)(nil)
		}
	case 139:
		yyVAL.item = yyS[yypt-0].item
	case 140:

		{
			yyVAL.item = [2]*expression{nil, nil}
		}
	case 141:

		{
			hi := yyS[yypt-1].item.(expression)
			yyVAL.item = [2]*expression{nil, &hi}
		}
	case 142:

		{
			lo := yyS[yypt-2].item.(expression)
			yyVAL.item = [2]*expression{&lo, nil}
		}
	case 143:

		{
			lo := yyS[yypt-3].item.(expression)
			hi := yyS[yypt-1].item.(expression)
			yyVAL.item = [2]*expression{&lo, &hi}
		}
	case 144:
		yyVAL.item = yyS[yypt-0].item
	case 145:
		yyVAL.item = yyS[yypt-0].item
	case 146:
		yyVAL.item = yyS[yypt-0].item
	case 147:
		yyVAL.item = yyS[yypt-0].item
	case 148:
		yyVAL.item = yyS[yypt-0].item
	case 149:
		yyVAL.item = yyS[yypt-0].item
	case 150:
		yyVAL.item = yyS[yypt-0].item
	case 151:
		yyVAL.item = yyS[yypt-0].item
	case 152:
		yyVAL.item = yyS[yypt-0].item
	case 153:
		yyVAL.item = yyS[yypt-0].item
	case 154:
		yyVAL.item = yyS[yypt-0].item
	case 155:
		yyVAL.item = yyS[yypt-0].item
	case 156:
		yyVAL.item = yyS[yypt-0].item
	case 157:
		yyVAL.item = yyS[yypt-0].item
	case 158:

		{
			if yyS[yypt-0].item != nil {
				yylex.(*lexer).list = []stmt{yyS[yypt-0].item.(stmt)}
			}
		}
	case 159:

		{
			if yyS[yypt-0].item != nil {
				yylex.(*lexer).list = append(yylex.(*lexer).list, yyS[yypt-0].item.(stmt))
			}
		}
	case 160:
		yyVAL.item = yyS[yypt-0].item
	case 161:
		yyVAL.item = yyS[yypt-0].item
	case 162:

		{
			var err error
			if yyVAL.item, err = newBinaryOperation(andand, yyS[yypt-2].item, yyS[yypt-0].item); err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 163:

		{
			yyVAL.item = &truncateTableStmt{tableName: yyS[yypt-0].item.(string)}
		}
	case 164:
		yyVAL.item = yyS[yypt-0].item
	case 165:
		yyVAL.item = yyS[yypt-0].item
	case 166:
		yyVAL.item = yyS[yypt-0].item
	case 167:
		yyVAL.item = yyS[yypt-0].item
	case 168:
		yyVAL.item = yyS[yypt-0].item
	case 169:
		yyVAL.item = yyS[yypt-0].item
	case 170:
		yyVAL.item = yyS[yypt-0].item
	case 171:
		yyVAL.item = yyS[yypt-0].item
	case 172:
		yyVAL.item = yyS[yypt-0].item
	case 173:
		yyVAL.item = yyS[yypt-0].item
	case 174:
		yyVAL.item = yyS[yypt-0].item
	case 175:
		yyVAL.item = yyS[yypt-0].item
	case 176:
		yyVAL.item = yyS[yypt-0].item
	case 177:
		yyVAL.item = yyS[yypt-0].item
	case 178:
		yyVAL.item = yyS[yypt-0].item
	case 179:
		yyVAL.item = yyS[yypt-0].item
	case 180:
		yyVAL.item = yyS[yypt-0].item
	case 181:
		yyVAL.item = yyS[yypt-0].item
	case 182:
		yyVAL.item = yyS[yypt-0].item
	case 183:
		yyVAL.item = yyS[yypt-0].item
	case 184:
		yyVAL.item = yyS[yypt-0].item
	case 185:
		yyVAL.item = yyS[yypt-0].item
	case 186:
		yyVAL.item = yyS[yypt-0].item
	case 187:
		yyVAL.item = yyS[yypt-0].item
	case 188:

		{
			yyVAL.item = &updateStmt{tableName: yyS[yypt-3].item.(string), list: yyS[yypt-1].item.([]assignment), where: yyS[yypt-0].item.(*whereRset).expr}
		}
	case 189:

		{
			yyVAL.item = nowhere
		}
	case 190:
		yyVAL.item = yyS[yypt-0].item
	case 191:
		yyVAL.item = yyS[yypt-0].item
	case 192:

		{
			var err error
			yyVAL.item, err = newUnaryOperation('^', yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 193:

		{
			var err error
			yyVAL.item, err = newUnaryOperation('!', yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 194:

		{
			var err error
			yyVAL.item, err = newUnaryOperation('-', yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 195:

		{
			var err error
			yyVAL.item, err = newUnaryOperation('+', yyS[yypt-0].item)
			if err != nil {
				yylex.(*lexer).err("%v", err)
				return 1
			}
		}
	case 196:

		{
			yyVAL.item = &whereRset{expr: yyS[yypt-0].item.(expression)}
		}
	}
	goto yystack /* stack new state and value */
}
Ejemplo n.º 11
0
func encUint(n uint64, b *[]byte) {
	bits := mathutil.Max(1, mathutil.BitLenUint64(n))
	encUintPrefix(gbUintP1+byte((bits-1)>>3), n, b)
}
Ejemplo n.º 12
0
func minmax(a, b int) {
	fmt.Println("Min:", mathutil.Min(a, b))
	fmt.Println("Max:", mathutil.Max(a, b))
}
Ejemplo n.º 13
0
func (tp *TemporalPooler) updateStatsInferEnd(stats *TpStats, bottomUpNZ []int,
	predictedState *SparseBinaryMatrix, colConfidence []float64) {
	// Return if not collecting stats
	if !tp.params.CollectStats {
		return
	}

	stats.NInfersSinceReset++

	// Compute the prediction score, how well the prediction from the last
	// time step predicted the current bottom-up input
	numExtra2, numMissing2, confidences2, _ := tp.checkPrediction2([][]int{bottomUpNZ}, predictedState, colConfidence, false)
	predictionScore := confidences2[0].PredictionScore
	positivePredictionScore := confidences2[0].PositivePredictionScore
	negativePredictionScore := confidences2[0].NegativePredictionScore

	// Store the stats that don't depend on burn-in
	stats.CurPredictionScore2 = predictionScore
	stats.CurFalseNegativeScore = negativePredictionScore
	stats.CurFalsePositiveScore = positivePredictionScore

	stats.CurMissing = float64(numMissing2)
	stats.CurExtra = float64(numExtra2)

	// If we are passed the burn-in period, update the accumulated stats
	// Here's what various burn-in values mean:
	// 0: try to predict the first element of each sequence and all subsequent
	// 1: try to predict the second element of each sequence and all subsequent
	// etc.
	if stats.NInfersSinceReset <= tp.params.BurnIn {
		return
	}

	// Burn-in related stats
	stats.NPredictions++
	numExpected := mathutil.Max(1, len(bottomUpNZ))

	stats.TotalMissing += float64(numMissing2)
	stats.TotalExtra += float64(numExtra2)
	stats.PctExtraTotal += 100.0 * float64(numExtra2) / float64(numExpected)
	stats.PctMissingTotal += 100.0 * float64(numMissing2) / float64(numExpected)
	stats.PredictionScoreTotal2 += predictionScore
	stats.FalseNegativeScoreTotal += 1.0 - positivePredictionScore
	stats.FalsePositiveScoreTotal += negativePredictionScore

	if tp.collectSequenceStats {
		// Collect cell confidences for every cell that correctly predicted current
		// bottom up input. Normalize confidence across each column
		cc := tp.DynamicState.CellConfidence.Copy()

		for r := 0; r < cc.Rows(); r++ {
			for c := 0; c < cc.Cols(); c++ {
				if !tp.DynamicState.InfActiveState.Get(r, c) {
					cc.Set(r, c, 0)
				}
			}
		}
		sconf := make([]int, cc.Rows())
		for r := 0; r < cc.Rows(); r++ {
			count := 0
			for c := 0; c < cc.Cols(); c++ {
				if cc.Get(r, c) > 0 {
					count++
				}
			}
			sconf[r] = count
		}

		for r := 0; r < cc.Rows(); r++ {
			for c := 0; c < cc.Cols(); c++ {
				temp := cc.Get(r, c)
				cc.Set(r, c, temp/float64(sconf[r]))
			}
		}

		// Update cell confidence histogram: add column-normalized confidence
		// scores to the histogram
		stats.ConfHistogram.Add(cc)
	}

}
Ejemplo n.º 14
0
func TestRollbackFiler4(t *testing.T) {
	const (
		maxSize    = 1e6
		maxChange  = maxSize/100 + 4
		maxChanges = 10
		maxNest    = 3
	)

	var r *RollbackFiler
	f := NewMemFiler()

	checkpoint := func(sz int64) (err error) {
		return f.Truncate(sz)
	}

	r, err := NewRollbackFiler(f, checkpoint, f)
	if err != nil {
		t.Fatal(err)
	}

	rng := rand.New(rand.NewSource(42))

	ref := make([]byte, 2*maxSize)
	for i := range ref {
		ref[i] = byte(rng.Int())
	}

	var finalSize int

	var fn func(int, int, []byte) (int, []byte)
	fn = func(nest, inSize int, in []byte) (outSize int, out []byte) {
		defer func() {
			for i := outSize; i < len(out); i++ {
				out[i] = 0
			}
			finalSize = mathutil.Max(finalSize, outSize)
		}()

		out = make([]byte, len(in), 2*maxSize)
		copy(out, in)
		if err := r.BeginUpdate(); err != nil {
			t.Fatal(err)
		}

		for i := 0; i < maxChanges; i++ {
			changeLen := rng.Intn(maxChange) + 4
			changeOff := rng.Intn(maxSize * 3 / 2)
			b := make([]byte, changeLen)
			for i := range b {
				b[i] = byte(rng.Int())
			}
			if n, err := r.WriteAt(b, int64(changeOff)); n != len(b) || err != nil {
				t.Fatal(n, len(b), err)
			}
		}

		if err := r.Rollback(); err != nil {
			t.Fatal(err)
		}

		if err := r.BeginUpdate(); err != nil {
			t.Fatal(err)
		}

		for i := 0; i < maxChanges; i++ {
			changeLen := rng.Intn(maxChange) + 4
			changeOff := rng.Intn(maxSize * 3 / 2)
			b := make([]byte, changeLen)
			for i := range b {
				b[i] = byte(rng.Int())
			}
			if n, err := r.WriteAt(b, int64(changeOff)); n != len(b) || err != nil {
				t.Fatal(n, len(b), err)
			}
			copy(out[changeOff:], b)
			copy(ref[changeOff:], b)
		}

		newSize := rng.Intn(maxSize*3/2) + 4
		if nest == maxNest {
			if err := r.EndUpdate(); err != nil {
				t.Fatal(err)
			}

			return newSize, out
		}

		outSize, out = fn(nest+1, newSize, out)
		if err := r.EndUpdate(); err != nil {
			t.Fatal(err)
		}

		return
	}

	sz, result := fn(0, maxSize, ref)
	if g, e := sz, finalSize; g != e {
		t.Fatal(err)
	}

	g, e := result[:sz], ref[:sz]
	if !bytes.Equal(g, e) {
		if len(g) == len(e) {
			x := make([]byte, len(g))
			for i := range x {
				if g[i] != e[i] {
					x[i] = 'X'
				}
			}
			//t.Logf("Data diff\n%s", hex.Dump(x))
		}
		//t.Fatalf("Data don't match: got\n%sexp:\n%s", hex.Dump(g), hex.Dump(e))
		t.Fatalf("Data don't match")
	}
}