Wednesday, May 30, 2012

Defining variables and variable scopes in Fuzuli



Fuzuli has a basic variable scoping strategy and it is easy to understand. Many Algol family languages support variable types, variable defining, initial value setting and updating values. Internally, everything in Fuzuli is a Token. A Token is a C++ object. It can hold an INTEGER, a FLOAT, a STRING, a LIST of all types and a COBJECT. Variables are basically type of an INTEGER, a FLOAT or a STRING. A Fuzuli LIST is a vector or array of those kind of objects. A LIST may be a member of an other LIST. So, objects in Fuzuli can be recursively deep. A COBJECT is a (void*) pointer and can be anything. This type of variable is used for external function calls, usually from C++ or C libraries such as Math, IO and MySQL. Integration of COBJECT types expands the universe that Fuzuli can access. However, a standard user does not need this kind of object because Nafile (nfl) packages wrap those external function calls.

We use (let) expression to set value of a variable in Fuzuli:


# Value of a is set to 10
(let a 10)

# a is now 20
(let a 20)

# a is now a string
(let a "Hello!")

# a is now a double precision number
(let a 3.141592)


(block) expression defines a block in which variables are locally scoped. If a variable defined in a higher scope, it is current in the child scope. In other terms, if a variable set in a deeper scope but it is first defined in the higher scope, the first defined variable is current. Have a look at the example below:


# Value of a is set to 10
# a is defined in higher level
(let a 10)

# this block defines a deeper (local) scope
(block
  (let a 20)
)

# out of the block, a is 20 because global a is accessed from the local scope
(print a)
# the answer is 20


However, one can want to define a local variable with same name of the higher scoped variable. For this, (def) expressions can be used. Look at the example below:

# Defined in an higher scope
(let a 10)

# deeper scope
(block
  (def a NULL)
  (let a 7)
)

# we are in higher scope again
(print a)
# the answer is 10.


So, in Fuzuli, a user does not need to define variables if they want to give a reference them. Look at the (for) expression given below:

(let i 5)

(for (let i 0) (< i 10) (inc i)
    (block
        (print "i is " i "\n")
    )
)

(print "Finally, i is " i  "\n")


The output is

i is 0
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
Finally, i is 10


because, i is first defined in a higher level of scope. If i is not defined before the (for) expression, the code should be like this

(for (let i 0) (< i 10) (inc i)
    (block
        (print "i is " i "\n")
    )
)

(print "Finally, i is " i  "\n")


and the output is

i is 0
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
Finally, i is NULL


because i is defined locally and it is invisible in the higher scope. Strategy of variable scoping in function calls is an other special issue in Fuzuli Suppose that we have an (sum) function defined as follows:

(function sum (params x)
    (block
        (def t FLOAT)(def i FLOAT)
        (let t 0.0)
        (foreach i in x
            (let t (+ t i))
        )
        (return t)
    )
)


In the example above, the (sum) function takes a list 'x' as parameter and returns the sum of elements of this list. The variable 't' is defined and set in the function body. This is unnecessary if we don't use a variable with name 't' in the higher level of scopes. For convenience and security, defining all local function variables before they were used is a good habit. For the rest of the example, the sum function can be called using

(let mylist (list 1 2 3 4 5))
(let result (sum mylist))
(print result "\n")
and gives the result of 15. Hope you have fun !

An informative video on RCaller

Somebody on the Internet submitted an informative video on how to use RCaller for calling R from withing Java applications in YouTube.

It is nice to see RCaller has an higher usage rates after its 2.0 version.

You can see the embedded video in this entry. Have a nice training!

Writing CGI scripts with Fuzuli Programming Language

Fuzuli is also suitable for writing CGI (Common Gateway Interface) scripts. The logic underlying the CGI script is to define a content type for the document and print out the content using the standard input & output routines. Form elements posted by a client are generally handled using some environmental variables and standard input. Handling those variables varies according to the sending method of html forms.

Fuzuli has a different approach for CGI scripts. For example, in Php, a script file has a file extension of .php and it must be started with some html tags or a php starter

<?php

tag. While the file extension is .php, we have to declare that "we have finished writing html codes and now we are starting a new php area!". However, in Fuzuli, the default behavior is to write fuzuli code and writing html is an exception.

A fuzuli cgi script must define the location of the interpreter in the top level of the code:

#!/usr/bin/fuzuli

Secondly, before writing any output, content-type should be defined :


#!/usr/bin/fuzuli
(print "Content-type: text/html\n\n")


Ok. That is too much naive to CGI scripters. How to add html tags to this script? Follow the example below:

#!/usr/bin/fuzuli
(print "Content-type: text/html\n\n")
<?html

<h1>Hello World!</h1>

?>


Very nice, hah? So we can easily combine our logic and html! Ok lets do some html forms and handle them in Fuzuli.


#!/usr/lib/fuzuli/fuzuli
(print "Content-type: text/html\n\n")

<?html
   <form method="get">
   Enter your name:
   <input type="text" name="name">
   <input type="text" name="email">
   <input type="submit" value="Click">
</form>
?>

(let name (REQUEST "name"))
(let email (REQUEST "email"))

(if (isset name)
    (print "Posted name is : " name)
)

<?html
<h1>-----------------</h1>
?>

(if (isset email)
    (print "Posted email is: " email)
)



In the example above, the name and the email variables are sent from the html form. If they are set, their values are printed. Very simple. A Fuzuli loop can include an html block so one can generate html codes efficiently. Look at the code below:

#!/usr/lib/fuzuli/fuzuli

(print "Content-type: text/html\n\n")

<?html
<table border = "1">
?>

(let days 
  (list "sunday" 
        "monday" 
        "tuesday" 
        "wednesday"
        "thursday" 
        "friday" 
        "saturday"
   )
)
(for (let i 0) (< i (length days)) (inc i)
    (block
            <?html <tr><td><b> ?>

            (print i)

            <?html </b></td>

            <td><i>
            ?>

            (print (nth days i))


            <?html
                </b></td></tr>
            ?>
    )
)

<?html
</table>
?>      


Fuzuli supports cookie operations internally. setcookie and getcookie functions were implemented for setting and getting cookies, respectively. Note that cookies must be set before sending the content-type header.

setcookie (var "This is cookie value")
(let value (getcookie "var"))


The next step is to implement session functions. We are still discussing the session structure of Fuzuli. It seems it is the most convenient method to implement this functionality in Nafile (nfl) packages. Hope you like scripting with Fuzuli...

Friday, May 25, 2012

Fuzuliproject.org

After publishing Fuzuli's source code and introductory blog entries, we have just published our web site, official Fuzuli home page. We hope you can easly follow up the news about our interpreter using this site.

Follow our news and us!

For the stdioe team, A.Gokhan Satman.

Thursday, May 24, 2012

File operations in Fuzuli

Fuzuli, our new programming language and interpreter, was first introduced in Fuzuli: A new general purpose interpreter and language has several input and output functions completely derived from standard C and C++.

Since our language is in its first stages and input and output functions have high priority, Fuzuli has adequate number of functions for those jobs.

First of all, the io package must be included before any function call. This package is stored in /usr/lib/fuzuli/nfl by default. Nfl packages (Nafile) have a similar file extension with the name Fuzuli. It means "useless" in English.

In Fuzuli, a package can be included using the require function:


(require "/usr/lib/fuzuli/nfl/io.nfl")


Fuzuli has fopen, fwrite, fread and fclose functions for opening, writing, reading and closing of files,respectively. The code shown below shows how to open file for writing.

(let f (fopen "/tmp/example.txt" "w"))
(let liste (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14))
(fwrite f liste)
(fclose f)


In the example above, we are opening the file example.txt for writing and writing elements of a list in it. This list contains integer numbers from 1 to 14. fwrite takes the reference of file and object to be written as parameters. A single element can be written in same manner. Finaly we are closing the file using 'fclose'. The example shown below shows how to read from values from the same file:

(let dd (fopen "/tmp/example.txt"  "r"))
(let a 0)
(let mysum 0)
(for (let i 0) (< i 5) (inc i)
                (let mysum (+ mysum (fread dd a)))
)


In the example above, dd is a reference to the example.txt. Note that, fopen shares the same file mode types with C++. So "r" means, the file will be opened in "read" mode. Variables "a" and "mysum" are set to zero before the loop. In loop, we are reading the next object using "fread" from the file reference "dd". mysum holds the sum of first 5 integers. The result is 15. Question: How fread knows the data type? The answer is easy. "a" is set using (let a 0) so a is an integer. If "a" is defined using (let a 0.1) then "a" is a double and fread will read a double from the file. In previous example, 5 objects were read from the file. Sometimes, it is impossible to know the number of records of objects that contained by the file. feof function can be used in a loop as in Fuzuli's parents.

(def a INTEGER)
(let dd (fopen "/tmp/kek.txt"  "r"))
(while (= (feof dd) 0)
        (block
                (print (fread dd a) " ")
        )
)
(fclose dd)


In the example above, the value of "a" is not set but it is defined as integer. We are reading from the file in a while loop and the stop condition is reaching the end of file. As we mentioned above, fread reads integers because of the type of variable "a". Fuzuli has several input and output functions beyond the file operations. The function fflush is used for writing bytes stored in the buffer.

(let f (fopen "/tmp/example.txt" "w"))
(let liste (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14))
(fwrite f liste)
(fflush f)
(fclose f)


In the example above, fflush sends content of the file buffer to the opened file after each fwrite operation. getpdw function is to get the working directory:

(let mydir (getpwd))
(print mydir "\n")


With chdir, current path can be changed:

(chdir "/tmp")


Function dir returns a list of directories and files of a given directory:

(let mydir (dir "."))
(foreach direntry in mydir
        (block
                (print direntry "\n")
        )
)


In the example above, dir takes a parameter of "." so content of the current directory is shown. Function unlink and rename deletes and renames files, respectively:

(let mydir (dir "."))
(unlink "/tmp/example.txt")
(rename "1.txt" "2.txt")


In the example above, example.txt was deleted and name of 1.txt is set to 2.txt.

Fuzuli currently has those io functions in io.nfl:
  • fopen
  • fclose
  • feof
  • fwrite
  • fread
  • fflush
  • chdir
  • getpwd
  • dir
  • unlink
  • rename
  • tmpfile
  • tmpnam
  • datetime
  • asctime
  • sleep
  • getenv
  • rnd
  • print_r
  • popen
  • pclose

There are several input and output functions in Fuzuli. Please have a look at the Fuzuli Documentation in Fuzuli Source Code page for further information. We will explain the language and its libraries with more examples in next posts.