#!/usr/bin/php parseArgs(); $this->parse(); $this->buildTree(); $this->resolve(); $method= $this->options['target']."Emit"; if (method_exists($this, $method)) { $this->$method(); } } // defaults.. var $options = array( "target" => "cil", ); function parseArgs() { // mapp of keys to values.. $valid = array( "E" => "preprocess", "p:" => "pipe", // -pipe "o:" => "output", "g" => "debug", "O::" => "optimization", "D" => "symbols", "U" => "symbol", "u:" => "symbols", // undef "I:" => "includeDir", "J:" => "includeCppDir", "K:" => "includeCppDir", "L:" => "libDir", "M:" => "systemLibDir", "l:" => "linkLibDir", "W:" => "warnings", "w:" => "nowarnings", "f:" => "features", "m:" => "machineExtensionFeatures", "d:" => "debugFlags", "P:" => "noHashLines", "t:" => "target" ); $args = Console_Getopt::ReadPHPArgV(); $vals = Console_Getopt::getopt($args,implode('', array_keys($valid))); foreach($vals[0] as $opts) { // flags if (isset($valid[$opts[0]])) { $this->options[$valid[$opts[0]]] = true; continue; } if (isset($valid[$opts[0].':'])) { $this->options[$valid[$opts[0].':']] = $opts[1]; continue; } } $files = $vals[1]; if (!$files) { PHPSharp::error(0,"No Files supplied"); } foreach($files as $file) { $realpath = realpath($file); if (!$realpath) { PHPSharp::error(0,"File $path Does not exist"); } $this->files[] = $realpath; } } var $files; // array of files to compile function parse() { foreach($this->files as $file) { $file = &PHPSharp_File::singleton($file); if ($file->tokenize()) { $file->parse(); } } exit; } function buildObject(&$node) { //echo "buildObject ". get_class($node) . "\n"; //new GTK_VarDump($node, "bulid node"); print_R($node); $vars = get_object_vars($node); foreach($vars as $k=>$v) { switch (gettype($v)) { case 'object': $node->$k->id = $GLOBALS['_PHPSHARP']['id']++; $GLOBALS['_PHPSHARP']['nodes'][$node->$k->id] = &$node->$k; $node->$k->_parent = $node->id; $node->$k->_children = array(); $node->_children[] = $node->$k->id; $this->buildObject($node->$k); break; case 'array': $this->buildArray($node->id,$node->$k); break; default: break; } } } function buildArray($id, &$node) { //echo "buildArray\n"; // new GTK_VarDump($node, "bulid array for $id"); foreach($node as $k=>$v) { switch (gettype($v)) { case 'object': $node[$k]->id = $GLOBALS['_PHPSHARP']['id']++; $GLOBALS['_PHPSHARP']['nodes'][$node[$k]->id] = &$node[$k]; $node[$k]->_parent = $id; $node[$k]->_children = array(); $GLOBALS['_PHPSHARP']['nodes'][$id]->_children[] = $node[$k]->id ; $this->buildObject($node[$k]); break; case 'array': $this->buildArray($id,$node[$k]); break; default: break; } } } function buildTree() { $s = &PHPSharp_State::singleton(); $s->members->id =0; $GLOBALS['_PHPSHARP']['nodes'][$s->members->id ] = &$s->members; $s->members->_children = array(); //new GTK_VarDump($s->members, 'state members'); $this->buildObject($s->members); //new GTK_VarDump($GLOBALS['_PHPSHARP']['nodes'], 'all nodes'); //exit; } function resolve() { $s = &PHPSharp_State::singleton(); $s->members->buildClassMembers(); $s->members->buildLocalVars(); //new GTK_VarDump($s->members, 'state members'); //exit; } // emit cil (kind of an opcode language) function cilEmit() { // $s = &PHPSharp_State::singleton(); new GTK_VarDump($s->members, 'state members'); $s->members->cilEmit(); } // emit C# code function CSEmit() { // $s = &PHPSharp_State::singleton(); new GTK_VarDump($s->members, 'state members'); $s->members->csEmit(); } function error($id,$msg) { echo "ERROR $id : $msg\n"; exit(255); } function debug($id,$msg) { echo "Debug Message ($id) : $msg\n"; } } new PHPSharp;