Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active May 18, 2019 02:24
Show Gist options
  • Save Superstar64/9e398b6b1be74a95a0218aa8839a5a6f to your computer and use it in GitHub Desktop.
Save Superstar64/9e398b6b1be74a95a0218aa8839a5a6f to your computer and use it in GitHub Desktop.
Mustache Templates with C
#include "stdint.h"
#include "stdlib.h"
typedef struct {
int* ptr;
size_t length;
} NFXHICQ_;
NFXHICQ_ create_NFXHICQ_(int* ptr, size_t length){
NFXHICQ_ array = { .ptr = ptr, .length = length };
return array;
}
typedef struct {
float* ptr;
size_t length;
} MZWG6YLUBI______;
MZWG6YLUBI______ create_MZWG6YLUBI______(float* ptr, size_t length){
MZWG6YLUBI______ array = { .ptr = ptr, .length = length };
return array;
}
int main(){
NFXHICQ_ array = create_NFXHICQ_(malloc(5*sizeof(int)),5);
return 0;
}
#include "stdint.h"
#include "stdlib.h"
{{#types}}
typedef struct {
{{{type}}}* ptr;
size_t length;
} {{id}};
{{id}} {{create_id}}({{{type}}}* ptr, size_t length){
{{id}} array = { .ptr = ptr, .length = length };
return array;
}
{{/types}}
{{#names}}
int main(){
{{int[]}} array = {{new int[]}}(malloc(5*sizeof(int)),5);
return 0;
}
{{/names}}
mustache = mustache.js
array.c: array.mustache.c
$(mustache) types.json $< > $@
{
"types":[
{
"type":"int",
"id":"NFXHICQ_",
"create_id":"create_NFXHICQ_"
},
{
"type":"float",
"id":"MZWG6YLUBI______",
"create_id":"create_MZWG6YLUBI______"
}
],
"names":{
"int[]":"NFXHICQ_",
"new int[]":"create_NFXHICQ_",
"float[]":"MZWG6YLUBI______",
"new float[]":"create_MZWG6YLUBI______"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment