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:
Each appearance of identifier in the text assumes its value. For the address
calculation operator @ must be used.
Number of operations with pointers (references) are limited, possible only
dereference (tacit and by means of square brackets), comparison with NULL
(equal/not equal) and assignment.
Local variables are to be described in any place of functions, each valiable
can be referenced from the place of description to the end of block, in which
they described.
(before the end of cycle etc.).
There are no internal functions.
Using of delimiter "semicolon" is reduced. It use only for termination of
struct and functions descriptions and termination of assignments.
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:
void - empty type
char - symbol
byte - byte (unsigned)
word - word (unsigned integer)
int - signed integer
real - floating point number
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
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:
If references powers of both operands equals to zero and source datatype can be
converted to dest datatype.
If one of operands has datatype void and its reference power is equal to one,
reference power of other operand equal or greater to one.
If one of operands has void datatype and reference powers of both operands are
greater then zero and equals.
If datatypes and reference powers of both operands are equal.
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
Cycles:
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:
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
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;
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. Object-oriented extension are implemented
in version 1.2
There are no way to create class instance dynamically in this version. One
of possible ways is using of type cast:
CClass @P=@CClass(@GetMem(sizeof CClass));
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...