Scriptol, JavaScript, PHP, comparison

Comparing syntaxes of the three programming languages.

Basic functions

Scriptol JavaScript PHP
print "Hello"
console.log("Hello");
echo "Hello", "\n";
print a[x .. y] 
console.log(a.slice(x,y-x+2));
print_r(array_slice($a,$x,$y-($x)+
   count($a)*(($y<0)-($x<0))+1));echo "\n";
a[.. y] = b[1 .. 3]
a.splice.apply(a,[0,y-0+1].
  concat(b.slice(1,4)));
array_splice($a,0,$y-(0)+count($a)*($y<0)+1,
  array_slice($b,1,3));
a[2 .. 8] = nil 
a.splice.apply(a,[2,7].concat([]));
array_splice($a,2,7);

JavaScript is sometimes easier than PHP, sometimes it's the opposite. Scriptol is always easier than the other two languages.

Data Structures

Scriptol JavaScript PHP
array a = [ 1, 2, 3 ]
var a=[1,2,3];
$a=array(1,2,3);
<car speed=150 name="Spitfire">
  <engine power=100></engine>
  <passengers num=4>
    "Clara, Dana, Elisa, Farah"
   </passengers>
</car>
var car={
  "speed":150,
  "name":"Spitfire",
  "engine":{
     "power":100
  },
  "passengers":{
  "num":4,
  "data":"Clara, Dana, Elisa, Farah"
 }
};
$car=[
  "speed"=>150,
  "name"=>"Spitfire",
  "engine"=>[
    "power"=>100],
    "passengers"=>[
      "num"=>4,
      "data"=>"Clara, Dana, Elisa, Farah"
   ]
];

An XML object is converted to an associative array in JS or PHP. The XML structure has a JavaScript equivalent form in nested objects, which can be saved in a file in JSON format. JavaScript has all the possibilities for advanced, dynamic data representation, but in a rudimentary and not very readable form. The other languages do not even have those features.
JSON is a way to serialize (copy in a file) a JavaScript array.

Control Structures

Scriptol JavaScript PHP
for int i in 0 .. 9 print i
for(i=0;i<=9;i++) 
 console.log(i);
for($i=0;$i<=9;$i++)
echo $i, "\n";
for int i in 0 .. 9
print i
/for
for(i=0;i<=9;i++) {
 console.log(i);
}
for($i=0;$i<=9;$i++)
{
echo $i, "\n";
}
if j in [ 1, 2, 3, 4 ] print "in"
if(([1,2,3,4].indexOf(j)!=-1)) {
console.log("in");
}
if(in_array($j,array(1,2,3,4)))
{
echo "in", "\n";
}
do
case x = 1: print 1
case x > 1: print 2
else print 0
/do
do {
   if(x===1) {
      console.log(1);
   }   
   else {   
     if(x>1) {
       console.log(2);
     }   
     else {
       console.log(0);
     }
   }
} while(false);
do
{
   if($x===1)
   {
      echo 1, "\n";
   }
   else
   {
   if($x>1)
   {
      echo 2, "\n";
   }
   else
   {
      echo 0, "\n";
   }
   }
}
while(false);
text t  
input "O/N?", t
if t.lower()
= "o": print "yes"
= "n": print "no"
else
print "what?"
/if
var t="";
process.stdout.write("O/N?");
t=fs.readSync(process.stdin.fd, 100, 0, 'utf8').shift();
process.stdin.pause();

t=t.toLowerCase();
if(t==="o") {
   console.log("yes");
}
else {
   if(t==="n") {
      console.log("no");
   }
else {
   console.log("what?");
}}
$t="";
echo "O/N?";
$fp=fopen("php://stdin","r");
$t=rtrim(fgets($fp,65536));
fclose($fp);

$t=strtolower($t);
if($t==="o")
{
   echo "yes", "\n";
}
else
{
   if($t==="n")
   {
      echo "no", "\n";
   }
else
{
   echo "what?", "\n";
}
}

The for loop is common to all languages, and the for in or for each variations that can scan the contents of a list, is added to all dynamic languages. However in JavaScript, forEach is so inefficient that it is better to use the classic for loop.

Pattern-matching is implemented in Scriptol with the do ... case structure.

Classes

Scriptol JavaScript PHP
class Car is Vehicle   
int speed   
int getSpeed()      
speed + 1
return speed
/class
var Car=(function(_super)
{
   scriptol.extends(Car,_super);
   Car.prototype.getSpeed=function()
   {
      this.speed+=1;
      return this.speed;
   }
   function Car() {
      _super.apply(this, arguments);
      this.speed=0;
   }
   return Car;
})(Vehicle);
class Car extends Vehicle
{
   var $speed=0;
   function getSpeed()
   {
      $this->speed+=1;
      return $this->speed;
   }
}

The declaration of a class in JavaScript version 6 is similar to that of Scriptol or PHP, but compatibility on the Web will not come before years.