Skip to content

Language Documentation

konsumlamm edited this page Aug 9, 2019 · 3 revisions

C-- Documentation

C-- is a language with syntax similar to C but with only a fractions of its features. It is compiled to brainfuck.

Types

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

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:

  1. () function call
  2. + - unary plus/minus
  3. * / % multiplication, division, remainder
  4. + - addition, subtraction
  5. < > <= >= relational
  6. == != relational
  7. not logical not
  8. and logical and
  9. or logical or

Note: Function calls in expressions must return int.

Comments

Text in a line is declared as a comment when preceded by a # character.

Declarations

Variables can only be declared as int and optionally initialized with = (same syntax as in C). Functions can't be declared.


Statements

  • 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


Functions

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.

Memory Organisation

  • 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

Clone this wiki locally