An introduction to KoiFish syntax

Despite the small blurb of code in the last post, the only syntax you’ve seen from KoiFish is what you’re able to piece together from the grammar posted.  I think it’s only fair to display some actual code that compiles and runs with the current build of the KoiFishCompiler (The KFC [mmm chicken…]).  Typical language tutorials always start you out with a simple hello world to give you the briefest introduction possible to not overwhelm the reader. I’ll stick with this since some syntax is not your typical C-style.


koi + MyHelloWorldEntity

fn + Main :args begin     
 puts "Hello, World!";

Let’s dive right in by looking at our File (“HelloWorld.koi”).  The first line of our program is our Koi Declaration. This is defined in our grammar as an object definition and is similar to a class of other C-style languages.  We know it’s an Koi Declaration because it’s prefaced with ‘koi’.  The ‘+’ sign is our Access Modifier that tells the compiler that ‘MyHelloWorldEntity’ is publicly initialize-able to other koi in the pond.  You make be familiar with the UML diagrams where this standard is pulled from. ( ‘+’ : public, ‘-‘ : private, ‘#’ : protected).  One last thing to note about the Koi Declarations is that they do not need to match the file name they’re declared within, and such is the case with out example.

Moving on the next non-whitespace line we see is a Function declaration.  We know this because of the ‘fn’ keyword.  Next, we see the access modifier for the function that, just like  our koi, is public.  Following that, we see our Function Identifier, “Main” in this case.

Before we continue, a quick blurb about the Main Function. A single function with an identifier of “Main” and taking a single argument is required for a pond. In case you missed what a pond file is, see last post.  The reason for this requirement is that it signals to the KFC where the entry point of your program.

Back to the function declaration statement, succeeding the identifier is the Argument ListArguments are prefixed with a ‘:’ symbol in the function declaration and are space delimited.  note: when using arguments inside the body of the function, you do not use the ‘:’ prefix.  Lastly, we begin the function body by using the begin keyword as shown above.

Inside our function body, we call puts, a simple output command, to display an expression to the bound output channel (usually a console window). We conclude the statement with a semicolon and if we had more to statements to follow, there would appear after. However, since we don’t have any other statements, we end the function by the end keyword followed by the terminating semicolon.

There you have it, a simple Hello, World! example in KoiFish!  Easy enough, eh? Let’s spice things up by doing some conditionals and arithmetic.


koi + Spicy

fn + Main :args begin
    def a = 2 + 3;
    def b = a + 95;

    if a > 10 then
        puts "a is greater than 10"; # should not see this!
         puts "a is less than 10";

    if b == 100 then
        puts b;

Running through some syntax that’s new, we see def that is a keyword used to define   a variable.  In our case we define the variable ‘a’ to the expression ‘2 + 3’.  Since I know you can do simple arithmetic,  variable ‘a’ is now bound to the value of 5 after this statement is evaluated.  In the next line we assign ‘b’ the current value of ‘a’ (5) added to 95 to give us a cool 100.

The next section of code is our conditional statements that check equality. If you take a peek at the grammar listed in the first post, you can see that we’ve defined the rules for a conditional to check equality on a boolean expression.  A boolean expression is further defined as an expression followed by an equality operator, such as ‘>’, ‘>=’, ‘!=’ etc., followed by another expression.  If this isn’t your first programming language then you know if the boolean expression is evaulated to TRUE the statements following the if case are then evaluated. If an Else case is defined, the program execution would jump to statements defined inside the else case if the boolean expression evaluated to FALSE.

The only other new syntax we see is a comment. Comments are indicated with a hash mark (‘#’) and anything else on that line is considered part of the comment.  The KFC ignores comments when parsing and is the only element of the language that terminates after a new line.  With that said, pending no comments in your code, you could write you’re entire program on one line, if you so choose to, and it would compile just fine.

There is my introduction to sytnax for KoiFish and I’ll attempt to splice in unseen syntax into future posts to see them in action. Enjoy!

This entry was posted in KoiFish and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s