Making a Programming Language From Scratch/Structures

Structural declarations

edit

Structures are groups of different types of data in continuous locations. It is necessarily the grouping of different types of data together.

Format of structural declarations

edit
Defstruct [Name of structure]{
.
.
.
}

Format of assembly:

[Name of structure] struct starts
.
.
.
[Name of structure] ends

Algorithm to process

edit
1.If keyword defstruct then
2.While char not { get char and store in array name
3.Write to output file format as given above.

[note that choice of keyword is totally up to you.]

Structure variable declarations

edit

All structures are useless unless they have some representatives in the physical memory. This is called a structure variable.
A structure variable is defined along with other variables with the important difference that structure variables are not defined during initializing (at least in assembly). Thus we will deal with only uninitialized structure variables.

Format as of assembly:

[structure variable name] [parent structure name] <?>

The <?> is the structure dup operator which sets value 0 or 0.0 to all sub-components

Algorithm

edit
1.If keyword struct then
2.Scan parent structure name
[Duplicate algorithm for getting simple variables]
3.Write to file in format as given above.

Sample Conversion

edit

In HLL

defstruct boy{
 int age,grade;
 float marks;
 char name[20];
}

In Assembly

boy struct starts
age dword ?
grade dword ?
marks real8 ?
name byte 19 dup (?),0
boy ends