Globals, Lists, and Kois …. [oh, my!]

Some staggering improvements to the language have occurred since the last post so staying true to the cause, I’ve gone back and edited all previous ones to keep them up to date and contains the latest syntax additions.

Out of the gate, I’m going to throw some new syntax your way:

koi + Person

def - firstName = "";
def - lastName = "";
def - age = 0;
def - eating = FALSE;
def - sleeping = FALSE;
def - pets = [];

fn + Initialize :fname :lname :a begin
   firstName = fname;
   lastName = lname;
   age = a;
   eating = FALSE;
   sleeping = FALSE;
   pets = [];
end;

fn + getStatus begin
   puts firstName ^ " " ^ lastName;
   puts age;

   if eating == TRUE then
       puts "nummy nummy, eating";
   else
       puts "stomach want food!";
   end;

   if sleeping == TRUE then
       puts "shh...sleeping";
   else
       puts "Me want sleepy time!";
   end;

   puts "Number Of Pets: " ^ call length@pets;

end;

After our koi decleration we have Globals.  Globals are variables that are bound to the scope of the koi instead of bound to the scope of a function.  Because these are at the koi level, they need access modifiers to indicate their visibility to the pond.  An important note about globals is that the value you assign does not stick! It is there soley to allow the compiler to infer the type while compiling your koi.

Next, we have our Initialize function. This is a special function (you can tell this because it’s capitalized) that is called when allocated and acts as your koi’s Constructor.   It’s here that we assign our arguments from an allocation statement and initialize our global since if left undone, they will be nil.

You’ll notice that pets is assigned ‘[]’. This is an empty list.  A list is a data structure in KoiFish that holds objects of any type.  If you wanted to initialize it with data you could do so like:

def emptyList = []; #empty list
def nonEmptyList = [1, "some string", emptyList, ["a new list"]];

A list also has functions associated with it like ‘push’, ‘pop’, ‘length’, ‘element’, ‘insertAt’, ‘concat’, and ‘toString’.

Going back to the koi, we next have a function that outputs the status of our Person.  The first new syntax you’ll see is the ‘^’ character.  This is KoiFish’s symbol for string concatenation.  It’s operation is simple, it attempts to convert the evaluated expression on it’s left to a string and concatenate it to the evaluated expression on the right after attempting to convert it to a string as well.  We can see this in more detail at the last statement in the function where it concatenates a string and a function call. Here it evaluates our call to length, which returns an integer, on our ‘pets’ global, which is a list, and then converts the evaluated result to a string and finally concatenates it with the string “Number Of Pets: “.

Posted in Uncategorized | Tagged , , , | Leave a comment

Allocating a Koi in KoiFish

Last post dealt with Call Targets, which as quick refresher was the koi instance in which you were calling a function on, and all was fine and dandy, but how do you get an Koi instance? Allocating it of course.

Here are two Koi definitions (they, of course reside in separate files and are in the same pond, but below is the contents of each):

#KoiA.koi
koi + KoiA

fn + Main :args begin
    def b = alloc KoiB;
    call Hello@b;
end;

fn + Hello begin
    puts "Hello from KoiA";
end;

#KoiB.koi
koi + KoiB

fn + Hello begin
    puts "Hello from KoiB";
end;

So if we compiled and ran this, the output would be:

Hello from KoiB

That’s because of the call target we discussed in the last post. KoiA allocates an instance of KoiB using the alloc keyword. What that keyword nicely takes care for us, is allocating the memory required for storing our Koi on the heap, constructing the actual koi and returning the reference to it.  We took that reference and and assigned it to the b variable.

Now that we have an instance of KoiB, we call the Hello function specifying our call target as b.  If we didn’t have a call target, since it is optional, that line of code would evaluate KoiA‘s version of Hello instead. However, if KoiA didn’t have a Hello function at all, the KoiFish Compiler would not compile this program.

A quick blurb about allocating kois: once an koi has been allocated, it returns an reference to the created koi. If you do not store this reference, or at any point in the program you set the reference to something else, the koi would be deallocated and the memory freed provided that no other references point to the koi. This functionality is extremely helpful since you don’t have to worry about leaving unneeded kois hogging up precious ram. This feature is called Garbage Collection.

Posted in Uncategorized | Tagged , , , | Leave a comment

Ponds,Functions and Call Targets in KoiFish

A couple posts ago, I spilled my conundrum about linking with the KoiFish Compiler (KFC).  KoiA and KoiB need some portal of communication if they both so desire to be public facing with public facing functions.  So, it begs the question:

If KoiA calls a function on KoiB, how does the KFC know that KoiB even exists, and if it does, that the function KoiA is calling exists, and if it does, it has access to do so?

If you’ve already read the post dealing with my short introduction to my linking problem, my solution was Ponds. Pond files (.pond) are a collection of KoiFish source code files (.koi). Their purpose is to outline the program as a whole which could span many Kois, and therefore many Koi files.  The KoiFish Compiler takes the Pond file, and pulls in all the Koi files to scan and parse each.  I said last time that a language like  uses header files to accomplish this but that I had no desire to replicate these as they’re tedious and potentially confusing (to language newcomers).  So, the KFC does this for us!  After each Koi is scanned and parsed, the collection of parsed Kois are then consumed by the linker which extracts all Kois with their respective access along with all the functions that belong to that Koi and their respective access.

So where does this leave us?  Call Targets.

A call target is the Koi instance (or Koi itself in static cases) that when making a function call.  Here is some syntax showing function calls in KoiFish

#without call targets call SomeFunction; 
call SomeFunction :arg1 :arg2;

#with call targets
call SomeFunction@KoiB;
call SomeFunction@KoiB :arg1 :arg2;

#calls as expressions
def x = call SomeFunction@KoiB :arg;

In KoiFish, a function call can be both a statement or an expression. In the latter, as you would expect, the function call is evaluated and it’s result used in the left hand side.  The first two in the above example show function calls without call targets.  When the KFC comes across this, it will make the assumption that you’re referring to a function within the koi that is making the call.  I’ll talk about function arguments in more detail in a minute, but the second line shows a function (SomeFunction, in this case) being called with the arguments(arg1 and arg2). Next we see 2 functions with call targets using the ‘@’ operator, appropriately named the Call Target Operator. This simply means that you want to call a function on a specific target–In our example, we’re saying that we want to call KoiB‘s version of SomeFunction.  Lastly, we have an example of a function being used as an expression where the return value of KoiB‘s SomeFunction with :arg is evaluated and assigned to x.

Now that we know how to call functions in KoiFish, there’s a good chance that we need to handle function arguments.  The grammar rules of KoiFish demand that we signify an argument with an identifier with a ‘:’ prefix and that they are space delimited.  How much space is left up to programmer, however it’s a KoiFish standard to have a single space between arguments.  


Note:
When referring to an argument outside of a function call or in the definition of a function, the ‘:’ is dropped and just the identifier is used to refer to it.

Posted in KoiFish | Tagged , , , | Leave a comment

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.

HelloWorld.koi

koi + MyHelloWorldEntity

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

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.

SpicedUp.koi

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!
    else
         puts "a is less than 10";
    end;

    if b == 100 then
        puts b;
    end;
end;

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!

Posted in KoiFish | Tagged , , | Leave a comment

Object Orientated Thinking & Definition of a Function

I’ve been writing this language with the goal being that the result of all this effort will be a language that’s object-orientated. So, sticking to my guns, I’ve created  the functionality of a “Koi” which is an object definition. This concept is nothing new of course and commonly called “classes” in most modern languages.  If you’ve seen the grammar in the last post, you’ve seen it baked in.  The Koi is a definition of an object and is one-to-one relationship for a koifile (KoiFish source code file with the file extension of .koi).

With this said, it presented me with an interesting problem that, for whatever reason, I completely forgot about–linking! I’ve been pondering (pun severely intended–you’ll get it in a second) a solution to the design of how the linker should work to incorporate multiple Entities and therefore multiple koi files.  Languages like use header files to accomplish this task but to me seem extremely cumbersome, too verbose, and a serious violation of the DRY (don’t repeat yourself) policy. So my next progression will be implementing multiple koi support through a single file that holds references to each koi-file.  I’ve appropriately named this collection of koi-files as a pond file (.pond file extension).

That aside, I’ve had to update the definition a function which I found takes the role of both an expression and a statement like below:

# here is an expression
def x = call SomeFunction;

# here is a statement
call AnotherFunction;

The difference being that the function call is either a stand alone or to be evaluated and have its result be the actual expression. As far as whether the function itself is ever really an expression is still up for debate in my mind, however what’s clear is that it needs to, at least from a parsing standpoint, be treated as such to adhere to the grammar defined.

Posted in KoiFish | Tagged , , , , , , | Leave a comment

Going Fishin’ for Koi

Here is the start of my journey to writing my own programming language and it’s counterpart compiler.  Since I’ve already spent considerable amount of time dedicated to it before starting this blog, my immediate goal is to document my current standings here so that as time progresses, it doesn’t appear to be jarred or difficult to follow.

What is the purpose of KoiFish?  KoiFish aims to be a general purpose programming language that is object-orientated and has an inferred type system with a emphasis on syntax readability based not on verbosity but rather the programmer’s familiarity with symbols and language associated with meta-programming techniques (such as UML and pseudo-code generation) and lingo.

Phew! What a mouth full.  All marketing aside, it’s an experiment into the depths of one of my favorite subjects of computer science!

All languages start with some grammar, and presently, here is the most updated version of KoiFish’s grammar:

<definition> := koi <access_modifier> <identifier> <function>+ | def <access_modifier> <identifier>
<function> := fn <access_modifier> <indentifier> {:<identifier> }+ begin
<statement>
{return <expression>}
end;
<access_modifier> := + | – | #
<statement> := def <identifier> = <expression>
| <identifier> = <expression>
| for <identifier> = <expression> to <expression> begin <statement> end
| if <boolean_expression> then <statement> end
| if <boolean_expression> then <statement> else <statement> end
| readi <identifier>
| readf <identifier>
| puts <expression>
| return <expression>
| call <identifier>{@<identifier>} {:<identifier> }+;
| <statement> ; <statement>
<expression> := <string>
| <float>
| <int>
| <bool>
| <array>
| call <identifier>{@<identifier>} {:<identifier> }+;
| <allocation_expression>
| <binary_expression>
| <concat_expression>
| <identifier>
| nil
<concat_expression> := <expression> . <expression>
<allocation_expression> := alloc <identifier>;
<boolean_expression> := <expression> <equality_operator> <expression>
<equality_operator> := > | >= | < | <= | == | !=
<binary_expression> := <expression> <binary_operator> <expression>
<binary_operator> := + | – | * | /
<identifier> := <char> <identifier_rest>+
<identifier_rest> := <char> | <digit>
<array> := [] | [{<float>|<int>|<bool>|<string>,}+]
<bool> := TRUE | FALSE
<float> := <int>.<int> | .<int>f
<int> := <digit>+
<digit> := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<string> := ” <string_element>+ ”
<string_element> := <any char other than “>
Posted in KoiFish | Tagged , , | Leave a comment