コード例 #1
0
 /**
  * Compile class from source and return compiled type
  *
  * @param   string src
  * @return  xp.compiler.types.TypeReflection
  */
 protected function compile($src)
 {
     $unique = 'FixtureClassFor' . $this->getClass()->getSimpleName() . ucfirst($this->name);
     $r = $this->emitter->emit(Syntax::forName('xp')->parse(new MemoryInputStream(sprintf($src, $unique))), $this->scope);
     $r->executeWith([]);
     return new TypeReflection(\lang\XPClass::forName($r->type()->name()));
 }
コード例 #2
0
ファイル: FileSource.class.php プロジェクト: xp-lang/compiler
 /**
  * Constructor
  *
  * @param   io.File file
  * @param   xp.compiler.Syntax s Syntax to use, determined via source file's syntax otherwise
  * @throws  lang.IllegalArgumentException in case the syntax cannot be determined
  */
 public function __construct(File $file, Syntax $s = null)
 {
     $this->file = $file;
     try {
         $this->syntax = $s ?: Syntax::forName($this->file->getExtension());
     } catch (IllegalArgumentException $e) {
         throw new IllegalArgumentException('Cannot determine syntax for "' . $this->file->getFileName() . '"', $e);
     }
 }
コード例 #3
0
 /**
  * Constructor
  *
  * @param   string syntax
  * @param   string fragment
  * @param   bool return whether to add return statement if not present in fragment
  * @throws  lang.IllegalArgumentException
  */
 public function __construct($syntax, $fragment, $return = false)
 {
     $this->syntax = \xp\compiler\Syntax::forName($syntax);
     // Add "return" statement if not present. TODO: If other languages are added
     // in which the string "return" is not the return statement, then this needs
     // to be rewritten
     $this->fragment = rtrim($fragment, ';') . ';';
     if ($return && !(strstr($fragment, 'return ') || strstr($fragment, 'return;'))) {
         $this->fragment = 'return ' . $this->fragment;
     }
     // Verify template
     $name = $this->syntax->name();
     if (!isset(self::$TEMPLATE[$name])) {
         throw new \lang\IllegalArgumentException('No command line code template for syntax "' . $name . '"');
     }
     $this->template = self::$TEMPLATE[$name];
 }
コード例 #4
0
 /**
  * Define class from a given name and source
  *
  * @param   string type
  * @param   string class
  * @param   var parent either a string or a lang.XPClass
  * @param   string src
  * @param   string[] imports
  * @return  lang.XPClass
  */
 protected static function define($type, $class, $parent, $src, array $imports = [])
 {
     $emitter = self::emitter();
     $emitter->clearMessages();
     $syntax = Syntax::forName('xp');
     $class = 'Source' . $class;
     $scope = new TaskScope(new CompilationTask(new FileSource(new File(__FILE__), $syntax), new NullDiagnosticListener(), new FileManager(), $emitter));
     // Parent class
     if ($parent instanceof XPClass) {
         $extends = (new XPClass(self::class))->getPackage()->getName() . '.' . $parent->getName();
         $scope->addResolved($extends, new TypeReflection($parent));
         $scope->addTypeImport($extends);
     } else {
         $extends = $parent;
     }
     // Emit
     $r = $emitter->emit($syntax->parse(new MemoryInputStream(implode("\n", $imports) . ' public ' . $type . ' ' . $class . ' ' . ($extends ? ' extends ' . $extends : '') . $src), $class), $scope);
     \xp::gc();
     // DEBUG $r->writeTo(\util\cmd\Console::$out->getStream());
     $r->executeWith([]);
     return XPClass::forName($r->type()->name());
 }
コード例 #5
0
 /**
  * Parse source
  *
  * @param  string $source
  * @return xp.compiler.ast.ParseTree
  */
 protected function parse($source)
 {
     return Syntax::forName('php')->parse(new \io\streams\MemoryInputStream($source), $this->name);
 }
コード例 #6
0
 /**
  * Parse sourcecode
  *
  * @param  string $source
  * @return xp.compiler.ast.TypeDeclarationNode
  */
 private function parse($source)
 {
     return Syntax::forName('xp')->parse(new MemoryInputStream($source))->declaration;
 }
コード例 #7
0
ファイル: ScopeTest.class.php プロジェクト: xp-lang/compiler
 /**
  * Sets up this testcase
  *
  * @return void
  */
 public function setUp()
 {
     $this->fixture = new TaskScope(new CompilationTask(new FileSource(new File(__FILE__), Syntax::forName('xp')), new NullDiagnosticListener(), new FileManager(), new V54Emitter()));
 }
コード例 #8
0
 public static function useXpSyntax()
 {
     self::$syntax = Syntax::forName('xp');
 }
コード例 #9
0
ファイル: TypeTest.class.php プロジェクト: xp-lang/compiler
 /**
  * Compile class from source and return compiled type
  *
  * @param   string src
  * @return  xp.compiler.types.Types
  */
 protected function compile($src)
 {
     $r = $this->emitter->emit(Syntax::forName('xp')->parse(new MemoryInputStream($src)), $this->scope);
     return $r->type();
 }
コード例 #10
0
 /**
  * Compile class from source and return compiled type
  *
  * @param   string src
  * @return  xp.compiler.types.Types
  */
 protected function compile($src)
 {
     $r = $this->emitter->emit(Syntax::forName('xp')->parse(new MemoryInputStream($src)), $this->scope);
     $r->executeWith([]);
     return XPClass::forName($r->type()->name());
 }