mirror of
https://github.com/nicbarker/clay.git
synced 2026-02-06 12:48:49 +00:00
initial commit
This commit is contained in:
commit
5aa61dcd60
37 changed files with 10622 additions and 0 deletions
7
generator/array_add.template.c
Normal file
7
generator/array_add.template.c
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
$TYPE$ *$NAME$_Add($NAME$ *array, $TYPE$ item) {
|
||||
if (Clay__Array_IncrementCapacityCheck(array->length, array->capacity)) {
|
||||
array->internalArray[array->length++] = item;
|
||||
return &array->internalArray[array->length - 1];
|
||||
}
|
||||
return $DEFAULT_VALUE$;
|
||||
}
|
||||
10
generator/array_define.template.c
Normal file
10
generator/array_define.template.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
typedef struct
|
||||
{
|
||||
uint32_t capacity;
|
||||
uint32_t length;
|
||||
$TYPE$ *internalArray;
|
||||
} $NAME$;
|
||||
|
||||
$NAME$ $NAME$_Allocate_Arena(uint32_t capacity, Clay_Arena *arena) {
|
||||
return ($NAME$){.capacity = capacity, .length = 0, .internalArray = ($TYPE$ *)Clay__Array_Allocate_Arena(capacity, sizeof($TYPE$), arena)};
|
||||
}
|
||||
3
generator/array_get.template.c
Normal file
3
generator/array_get.template.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$TYPE$ *$NAME$_Get($NAME$ *array, int index) {
|
||||
return Clay__Array_RangeCheck(index, array->length) ? &array->internalArray[index] : $DEFAULT_VALUE$;
|
||||
}
|
||||
9
generator/array_remove_swapback.template.c
Normal file
9
generator/array_remove_swapback.template.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
$TYPE$ $NAME$_RemoveSwapback($NAME$ *array, int index) {
|
||||
if (Clay__Array_RangeCheck(index, array->length)) {
|
||||
array->length--;
|
||||
$TYPE$ removed = array->internalArray[index];
|
||||
array->internalArray[index] = array->internalArray[array->length];
|
||||
return removed;
|
||||
}
|
||||
return $DEFAULT_VALUE$;
|
||||
}
|
||||
11
generator/array_set.template.c
Normal file
11
generator/array_set.template.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
void $NAME$_Set($NAME$ *array, int index, $TYPE$ value) {
|
||||
if (index < array->capacity && index >= 0) {
|
||||
array->internalArray[index] = value;
|
||||
array->length = index < array->length ? array->length : index + 1;
|
||||
} else {
|
||||
Clay_StringArray_Add(&Clay_warnings, CLAY_STRING("Attempting to allocate array in arena, but arena is already at capacity and would overflow."));
|
||||
#ifdef CLAY_OVERFLOW_TRAP
|
||||
raise(SIGTRAP);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
69
generator/generate_templates.js
Normal file
69
generator/generate_templates.js
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let files = ['../clay.h'];
|
||||
|
||||
let templates = ['./'];
|
||||
function readCTemplatesRecursive(directory) {
|
||||
fs.readdirSync(directory).forEach(template => {
|
||||
const absolute = path.join(directory, template);
|
||||
if (fs.statSync(absolute).isDirectory()) return readCTemplatesRecursive(absolute);
|
||||
else if (template.endsWith('template.c')) {
|
||||
return templates.push(absolute);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
readCTemplatesRecursive(__dirname);
|
||||
|
||||
for (const file of files) {
|
||||
const contents = fs.readFileSync(file, 'utf8');
|
||||
const lines = contents.split('\n');
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (line.startsWith('// __GENERATED__ template')) {
|
||||
const [comment, generated, templateOpen, templateNames, ...args] = line.split(" ");
|
||||
let matchingEndingLine = -1;
|
||||
for (let j = i + 1; j < lines.length; j++) {
|
||||
if (lines[j].startsWith('// __GENERATED__ template')) {
|
||||
matchingEndingLine = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (matchingEndingLine !== -1) {
|
||||
i++;
|
||||
lines.splice(i, matchingEndingLine - (i));
|
||||
lines.splice(i, 0, ['#pragma region generated']);
|
||||
i++;
|
||||
for (const templateName of templateNames.split(',')) {
|
||||
var matchingTemplate = templates.find(t => t.endsWith(`${templateName}.template.c`));
|
||||
if (matchingTemplate) {
|
||||
let templateContents = fs.readFileSync(matchingTemplate, 'utf8');
|
||||
for (const arg of args) {
|
||||
[argName, argValue] = arg.split('=');
|
||||
templateContents = templateContents.replaceAll(`\$${argName}\$`, argValue);
|
||||
}
|
||||
let remainingTokens = templateContents.split('$');
|
||||
if (remainingTokens.length > 1) {
|
||||
console.log(`Error at ${file}:${i}: Template is missing parameter ${remainingTokens[1]}`)
|
||||
process.exit();
|
||||
} else {
|
||||
templateContents = templateContents.split('\n');
|
||||
lines.splice(i, 0, ...templateContents);
|
||||
i += templateContents.length;
|
||||
}
|
||||
} else {
|
||||
console.log(`Error at ${file}:${i + 1}: no template with name ${templateName}.template.c was found.`);
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
lines.splice(i, 0, ['#pragma endregion']);
|
||||
i++;
|
||||
} else {
|
||||
console.log(`Error at ${file}:${i + 1}: template was opened and not closed again.`);
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(file, lines.join('\n'));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue