Basick-1

From ThorstensHome
Revision as of 08:43, 28 October 2006 by ThorstenStaerk (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

What does this mean ? See basick.

#!/usr/bin/php
# note this basic dialect only allows for print 2+2 and print 2
# all must be de-capitalized
# only works for one line of code
# need several expressions in one line
<?
echo "Welcome to my first programming language, ";
echo "BASICK, a basic for kde that sometimes makes me sick.";
echo "Type your code here. End it with a line with only a period.\n";
$line=0;
while (($a=fgets(STDIN))!=".\n")
{
  $lines[$line++]=$a;
} 

echo "The following code has been entered:\n";
for ($i=0; $i<=count($lines); $i++)
{
  echo $lines[$i];
}
echo count($lines)."lines";


echo "\nThe lexical analyzer forms the following:\n";
for ($i=0; $i<count($lines); $i++)
{
  # here, comments and strings should be handled somehow.
  $line=$lines[$i];
  $line=str_replace("\n","",$line);
  $linepartnumber=0;
  $atEnd=false;

  while (!$atEnd)
  {
    if (preg_match("/^print /",$line))
    {
      $linepart[$linepartnumber]="print ";
      $lexpart[$linepartnumber]="print";
      $line=preg_replace("/^print /","",$line);
      $linepartnumber++;
    }
    if (preg_match("/^\+/",$line))
    {
      $linepart[$linepartnumber]="+";
      $lexpart[$linepartnumber]="plus";
      $line=preg_replace("/^\+/","",$line);
      $linepartnumber++;
    }
    if (preg_match("/^[0-9]+/",$line))
    {
      $i=0;
      while ($line[$i]>=1 && $line[$i]<=9) $i++;
      $linepart[$linepartnumber]=substr($line,0,$i);
      $lexpart[$linepartnumber]="number";
      $line=preg_replace("/^[0-9]+/","",$line);
      $linepartnumber++;
    }
    if (preg_match("/ /",$line)) $line=preg_replace("/ /",$line);
    if ($line=="") $atEnd=true;
  }

  # report what the lexical analyzer formed:
  for ($n=0; $n<count($linepart); $n++)
  {
    echo $linepart[$n]."---".$lexpart[$n]."\n";
  }
}

echo "\nThe parser forms the following:\n";
# the parser builds up a tree, e.g. print 2+2 ->
# print
#          +
#        2   2

for ($i=0; $i<count($lines); $i++)
{
  $line=$lines[$i];
  for ($n=0; $n<count($lexpart); $n++) $level[$n]=0;
  for ($n=0; $n<count($lexpart); $n++)
  {
    if ($lexpart[$n]=="print") 
    {
      for ($l=$n+1; $l<count($lexpart); $l++)
      $level[$l]+=1;
    }
  }
  for ($n=0; $n<count($lexpart); $n++)
  {
    if ($lexpart[$n]=="plus") 
    {
      $level[$n-1]+=1;
      $level[$n+1]+=1;
    }
  }
  for ($m=0; $m<count($lexpart); $m++)
  {
    echo $lexpart[$m]."---".$level[$m]."\n";
  }
}

echo "\nNow we execute\n";
# find out maxlevel
$maxlevel=0;
$result=0;
for ($i=0; $i<count($level); $i++)
{
  if ($maxlevel<$level[$i]) $maxlevel=$level[$i];
}
for ($line=$maxlevel; $line>=0; $line--)
{
  for ($i=0; $i<count($lexpart); $i++)
  {
    if ($level[$i]==$line)
    {
      if ($lexpart[$i]=="number") $result=$linepart[$i];
      if ($lexpart[$i]=="plus") $result=$linepart[$i-1]+$linepart[$i+1];
      if ($lexpart[$i]=="print") echo $result;
    }
  }
}

?>