diff --git a/CHANGELOG.md b/CHANGELOG.md index 026553c..28ba31f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. Each batch It is a summary of changes that would be pertinent to the end user of the PGEtinker website. For a comprehensive history of changes made to the project, please refer to the repository's commit history. +## 2024-05-28 + +- Fixed [Issue #78](https://github.com/Moros1138/PGEtinker/issues/78) + ## 2024-05-27 - Changed complete revamp of the frontend code, much more organized diff --git a/pgetinker/Compiler.php b/pgetinker/Compiler.php index ed48e0a..b858945 100644 --- a/pgetinker/Compiler.php +++ b/pgetinker/Compiler.php @@ -83,6 +83,11 @@ public function deserialize(string $json) $this->workingDirectory = ""; } + public function getCode() + { + return implode("\n", $this->code); + } + public function setCode(string $code) { $this->code = explode("\n", $code); @@ -162,6 +167,10 @@ private function processCodeDetectGeometryUtility($index) private function processCodeDetectImplementationMacros($index) { + // broad phase, don't process if this line doesn't at least contain the word "define" + if(!str_contains($this->code[$index], "define")) + return false; + $libraryMap = [ 'OLC_PGE_APPLICATION' => 'olcPixelGameEngine.o', 'OLC_SOUNDWAVE_ENGINE' => 'olcSoundWaveEngine.o', @@ -176,35 +185,44 @@ private function processCodeDetectImplementationMacros($index) 'OLC_PGEX_WIREFRAME' => 'olcPGEX_Wireframe.o', ]; - // filter macros to detect implementation #define - if(str_contains($this->code[$index], "#define")) + $foundImplementationMacro = false; + + foreach($libraryMap as $macro => $objectFileName) { - $foundImplementationMacro = false; - foreach($libraryMap as $macro => $objectFileName) + preg_match( + '/(.*)\s*#\s*define?\s+' . $macro . '(.*)/', + $this->code[$index], + $match, + PREG_OFFSET_CAPTURE, + 0 + ); + + // no match, this time + if(count($match) == 0) + continue; + + if(!empty(trim($match[1][0])) || !empty(trim($match[2][0]))) + continue; + + // blank the line + $this->code[$index] = ""; + + if($macro == "OLC_PGE_APPLICATION" && $this->foundGeometryHeader) { - if(str_contains($this->code[$index], $macro)) - { - // blank the line - $this->code[$index] = ""; - - if($macro == "OLC_PGE_APPLICATION" && $this->foundGeometryHeader) - { - $objectFileName = "olcPixelGameEngine_withGeometry.o"; - $this->logger->info("Found the need for geometry utility support"); - } + $objectFileName = "olcPixelGameEngine_withGeometry.o"; + $this->logger->info("Found the need for geometry utility support"); + } - // indicate that we use this library - $this->linkerInputFiles[] = "./lib/{$objectFileName}"; + // indicate that we use this library + $this->linkerInputFiles[] = "./lib/{$objectFileName}"; - $this->logger->info("Found implementation macro: {$macro}"); - $foundImplementationMacro = true; - break; - } - } + $this->logger->info("Found implementation macro: {$macro}"); + $foundImplementationMacro = true; + break; + } - if($foundImplementationMacro) - return true; - } + if($foundImplementationMacro) + return true; return false; } @@ -341,7 +359,7 @@ private function processCodeRemoteInclude($index) } } - private function processCode() + public function processCode() { $this->logger->info("begin processing code"); $startTime = microtime(true); diff --git a/tests/Feature/ProcessCodeTest.php b/tests/Feature/ProcessCodeTest.php new file mode 100644 index 0000000..3029c71 --- /dev/null +++ b/tests/Feature/ProcessCodeTest.php @@ -0,0 +1,69 @@ +setCode($code); + $compiler->processCode(); + $this->assertTrue($code == $compiler->getCode()); + + $code = "#define OLC_PGE_APPLICATIONYEAAAAAAA\n"; + $compiler->setCode($code); + $compiler->processCode(); + $this->assertTrue($code == $compiler->getCode()); + + $code = "I can write anything wheeeee!#define OLC_PGE_APPLICATIONYEAAAAAAA\n"; + $compiler->setCode($code); + $compiler->processCode(); + $this->assertTrue($code == $compiler->getCode()); + } + + public function test_valid_white_space_macros(): void + { + $compiler = new Compiler(); + + $code = "\t #define OLC_PGE_APPLICATION\n"; + $compiler->setCode($code); + $compiler->processCode(); + print_r($code); + print_r($compiler->getCode()); + $this->assertTrue($code != $compiler->getCode()); + + $code = "# define OLC_PGE_APPLICATION\n"; + $compiler->setCode($code); + $compiler->processCode(); + $this->assertTrue($code != $compiler->getCode()); + + $code = "#define OLC_PGE_APPLICATION\n"; + $compiler->setCode($code); + $compiler->processCode(); + $this->assertTrue($code != $compiler->getCode()); + + $code = "#define OLC_PGE_APPLICATION \n"; + $compiler->setCode($code); + $compiler->processCode(); + $this->assertTrue($code != $compiler->getCode()); + } + + // public function test_process_code_fails_on_bad_macro(): void + // { + // $compiler = new Compiler(); + + // $code = "I can write anything wheeeee!#define OLC_PGE_APPLICATIONYEAAAAAAA\n"; + // $compiler->setCode($code); + // $compiler->processCode(); + + // $this->assertTrue($code == $compiler->getCode()); + // } + +}