Skip to content

Commit

Permalink
New type names
Browse files Browse the repository at this point in the history
Especially "Int" is now "Integer", "UInt" is "Cardinal" and "Bool" is "Boolean".
  • Loading branch information
BenediktMagnus committed Mar 7, 2024
1 parent 789b0c8 commit 091f727
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/headers/backseat/io.ph
Original file line number Diff line number Diff line change
@@ -1 +1 @@
header function write (character: Int);
header function write (character: Integer);
4 changes: 2 additions & 2 deletions src/headers/linuxAmd64/conversion.ph
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Standard.Conversion;

header function intToString (integer: Int): String;
header function stringToInt (string: String): Int;
header function intToString (integer: Integer): String;
header function stringToInt (string: String): Integer;
2 changes: 1 addition & 1 deletion src/headers/linuxAmd64/random.ph
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Standard.Random;

header function getRandom (range: Int): Int;
header function getRandom (range: Integer): Integer;
header function randomise ();
24 changes: 12 additions & 12 deletions src/platforms/amd64/linux/conversion.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include "../../common/types.h"
#include "string.h"

String intToString (Int integer) asm ("\"Standard.Conversion~intToString\"");
String intToString (Int integer)
String intToString (Integer integer) asm ("\"Standard.Conversion~intToString\"");
String intToString (Integer integer)
{
// The following is a hack.
// The string length of a number in decimal (including the minus for negative values) is never greater then three times plus one
// of the number of bytes in binary notation. We need this for the memory allocation of the buffer.
const UInt maxStringLength = sizeof(Int) * 3 + 1;
const Cardinal maxStringLength = sizeof(Integer) * 3 + 1;

UInt8 characters[maxStringLength];
Cardinal8 characters[maxStringLength];

UInt stringLength = 0;
Int rest = integer;
Cardinal stringLength = 0;
Integer rest = integer;

if (rest < 0)
{
Expand All @@ -21,7 +21,7 @@ String intToString (Int integer)

do
{
UInt8 character = rest % 10 + '0';
Cardinal8 character = rest % 10 + '0';

characters[maxStringLength - stringLength - 1] = character;

Expand All @@ -43,18 +43,18 @@ String intToString (Int integer)
return result;
}

Int stringToInt (const String string) asm ("\"Standard.Conversion~stringToInt\"");
Int stringToInt (const String string)
Integer stringToInt (const String string) asm ("\"Standard.Conversion~stringToInt\"");
Integer stringToInt (const String string)
{
if (string->size == 0)
{
return 0;
}

Int result = 0;
UInt multiplier = 1;
Integer result = 0;
Cardinal multiplier = 1;

for (Int i = string->size - 1; i >= 0; i--)
for (Integer i = string->size - 1; i >= 0; i--)
{
if ((string->data[i] >= '0') && (string->data[i] <= '9'))
{
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/amd64/linux/conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* Converts an integer into a string.
* @return A new string instance, representing the given integer value.
*/
String intToString (Int integer) asm ("\"Standard.Conversion~intToString\"");
String intToString (Integer integer) asm ("\"Standard.Conversion~intToString\"");

/**
* Convert a string into an integer.
* @return The integer value the given string represents.
*/
Int stringToInt (const String string) asm ("\"Standard.Conversion~stringToInt\"");
Integer stringToInt (const String string) asm ("\"Standard.Conversion~stringToInt\"");
28 changes: 14 additions & 14 deletions src/platforms/amd64/linux/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
void write (const String text) asm ("\"Standard.Io~write\"");
void write (const String text)
{
Int fileDescriptor = 1; // File descriptor ID for stdout
UInt syscode = SYSCODE_WRITE;
Integer fileDescriptor = 1; // File descriptor ID for stdout
Cardinal syscode = SYSCODE_WRITE;

Int result;
Integer result;

asm volatile ("syscall" : "=a" (result)
: "d" (text->size), "S" (text->data), "D" (fileDescriptor), "a" (syscode)
Expand All @@ -29,7 +29,7 @@ void write (const String text)
void writeLine (const String text) asm ("\"Standard.Io~writeLine\"");
void writeLine (const String text)
{
UInt8* lineBreakLiteralChar = { "\n" };
Cardinal8* lineBreakLiteralChar = { "\n" };

String lineBreak = createString(lineBreakLiteralChar, 1);

Expand All @@ -48,15 +48,15 @@ void writeLine (const String text)
String readLine () asm ("\"Standard.Io~readLine\"");
String readLine ()
{
Int fileDescriptor = 0; // File descriptor ID for stdin
UInt syscode = SYSCODE_READ;
Integer fileDescriptor = 0; // File descriptor ID for stdin
Cardinal syscode = SYSCODE_READ;

const UInt bufferSize = 4096;
UInt8 buffer[bufferSize];
const Cardinal bufferSize = 4096;
Cardinal8 buffer[bufferSize];

Int bytesRead = 0;
UInt resultSize = 0;
UInt8* result = null;
Integer bytesRead = 0;
Cardinal resultSize = 0;
Cardinal8* result = null;

while (true)
{
Expand All @@ -69,8 +69,8 @@ String readLine ()
if (bytesRead > 0)
{
// Allocate memory for the new total result size:
UInt newSize = resultSize + bytesRead;
UInt8* newResult = allocate(newSize);
Cardinal newSize = resultSize + bytesRead;
Cardinal8* newResult = allocate(newSize);

// Copy the old result and the buffer to the new result:
copy(newResult, result, resultSize);
Expand All @@ -97,7 +97,7 @@ String readLine ()
}
}

UInt stringSize = resultSize;
Cardinal stringSize = resultSize;

// Remove the line break at the end if there is one:
if (result[stringSize - 1] == '\n') // TODO: Replace the hard coded line break with a system dependent constant.
Expand Down
20 changes: 10 additions & 10 deletions src/platforms/amd64/linux/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
* @param size The size of the memory block to allocate.
* @return The pointer to the allocated memory.
*/
void* allocate (UInt size) asm ("\"Standard.Memory~allocate\"");
void* allocate (UInt size)
void* allocate (Cardinal size) asm ("\"Standard.Memory~allocate\"");
void* allocate (Cardinal size)
{
void* address = 0;
UInt prot = 0x3; // 0x3 = PROT_READ|PROT_WRITE
register UInt flags asm("r10") = 0x22; // 0x22 = MAP_PRIVATE|MAP_ANONYMOUS
register Int fileDescriptor asm("r8") = -1;
register UInt offset asm("r9") = 0;
UInt syscode = SYSCODE_MEMORY_MAP;
Cardinal prot = 0x3; // 0x3 = PROT_READ|PROT_WRITE
register Cardinal flags asm("r10") = 0x22; // 0x22 = MAP_PRIVATE|MAP_ANONYMOUS
register Integer fileDescriptor asm("r8") = -1;
register Cardinal offset asm("r9") = 0;
Cardinal syscode = SYSCODE_MEMORY_MAP;
void* result;

asm volatile ("syscall" : "=a" (result)
Expand All @@ -29,11 +29,11 @@ void* allocate (UInt size)
* @param address The pointer to the memory to free.
* @param size The size of the memory block.
*/
void free (const void* address, UInt size)
void free (const void* address, Cardinal size)
{
UInt syscode = SYSCODE_MEMORY_UNMAP;
Cardinal syscode = SYSCODE_MEMORY_UNMAP;

Int result;
Integer result;

asm volatile ("syscall" : "=a" (result)
: "D" (address), "S" (size), "a" (syscode)
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/amd64/linux/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
#include "../../common/memory.h"
#include "../../common/types.h"

void* allocate (UInt size) asm ("\"Standard.Memory~allocate\"");
void free (const void* address, UInt size);
void* allocate (Cardinal size) asm ("\"Standard.Memory~allocate\"");
void free (const void* address, Cardinal size) asm ("\"Standard.Memory~free\"");
18 changes: 9 additions & 9 deletions src/platforms/amd64/linux/random.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "syscodes.h"
#include "../../common/types.h"

static Int seed = 0;
static Integer seed = 0;

void randomise () asm ("\"Standard.Random~randomise\"");
void randomise ()
{
UInt syscode = SYSCODE_GET_RANDOM;
UInt byteCount = sizeof(Int);
UInt flags = 0;
Cardinal syscode = SYSCODE_GET_RANDOM;
Cardinal byteCount = sizeof(Integer);
Cardinal flags = 0;

Int newSeed;
Int result;
Integer newSeed;
Integer result;

asm volatile ("syscall" : "=a" (result)
: "D" (&newSeed), "S" (byteCount), "d" (flags), "a" (syscode)
Expand All @@ -29,12 +29,12 @@ void randomise ()
// TODO: Check return value.
}

Int getRandom (Int range) asm ("\"Standard.Random~getRandom\"");
Int getRandom (Int range)
Integer getRandom (Integer range) asm ("\"Standard.Random~getRandom\"");
Integer getRandom (Integer range)
{
seed = (seed * 1103515245 + 12345);

Int result = seed % range;
Integer result = seed % range;

return result;
}
2 changes: 1 addition & 1 deletion src/platforms/amd64/linux/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

void randomise () asm ("Standard.Random.randomise");

Int getRandom (Int range) asm ("Standard.Random..getRandom");
Integer getRandom (Integer range) asm ("Standard.Random..getRandom");
12 changes: 6 additions & 6 deletions src/platforms/amd64/linux/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
struct SleepTimeSpec
{
// TODO: Are the integer types on all platforms correct?
UInt seconds;
UInt nanoseconds;
Cardinal seconds;
Cardinal nanoseconds;
};

/**
* Waits for the given amount of milliseconds.
* @param milliseconds The number of milliseconds to wait for.
*/
void sleep (UInt milliseconds) asm ("\"Standard.Sleep~sleep\"");
void sleep (UInt milliseconds)
void sleep (Cardinal milliseconds) asm ("\"Standard.Sleep~sleep\"");
void sleep (Cardinal milliseconds)
{
struct SleepTimeSpec sleepTimeSpec = {
.seconds = milliseconds / 1000,
.nanoseconds = milliseconds % 1000 * 1000000
};

UInt syscode = SYSCODE_NANO_SLEEP;
Int result;
Cardinal syscode = SYSCODE_NANO_SLEEP;
Integer result;

asm volatile ("syscall" : "=a" (result)
: "D" (&sleepTimeSpec), "S" (&sleepTimeSpec), "a" (syscode)
Expand Down
10 changes: 5 additions & 5 deletions src/platforms/amd64/linux/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#include "../../common/types.h"
#include "../../common/string.h"

String createString (const UInt8* data, UInt size)
String createString (const Cardinal8* data, Cardinal size)
{
const UInt stringSize = size + sizeof(UInt);
const Cardinal stringSize = size + sizeof(Cardinal);

UInt8* stringArray = allocate(stringSize);
Cardinal8* stringArray = allocate(stringSize);

UInt* stringIntArray = (UInt*)stringArray;
Cardinal* stringIntArray = (Cardinal*)stringArray;

stringIntArray[0] = size;

copy(&stringArray[sizeof(UInt)], data, size);
copy(&stringArray[sizeof(Cardinal)], data, size);

return (String)stringArray;
}
2 changes: 1 addition & 1 deletion src/platforms/amd64/linux/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
#include "../../common/types.h"
#include "../../common/string.h"

String createString (const UInt8* data, UInt size);
String createString (const Cardinal8* data, Cardinal size);
4 changes: 2 additions & 2 deletions src/platforms/common/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* @param source The location to copy the block from.
* @param size The size of the memory block to copy.
*/
void copy (UInt8* destination, const UInt8* source, UInt size)
void copy (Cardinal8* destination, const Cardinal8* source, Cardinal size)
{
for (UInt i = 0; i < size; i++)
for (Cardinal i = 0; i < size; i++)
{
destination[i] = source[i];
}
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/common/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#include "types.h"

void copy (UInt8* destination, const UInt8* source, UInt size);
void copy (Cardinal8* destination, const Cardinal8* source, Cardinal size);
6 changes: 3 additions & 3 deletions src/platforms/common/string.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "types.h"
#include "string.h"

Bool stringsAreEqual (const String string1, const String string2) asm ("\"Standard.String~stringsAreEqual\"");
Bool stringsAreEqual (const String string1, const String string2)
Boolean stringsAreEqual (const String string1, const String string2) asm ("\"Standard.String~stringsAreEqual\"");
Boolean stringsAreEqual (const String string1, const String string2)
{
if (string1->size != string2->size)
{
return false;
}

for (UInt i = 0; i < string1->size; i++)
for (Cardinal i = 0; i < string1->size; i++)
{
if (string1->data[i] != string2->data[i])
{
Expand Down
6 changes: 3 additions & 3 deletions src/platforms/common/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/** A string value contains the size of the data as an integer and the data itself as an array. */
typedef struct
{
const UInt size;
const UInt8 data[];
const Cardinal size;
const Cardinal8 data[];
} __attribute__((__packed__)) StringValue;

/**
Expand All @@ -15,4 +15,4 @@ typedef struct
*/
typedef const StringValue* String;

Bool stringsAreEqual (const String string1, const String string2) asm ("\"Standard.String~stringsAreEqual\"");
Boolean stringsAreEqual (const String string1, const String string2) asm ("\"Standard.String~stringsAreEqual\"");
Loading

0 comments on commit 091f727

Please sign in to comment.