Showing posts with label fuzuli. Show all posts
Showing posts with label fuzuli. Show all posts

Friday, June 1, 2012

Installing Fuzuli on Ubuntu 12.04 LTS


Following pictures are explaining how to install Fuzuli on Ubuntu 12.04 LTS using the DEB package given in http://code.google.com/p/fuzuli/downloads/list









Thursday, May 31, 2012

Mathematics with Fuzuli

Our new general purpose interpreter and language Fuzuli was first introduced in Practical Code Solutions and has the official web page in FuzuliProject.org.

Fuzuli has several packages which are mostly wrappers of C++ libraries. One of them is the math package. It is math.nfl and stated in /usr/lib/fuzuli/nfl in Linux installations by default.

The math.nfl package simple wraps some basic mathematical functions such as square root, exponential, logarithms, floor, ceil and round as well as trigonometric functions such as sin, cos, tan, atan, acos, etc. These functions are directly inherited from the standard C++ library.  A list of functions in the math.nfl package is shown below:


FunctionDescriptionExampleResult
PIReturns a short presentation of PI
(let a (sin (/ PI 2)))
1.0
sinReturns sin of a given reference
(let a (sin 0))
0.0
cosReturns cos of a given reference
(let a (cos 0))
1.0
tanReturns tan of a given reference
(print (tan 1))
1.55741
absReturns absolute value
(print (abs -5))
5
sqrtReturns square root
(print (sqrt 25))
5
expReturns E^x
(print (exp 1))
2.71828
logReturns natural logarithm
(print (log (exp 1)))
1.0
log10Returns logarithm in base 10
(print (log10 10))
1.0
log2Returns logarithm in base 2
(print (log2 2))
1.0
coshReturns hyperbolic cos
(cosh x)
sinhReturns hyperbolic sin
(sinh x)
tanhReturns hyperbolic tan
(tanh x)
asinReturns inverse sine
(asin 1)
acosReturns inverse cosine
(acos 0)
atanReturns inverse tangent
(atan 1.55741)
1
atan2Two arguments arctan function
(atan2 1 1)
0.7853981634
powReturns a^x
(print (pow 5 2))
25
ceilReturns ceil of a given reference
(print (ceil 2.3))
3.0
floorReturns floor of a given reference
(print (floor 2.3))
2.0
roundReturns round of a given reference. (nearest integer)
(print (round 2.3))
2.0
isinfReturns 1 if the given reference is either negative or positive infinitive else returns 0
(print (isinf (/ 19 0)))
1


The table shown above is hopefully enough to introduce our functions in the math package. Any Fuzuli program should include the math package using the (require) expression if any math function shown above is planned to use. Look at the code below:


(require "/usr/lib/fuzuli/nfl/math.nfl")
(print (isinf (/ 19 0)))


Although math functions are collected in an external package, Fuzuli supports built-in arithmetic functions. The code shown below is calculating ordinary least squares regression coefficients. As you see, no math functions required and only the arithmetic functions are used.

(let x (list 1 2 3 4 5 6 7 8 9 10))
(let y (list 10 20 30 40 50 60 70 80 90 100))

(function sum (params a)
 (block
  (def s FLOAT)
  (def i INTEGER)
  (let s 0.0)
  (for (let i 0) (< i (length a)) (inc i)
   (let s (+ s (nth a i)))
  )
  (return s)
 )
)

(function mean (params a)
 (block 
  (def total FLOAT)
  (def result FLOAT)

  (let total (sum a))
  (let result (/ total (length a)))
   
  (return result)
 )
)


(function covariance (params c d)
 (block
  (def meana FLOAT)
  (def meanb FLOAT)
  (def xy FLOAT)
  (def i INTEGER)
  (let meana (mean c)) 
  (let meanb (mean d))
  (let xy 0.0)
  (for (let i 0) (< i (length a)) (inc i)
   (block
    (let xy (+ xy (* (- (nth c i) meana) (- (nth d i) meanb ))))
   )
  ) 
  (return xy)
 )

)


(let beta1 (/ (covariance x y) (covariance x x)))
(let beta0 (- (mean y) (* beta1 (mean x))))
(print "beta0 " beta0  "\n") 
(print "beta1 " beta1  "\n")


The result is 0.0 for beta0 and 10.0 for beta1.

See you in next entry. Have fun!

Wednesday, May 30, 2012

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.

Wednesday, May 23, 2012

Fuzuli: A new general purpose interpreter and language

We are happy to announce that we have just released 0.1 revision of our general purpose interpreter Fuzuli.

Although Fuzuli has a syntax similar to Lisp, it is not intended to be a Lisp clone. We just wanted to have an interpreter that has lots of properties from both Algol and Lisp languages.

Fuzûlî is a 16th century poet, writer and thinker that lived in Ottoman Empire. Since his real name was Muhammad bin Suleyman, he used the name Fuzuli as his pen name. The name Fuzuli is very interesting because of its meaning. It means impertinent, improper, unnecessary in English. However it is an Arabic word with more than one meanings. With its primary meaning, Fuzuli has its roots from the word "science" and the word "arts". Please read more about the name and the poet Fuzuli in Wikipedia.

 It is not a coincidence, we choose this name for our interpreter because we had no motivation for writing it. We have just written it for fun! There are lots of languages that we use such as C, C++, Java, Php and Perl and  they satisfy what we need at all. Writing such a language was just a fuzuli work! Note that we are not computer science guys, so our code may not be a real king!

Fuzuli has been written in C++ and Fuzuli. It currently supports dynamic library loading, Algol type loops and control statements with a Lisp syntax, a primitive garbage collector, scopes for global and local variables, MySql databases and socket connections. It has an IO library that completely inherited from C++. It has libraries about GD2 and Tcl/Tk in an elementary manner. 

 The project page is hosted in Fuzuli Google Code page and the source code is ready for downloading and compiling. Our friends are preparing installation packages for several Linux Distributions such as Ubuntu and Slax. We are also working to build up a comprehensive documentation. Your help about coding, documentation, making packages for the other distributions, Windows and Mac binaries or anything else is also welcome! We also need your ideas and suggestions on Fuzuli for further revisions.

A fibonacci function written in Fuzuli is shown below:

(function fibonacci (params x)
     (block
        (if (<= x 2)
          (return 1)
          (return (+ (fibonacci (- x 1)) (fibonacci (- x 2))))
        )
     )
 ) 
 

This function is called using an expression similar to this:

(let result (fibonacci 5))

A list that contains integers from 1 to 10 can be written as:


(let alist (: 1 10))

and R users would be happy when they see the operator ':' in Fuzuli. A for loop is something like

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

in fuzuli. The variable 'i' is local and invisible to upper scopes, such as global scope. We define variables using a notation like this:

(def i INTEGER)
(def d FLOAT)
(def s STRING)

and variables are visible in the scope which they were created and scopes under that scope. Block, for, while and foreach expressions define blocks. For example 

(block
   (def i INTEGER)
   (let i 10)
)
(print i)

writes NULL because i is defined and set in a local block. However 

(def i INTEGER)
(block
  (let i 10)
)
(print i)

prints a "10" because the variable 'i' is defined at the top of the block.
We have implemented some functionality of Fuzuli in seperated dynamic libraries. The fuzuli package math.nfl has a content similar to
(let mathlib (dynload "FuzuliCore"))



(let PI (C mathlib "pi") 0)


(function sin (params x)
    (return (C mathlib "sind" x))
)

(function cos (params x)
    (return (C mathlib "cosd" x) )
)

(function tan (params x)
    (return (C mathlib "tand" x) )
)

This package loads the dynamic library FuzuliCore (it is libFuzuliCore.so in Linux) and defines some math functions. Actually, they do nothing but acting like wrappers. The fuzuli function 'C' is used to call C functions from fuzuli. In example below, C functions pi, sin, cos and tan are wrapped. The sin and the cos functions are defined as
Token *sind(Token *params, Environment *env){
    Token *resultToken = new Token(0.0, FLOAT);
    resultToken->setFloatValue(sin(params->tokens[0]->getFloatValue()));
    return(resultToken);
}

Token *cosd(Token *params, Environment *env){
    Token *resultToken = new Token(0.0, FLOAT);
    resultToken->setFloatValue(cos(params->tokens[0]->getFloatValue()));
    return(resultToken);
}


Note that this code is not integrated with our primitive garbage collector and the code should be

Token *sind(Token *params, Environment *env){
    Token *resultToken = env->newToken(0.0, FLOAT);
    resultToken->setFloatValue(sin(params->tokens[0]->getFloatValue()));
    return(resultToken);
}


Fuzuli can also used as a CGI language. It supports HTML tags in a manner similar to Php. Here, there is an interesting example:



The example shown below creates an HTML table with content of days of a week! The interesting point is that it starts with an html tag and it ends with html tag end. Setting this file as executable is what we need for running it as a CGI program.

We also plan to implement more languages that run in fuzuli.

Finally, Fuzuli is in its early stages. We need usage statistics, suggestions and ideas. We are also preparing the documentation. Follow us at stdioe!