#!/usr/bin/php convert.php --imc - convert.php --imc \nOptions: --imc output IMC code --parrot parot binary location. (eg. /usr/bin/parrot) --debug debug on --help display this.. --C output C!?? - well at least try! \n\n"; exit; } var $options = array( 'input' => '', 'imc' => '', // we are defaulting to stdout for imc.. 'debug' => false, 'help' => '', 'parrot' => '', 'C' => false, // output C code? ); function main() { // arguments // for($i = 1; $i < $_SERVER['argc']; $i++) { $arg = $_SERVER['argv'][$i]; //echo "$arg\n"; if ($arg{0} != '-') { $this->options['input'] = $arg; continue; } $arg = ltrim($arg,'-'); switch ($arg) { case 'imc': case 'parrot': $i++; if (!isset($_SERVER['argv'][$i])) { echo "Error: nothing following $arg\n"; $this->showUsage(); } $next = $_SERVER['argv'][$i]; if (($next == '-') && $arg == 'imc') { $this->options[$arg] = '-'; break; } if ($next{0} == '-') { echo "Error: $arg expects a value.\n"; $this->showUsage(); } $this->options[$arg] = $next; break; case 'debug': case 'C': $this->options[$arg] = true; break; case 'help': $this->showUsage(); // exits default: echo "unknown option : --{$arg}"; $this->showUsage(); } } // check input file if (!strlen(trim($this->options['input']))) { echo "Error: no input file"; $this->showUsage(); } if (!is_file($this->options['input'])) { echo $this->options['input'] . " isn't a file\n"; $this->showUsage(); } } function buildTree() // new classy tree builder.. { require_once 'PintCompiler/Generator.php'; $t = false; require_once 'PintCompiler/Generator/Function.php'; $func = new PintCompiler_Generator_Function; $func->name = 'main'; //$func->retType = 'int'; //$func->args = array(array('int', 'argc'), array('char**', 'argv')); $id = PintCompiler_Generator::addFunction($func); PintCompiler_Generator::setActiveFunction(0); // the parser will create the nodes in the generator.. require_once 'PintCompiler/Node.php'; require_once 'PintCompiler/Parser.php'; $GLOBALS['PintCompiler_convert']['active_file'] = $this->options['input']; $r = PintCompiler_Parser::parseString(file_get_contents($this->options['input'])); if (is_a('PEAR_Error',$r)) { echo "ERROR parsing"; print_r($r); exit; } if ($this->options['C']) { $this->emitC(); echo $this->code; exit; } else { $this->code = $this->emitImc(); } if ($this->options['debug']) { echo $this->code; } if ($this->options['imc'] == '-') { echo $this->code; exit; } require_once 'PintCompiler/Parrot.php'; $p = new PintCompiler_Parrot; $p->run($this); } function emitImc() { // go backwards down the function stack... // theoretically so defined functions work... $code = ''; $n = PintCompiler_Generator::countFunctions() ; for ( $i =0;$i< $n;$i++) { $func = &PintCompiler_Generator::getFunction($i); if (!$func) { continue; } if ($this->options['debug']) { print_r($func); } //var_dump($func); $func->emitImc(); $code .= $func->codeToImc(); } // //$compiler = &BinaryPHP_compile::singleton(); //$code = $compiler->getHeadCode() . $code; return $code; } function emitC() { // go backwards down the function stack... // theoretically so defined functions work... $code = ''; $n = PintCompiler_Generator::countFunctions() ; for ( $i =0;$i< $n;$i++) { $func = &PintCompiler_Generator::getFunction($i); if (!$func) { continue; } if ($this->options['debug']) { print_r($func); } //var_dump($func); $func->emitC(); $code .= $func->codeToC(); } // //$compiler = &BinaryPHP_compile::singleton(); //$code = $compiler->getHeadCode() . $code; return $code; } } $c = new PintCompiler_Convert; $c->main(); //$c->parseAndCompile(); $c->buildTree();