-
Notifications
You must be signed in to change notification settings - Fork 0
Language Documentation
C-- is a language with syntax similar to C but with only a fractions of its features. It is compiled to brainfuck.
There are two types in C--, void
and int
.
void
is used as return type of a function, to indicate that it doesn't return anything. A variable can't have void
as type.
int
is an integer type. It is represented by the brainfuck cells in the underlying brainfuck implementation, so its size may vary.
int
literals can be normal (positive) decimal integers ([0-9]+
), true
, which gets interpreted as 1, false
, which gets interpreted as 0 or char literals ('(.)'
) which get interpreted as their ASCII value.
Expressions in C-- are similar to C expressions, but with less operators. Note that assignment is NO operator in C-- and is instead interpreted as statement. Standalone expressions are not allowed.
Operator Precedence:
- () function call
- + - unary plus/minus
- * / % multiplication, division, remainder
- + - addition, subtraction
- < > <= >= relational
- == != relational
- not logical not
- and logical and
- or logical or
Note: Function calls in expressions must return int
.
Text in a line is declared as a comment when preceded by a # character.
Variables can only be declared as int
and optionally initialized with =
(same syntax as in C). Functions can't be declared.
-
blocks:
{ {<block-item>}* }
like in C, block items can be statements or declarations, but the declarations don't need to be before the statements
-
if-else:
if ( <exp> ) <statement1> {else <statement2>}?
if <exp> is true (nonzero), <statement1> is executed, else (if an else is present) <statement2> is executed
-
while:
while ( <exp> ) <statement>
like in C, <statement> is repeated until <exp> is false (zero)
-
repeat:
repeat ( <exp> ) <statement>
<statement> is repeated <exp> amount of times
-
assignments:
<id> <assign-op> <exp> ;
just like in C, but assignments are no expressions, <assign-op> can be
=
,+=
,-=
,*=
,/=
or%=
-
function calls:
<id> ( <args> ) ;
return type must be
void
Because brainfuck has no functions, C-- only has inline functions. That means functions can't be recursive. The syntax for function definition is the same as in C, but they don't need to be declared.
-
cells are used as stack for variables and temporary data
-
stack pointer (SP) points to the first free cell
-
cells right to the SP can always be used (for new variables and temporary data for expression evaluation)
-
variable declaration reserves a new cell (SP++) and the name and the address are saved in a variable map
-
upon usage, variables get looked up in the variable map from right to left
-
when leaving a scope, the SP gets reset to its value before entering the scope and local variables get removed from the variable map