Refresh the repo
This commit is contained in:
4
tools/meta.bat
Normal file
4
tools/meta.bat
Normal file
@@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
|
||||
call meta_run.bat preprocess cpp
|
||||
clang-format -i *.cpp *.hpp *.h
|
||||
159
tools/meta.py
Normal file
159
tools/meta.py
Normal file
@@ -0,0 +1,159 @@
|
||||
import re
|
||||
snake_case_pattern = re.compile(r'(?<!^)(?=[A-Z])')
|
||||
|
||||
def pascal_to_snake(v):
|
||||
name = snake_case_pattern.sub('_', v).lower()
|
||||
return name
|
||||
|
||||
BINARY_EXPR = 1
|
||||
UNARY_EXPR = 2
|
||||
|
||||
token_simple_expr = [
|
||||
["Mul", "*", BINARY_EXPR],
|
||||
["Div", "/", BINARY_EXPR],
|
||||
["Mod", "%", BINARY_EXPR],
|
||||
["LeftShift", "<<", BINARY_EXPR],
|
||||
["RightShift", ">>", BINARY_EXPR],
|
||||
["FirstMul = TK_Mul", "SPECIAL"],
|
||||
["LastMul = TK_RightShift", "SPECIAL"],
|
||||
["Add", "+", BINARY_EXPR | UNARY_EXPR],
|
||||
["Sub", "-", BINARY_EXPR | UNARY_EXPR],
|
||||
["FirstAdd = TK_Add", "SPECIAL"],
|
||||
["LastAdd = TK_Sub", "SPECIAL"],
|
||||
["Equals", "==", BINARY_EXPR],
|
||||
["LesserThenOrEqual", "<=", BINARY_EXPR],
|
||||
["GreaterThenOrEqual", ">=", BINARY_EXPR],
|
||||
["LesserThen", "<", BINARY_EXPR],
|
||||
["GreaterThen", ">", BINARY_EXPR],
|
||||
["NotEquals", "!=", BINARY_EXPR],
|
||||
["FirstCompare = TK_Equals", "SPECIAL"],
|
||||
["LastCompare = TK_NotEquals", "SPECIAL"],
|
||||
["BitAnd", "&", BINARY_EXPR],
|
||||
["BitOr", "|", BINARY_EXPR],
|
||||
["BitXor", "^", BINARY_EXPR],
|
||||
["And", "&&", BINARY_EXPR],
|
||||
["Or", "||", BINARY_EXPR],
|
||||
["FirstLogical = TK_BitAnd", "SPECIAL"],
|
||||
["LastLogical = TK_Or", "SPECIAL"],
|
||||
|
||||
["Neg", "~", UNARY_EXPR],
|
||||
["Not", "!", UNARY_EXPR],
|
||||
]
|
||||
|
||||
token_inc_expr = [
|
||||
["Decrement", "--", UNARY_EXPR],
|
||||
["Increment", "++", UNARY_EXPR],
|
||||
["PostDecrement", "--", UNARY_EXPR],
|
||||
["PostIncrement", "++", UNARY_EXPR],
|
||||
]
|
||||
|
||||
token_assign_expr = [
|
||||
["Assign", "="],
|
||||
["ColonAssign", ":="],
|
||||
["DivAssign", "/="],
|
||||
["MulAssign", "*="],
|
||||
["ModAssign", "%="],
|
||||
["SubAssign", "-="],
|
||||
["AddAssign", "+="],
|
||||
["AndAssign", "&="],
|
||||
["OrAssign", "|="],
|
||||
["XorAssign", "^="],
|
||||
["LeftShiftAssign", "<<="],
|
||||
["RightShiftAssign", ">>="],
|
||||
["FirstAssign = TK_Assign", "SPECIAL"],
|
||||
["LastAssign = TK_RightShiftAssign", "SPECIAL"],
|
||||
]
|
||||
|
||||
token_rest = [
|
||||
["OpenParen", "("],
|
||||
["CloseParen", ")"],
|
||||
["OpenBrace", "{"],
|
||||
["CloseBrace", "}"],
|
||||
["OpenBracket", "["],
|
||||
["CloseBracket", "]"],
|
||||
["Comma", ","],
|
||||
["Pound", "#"],
|
||||
["Question", "?"],
|
||||
["ThreeDots", "..."],
|
||||
["Semicolon", ";"],
|
||||
["Dot", "."],
|
||||
["TwoDots", ".."],
|
||||
["NewLine", "[NewLine]"],
|
||||
["Colon", ":"],
|
||||
["DoubleColon", "::"],
|
||||
["At", "@"],
|
||||
["Arrow", "->"],
|
||||
["Polymorph", "$"],
|
||||
["ExprSizeof", "[sizeof]"],
|
||||
["DocComment", "[///]"],
|
||||
["Comment", "//"],
|
||||
["Identifier", "[Ident]"],
|
||||
["UnicodeLit", "[Unicode]"],
|
||||
["StringLit", "[String]"],
|
||||
["Error", "[Error]"],
|
||||
["Float", "[Float]"],
|
||||
["Integer", "[Int]"],
|
||||
["Keyword", "[Keyword]"],
|
||||
]
|
||||
|
||||
token_kinds = token_simple_expr + token_inc_expr + token_assign_expr + token_rest
|
||||
|
||||
keywords = [
|
||||
"struct",
|
||||
"union",
|
||||
"true",
|
||||
"default",
|
||||
"continue",
|
||||
"break",
|
||||
"false",
|
||||
"return",
|
||||
"switch",
|
||||
"Assert",
|
||||
"if",
|
||||
"elif",
|
||||
"pass",
|
||||
"else",
|
||||
"for",
|
||||
"enum",
|
||||
"goto",
|
||||
"defer",
|
||||
]
|
||||
|
||||
interns = [
|
||||
"typeof",
|
||||
"sizeof",
|
||||
"Len",
|
||||
"alignof",
|
||||
"foreign",
|
||||
"strict",
|
||||
"void",
|
||||
"flag",
|
||||
"it",
|
||||
"load",
|
||||
"import",
|
||||
"link",
|
||||
"compiler_breakpoint",
|
||||
]
|
||||
|
||||
|
||||
value_struct_content = """
|
||||
Ast_Type *type;
|
||||
Ast_Decl *resolved_decl;
|
||||
union {
|
||||
bool bool_val;
|
||||
double f64_val;
|
||||
Intern_String intern_val;
|
||||
BigInt big_int_val;
|
||||
Ast_Type *type_val;
|
||||
};
|
||||
""".strip()
|
||||
|
||||
def inline_value_fields():
|
||||
print(f"""
|
||||
union {{
|
||||
Value value;
|
||||
struct {{
|
||||
{value_struct_content}
|
||||
}};
|
||||
}};
|
||||
""".strip())
|
||||
65
tools/meta_run.py
Normal file
65
tools/meta_run.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
|
||||
files = os.listdir(".")
|
||||
files = [i for i in files if (i.endswith(".cpp") or i.endswith(".h")) and i != "test.cpp"]
|
||||
|
||||
for file_to_modify in files:
|
||||
print(file_to_modify)
|
||||
fd = open(file_to_modify, "r+")
|
||||
f = fd.read()
|
||||
END = "/*END*/"
|
||||
START = "/*#"
|
||||
STOP = "*/"
|
||||
|
||||
program_counter = 0
|
||||
valid = True
|
||||
index = 0
|
||||
while True:
|
||||
begin = f.find(START, index)
|
||||
if begin == -1:
|
||||
break
|
||||
index = begin
|
||||
begin += 3
|
||||
|
||||
end = f.find(STOP, index)
|
||||
if end == -1:
|
||||
valid = False
|
||||
print(f"One of the registered comments inside {file_to_modify} doesn't have an end")
|
||||
break
|
||||
|
||||
gen_end = f.find(END, end)
|
||||
next_block = f.find(START, end)
|
||||
if next_block != -1 and gen_end > next_block:
|
||||
gen_end = -1
|
||||
|
||||
before = f[:begin]
|
||||
program = f[begin:end]
|
||||
if gen_end == -1:
|
||||
after = f[end+2:]
|
||||
else:
|
||||
after = f[gen_end+len(END):]
|
||||
|
||||
temp_filename = "_metaprogram" + str(program_counter) + ".py"
|
||||
if os.path.isfile(temp_filename):
|
||||
print(temp_filename + " exists, dont want to risk overwriting something important")
|
||||
valid = False
|
||||
break
|
||||
program_counter += 1
|
||||
with open(temp_filename, "w") as meta_file:
|
||||
meta_file.write(program)
|
||||
|
||||
result = subprocess.run([sys.executable, temp_filename], stdout=subprocess.PIPE)
|
||||
program_result = result.stdout.decode('utf-8').replace('\r\n', '\n') + END
|
||||
f = before + program + "*/\n" + program_result + after
|
||||
os.remove(temp_filename)
|
||||
|
||||
index = end
|
||||
|
||||
if valid:
|
||||
fd.seek(0)
|
||||
fd.write(f)
|
||||
fd.truncate()
|
||||
fd.close()
|
||||
|
||||
Reference in New Issue
Block a user