Examples of Scriptol programs

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

Recursion

Recursive Fibonacci Algorithm. Calculates Fibonacci numbers

const int fibmax = 16
int z = 0

int fib(int n)
  if n <= 1 ? z = n
  else
     z = 0 + fib(n - 1) + fib(n - 2)
  /if
return z

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

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 
    /for 
 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")
f1.load(arglist[1])
f2.load(arglist[2])
if f1.size() <> f2.size() ? die("sizes differ")
for int i in 0..f1.size() - 1
  if f1[i] <> f2[i] ? die("files differ")
/for
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
  /for
return 0

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

Classes

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
  /while
  return ctr
/class

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"
        /if
    return "color is " + tc
    
/class

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

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
button.create(88,31)
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.fill(green)
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.setFontSize(5)
button.write(26, 8, "Label", black)

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

XML made simple

You have an XML file and you want to access the contents.

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

To load the file in scriptol 2, just write:

dict d
d.load("article.xml")

Here is the content:

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

It may be saved in an XML file:

d.store("article.xml")

The XML file created:

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

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
/while
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)
gtk_widget_show(window)
gtk_main()
return 0

More sources...