Skip to content

Commit

Permalink
Merge pull request #423 from phel-lang/feature/store-definitions-in-r…
Browse files Browse the repository at this point in the history
…egistry-instead-of-globals

Introduce a Registry class to store the definitions.
  • Loading branch information
Chemaclass committed Jan 15, 2022
2 parents 846d2bf + 47d464c commit f639bb7
Show file tree
Hide file tree
Showing 52 changed files with 828 additions and 578 deletions.
16 changes: 7 additions & 9 deletions src/phel/repl.phel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns phel\repl
(:use Phel\Lang\Symbol)
(:use Phel\Lang\Registry)
(:use Phel\Build\BuildFacade)
(:use Phel\Compiler\Analyzer\Environment\GlobalEnvironmentSingleton)
(:use Phel\Compiler\Analyzer\Environment\NodeEnvironment)
Expand All @@ -13,7 +14,7 @@
(php/:: GlobalEnvironmentSingleton (getInstance)))

(defn- loaded-namespaces []
(php/array_keys (php/aget php/$GLOBALS "__phel")))
(php/-> (php/:: Registry (getInstance)) (getNamespaces)))

(defn- eval-file [file]
(php/-> build-facade (evalFile file)))
Expand All @@ -35,12 +36,10 @@
(php/trim (php/str_replace (php/array "```phel\n" "```") "" str)))

(defn- find-doc [namespace name]
(-> php/$GLOBALS
(php/aget "__phel_meta")
(php/aget namespace)
(php/aget name)
(get :doc)
(clean-doc)))
(let [registry (php/:: Registry (getInstance))
meta (php/-> registry (getDefintionMetaData namespace name))]
(when meta
(clean-doc (get meta :doc)))))

(defmacro doc
"Prints the documentation for the given symbol"
Expand All @@ -62,8 +61,7 @@
(defn- require-namespace
[namespace alias refers]
(let [env (get-global-env)
current-ns *ns*
ns-var-array (php/aget (php/aget php/$GLOBALS "__phel") "phel\\core")]
current-ns *ns*]
(php/-> env (addRequireAlias current-ns alias namespace))
(foreach [r refers]
(php/-> env (addRefer current-ns r namespace)))
Expand Down
10 changes: 6 additions & 4 deletions src/phel/test.phel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns phel\test
(:use Phel\Lang\Symbol)
(:use Phel\Lang\Registry)
(:use Phel\Compiler\Emitter\OutputEmitter\Munge)
(:use Phel\Command\CommandFacade))

Expand Down Expand Up @@ -300,11 +301,12 @@

(defn- find-test-fns [ns {:filter filter}]
(let [munge (php/new Munge)
munge-ns (php/-> munge (encodeNs ns))]
(for [fn-name :keys (get-in php/$GLOBALS ["__phel" munge-ns])
:let [meta (get-in php/$GLOBALS ["__phel_meta" munge-ns fn-name])]
munge-ns (php/-> munge (encodeNs ns))
registry (php/:: Registry (getInstance))]
(for [fn-name :keys (php/-> registry (getDefinitionInNamespace munge-ns))
:let [meta (php/-> registry (getDefintionMetaData munge-ns fn-name))]
:when (and (:test meta) (or (nil? filter) (str-contains? (:test-name meta) filter)))]
(get-in php/$GLOBALS ["__phel" munge-ns fn-name]))))
(php/-> registry (getDefinition munge-ns fn-name)))))

(defn- run-test [options ns]
(let [tests (find-test-fns ns options)]
Expand Down
5 changes: 3 additions & 2 deletions src/php/Build/Compile/FileCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Phel\Build\Extractor\NamespaceExtractorInterface;
use Phel\Compiler\Compiler\CompileOptions;
use Phel\Compiler\CompilerFacadeInterface;
use Phel\Lang\Registry;

final class FileCompiler implements FileCompilerInterface
{
Expand All @@ -30,9 +31,9 @@ public function compileFile(string $src, string $dest, bool $enableSourceMaps):
->setSource($src)
->setIsEnabledSourceMaps($enableSourceMaps);

$GLOBALS['__phel']['phel\core']['*compile-mode*'] = true;
Registry::getInstance()->addDefinition("phel\core", '*compile-mode*', true);
$result = $this->compilerFacade->compile($phelCode, $options);
$GLOBALS['__phel']['phel\core']['*compile-mode*'] = false;
Registry::getInstance()->addDefinition("phel\core", '*compile-mode*', false);

file_put_contents($dest, "<?php\n" . $result->getCode());
file_put_contents(str_replace('.php', '.phel', $dest), $phelCode);
Expand Down
9 changes: 5 additions & 4 deletions src/php/Compiler/Analyzer/Environment/GlobalEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Phel\Compiler\Analyzer\Ast\PhpClassNameNode;
use Phel\Lang\Collections\Map\PersistentMapInterface;
use Phel\Lang\Keyword;
use Phel\Lang\Registry;
use Phel\Lang\SourceLocation;
use Phel\Lang\Symbol;
use Phel\Lang\TypeFactory;
Expand Down Expand Up @@ -46,12 +47,12 @@ public function __construct()
private function addInternalCompileModeDefinition(): void
{
$symbol = Symbol::create('*compile-mode*');
$GLOBALS['__phel'][self::PHEL_CORE_NAMESPACE][$symbol->getName()] = false;

$this->addDefinition(self::PHEL_CORE_NAMESPACE, $symbol, TypeFactory::getInstance()->persistentMapFromKVs(
$meta = TypeFactory::getInstance()->persistentMapFromKVs(
Keyword::create('doc'),
'Set to true when a file is compiled, false otherwise',
));
);
Registry::getInstance()->addDefinition(self::PHEL_CORE_NAMESPACE, $symbol->getName(), false, $meta);
$this->addDefinition(self::PHEL_CORE_NAMESPACE, $symbol, $meta);
}

public function getNs(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Phel\Compiler\Analyzer\Exceptions\GlobalEnvironmentAlreadyInitializedException;
use Phel\Compiler\Analyzer\Exceptions\GlobalEnvironmentNotInitializedException;
use Phel\Lang\Registry;

final class GlobalEnvironmentSingleton
{
Expand Down Expand Up @@ -52,7 +53,7 @@ public static function initialize(): GlobalEnvironmentInterface
*/
public static function initializeNew(): GlobalEnvironmentInterface
{
unset($GLOBALS['__phel']);
Registry::getInstance()->clear();
self::$instance = new GlobalEnvironment();

return self::$instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Phel\Compiler\Analyzer\Exceptions\AnalyzerException;
use Phel\Compiler\Analyzer\TypeAnalyzer\WithAnalyzerTrait;
use Phel\Lang\Collections\LinkedList\PersistentListInterface;
use Phel\Lang\Registry;
use Phel\Lang\TypeFactory;
use Phel\Lang\TypeInterface;

Expand Down Expand Up @@ -51,7 +52,7 @@ private function macroExpand(PersistentListInterface $list, GlobalVarNode $macro
$listCount = count($list);
/** @psalm-suppress PossiblyNullArgument */
$nodeName = $macroNode->getName()->getName();
$fn = $GLOBALS['__phel'][$macroNode->getNamespace()][$nodeName];
$fn = Registry::getInstance()->getDefinition($macroNode->getNamespace(), $nodeName);

$arguments = $list->rest()->toArray();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function analyze(PersistentListInterface $list, NodeEnvironmentInterface
throw AnalyzerException::withLocation("First argument of 'def must be a Symbol.", $list);
}



return new SetVarNode(
$env,
$this->analyzer->analyze($nameSymbol, $env->withContext(NodeEnvironmentInterface::CONTEXT_EXPRESSION)),
Expand Down
16 changes: 0 additions & 16 deletions src/php/Compiler/Emitter/OutputEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,6 @@ public function emitArgList(array $nodes, ?SourceLocation $sepLoc, string $sep =
}
}

public function emitGlobalBase(string $namespace, Symbol $name): void
{
$this->emitStr(
'$GLOBALS["__phel"]["' . addslashes($this->mungeEncodeNs($namespace)) . '"]["' . addslashes($name->getName()) . '"]',
$name->getStartLocation()
);
}

public function emitGlobalBaseMeta(string $namespace, Symbol $name): void
{
$this->emitStr(
'$GLOBALS["__phel_meta"]["' . addslashes($this->mungeEncodeNs($namespace)) . '"]["' . addslashes($name->getName()) . '"]',
$name->getStartLocation()
);
}

public function emitContextPrefix(NodeEnvironmentInterface $env, ?SourceLocation $sl = null): void
{
if ($env->getContext() === NodeEnvironmentInterface::CONTEXT_RETURN) {
Expand Down
19 changes: 12 additions & 7 deletions src/php/Compiler/Emitter/OutputEmitter/NodeEmitter/DefEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ public function emit(AbstractNode $node): void
{
assert($node instanceof DefNode);

$this->outputEmitter->emitGlobalBase($node->getNamespace(), $node->getName());
$this->outputEmitter->emitStr(' = ', $node->getStartSourceLocation());
$this->outputEmitter->emitLine('\\Phel\\Lang\\Registry::getInstance()->addDefinition(');
$this->outputEmitter->increaseIndentLevel();
$this->outputEmitter->emitStr('"');
$this->outputEmitter->emitStr(addslashes($this->outputEmitter->mungeEncodeNs($node->getNamespace())));
$this->outputEmitter->emitLine('",');
$this->outputEmitter->emitStr('"');
$this->outputEmitter->emitStr(addslashes($node->getName()->getName()));
$this->outputEmitter->emitLine('",');
$this->outputEmitter->emitNode($node->getInit());
$this->outputEmitter->emitLine(';', $node->getStartSourceLocation());

if (count($node->getMeta()) > 0) {
$this->outputEmitter->emitGlobalBaseMeta($node->getNamespace(), $node->getName());
$this->outputEmitter->emitStr(' = ', $node->getStartSourceLocation());
$this->outputEmitter->emitLine(',');
$this->outputEmitter->emitLiteral($node->getMeta());
$this->outputEmitter->emitLine(';', $node->getStartSourceLocation());
}
$this->outputEmitter->emitLine();
$this->outputEmitter->decreaseIndentLevel();
$this->outputEmitter->emitLine(');');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public function emit(AbstractNode $node): void
assert($node instanceof GlobalVarNode);

$this->outputEmitter->emitContextPrefix($node->getEnv(), $node->getStartSourceLocation());
$this->outputEmitter->emitGlobalBase($node->getNamespace(), $node->getName());
$this->outputEmitter->emitStr('\\Phel\\Lang\\Registry::getInstance()->getDefinition("');
$this->outputEmitter->emitStr(addslashes($this->outputEmitter->mungeEncodeNs($node->getNamespace())));
$this->outputEmitter->emitStr('", "');
$this->outputEmitter->emitStr(addslashes($node->getName()->getName()));
$this->outputEmitter->emitStr('")');
$this->outputEmitter->emitContextSuffix($node->getEnv(), $node->getStartSourceLocation());
}
}
17 changes: 11 additions & 6 deletions src/php/Compiler/Emitter/OutputEmitter/NodeEmitter/NsEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Phel\Compiler\Analyzer\Ast\AbstractNode;
use Phel\Compiler\Analyzer\Ast\NsNode;
use Phel\Compiler\Emitter\OutputEmitter\NodeEmitterInterface;
use Phel\Lang\Symbol;
use function addslashes;

final class NsEmitter implements NodeEmitterInterface
Expand Down Expand Up @@ -70,11 +69,17 @@ private function emitCurrentNamespace(NsNode $node): void
);
}

$nsSym = Symbol::create('*ns*');
$nsSym->setStartLocation($node->getStartSourceLocation());
$this->outputEmitter->emitGlobalBase('phel\\core', $nsSym);
$this->outputEmitter->emitStr(' = ', $node->getStartSourceLocation());
$this->outputEmitter->emitLine('\\Phel\\Lang\\Registry::getInstance()->addDefinition(');
$this->outputEmitter->increaseIndentLevel();
$this->outputEmitter->emitStr('"');
$this->outputEmitter->emitStr(addslashes($this->outputEmitter->mungeEncodeNs('phel\\core')));
$this->outputEmitter->emitLine('",');
$this->outputEmitter->emitStr('"');
$this->outputEmitter->emitStr(addslashes('*ns*'));
$this->outputEmitter->emitLine('",');
$this->outputEmitter->emitLiteral($this->outputEmitter->mungeEncodeNs($node->getNamespace()));
$this->outputEmitter->emitLine(';', $node->getStartSourceLocation());
$this->outputEmitter->emitLine();
$this->outputEmitter->decreaseIndentLevel();
$this->outputEmitter->emitLine(');');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phel\Compiler\Emitter\OutputEmitter\NodeEmitter;

use Phel\Compiler\Analyzer\Ast\AbstractNode;
use Phel\Compiler\Analyzer\Ast\GlobalVarNode;
use Phel\Compiler\Analyzer\Ast\SetVarNode;
use Phel\Compiler\Emitter\OutputEmitter\NodeEmitterInterface;

Expand All @@ -15,10 +16,20 @@ final class SetVarEmitter implements NodeEmitterInterface
public function emit(AbstractNode $node): void
{
assert($node instanceof SetVarNode);
$symbolNode = $node->getSymbol();
assert($symbolNode instanceof GlobalVarNode);

$this->outputEmitter->emitNode($node->getSymbol());
$this->outputEmitter->emitStr(' = ', $node->getStartSourceLocation());
$this->outputEmitter->emitLine('\\Phel\\Lang\\Registry::getInstance()->addDefinition(');
$this->outputEmitter->increaseIndentLevel();
$this->outputEmitter->emitStr('"');
$this->outputEmitter->emitStr(addslashes($this->outputEmitter->mungeEncodeNs($symbolNode->getNamespace())));
$this->outputEmitter->emitLine('",');
$this->outputEmitter->emitStr('"');
$this->outputEmitter->emitStr(addslashes($symbolNode->getName()->getName()));
$this->outputEmitter->emitLine('",');
$this->outputEmitter->emitNode($node->getValueExpr());
$this->outputEmitter->emitLine(';', $node->getStartSourceLocation());
$this->outputEmitter->emitLine();
$this->outputEmitter->decreaseIndentLevel();
$this->outputEmitter->emitLine(');');
}
}
4 changes: 0 additions & 4 deletions src/php/Compiler/Emitter/OutputEmitterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public function emitStr(string $str, ?SourceLocation $sl = null): void;

public function emitArgList(array $nodes, ?SourceLocation $sepLoc, string $sep = ', '): void;

public function emitGlobalBase(string $namespace, Symbol $name): void;

public function emitGlobalBaseMeta(string $namespace, Symbol $name): void;

public function emitContextPrefix(NodeEnvironmentInterface $env, ?SourceLocation $sl = null): void;

public function emitContextSuffix(NodeEnvironmentInterface $env, ?SourceLocation $sl = null): void;
Expand Down
8 changes: 5 additions & 3 deletions src/php/Interop/ExportFinder/FunctionsToExportFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Phel\Interop\ReadModel\FunctionToExport;
use Phel\Lang\Collections\Map\PersistentMapInterface;
use Phel\Lang\Keyword;
use Phel\Lang\Registry;
use Phel\Lang\TypeFactory;

final class FunctionsToExportFinder implements FunctionsToExportFinderInterface
Expand Down Expand Up @@ -85,8 +86,8 @@ private function findAllFunctionsToExport(): array
{
$functionsToExport = [];

foreach ($GLOBALS['__phel'] as $ns => $functions) {
foreach ($functions as $fnName => $fn) {
foreach (Registry::getInstance()->getNamespaces() as $ns) {
foreach (Registry::getInstance()->getDefinitionInNamespace($ns) as $fnName => $fn) {
if ($this->isExport($ns, $fnName)) {
$functionsToExport[$ns] ??= [];
$functionsToExport[$ns][] = new FunctionToExport($fn);
Expand All @@ -100,7 +101,8 @@ private function findAllFunctionsToExport(): array
private function isExport(string $ns, string $fnName): bool
{
/** @var PersistentMapInterface $meta */
$meta = $GLOBALS['__phel_meta'][$ns][$fnName] ?? TypeFactory::getInstance()->emptyPersistentList();
$meta = Registry::getInstance()->getDefintionMetaData($ns, $fnName)
?? TypeFactory::getInstance()->emptyPersistentList();

return (bool)($meta[Keyword::create('export')] ?? false);
}
Expand Down
4 changes: 3 additions & 1 deletion src/php/Interop/PhelCallerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Phel\Interop;

use Phel\Lang\Registry;

trait PhelCallerTrait
{
/**
Expand All @@ -25,6 +27,6 @@ private function getPhelDefinition(string $namespace, string $definitionName)
{
$phelNs = str_replace('-', '_', $namespace);

return $GLOBALS['__phel'][$phelNs][$definitionName];
return Registry::getInstance()->getDefinition($phelNs, $definitionName);
}
}
Loading

0 comments on commit f639bb7

Please sign in to comment.