/** * Write a program to a file * * @param Program $prog * @param string $filename */ public static function writeProgram(Program $prog, $filename) { if ($handle = fopen($filename, 'w')) { for ($k = 0; $k < $prog->getStatementCount(); $k++) { fprintf($handle, "%s\n", $prog->getStatement($k)->dumpWamCode()); } $handle = fclose($handle); } return $handle; }
public function addClause($label, Program $code) { $line = $this->getLastClauseOf($label); if ($line >= 0) { // there already exists such a label: add via try_me_else $s = $this->getStatement($line); $index = strpos($s->getLabel(), '~'); try { //int i; if ($index > 0) { $i = 1 + substr($s->getLabel(), $index + 1); } else { $i = 2; } // update the just-compiled program $newLabel = $label . "~" . $i; $code->getStatement(0)->setLabel($newLabel); // update the previous clause: trust_me -> try_me_else $s->setFunction("try_me_else"); $s->setArgAt($newLabel, 0); $s->arg1 = $newLabel; $s->setJump(count($this->statements)); // update labels and program itself $this->addProgram($code); } catch (\Exception $e) { // TODO maybe do something ? } } else { // first label of that kind: just add to code and update jumpings $this->addProgram($code); $this->updateLabels(); } }