public function testVM() { $vm = new VM("", 10); $this->assertInstanceOf("Brainfart\\VM\\Memory", $vm->getMemory()); $this->assertInstanceOf("Brainfart\\VM\\Output", $vm->getOutput()); $this->assertInstanceOf("Brainfart\\VM\\Input", $vm->getInput()); $this->assertEquals(10, $vm->getLoopLimit()); }
/** * @param string $source * @param string $input * @param int $fetchMode * * @return array|string */ public function run($source, $input = "", $fetchMode = Output::FETCH_ARRAY) { $this->parser->loadSource($source); if ($this->parser->getFlag("string_output") === true) { $fetchMode = Output::FETCH_STRING; } $appLoop = $this->parser->parse($this->optimize); $appInput = $this->parser->getInput(); if (!empty($appInput)) { $input = $appInput; } $this->vm->init($input); $appLoop->execute($this->vm); return $this->vm->getOutput()->fetch($fetchMode); }
/** * @param \Brainfart\VM\VM $vm * * @throws \RuntimeException */ public function execute(VM $vm) { $operations = $this->getOperations(); $limit = $vm->getLoopLimit(); $i = 0; while ($this->getMaster() || $vm->getMemory()->fetch() != 0) { foreach ($operations as $operation) { /** @noinspection PhpUndefinedMethodInspection */ $operation->execute($vm); } if ($this->getMaster()) { break; } $i++; if ($limit > 0 && $limit < $i) { throw new \RuntimeException("Limit of {$limit} operations per loop reached."); } } }
/** * @param \Brainfart\VM\VM $vm */ public function execute(VM $vm) { $vm->getOutput()->store($vm->getMemory()->fetch()); }
/** * @param \Brainfart\VM\VM $vm */ public function execute(VM $vm) { $vm->getMemory()->move($this->getValue()); }
/** * @param \Brainfart\VM\VM $vm */ public function execute(VM $vm) { sleep($vm->getMemory()->fetch()); }