-
Notifications
You must be signed in to change notification settings - Fork 17
PTL (Pythonic Template Language)
PTL is a python-like script language to generate a readable file from template, which support some [functions] defined in python. Here is an simple example:
// Template we used
int main() {
{%
for {<index>}, {<var>} in {<enumerate(vars)>}:
if {<index>} >= {<2d>}:
printf("%d", {<index>});
elif {<index>} == {<1d>}:
scanf("%d", &test{<var>});
else:
{<index>} = {<5d>}
NULL; // {<index>}
%}
}
// After running
int main() {
NULL; // 5
scanf("%d", &test2);
printf("%d", 2);
printf("%d", 3);
}
In a template file, you can write a PTL script by enclosing it with "{%" and "%}", any other text out of these signs will be treated as plain text.
In PTL, we enclose variables or function calls with "{<" and ">}". For example {} is a variable, and {<len(a)>} is a function call. All function calls MUST be enclosed by "{<" and ">}", or it may raise SyntaxError.
To use an instant variable in PTL, you should following rules:
- Integer: An integer number followed by small letter d, for example,
{<1d>}
- Float: An float point number followed by small letter f, for example,
{<2.1f>}
- String: Enclosed by " and ", for example,
{<"test">}
There are three types of statements in PTL: control flow statements, expression statements, string statements.
Like python, PTL provides a same experience with control flow. You can feel free to use if
, elif
, else
, while
, for
.
You can assign value to a new variable without declaration. Just use =
to assign a result to a new variable.
String statements can contain variables and it will be replaced by variables' value. The results of the string statement will be combined and returned as plain text. If you want to have =
in string statements, please use \=
instead. Or it will be recognized as expression statements.
When you are trying to generate text from a template, you must provide initial values for variables used without assignment. When you are writing your own script, do handle this properly, or it can raise KeyError.