Examples of Scriptol source code

These source codes has been tested with the Scriptol PHP compiler, Scriptol JavaScript compiler and the Scriptol C++ / Binary compiler.


Recursive Fibonacci Algorithm. Calculates Fibonacci numbers

const int fibmax = 16
int z = 0

int fib(int n)
  if n <= 1 :  z = n
     z = 0 + fib(n - 1) + fib(n - 2)
return z

for int i in 0..fibmax
  print "fib($i)=" , fib(i)

Iterative Fibonacci algorithm

 int fibmax = 20<
    int fibonacci(int     n) 
    int u = 0 
    int v = 1
    int t
    for int     i in 2 .. n 
      t = u + v 
      u = v 
      v = t 
 return v 

 for int x in 1..fibmax  echo "fib("     , x , ") ", fibonacci(x), "\n" 

Download the source compiled to PHP, JavaScript and C++.

Arrays, comparing two files

Compare two text files loaded into arrays.

array f1, f2
if argsize <> 3 ? die("usage:  fcomp file1 file2")
echo "Comparing ", arglist[1]," ", arglist[2] , ": "
if not file_exists(arglist[1]) ? die(arglist[1] + " not found")
if not file_exists(arglist[2]) ? die(arglist[2] + " not found")
if f1.size() <> f2.size() ? die("sizes differ")
for int i in 0..f1.size() - 1
  if f1[i] <> f2[i] ? die("files differ")
print "no difference found."

Integrated functions

Current time or date and time of last modification of a file.

print "Example of date and time"
print "Format ISO 8601 (PHP 5/Scriptol C++)"
real d = time()
print "Current date and time:", date("c", d)
d = filetime("exdate.sol")
text filedate = date("c", d)
print "Date of this file:", filedate[ .. 9]

Command line

"Hello You!" program. Basic input and output functions at command line.

text name
input "What is you name? ", name
print "Hello", name

Passing arguments at command line.

int main(int argnum, array arglist)
  print argnum, "arguments"
  for int i, var v in arglist             // scan the array
    print "$i)", v                        // display index and value
return 0

main($argc, $argv)  // $argv and $argc are system variables.


Simple class with one method. Count occurences of words in a string.

class Words
  int count(text base, text sea)
  int ctr = 0
  int i = 0
  while forever
    i = base.find(sea, i)
    if i = nil break
      i + 1
      ctr + 1
  return ctr


Words demos
text sample = "a b c de a hello a"
print "Number of words:", demos.count(sample, "a")

Inheritance and static method. Defining a class and a sub-class. Instance, method and static method.

class Car
    int power = 850
    int getPower() return power
    static text color(int c)
        text tc = "other"
        if c
        = 1: tc = "blue"
        = 2: tc = "green"
        = 4: tc = "red"
    return "color is " + tc

class FormulaOne is Car
    int speed
    int getSpeed()
        speed = getPower() * 2 / 5
    return speed

FormulaOne f1
print f1.power
print f1.getPower()
print f1.getSpeed()
print FormulaOne.color(4)  

Graphics using GD (Scriptol PHP)

Draw a button. Create a button with a label.

include "phpgd.h"
include "image.sol"

print "Program Button - Testing GD"

Image button
int black = button.createColor(0, 0, 0)
int white = button.createColor(255,255,255)
int green = button.createColor(0, 192, 0)

` filling background and drawing borders
button.line(0,0,87,0, white)
button.line(0,0,0,30, white)
button.line(0,30,87,30, black)
button.line(87,0,87,30, black)

` now, writing a label
button.write(26, 8, "Label", black)

text bname = "button.jpg"
button.saveJpeg(bname, 100)
print bname,"created"


Create a simple XML file to be accessed by DOM's methods. (Scriptol 1/PHP)

text tag = "Internet"
text title = "My article"
text article ="Here is the story I am speaking of..."
text fname="article.xml"

array arr = array("<?xml version=\"1.0\" ?>\n",  "<articles>\n", 
" <article tag=\"" + tag + "\" title=\"" + title+ "\" >\n", article + "\n",
" </article>\n", "</articles>\n" ) arr.store(fname)

It is possible in Scriptol 2 to use a dict and assign the values to keys.

text fname="article.xml"

dict d = {
  "articles" : {
    "article": {
       "tag" : "Internet",
       "title" : "My article",
       "data" : "Here is the story I am speaking of..."


The XML file created:

<?xml version="1.0" ?>
<article tag="Internet" title="My article" >
Here is the story I am speaking of...

To load the file in scriptol 2, just write:

dict d

Counting the occurences of a substring

Count is a method of the String class.

class String
  int count(text base, text sea) 
int ctr = 0
int i = 0
while forever
i = base.find(sea, i) // return the position, if found
if i = nil break // else, return nil (not in list).
i + 1 ` advancing the pointer inside the base text
ctr + 1 ` counter of occurences
return ctr /class
String str // an instance of String
print str.count("somestring", "string")

Using the GTK graphical user interface (Scriptol C++)

include "gtk.sol"

include <gtk/gtk.h>
int main(int argc, array argv)
GtkWidget window = null
gtk_init(null, null)
window = gtk_window_new($GTK_WINDOW_TOPLEVEL)
return 0

More sources