Esempio n. 1
0
// BuildContext to initialize a new scope for evaluating
// production grammars. The scope contains the following
// elements,
//
//  _globalForms:  list of top-level S-expression definitions
//  _nonterminals: list of top-level non-terminals in production
//  _weights:      running weights for each non-terminal rule
//  _globals:      global scope
//      _bagdir:       absolute path to directory containing bags of data
//      _prodfile:     absolute path to production file
//      _random:       reference to seeded *math.rand.Rand object
func BuildContext(
	scope common.Scope,
	seed uint64,
	bagdir, prodfile string) common.Scope {

	scope["_prodfile"] = prodfile
	scope.SetBagdir(bagdir)
	if seed != 0 {
		scope.SetRandom(rand.New(rand.NewSource(int64(seed))))
	} else {
		now := time.Now().UnixNano()
		scope.SetRandom(rand.New(rand.NewSource(int64(now))))
	}
	// verify conflicts between user provided form-names
	// and builtin form-names.
	for name := range scope["_nonterminals"].(common.NTForms) {
		if _, ok := builtins[name]; ok {
			log.Printf("warn: `%v` non-terminal is defined as builtin\n", name)
		}
	}
	return scope.RebuildContext()
}