Exemple #1
0
func (i *Interpreter) randomInstruction() Code {
	var instr string

	n := i.Rand.Intn(len(i.listOfInstructions) + len(i.listOfDefinitions))

	if n < len(i.listOfInstructions) {
		instr = i.listOfInstructions[n]
	} else {
		instr = i.listOfDefinitions[n-len(i.listOfInstructions)]
	}

	switch instr {
	case "INTEGER-ERC":
		// Generate ephemeral random constant integer
		high := i.Options.MaxRandomInteger
		low := i.Options.MinRandomInteger
		instr = fmt.Sprint(i.Rand.Int63n(high+1-low) + low)

	case "FLOAT-ERC":
		// Generate ephemeral random constant float
		high := i.Options.MaxRandomFloat
		low := i.Options.MinRandomFloat
		instr = fmt.Sprint(i.Rand.Float64()*(high-low) + low)
		if !strings.Contains(instr, ".") {
			instr += ".0"
		}

	case "NAME-ERC":
		// Generate ephemeral random constant NAME
		if i.Rand.Float64() < i.Options.NewERCNameProbabilty || i.numNamesGenerated == 0 {
			// Generate a new random NAME
			instr = goremutake.Encode(i.numNamesGenerated)
			i.numNamesGenerated++
		} else {
			// Use a random, already generated NAME
			instr = goremutake.Encode(uint(i.Rand.Intn(int(i.numNamesGenerated))))
		}
	}

	return Code{Length: 1, Literal: instr}
}
Exemple #2
0
// SaveTask saves a task (or tasks) to the store and returns the first error
// encountered. It does not commit to disk, use `Commit` to do that.
func (j *JSONStore) SaveTask(ts ...*Task) error {
	j.taskLock.Lock()
	defer j.taskLock.Unlock()

	for _, t := range ts {
		// assign the Task an ID if it doesn't have one already
		if t.ID == "" {
			t.ID = goremutake.Encode(j.NextID())
		}

		j.Tasks[t.ID] = t
	}

	return nil
}
Exemple #3
0
// newNameStack returns a new NAME stack
func newNameStack(interpreter *Interpreter) *Stack {
	s := &Stack{
		Functions: make(map[string]func()),
	}

	s.Functions["="] = func() {
		if !interpreter.StackOK("name", 2) || !interpreter.StackOK("boolean", 0) {
			return
		}

		n1 := interpreter.Stacks["name"].Pop().(string)
		n2 := interpreter.Stacks["name"].Pop().(string)
		interpreter.Stacks["boolean"].Push(n1 == n2)
	}

	s.Functions["dup"] = func() {
		interpreter.Stacks["name"].Dup()
	}

	s.Functions["flush"] = func() {
		interpreter.Stacks["name"].Flush()
	}

	s.Functions["pop"] = func() {
		interpreter.Stacks["name"].Pop()
	}

	s.Functions["quote"] = func() {
		interpreter.quoteNextName = true
	}

	s.Functions["rand"] = func() {
		randName := goremutake.Encode(interpreter.numNamesGenerated)
		interpreter.Stacks["name"].Push(randName)
		interpreter.numNamesGenerated++
	}

	s.Functions["randboundname"] = func() {
		l := len(interpreter.listOfDefinitions)

		if l == 0 {
			return
		}

		idx := interpreter.Rand.Intn(l)
		randName := interpreter.listOfDefinitions[idx]
		interpreter.Stacks["name"].Push(randName)
	}

	s.Functions["rot"] = func() {
		interpreter.Stacks["name"].Rot()
	}

	s.Functions["shove"] = func() {
		if !interpreter.StackOK("name", 1) || !interpreter.StackOK("integer", 1) {
			return
		}

		idx := interpreter.Stacks["integer"].Pop().(int64)
		name := interpreter.Stacks["name"].Peek().(string)
		interpreter.Stacks["name"].Shove(name, idx)
		interpreter.Stacks["name"].Pop()
	}

	s.Functions["stackdepth"] = func() {
		if !interpreter.StackOK("integer", 0) {
			return
		}

		interpreter.Stacks["integer"].Push(interpreter.Stacks["name"].Len())
	}

	s.Functions["swap"] = func() {
		interpreter.Stacks["name"].Swap()
	}

	s.Functions["yank"] = func() {
		if !interpreter.StackOK("integer", 1) || !interpreter.StackOK("name", 1) {
			return
		}

		idx := interpreter.Stacks["integer"].Pop().(int64)
		interpreter.Stacks["name"].Yank(idx)
	}

	s.Functions["yankdup"] = func() {
		if !interpreter.StackOK("integer", 1) || !interpreter.StackOK("name", 1) {
			return
		}

		idx := interpreter.Stacks["integer"].Pop().(int64)
		interpreter.Stacks["name"].YankDup(idx)
	}

	return s
}