Scriptol/Printable version

From Wikibooks, open books for an open world
Jump to navigation Jump to search


Scriptol

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Scriptol

Permission is granted to copy, distribute, and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 License.

The Scriptol Programming Language

The Scriptol Programming Language[edit | edit source]

A clean syntax[edit | edit source]

C++ syntax:

for(int x = 0; x < 10; x++)
{
 printf("%d\n", x);
} 


Scriptol syntax:

for int x in 0 .. 9 print x

Scriptol doesn't need for semi-colon et end of statements. The end of line is also the end of statement unless several statements fits on a same line, in this case they are separated by semi-colons. If an instruction fits on two lines, the compiler recognizes the instruction.

Objective design[edit | edit source]

Unlike C that was designed with limited hardware in mind, Perl that has added features day after day, and other languages that depends upon the fantasy of the author, Scriptol apply objectives rules, and it near the more used syntax in computer world, the xml one... Xml is tagged and has as C one-line syntax. Scriptol has one-line syntax, (see above) and is tagged:

for 
... 
/for

Universal operators[edit | edit source]

Some programming languages use the same operator for unrelated things. For example, The C programming language uses the "*" operator for both "deferencing" and "multiply". The C programming language uses the "&" for both "address-of" and "binary and".

Scriptol tries to avoid such confusion by making each operator mean something very similar in all the different ways it is used.

For example, the range operator " .. " is used:

  • as the range in the "for" loop (see above).
  • as an interval of an array or a dictionary (see array).
  • as a range in an expression:


if x in 0 .. 9 
  print "x inside range"
/if

Types from the real word[edit | edit source]

In the 1970s, C and Pascal invented types that were related to hardware: char, long, short, char *, float, etc...

Scriptol uses types related to the real word: text, number, integer, natural, real, array, dict, dir, etc...

Readability[edit | edit source]

A complicated and unreadable part of C++ code...

int x[] = { 1, 2, 3, 4 };
int i;
for(i = 0; i < 4; i++)
{
  if(x[i] == test) std::cout << test << " found" << std::endl;
} 

may be replaced by a single and clear Scriptol statement.

if test in { 1, 2, 3, 4 } print test, "found"


If Control Structure

If Control Structure[edit | edit source]

Common syntax[edit | edit source]

For a conditional process, the statement is a "if" keyword, followed by a boolean expression, a list of statements to execute or no, depending of the condition, and the "/if" keyword.

If x = 5
  print "equal"
/if 


Introducing an alternative by the "else" keyword: when the condition is false, another list of statements may be processed.

If x = 5
  print "equal"
else
  print "not equal"
/if 


One-line structure[edit | edit source]

When the body of the structure is just one statement, and not another structure, the syntax may be shortened to only a single line.

If x = 5 print x 


If the statement is not a command as "print", "break", "continue" etc..., the "let" keyword is required.

If x = 5 let y + 1 


Composite if[edit | edit source]

The structure may be extended into a more powerful construct that is both an "if" and a "switch case" structure, even more advanced than the C counterpart since you can test any type of variable.

if x 
  = 5: print "equal"
  > 5: print "more"
else 
  print "less"
/if


For In

For In[edit | edit source]

Common syntax[edit | edit source]

The standard syntax assigns a variable with the elements of a range, and Scriptol writes a range as it is commonly written, with a double dot between two values (or variables).


for int x in 0 .. 10
  print x
/for 


One-line syntax[edit | edit source]

If the body of the structure is only one statement, a simplified syntax is used.

for int x in 0 .. 10 print x 


For in list[edit | edit source]

The for control structure may scan an interal as above, but also a text or an array.

text hello = "hello"
for text t in hello print t 


array a = { 1, 2, 3 }
for dyn x in a print x


While Let

While Let[edit | edit source]

Common syntax[edit | edit source]

while x < 5
 print x
 x + 1
/while 


To increment a variable, Scriptol uses x + 1. This could not work in C and other languages.


Infinite loop in C[edit | edit source]

The C and C-like languages (C++, Java, C#) leads easily into infinite loop. Here hare two simple examples.

while(x < 5);
{
  printf("%d\n", x);
  x += 1:
}

Perhaps you have instantaneously noticed it (and perhaps no), but the construct is bad, due to the semi-colon after the condition!

Another example without the misplaced semi-colon:

while(x < 10)
{
  printf("%d\n", x);
  if(x = 5) continue;
  ... some statements ...
  x += 1;
}


Scan By

Scan By[edit | edit source]

Common syntax[edit | edit source]

The scan control structure allows to parse one or several arrays, and to apply a process onto each element of the array(s).

scan a

a[] * a[] 
print a[] 

/scan


The current element is denoted by an empty indice.

Example with two arrays:

scan a, b
 print a[] + b[] 
/scan 

Scan with a function[edit | edit source]

You can apply instead a predefined function. The argument of the function is an element of the array.

array a = {1,2,3,4 } 
void fun(number x) 
 print x * x
return 
scan a by fun 


If you use several arrays, the number of argument of the function must be the number of array.


Xml or class

Xml or class[edit | edit source]

Common class[edit | edit source]

Scriptol is object-oriented and has classes and inheritance. Defining a class is easy. Example of a very simple class.

class car
  int speed
  void getSpeed()
     ... some statements ...
  return speed 
/class 


Xml[edit | edit source]

But Scriptol has also xml. Defining a xml document is simple also.

xml Car
  speed value = "10" /
  wheels number = "4"
  passengers number = "5"
     Clara, Charly, Corinna, Cyril, Cecilia
  /passengers  
/xml 


Using Xml[edit | edit source]

Classes are used directly or by creating an instance, and making reference to attributes or calls to methods.

Using Xml is not different. You can define how many instances of xml documents you want, and modify the content of each of them. The content of elements or attributes may be assigned, and elements and attributes may be added of removed.

Car myCar ` myCar is an instance of the Car Xml document.
print myCar.speed ` displaying the value of the "speed" attribute.
print myCar.passengers ` displaying the content of the "passengers" tag.

Importing xml document[edit | edit source]

Xml is mainly useful for performing processes on documents produced by various tools, word processors, spreadsheets, etc....

To load such documents, Scriptol integrates a sax parser. You have just to declare a xml document, load le xml file, and you can use the document as a class declared in the source.

xml Car ` declaring an empty xml document.
/xml
Car myCar` defining an instance (not required).
myCar.load("document.xml")  ` loading an external xml document
Car.load("document.xml")  ` you can use directly the xml class itself 


Iterator[edit | edit source]

Iterator on xml document is as iterator on array, plus the down() and up() methods. Iterator allows to parse an entire xml document or just sub-elements of of an element.


demo.begin() 
while demo.isFound() 
print demo.getData() 
let demo.inc()