mirror of
https://github.com/nicbarker/clay.git
synced 2025-05-14 22:38:03 +00:00
Compare commits
7 Commits
5bb70d9469
...
acb9c2b150
Author | SHA1 | Date | |
---|---|---|---|
|
acb9c2b150 | ||
|
df79cc9e53 | ||
|
8718ce6303 | ||
|
c5726b68d9 | ||
|
81032d9457 | ||
|
73f30d10b6 | ||
|
8df5da50c7 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7
generator/clay_header_generator/array_add.template.c
Normal file
7
generator/clay_header_generator/array_add.template.c
Normal file
@ -0,0 +1,7 @@
|
||||
$TYPE$ *$NAME$_Add($NAME$ *array, $TYPE$ item) {
|
||||
if (Clay__Array_AddCapacityCheck(array->length, array->capacity)) {
|
||||
array->internalArray[array->length++] = item;
|
||||
return &array->internalArray[array->length - 1];
|
||||
}
|
||||
return $DEFAULT_VALUE$;
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
void $NAME$_Add($NAME$ *array, $TYPE$ item) {
|
||||
if (Clay__Array_AddCapacityCheck(array->length, array->capacity)) {
|
||||
array->internalArray[array->length++] = item;
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
$NAME$ $NAME$_Allocate_Arena(int32_t capacity, Clay_Arena *arena) {
|
||||
return CLAY__INIT($NAME$){.capacity = capacity, .length = 0, .internalArray = ($TYPE$ *)Clay__Array_Allocate_Arena(capacity, sizeof($TYPE$), CLAY__ALIGNMENT($TYPE$), arena)};
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
$NAME$ $NAME$_Allocate_Arena(int32_t capacity, Clay_Arena *arena) {
|
||||
return CLAY__INIT($NAME$){.capacity = capacity, .length = 0, .internalArray = ($TYPE$ *)Clay__Array_Allocate_Arena(capacity, sizeof($TYPE$), CLAY__POINTER_ALIGNMENT, arena)};
|
||||
}
|
6
generator/clay_header_generator/array_define.template.c
Normal file
6
generator/clay_header_generator/array_define.template.c
Normal file
@ -0,0 +1,6 @@
|
||||
CLAY__TYPEDEF($NAME$, struct
|
||||
{
|
||||
int32_t capacity;
|
||||
int32_t length;
|
||||
$TYPE$ *internalArray;
|
||||
});
|
@ -0,0 +1,5 @@
|
||||
CLAY__TYPEDEF($NAME$Slice, struct
|
||||
{
|
||||
int32_t length;
|
||||
$TYPE$ *internalArray;
|
||||
});
|
3
generator/clay_header_generator/array_get.template.c
Normal file
3
generator/clay_header_generator/array_get.template.c
Normal file
@ -0,0 +1,3 @@
|
||||
$TYPE$ *$NAME$_Get($NAME$ *array, int32_t index) {
|
||||
return Clay__Array_RangeCheck(index, array->length) ? &array->internalArray[index] : $DEFAULT_VALUE$;
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
$TYPE$ *$NAME$Slice_Get($NAME$Slice *slice, int32_t index) {
|
||||
return Clay__Array_RangeCheck(index, slice->length) ? &slice->internalArray[index] : $DEFAULT_VALUE$;
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
$TYPE$ $NAME$_Get($NAME$ *array, int32_t index) {
|
||||
return Clay__Array_RangeCheck(index, array->length) ? array->internalArray[index] : $DEFAULT_VALUE$;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
$TYPE$ $NAME$_RemoveSwapback($NAME$ *array, int32_t 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$;
|
||||
}
|
6
generator/clay_header_generator/array_set.template.c
Normal file
6
generator/clay_header_generator/array_set.template.c
Normal file
@ -0,0 +1,6 @@
|
||||
void $NAME$_Set($NAME$ *array, int32_t index, $TYPE$ value) {
|
||||
if (Clay__Array_RangeCheck(index, array->capacity)) {
|
||||
array->internalArray[index] = value;
|
||||
array->length = index < array->length ? array->length : index + 1;
|
||||
}
|
||||
}
|
69
generator/clay_header_generator/generate_templates.js
Normal file
69
generator/clay_header_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…
Reference in New Issue
Block a user