# Reflex and Routine Definition¶

There are two types of top level definitions in the reflex language, reflex definitions and routine definitions. A reflex file consists of 1 or more reflex definitions, and zero or more routine definitions. The major syntactic difference between a routine definition and a reflex definition is that a reflex definition must be preceded by the keyword reflex, the fact that reflex definitions often include an entity index specification [ index ] as part of the reflex name declaration and reflexes do not return values.

Semantically the two types are very different. Routines are very similar to functions in other C-like languages. They exist to perform a function and are called within other code blocks. Reflexes on the other hand are tied to events which occur in the BrainStem module, such as an analog reading, timer expiration, or in the case of map enable and disable, the enabling of the compiled reflex file itself. In the reflex nomenclature we say that a reflex definition is tied to a reflex ‘origin’, multiple reflexes can be attached to a reflex origin through the enabling of multiple reflex files, and each reflex definition that is ‘mapped’ to an origin will execute when the condition triggering the origin occurs. In this way reflexes form a reactive system of behaviors on each BrainStem module.

Note

Reflexes cannot return values themselves. These are behaviors performed as the result of some system event, and as such have no place, like main, to return a value to. If a reflex needs to retain state at the end of its execution, it should place such state information into the Scratchpad.

## Reflex Definition¶

reflex system.timer[0].expiration(void)
{
...
}


## Routine Definition Example¶

short linearizeIRData(short data) {
...
return linearizedData;
}


Routine definitions are syntactically much like function definitions in C. The declare a return type, and take zero or more parameters.

### Reflex Definition EBNF¶

reflex_definition  ::=  'reflex' , entity_specifier , parameter_list ,
compound_statement ;
parameter_list     ::=  '(' [ parameter , {',' parameter } ] ')' ;
parameter          ::=  type_specifier , ID ;
compound_statement ::=  '{' statement_list '}' ;
statment_list      ::=  { variable_declaration } , { statement } ;
statment           ::=  routine_call | assignment_statement | control_statement ;


### Routine Definition EBNF¶

routine_definition ::=  type_specifier , ID , parameter_list ,
compound_statement ;


### Entity Specifier EBNF¶

The entity specifier fully describes a brainstem UEI. See the UEI appendix for more information. UEI’s consist of an entity class, the entity index, and an option/command.

entity_specifier ::=  entity_class , [ "[" , index , "]" ] , "." , class_option ;
index            ::=  '0' | digit-not-zero , digit ;
entity_class     ::=  "Module specific list of possible entities"
class_option     ::=  "Entity specific list of possible options for the entity"