First program generation

This commit is contained in:
Ian Gulliver
2021-11-20 19:25:16 -10:00
parent f6a598a559
commit a5a4c49e37
10 changed files with 28 additions and 13 deletions

View File

@@ -3,5 +3,6 @@ package vm
type Program struct {
GlobalMemorySize uint64
FunctionMemorySize uint64
InstructionLimit uint64
Functions []*Function
}

View File

@@ -13,6 +13,8 @@ type State struct {
comparisonResult bool
globalMemory *Memory
stack []*stackFrame
instructionCount uint64
}
func NewState(prog *Program) (*State, error) {
@@ -70,6 +72,12 @@ func (state *State) processInstruction() {
instr := fnc.Instructions[state.instructionIndex]
state.instructionIndex += 1
instr.opHandler(state, instr)
state.instructionCount += 1
if state.program.InstructionLimit > 0 && state.instructionCount > state.program.InstructionLimit {
state.err = fmt.Errorf("Instruction limit (%d) exceeded", state.program.InstructionLimit)
state.running = false
}
if state.functionIndex < 0 || state.functionIndex >= int64(len(state.program.Functions)) {
state.ret()