Scriptol Examples and source code
These source codes has been tested with the Scriptol PHP compiler and the Scriptol C++ / Binary compiler.Recursion
Recursive Fibonacci Algorithm. Calculates Fibonacci numbers
constant 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
Arrays
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.
extern int argc
extern array argv
int main(int argnum, array arglist)
print argnum, "arguments"
int i = 0
scan arglist ` scan the array
echo i, ")", " ", arglist[], "\n" ` display next argument
i + 1
/scan
return 0
main(argc, argv) ` argv and argc are PHP or C 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"
DOM and XML
Create a simple XML file to be accessed by DOM's methods.
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" )
file nfile = fopen(fname, "w")
for text x in arr
nfile.write(x)
/for
nfile.close()
The XML file created:
<?xml version="1.0" ?>
<articles>
<article tag="Internet" title="My article" >
Here is the story I am speaking of...
</article>
</articles>
Load the XML file and get the content by the use of DOM's attributes and methods.
text name = "article.xml";
DOMDocument doc
DOMNodeList articles
DOMNode node
DOMElement article = null
doc.load(name)
articles = doc.getElementsByTagName("article")
node= articles.item(0)
article = node
text tag = article.getAttribute("tag")
text title= article.getAttribute("title")
text content= article.textContent
print "Title:",title,"Tag:",tag,"Content:",content
More sources
- Commonly used algorithms are provided on the Algorithms page.
- The XML directory provides more scripts and tools.
- A XUL example is available on the XUL site.
| Tweet |
|