About

Examples

Source Code

Architecture

Help Wanted

Goals

Doxygen

Code Coverage

Support

Gyoji Programming Language

Examples

Learning languages is best done by example. The following code fragments illustrate the constructs supported by the language and describe some of the differences between Gyoji and other programming languages like C, Java, C++, and Rust since they are the most comparable.

Hello World

Let's get this out of the way. How do you make hello world?

Gyoji does not have a standard library, so all function calls like "strlen" and "write" rely on the C standard library or other libraries. This is not a limitation, instead, this makes the language usable in a number of environments which would otherwise be unsuitable because it can produce valid code even with no dependencies.

u32 strlen(u8*);
u32 write(u32 fd, u8* data, u32 size);

u32 main(u32 argc, u8** argv)
{
    u8* s = "Hello World\n";
    write(0, s, strlen(s));
    return 0;
}

The first think you may notice about this is the type declarations. Instead of the C, Java, C++ declarations of 'int' and 'unsigned int', Gyoji is explicit about the numeric boundaries of the nubmers you can declare. This is done so that there is no question about the size of declarations or what the allowed values are.

Lines 1,2 declare external C functions that perform a string length and write to stdout respectively.

Line 4 starts the declaration of a standard C main function. The signature is equivalent to the "int main(int argc, char **argv)" that is used in C and C++. Unlike Java, however, it requires a return-value so that the program can easily signal to the OS the exit status.

The Line 6 declares a string literal which will be placed in the data segment of the program and "s" will be a pointer to a u8 (char) at that location.

Line 7 makes a function call to the Posix "write" function which takes a file descriptor (0) and writes to it the value pointed to by 's' for the number of bytes given by the "strlen" function.

Line 8 returns from the function and ends execution.

Conditionals

Any programming language needs the ability to make decisions. This is done in the familiar style of C and C++, but with a twist.
  u32 a = 2;
  if (a == 2u32) {
      // do something if the expression is true.
  }

The twist here is that the expression inside the "if" statement MUST evaluate to a "bool" value. That is, the practice in C and C++ of passing integers and pointers to 'if' directly is forbidden.

  u32 a = 2;
  if (a) { // This is an error because a is a u32 and not a bool.
      // do something if the expression is true.
  }

This choice was made to ensure that it is always clear when the statement will execute and eliminates sometimes unknown or unforeseen "casts" to different types which could affect control-flow. Consider a C++ block which has an implicit constructor that allows conversion of an class to a bool. Calling this cast operation may not be obvious and may not be what a reader of the code would expect.

  // Foo contains a cast to int.
  class Foo {
       operator int() const { return 1; }
  };
  ...
  Foo f;
  if (f) {  // It's not obvious that this calls a function
      // Do something
  }

Rather than doing something unexpected, it is worthwhile to avoid such accidents and keep the langugage minimal in the process, so this type of thing just isn't allowed.

In addition, let's consider another fragment of C that is invalid in Gyoji. if statements (and most conditionals) REQUIRE curly-braces in order to work. This removes the so-called "dangling else" ambiguity of the langugage and prevents mistakes that are pretty common in C and C++:

This is not allowed. If you don't read carefully, you may miss the fact that the "return" here is actually unconditional. It's easy to miss that fact because it's indended (unfortunately). While we don't go so far as to enforce indentation like Python, we do at least prevent this type of accident by requiring "{}" around the conditional statements.
  if (condition)
      print("Hello");
      return 5;

While loops

Again, like C and C++, while loops are allowed and function pretty much the same except that like 'if' statements, the condition in the 'while' must evaluate to a 'bool' value. For all of the same reasons as 'if', no implicit casting is allowed.

This example also shows off some numeric operations. The '+,-,*,/,%' operators work pretty much exactly as they do in C. Also, there are operators like '>>', '<<', '|', '^', '&', '&&' and pretty much the whole complement of binary operations complete with the usual precedence rules.

u32 print_value(u32 number);

u32 main(u32 argc, u8**argv)
{
    u32 a = 4u32;

    while (a < 10) {
        if ((a % 2) == 0) {
            print_value(a);
        }
        else {
            print_value(17u32);
        }
        if (a == 6) {
            print_value(0xffff);
            break;
        }
    }
    return 0;
}

For loops

One of the basic features of any programming language is the ability to loop over some range. Gyoji uses essentially the same syntax as C, Java, and C++ for this task. Iteration over containers and collections is not supported although this may be supported in future.

u32 print_value(u32 number);

u32 main(u32 argc, u8**argv)
{
        for (u32 a = 0; a < 20; a += 2) {
            print_value(a);
        }
}