1 /*
   2  * This is a nonsense program to test whether structures are compiled
   3  * correctly.
   4  */
   5 
   6 Structure Point Consists Of {
   7   Integer16 dimensions : = 3;
   8   Decimal32 x, y, z;
   9 }
  10 EndStructure;
  11 Integer32 a : = 0;
  12 InstantiateStructure Point points[12];
  13 Integer32 b : = 1;
  14 Structure String Consists Of {
  15   Integer32 length;
  16   Character string[16];
  17 }
  18 EndStructure;
  19 InstantiateStructure String helloWorld;
  20 Integer32 c : = 2;
  21 
  22 Function foo() Which Returns Integer16 Does {
  23   // Should return 3.
  24   Return points[c].dimensions;
  25 }
  26 EndFunction;
  27 
  28 Function strlen(CharacterPointer string) Which Returns Integer32 Does {
  29   Return(ValueAt(string) = 0) ? (0) : (1 + strlen(string + 1));
  30 }
  31 EndFunction;
  32 
  33 Function strcpy(CharacterPointer dest,
  34                 CharacterPointer src) Which Returns Nothing Does {
  35   While ValueAt(src) Loop {
  36     ValueAt(dest) : = ValueAt(src);
  37     dest += 1;
  38     src += 1;
  39   }
  40   EndWhile;
  41   ValueAt(dest) : = 0;
  42 }
  43 EndFunction;
  44 
  45 Function bar() Which Returns Integer32 Does {
  46   // Should return 12.
  47   CharacterPointer hw : = "Hello world!";
  48   strcpy(AddressOf(helloWorld.string[0]), hw);
  49   helloWorld.length : = strlen(hw);
  50   Return helloWorld.length;
  51 }
  52 EndFunction;
  53 
  54 Function spam() Which Returns Integer32 Does {
  55   // Should return 3.
  56   InstantiateStructure Point localPoints[3];
  57   Return localPoints[b].dimensions;
  58 }
  59 EndFunction;
  60 
  61 Function eggs() Which Returns Integer32 Does {
  62   // Should return 3.
  63   InstantiateStructure Point localPoint;
  64   Return localPoint.dimensions;
  65 }
  66 EndFunction;
  67 
  68 Structure innerStructure Consists Of { Integer32 innerNumber : = 1; }
  69 EndStructure;
  70 
  71 Structure outerStructure Consists Of {
  72   Integer32 outerNumber : = 2;
  73   innerStructure structureWithInnerNumber;
  74   innerStructure arrayOfInnerStructures[2];
  75   innerStructurePointer structurePointer;
  76 }
  77 EndStructure;
  78 
  79 InstantiateStructure outerStructure globalNestedStructure;
  80 
  81 Function onion() Which Returns Integer32 Does {
  82   // Should return 1.
  83   InstantiateStructure outerStructure nestedStructure,
  84       copyOfTheNestedStructure : = nestedStructure;
  85   Return(nestedStructure.structureWithInnerNumber.innerNumber =
  86              globalNestedStructure.structureWithInnerNumber
  87                  .innerNumber and nestedStructure.outerNumber =
  88                  2 and globalNestedStructure.outerNumber =
  89                      2 and copyOfTheNestedStructure.structureWithInnerNumber
  90                          .innerNumber = 1 and copyOfTheNestedStructure
  91                                                   .arrayOfInnerStructures[1]
  92                                                   .innerNumber = 1);
  93 }
  94 EndFunction;
  95 
  96 Function copyString(StringPointer destination,
  97                     StringPointer source) Which Returns Nothing Does {
  98   ValueAt(destination).length : = ValueAt(source).length;
  99   Integer32 i : = 0;
 100   While i < ValueAt(source).length Loop {
 101     ValueAt(destination).string[i] : = ValueAt(source).string[i];
 102     i += 1;
 103   }
 104   EndWhile;
 105 }
 106 EndFunction;
 107 
 108 Function pomegranate() Which Returns Integer32 Does {
 109   // Should return 12.
 110   CharacterPointer hw : = "Hello world!";
 111   strcpy(AddressOf(helloWorld.string[0]), hw);
 112   helloWorld.length : = strlen(hw);
 113   InstantiateStructure String localString;
 114   copyString(AddressOf(localString), AddressOf(helloWorld));
 115   InstantiateStructure String copyOfLocalString : = localString;
 116   Return copyOfLocalString.length;
 117 }
 118 EndFunction;
 119 
 120 Structure ListNode Consists Of {
 121   ListNodePointer next;
 122   Integer32 value;
 123 }
 124 EndStructure;
 125 
 126 Function spaghetti() Which Returns Integer32 Does {
 127   // https://stackoverflow.com/questions/63951270/using-default-copy-constructor-corrupts-a-tree-in-c
 128   // By common sense, this should return 2. By C++ semantics, this should
 129   // return 3.
 130   InstantiateStructure ListNode list[3];
 131   list[0].value : = 1;
 132   list[1].value : = 2;
 133   list[2].value : = 3;
 134   list[0].next : = AddressOf(list[1]);
 135   list[1].next : = AddressOf(list[2]);
 136   list[0] : = ValueAt(list[0].next);
 137   CharacterPointer pointer : = AddressOf(list[0]);
 138   Return ValueAt(ListNodePointer(pointer)).value;
 139 }
 140 EndFunction;
 141 
 142 Function elephant() Which Returns Integer32 Does {
 143   // Should return 1.
 144   Return(SizeOf(outerStructure) = SizeOf(Integer32) +
 145                                   3 * SizeOf(innerStructure) +
 146                                   SizeOf(innerStructurePointer));
 147 }
 148 EndFunction;
 149 
 150 ListNodePointer globalStructurePointer, globalStructurePointerArray[2];
 151 
 152 Function arrow() Which Returns Integer32 Does {
 153   // Should return 2.
 154   InstantiateStructure ListNode node[2];
 155   node[0].value : = 1;
 156   node[0].next : = AddressOf(node[1]);
 157   node[1].value : = 2;
 158   ListNodePointer ptr : = AddressOf(node[0]), localPointerArray[2];
 159   ptr += 1;
 160   Return(ptr->value);
 161 }
 162 EndFunction
 163