Skip to content

Syntax and Example Code

seoushi edited this page Aug 13, 2010 · 12 revisions

Disclaimer

These are all just ideas at the moment, feel free to comment on them and let me know what you like and don’t like. There is still a good chance that the syntax will change (in fact it has several times since this page has existed). This guide assumes you know how to program.

Hello world:

“Hello World” print-line .

“Hello World” is a string literal and when compiling actually converts itself to an object. “print-line” send the message “print-line” to the object “Hello World”. A dot (‘.’) denotes an end of a statement.

Identifiers

Identifiers are anything to do with valid names such as variables, objects and function/value slot names.

Identifier rules:
•Can contain 0-9 a-z A-Z – / : ? < > _ * + % @ = ^ ! & ~
•Must not start with a number
•Case sensitive

standard practice on naming most identifiers is all lower-case with hyphens to separate words (ex: print-line)

Variables

Variables are a way to store a value, they can be changed during run time. Variables are assigned using the equals operator “=”.

def b = 1 .

Chaining Methods

Lets say you wanted to reverse a string and then print it. Here is the sample code for that:

"hello" reverse print-line .

Kitsune is smart in the way it knows when to call functions and when to pass arguments. The way it does this is important to know. Any time you have an object literal (string, numbers, arrays and parenthesised expressions) it will treat it like an argument, otherwise it treats it like a method call.

Returning Values

returning a value is just like C based languages.

return 0 .
return "hello" .
return ("hello" reverse) .

The three lines above return the values
0
“hello”
“olleh”

Conditionals

Kitsune supports the if and match conditionals.

An if statement starts with an ‘if’ and optional case have multiple ‘elif’ and optional one ‘else’ statement at the end.


 1:   if `name == "bob" then
 2:      "hi bob" print-line .
 3:   elif `name == "jane" then
 4:      "hi jane" print-line .
 5:   else
 6:      "I don't know you, what are you doing here?" print-line .
 7:   end

In the case of code above a match statement might be a better fit. The match statement is somewhat like the switch statement from other C like languages but it supports objects of any type as long as it implements the “==” method.

 
 1:   match `name in
 2:      "bob" then "hi bob" print-line . end
 3:      "jane" then "hi jane" print-line . end
 4:      default "I don't know you, what are you doing here?" print-line . end
 5:   end

NOTE: at this point all the currently implemented features have been introduced. The following sections are an idea of where Kitsune is headed but subject to change.

Objects

Objects are just a set of functions and data, these are stored in slots. At any time you can add, remove or change slots on an object. Also it’s useful to know that the only way to create a new object is to clone an existing one. If you wish to know the merits of this system lookup prototype vs class object systems. The traditional way to make an object is as follows:

 1:   def person = object clone .
 2:   person setMethod 'printName { self name print-line . } .
 3:   person setMethod 'says { |saying| 
 4:       self name ++ "said: " ++ saying print-line .
 5:   } .
 6:   person setValue 'name "person" .

Since this is a common operation, there is some special syntax for it to make a little less verbose. “self” refers to the object that called the function, if you know a variant of C++ then you might know it as “this”.


 1:   def person = object clone do
 2:   {
 3:        $ setMethod 'printName { self name print-line . } .
 4:        $ setMethod 'says { |saying| self name ++ "said: " ++ saying print-line . } .
 5:        $ setValue 'name "person" .
 6:   } 

Note that “$” is short for self and can be used anywhere and not just in this special syntax form.

Loops

There is only one kind of loop, the while loop.


 1:   while condition { 
 2:       # do something
 3:   }

This may seem kind of limiting however you can use range objects, arrays, lists, maps and other containers to make loops as well.


 1:   range clone set 1 3 each { |number |  
 2:       number to-s print-line .
 3:    } .
 4:   
 5:   [1 2 3] each { |number| 
 6:       number to-s print-line .
 7:    } .

The previous code prints out the numbers 1, 2 and 3 on their own line. The special syntax [1 2 3] is an array literal.