diff --git a/bin/compiler.php b/bin/compiler.php index 9a2560da..212e7319 100644 --- a/bin/compiler.php +++ b/bin/compiler.php @@ -6,7 +6,6 @@ use PhpAot\Php\Translator; define('DEBUG', true); - if (empty($argv[1])) { die("php compiler.php [file]\n"); } diff --git a/bin/link.php b/bin/link.php new file mode 100644 index 00000000..10ed39e5 --- /dev/null +++ b/bin/link.php @@ -0,0 +1,21 @@ +compileBinary($info['filename'], $objectFile); +} catch (Error $error) { + echo "Parse error: {$error->getMessage()}\n"; + return; +} diff --git a/examples/leibniz.js b/examples/leibniz.js new file mode 100644 index 00000000..b1855a26 --- /dev/null +++ b/examples/leibniz.js @@ -0,0 +1,16 @@ +"use strict"; + +let fs = require("fs"); +let rounds = parseInt(fs.readFileSync("./rounds.txt", "utf8")); + +let x = 1.0; +let pi = 1.0; + +for (let i = 2; i < rounds + 2; i++) { + x *= -1; + pi += x / (2 * i - 1); +} + +pi *= 4; + +console.log(pi); diff --git a/examples/pi.go b/examples/pi.go new file mode 100644 index 00000000..55c9d2f8 --- /dev/null +++ b/examples/pi.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + file, _ := os.ReadFile("rounds.txt") + rounds, _ := strconv.Atoi(strings.TrimSpace(string(file))) + + x := 1.0 + pi := 1.0 + stop := float64(rounds + 2) + + for i := 2.0; i <= stop; i++ { + x = -x + pi += x / (2.0*i - 1.0) + } + + pi *= 4.0 + fmt.Println(pi) +} \ No newline at end of file diff --git a/examples/pi.php b/examples/pi.php index b47a663b..2ad32e0c 100644 --- a/examples/pi.php +++ b/examples/pi.php @@ -2,7 +2,7 @@ function main() { ini_set("precision", 17); - $rounds = (int)file_get_contents("./rounds.txt", true); + $rounds = (int) file_get_contents("./rounds.txt", true); $stop = $rounds + 2; var_dump($stop); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index b4ee6b38..8c618b38 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -16,6 +16,7 @@ class Translator extends \PhpAot\Core\Translator const TYPE_VAR = 'php::Variant'; const TYPE_INT = 'php::Int'; const TYPE_FLOAT = 'php::Float'; + const TYPE_BOOL = 'bool'; protected string $phpxDir = '~/workspace/projects/phpx'; protected string $lang = 'PHP'; @@ -335,7 +336,7 @@ class Translator extends \PhpAot\Core\Translator } } - private function parseAssign(mixed $v) + private function parseAssign(Node $v): string { $var = $this->parseIdentifier($v->var); $expr = $this->parseExpr($v->expr); @@ -381,17 +382,27 @@ class Translator extends \PhpAot\Core\Translator { $exprType = $expr->getType(); switch ($exprType) { + case 'Expr_Cast_Int': case 'Scalar_Int': - return $this->getZendType('int'); + return self::TYPE_INT; + case 'Expr_Cast_Float': case 'Scalar_Float': - return $this->getZendType('float'); + return self::TYPE_FLOAT; case 'Scalar_Bool': - return $this->getZendType('bool'); + return self::TYPE_BOOL; case 'Expr_Array': return 'php::Array'; + case 'Expr_BinaryOp_Plus': + $leftType = $this->detectType($var, $expr->left); + $rightType = $this->detectType($var, $expr->right); + if ($leftType === self::TYPE_INT || $rightType === self::TYPE_INT) { + return self::TYPE_INT; + } else { + return self::TYPE_VAR; + } case 'Scalar_String': default: - return 'php::Variant'; + return self::TYPE_VAR; } } @@ -469,7 +480,14 @@ class Translator extends \PhpAot\Core\Translator public function compileFile($file): void { - $cmd = 'g++ -c ' . $file . ' -o ' . $file . '.o ' . $this->parseIncludes() . $this->parseLdflags() . $this->parseLibs(); + $cmd = 'g++ -c ' . $file . ' -o ' . $file . '.o ' . $this->parseIncludes() . ' -O' . $this->optimizeLevel; + echo $cmd . PHP_EOL; + shell_exec($cmd); + } + + public function compileBinary($targetFile, $objectFile): void + { + $cmd = 'g++ main.cc ' . $objectFile . ' -o ' . $targetFile . ' ' . $this->parseIncludes() . $this->parseLdflags() . $this->parseLibs(); $cmd .= ' -O' . $this->optimizeLevel; echo $cmd . PHP_EOL; shell_exec($cmd);