Skip to content

Structures in my language

My language can group variables together.

It uses the structure mechanism found in other compiled languages.

I use a basic prototype/allocation system.

First, we create a structure prototype. We can then declare it as a memory reference or as organized data allocated on the stack.

Demonstration

Consider the following code:

// gaspardl.exe console g/Consoleinc.g g/Struct.g
// requires gaspardl 1.2!!!
struct pack teststruct = begin

element uint32_t texte2;
element uint32_t texte;

element double double1;

end

global struct teststruct test_pile;
global struct teststruct test_pile_arr[255];
global struct teststruct ptr test_struct_ptr;

global string log1 = "log : %d \n";
global string log2 = "log : %f \n";

procedure main return uint32_t = begin

structptr test_struct_ptr = 0xCAFEBABE; // dummy address; you must allocate it with MALLOC()

test_pile_arr[255].double1 = 14.0444;

test_pile.texte = cast uint32_t 26666;
call printf log2 test_pile_arr[255].double1;

call printf log1 sizeof test_pile_arr;

return cast uint32_t 1;

end

Comments

Special case

struct pack teststruct = begin

element uint32_t texte2;
element uint32_t texte;

element double double1;

end

A structure can be declared without pack. pack simply prevents certain LLVM optimizations that aim to round up the structure's size.

Declaration

Consider the following code:

global struct teststruct test_pile; // no difficulty

global struct teststruct test_pile_arr[255]; // OK, 255 * teststruct allocated on the stack
global struct teststruct ptr test_struct_ptr; // pointer

For a pointer, the structptr operator is used to associate it with a memory address.

I decided to introduce the structptr keyword to avoid ambiguity with =.

If structptr is present alongside the name, we know that it refers to a pointer to a structure.

Limitations

  • For now, a structure cannot contain a reference to another structure (stack or pointer). This is a serious problem, and I am working hard on it.
  • Bit fields exist. For example:
element bits 4 limit_high;
element bits 4 flags;

It works, but it is buggy.