Skip to content

Instantly share code, notes, and snippets.

@snail5008
Created April 12, 2023 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snail5008/724577a1920b0e31592ee5030ca99bb2 to your computer and use it in GitHub Desktop.
Save snail5008/724577a1920b0e31592ee5030ca99bb2 to your computer and use it in GitHub Desktop.
Templates in C -- basically what you see in your nightmares
#!/usr/bin/env python3
import sys
with open(sys.argv[1], 'r') as f:
contents = f.read()
contents += "\n\n"
i = 0
final_str = ""
while i < len(contents) - 1:
if contents[i] == "%":
i += 1
if contents[i] == "^":
template_string = ""
i += 1
while contents[i] != "\n":
template_string += contents[i]
i += 1
old_i = i
for j in template_string.split():
i = old_i
while contents[i] != "^" and contents[i + 1] != "%":
if contents[i] == "$" and contents[i + 1] != "$":
final_str += j
else:
final_str += contents[i]
i += 1
i += 2
final_str += contents[i]
i += 1
with open(sys.argv[2], 'w') as f:
autogenerated_message = "This file was automatically generated. Modify the file that generated this file instead, and rerun the python script."
f.write(f"/// {'*' * len(autogenerated_message)} ///\n/// {autogenerated_message} /// \n/// {'*' * len(autogenerated_message)} ///\n\n" + final_str)
#pragma once
%^u32 i32 f32 f64
typedef union {
struct {
$ x;
$ y;
};
$ data[2];
} V2$;
^%
/// ********************************************************************************************************************* ///
/// This file was automatically generated. Modify the file that generated this file instead, and rerun the python script. ///
/// ********************************************************************************************************************* ///
#pragma once
typedef union {
struct {
u32 x;
u32 y;
};
u32 data[2];
} V2u32;
typedef union {
struct {
i32 x;
i32 y;
};
i32 data[2];
} V2i32;
typedef union {
struct {
f32 x;
f32 y;
};
f32 data[2];
} V2f32;
typedef union {
struct {
f64 x;
f64 y;
};
f64 data[2];
} V2f64;
@snail5008
Copy link
Author

Although it looks like the worst idea I've ever had, if it's polished up a bit and the templating features are improved, this could really be useful (for me at least).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment