0

I have found a good Prolog transpiler, which is released at here (https://gist.github.com/rla/3869174).


I have converted it from JavaScript to PHP. Below is my output code in PHP language:

<?php

$util = require ('util');

function Var ()
    {
    $this->ref = $this;
    }

function Struct()
    {
    $this->arguments = $arguments;
    } =
function ()
    {
    $args = call_user_func(array(
        Array ::prototype,
        'slice'
    ) , 1);
    return $this->arguments[0] + '(' + join(', ', $args->map($toString)) + ')';
    };
$exports::Var = $Var;
$exports::Struct = $Struct;

function deref($term)
    {
    while (true)
        {
        if ($term instanceof $Var)
            {
            if ($term == $term->ref)
                {
                return $term;
                }
              else
                {
                $term = $term->ref;
                }
            }
          else
            {
            return $term;
            }
        }
    }

$exports->unify =
function ($a, $b, $stack, $cb)
    {
    if (unification($stack, $a, $b))
        {
        return $cb;
        }
      else
        {
        return backtrack($stack);
        }
    };
$exports->run =
function ($cb)
    {
    while ($cb = $cb())
        {
        }
    };

function backtrack($stack)
    {
    $top = null;
    while ($top = array_pop($stack) ())
        {
        if ($top instanceof $Var)
            {
            $top->ref = $top;
            }
          else
            {
            return $top;
            }
        }
    }

function toString($term)
    {
    $term = deref($term);
    if ($term instanceof $Var)
        {
        return '_';
        }
      else
        {
        return ();
        }
    }

function unification($stack, $a, $b)
    {
    $ad = deref($a);
    $bd = deref($b);
    $console->log(toString($ad) + ' ' + toString($bd));
    if ($ad instanceof $Var)
        {
        $ad->ref = $bd;
        array_push($stack, $ad);
        }
      else
    if ($bd instanceof $Var)
        {
        $bd->ref = $ad;
        array_push($stack, $bd);
        }
      else
    if ($ad instanceof $Struct)
        {
        if ($bd instanceof $Struct)
            {
            if ($ad->arguments[0] == $bd->arguments[0] && count($ad->arguments) == count($bd->arguments))
                {
                for ($i = count($ad->arguments) - 1; $i >= 1; $i--)
                    {
                    if (!unification($stack, $ad->arguments[$i], $bd->arguments[$i]))
                        {
                        return false;
                        }
                    }
                }
              else
                {
                return false;
                }
            }
          else
            {
            return false;
            }
        }
      else
        {
        return $ad == $bd;
        }

    return true;
    }

?>

Then, I have used a PHP Code Checker to check about my output code. And, they alert that my code has some errors:

  1. Error: There is 1 line that is defining variables with too many equal signs '=': $ad == $bd;
  2. Parse error: syntax error, unexpected 'Var' (T_VAR), expecting identifier (T_STRING) or '(' in your code on line 5 function Var ()

Can you help me to fix it?

fpj
  • 315
  • 1
  • 14
  • 1
    Honestly, that is one horrible mess of code. I'd say the only way to debug it is to do a ctrl+a, del, and then write it from scratch. What exactly should the code be doing, and could you show the original Javascript code? – Schlaus May 13 '15 at 12:50
  • it is possible to create javascript code which cannot be converted to php. It seems to be the case that your code is such. You should not use a converter-tool but do it by hand. Commands like `console.log()` cannot be converted to php. The posted code has got so many errors that i don't know where to start. You should learn php if you want to write some php code. – steven May 13 '15 at 12:53
  • @fpj i did not voted your question down. But i think we cannot solve your problem here. And your question is marked as dublicate so nobody can answer it. – steven May 13 '15 at 13:01

0 Answers0