The following strings are C-flat keywords and may not be used as identifiers: boolean char else false if int return string true void while
Whitespace is not significant in C-flat. Both C-style and C++-style comments are valid in C-flat:
/* A C-style comment */ a=5; // A C++ style comment
C-flat has four types: integers, characters, strings, and booleans. Each variable must be declared individually. Sample declarations are:
int x = 123; char c = 'q'; string s = "hello world\n"; boolean b = false;
As you can see, integer, character, and string declarations are just like C. The boolean type is new; it may only take the values true or false, much as in C++ or Java. C-flat does not have arrays, pointers, or structs.
Note that constant values are similar to C: an integer is a string of digits, a single character is single quoted and strings are double quoted. Two backslash codes are defined: \n indicates a linefeed (ASCII 10) and \\ indicates a backslash.
| ( ) |
(highest) grouping |
| - | unary ops |
| * / % | multiplication |
| + - | addition |
| < <= >= == != | comparison |
| = |
assignment (lowest) |
int x=65; char y='A'; if(x>y) ... // error: x and y are of different types! int f=0; if(f) ... // error: f is not a boolean! void writechar( char c ); int a=65; writechar(a); // error: a is not a char!However, the following constructs are legal:
boolean b; int x=3, y=5; b = x<y; // ok: the expression x<y is boolean int f=0; if(f==0) ... // ok: f==0 is a boolean expression char c='a'; if(c=='a') ... // ok: c and 'a' are both chars
Within functions, basic statements may be arithmetic expressions, return statements, if and if-else statements, while loops, or code within inner { } groups. C-flat does not have switch statements, for-loops, or do-while loops.
Here is an example that pulls it all together:
int x=35;
void write_string( string s );
void fib( int x )
{
if(x<2) {
return 1;
} else {
return fib(x-1)+fib(x-2);
}
}
int main()
{
int i=0;
while(i<10) {
write_string("hello fib!");
i=i+1;
}
return fib(x);
}
cflat pgm.cflat pgm.s as pgm.s -o pgm.o gcc pgm.o -lc -o pgmC-flat programs may link against routines written in other languages. For example, suppose that you would like to have a function that writes an integer to the console. You could implement in in traditional C like so:
/* Traditional C program in helper.c */
#include <stdio.h>
void write_integer( int x )
{
printf("%d\n",x);
}
Now, a C-flat program may reference this
external function by simply giving the appropriate prototype:
/* C-flat program in pgm.cflat */
void write_integer( int x );
int main()
{
write_integer(8675309);
}
To build all the pieces together, you must compile
and assemble the c-flat as before:
cflat pgm.cflat pgm.s as pgm.s -o pgm.oThen build the C helper module and link both modules together into the final program:
gcc helper.c -o helper.o -c gcc pgm.o helper.o -o pgm