Context programming language

Andrei V. Khokhlov

[Executable files and sources (Win32)] [More info...] [About compiler design. Part 2]
[Executable files and sources (DOS)] [Samples] [About compiler design. Part 1]
[Archive] [Forum] [Guest book]

This is result of machine translation from russian with minimum manual correction...


Context 2.0 for Windows. Simplified DOS version of compiler also available.

If You come here not accidentally, please answer to my questions about programming languages.


If You come here not accidentally, please answer to my question


One more sample - a compiler context.bin. This is base version of compiler for DOS creates COM-file without assembler and support two sets of keywords. Near a year back I thought on changing part of the words (if/then/elseif/else instead of select/case, do ... end instead of while TRUE do ... end, const instead of define, := instead of = for the assignment, empty operator null) and reject nearly all of them - in future Windows version will only do ... end, null and, can be, second assignment operator. Built-in assembler will recognize only db command. Structure context.bin is similar to structure of base version, but it several more complex - there are no sense to made large changes in the old text, future version for Windows will be more simple...

More about Context compiler design and simplified version of compiler.


Compiler, assembler and samples update. Initial version of compiler are replaced to small version. To compile all samples OO-version of compiler (context.sim) must be used. Multi-pass assembler now unavailable - use one-pass assembler. Some minor changes in compiler structure are made. Some bugs fixed:

Compilers seemed me extremely difficult and available books not in the least did not change this presentation. But I wanted to understand, how they work. In 1994 I tried to write a compiler. And though I was able to get something working for a month and between the deal, this was a long way. The first version (0.5) was written on Pascal language (Borland). It was not too well thought-out and one year ago I completely rewrite it (on C language). In the result I got simple and expressive (as I think) language and corresponding compiler. Later I translate my compiler from C language to it's own input language and write a simple assembler. After 1997 I add object-oriented extension and correct several mistakes. Here You may view and download language description, source codes and executable files. In the description I tried to show that decision of problem is not extremely difficult. Word-combination "formal grammatics" is meet only once upon a time and that sense, that knowledge of these words is not require (I at all not against the science, but suppose, that begin is necessary with simple).

Short description

General conventions:

First point is most important. If reference @P defined (for example, reference to char), P means a char, @P - means an address of char, and @@P - means an address of reference.

Internal datatypes:

Boolean datatype also present, but variables of this type are not allowed. There are limited support of real datatype in version 1.2 (context.sim, FPU required - NOT TESTED!).

Only one way to define you own datatype is define the structure:

struct Struct_name Field1; Field2; ... section // variant part FieldN+1; ... end

Variant part can be absent.

Variable description:

Type_name [@[@[...]]]Variable_name; // @ - reference (address) Type_name Variable_name [10][10]; // array

Operator @ show that variable is reference to data. For example:

char @PC; // reference to char (and to array of char's) char @@PP; // reference to (reference to char)

Number of @ is reference power.

Function description:

Type_name [@[@[...]]]Function_name(Type_name [@[@[...]]]Parameter_name[, ...]) //Operators end

Declarations of fynctions are allowed. This allows to use indirect recursion.

Constant Description:

define @S "String" // reference to string define C1 'C' // char define C2 #10 // char define M 16 // number define N $10 // number (hex)

Program structure:

Constants descriptions Structures descriptions Global variables description Functions descriptions Main function (begin)

Operators (in order of priority, from lowest to highest):

| or (boolean and bitwise) ^ exclude or (boolean and bitwise) & and (boolean and bitwise) < less <= less or equal = equal != not equal >= greater or equal > greater + addition - subtraction * multiplication / division % calculation of the remainder from division ! not (boolean and bitwise) - negation @ calculation of object reference (address) [] indexation and dereference

Assignment to variable or function parameter are possible in following cases:

There are some control statements:

Condition operator:

if Boolean_expression then //Operators else //Operators end

Select from many choices:

select case Boolean_expression: //Operators case Boolean_expression: //Operators ... default: //Operators end


while Boolean_expression do //Operators end repeat //Operators until Boolean_expression;

Jump operators:

loop // Jump to beginning of cycle exit // Exit from cycle

Infinite cycle:

while TRUE do //Operators end

Returns from function:

return Expression; // Returm value

return // Return from void function

Additional opeartors:

inc Variable_name; // Increment by 1 dec Variable_name; // Decrement by 1

Inline assembler:

asm OpCode [Operands]

Are to be use in any place of functions, In given version using the variable names not allowed. Mistakes are not reveal.

Object-oriented extension (context 1.2):

For the definition of class old keyword "struct" is used, only single inheritance allowed (i.e. derived class can be build on the base of only one base class), variant part (section) not allowed in classes:

struct Class_name [(Base_class_name)] //Fields Definitions ... //Methods Declarations Type_name [@[...]]Method_name([parameters]) [virtual]; end // Method implementation Type_name [@[...]]Class_name.Method_name([parameters]) [virtual] ... end

In method implementation available two predefined references:

Class_name @self; Base_class_name @base; // Only for derived classes

Constructor of class instance only implicit, its single function - an initialization a reference to the virtual methods table (VMT) and calling the constructors for class members. Constructor is called automatically for each declared class instance.

Assignment of reference to derived class to the reference to base class is possible, for example

struct CBase ... end struct CDerived (CBase) ... end CDerived D; CBase @P=@D;
First name of my compiler was mix8086. How has appear a name Context I presently can not recall. Word Itself a context (from lat contextus - a join, relationship) means comparatively complete fragment of written or spoken speech (text), which general sense allows to elaborate a value separate fall into it words, expressions etc. (Soviet encyclopedic dictionary, 1980). As I think, this name at all not bad...

Сайт создан в системе uCoz