예제 #1
0
파일: VMTest.php 프로젝트: yrizos/brainfart
 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());
 }
예제 #2
0
 /**
  * @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);
 }
예제 #3
0
 /**
  * @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.");
         }
     }
 }
예제 #4
0
 /**
  * @param \Brainfart\VM\VM $vm
  */
 public function execute(VM $vm)
 {
     $vm->getOutput()->store($vm->getMemory()->fetch());
 }
예제 #5
0
 /**
  * @param \Brainfart\VM\VM $vm
  */
 public function execute(VM $vm)
 {
     $vm->getMemory()->move($this->getValue());
 }
예제 #6
0
 /**
  * @param \Brainfart\VM\VM $vm
  */
 public function execute(VM $vm)
 {
     sleep($vm->getMemory()->fetch());
 }