Skip to content

Commit

Permalink
Fix and/or conditions (they should return the value in the expression…
Browse files Browse the repository at this point in the history
…, not true/false)
  • Loading branch information
Raudius committed Nov 22, 2022
1 parent 69b6805 commit bcde079
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/Interpreter/LuarExpressionVisitor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Raudius\Luar\Interpreter;

use Raudius\Luar\Interpreter\LuarObject\DivZero;
use Raudius\Luar\Interpreter\LuarObject\ObjectList;
use Raudius\Luar\Interpreter\LuarObject\Literal;
use Raudius\Luar\Interpreter\LuarObject\LuarObject;
Expand Down Expand Up @@ -91,9 +90,12 @@ public function visitFunctiondef(Context\FunctiondefContext $context): LuarObjec

public function visitTableconstructor(Context\TableconstructorContext $context): Table {
$fieldList = $context->fieldlist() ? $this->visitFieldlist($context->fieldlist()) : [];
return Table::fromArray($fieldList);
return new Table(null, $fieldList);
}

/**
* @return LuarObject[]
*/
public function visitFieldlist(Context\FieldlistContext $context): array {
$fieldContexts = $context->field();
if (!$fieldContexts) {
Expand All @@ -111,13 +113,13 @@ public function visitFieldlist(Context\FieldlistContext $context): array {
}

if ($key === null && $object instanceof ObjectList) {
$objects = $object->getObjects();
foreach ($objects as $object) {
$fields[$n++] = $object->getValue();
$count = $object->count();
for ($i=0; $i<$count; $i++) {
$fields[$n++] = $object->getObject($i);
}
} else {
$key = $key ?? $n++;
$fields[$key] = $object->getValue();
$fields[$key] = $object;
}
}
return $fields;
Expand Down Expand Up @@ -182,7 +184,6 @@ public function visitExpComparison(Context\ExpComparisonContext $context): LuarO
throw new LuarRuntimeException("attempt to compare {$o1->getType()} with {$o2->getType()}", $context);
}


switch ($op) {
case '<': return new Literal(($v1 <=> $v2) === -1);
case '>': return new Literal(($v1 <=> $v2) === 1);
Expand Down Expand Up @@ -250,22 +251,16 @@ public function visitExpPower(Context\ExpPowerContext $context): LuarObject {

public function visitExpOr(Context\ExpOrContext $context): LuarObject {
$o1 = $this->visitExp($context->exp(0));
$o2 = $this->visitExp($context->exp(1));

if ($this->isTrue($o1->getValue())) return $o1;
if ($this->isTrue($o2->getValue())) return $o2;

return new Literal(null);
return $this->visitExp($context->exp(1));
}

public function visitExpAnd(Context\ExpAndContext $context): LuarObject {
$o1 = $this->visitExp($context->exp(0));
$o2 = $this->visitExp($context->exp(1));

if (!$this->isTrue($o1->getValue())) return new Literal(null);
if (!$this->isTrue($o2->getValue())) return new Literal(null);
if (!$this->isTrue($o1->getValue())) return $o1;

return $o2;
return $this->visitExp($context->exp(1));
}

public function visitExpConcat(Context\ExpConcatContext $context): LuarObject {
Expand Down
32 changes: 32 additions & 0 deletions src/Library/LibString.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,36 @@ private function gsub(): Invokable {
return new ObjectList([ new Literal($return) ]);
});
}

private function gmatch(): Invokable {
return new Invokable(function (ObjectList $ol) {
$subject = (string) $this->validateTypeN($ol, ['string'], 0)->getValue();
$pattern = (string) $this->validateTypeN($ol, ['string'], 1)->getValue();

$regex = $this->patternHelper->patternToRegex($pattern);
preg_match_all($regex, $subject, $matches);

$countMatches = count($matches);
$results = [];
foreach ($matches[0] as $i => $match) {
$result = [];

for ($group=1; $group<$countMatches; $group++) {
$result[] = new Literal($matches[$group][$i]);
}

if ($result === []) { // No groups -> return full match
$result[] = new Literal($match);
}

$results[] = new ObjectList($result);
}

return Invokable::fromPhpCallable(function () use (&$results) {
$current = current($results);
next($results);
return $current ?: new ObjectList([]);
});
});
}
}
1 change: 1 addition & 0 deletions tests/unit/LuaSuiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
class LuaSuiteTest extends TestCase {
private function testLua(string $program): void {
ini_set('xdebug.max_nesting_level', -1);
$successReturn = "Luar OK!";
$program .= "\nreturn '$successReturn'";

Expand Down

0 comments on commit bcde079

Please sign in to comment.