diff --git a/examples/prime.go b/examples/prime.go new file mode 100644 index 00000000..0f3d027e --- /dev/null +++ b/examples/prime.go @@ -0,0 +1,40 @@ +package main + +import ( + "strconv" + "time" +) + +func NthPrime(n int) int { + + var ( + i int = 2 + j int = 1 + ) + + for { + j = j + 1; + if j > i/j { + n-- + if n == 0 { + break + } + j = 1 + } + if i%j == 0 { + i++; + j = 1; + } + } + return i +} + +func main() { + s := time.Now().UnixNano() / 1e6 + n := 300000 + result := NthPrime(n) + e := time.Now().UnixNano() / 1e6 + time := e - s + println("第" + strconv.Itoa(n) + "个素数的值是:" + strconv.Itoa(result) + " 耗时" + strconv.Itoa(int(time)) + "毫秒") +} +//第300000个素数的值是:4256233 耗时10417毫秒 \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 3212a6bb..bf2a88f1 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -709,7 +709,11 @@ class CompilerBase extends \PhpAot\Core\Translator if (isset($this->arguments[$name])) { continue; } - $code .= $this->getIndent() . $type . ' ' . $name . ';' . PHP_EOL; + $code .= $this->getIndent() . $type . ' ' . $name; + if ($type === self::TYPE_INT or $type === self::TYPE_FLOAT or $type === self::TYPE_BOOL) { + $code .= ' = 0'; + } + $code .= ';' . PHP_EOL; } $code .= "\n"; $this->indentLevel--; @@ -1715,9 +1719,11 @@ class CompilerBase extends \PhpAot\Core\Translator return $var . '.newItem()'; } } else { - $dim = $this->trimBrackets($this->parseIdentifier($node->dim)); - - return $var . '.item(' . $dim . ', ' . $this->escapeBool($write) . ')'; + $oriInAssignExpr = $this->inAssignExpr; + $this->inAssignExpr = false; + $dim = $this->parseIdentifier($node->dim); + $this->inAssignExpr = $oriInAssignExpr; + return $var . '.item(' . $this->trimBrackets($dim) . ', ' . $this->escapeBool($write) . ')'; } } diff --git a/tests/misc/2.phpt b/tests/misc/2.phpt new file mode 100644 index 00000000..859b20d2 --- /dev/null +++ b/tests/misc/2.phpt @@ -0,0 +1,75 @@ +--TEST-- +mixed/2.phpt +--FILE-- +root = new Node(); + } + + function insert($word) + { + $node = $this->root; + + for ($i = 0; $i < strlen($word); $i++) { + if (!isset($node->child[$word[$i]])) { + $node->child[$word[$i]] = new Node(); + } + + $node = $node->child[$word[$i]]; + if ($i == strlen($word) - 1) { + $node->is_end = true; + } + } + } + + function search($word) + { + $node = $this->root; + for ($i = 0; $i < strlen($word); $i++) { + if (isset($node->child[$word[$i]])) { + $node = $node->child[$word[$i]]; + } else { + return false; + } + } + + return $node->is_end; + } + + function startsWith($prefix) + { + $node = $this->root; + for ($i = 0; $i < strlen($prefix); $i++) { + if (isset($node->child[$prefix[$i]])) { + $node = $node->child[$prefix[$i]]; + } else { + return false; + } + } + return true; + } +} + +function main() +{ + $Trie = new Trie(); + $Trie->insert('apple'); + var_dump($Trie->search("apple")); + var_dump($Trie->startsWith("apps")); +} +?> +--EXPECT-- +bool(true) +bool(false)