mirror of
https://github.com/sigmasternchen/libparcival
synced 2025-03-15 11:58:53 +00:00
basic render structure block works
This commit is contained in:
parent
59d69f71ab
commit
8b779d6e90
4 changed files with 45 additions and 1 deletions
18
src/main.c
18
src/main.c
|
@ -30,6 +30,8 @@ void generateHeader() {
|
|||
fprintf(output, "\n");
|
||||
fprintf(output, "extern void _registerTemplate(const char*, void (*)(FILE*, va_list), size_t (*)(va_list));\n");
|
||||
fprintf(output, "\n");
|
||||
fprintf(output, "#define _renderTemplate(f, t, ...) renderTemplate(t, f, __VA_ARGS__)\n");
|
||||
fprintf(output, "\n");
|
||||
for (size_t i = 0; i < result.stats.no; i++) {
|
||||
fprintf(output, "%s\n", result.stats.texts[i]);
|
||||
}
|
||||
|
@ -117,6 +119,11 @@ void generateOutputNodeSize(int indentation, struct node node) {
|
|||
fprintf(output, "%s += snprintf(NULL, 0, %s);\n", SIZE_ACCUMULATOR_VAR, node.value.text);
|
||||
}
|
||||
|
||||
void generateRenderNodeSize(int indentation, struct node node) {
|
||||
indent(indentation);
|
||||
fprintf(output, "%s += sizeTemplate(%s);\n", SIZE_ACCUMULATOR_VAR, node.value.text);
|
||||
}
|
||||
|
||||
void parseTreeSize(int indentation, struct tree tree) {
|
||||
for (size_t i = 0; i < tree.kidsno; i++) {
|
||||
switch(tree.kids[i].type) {
|
||||
|
@ -129,6 +136,9 @@ void parseTreeSize(int indentation, struct tree tree) {
|
|||
case OUTPUT_NODE:
|
||||
generateOutputNodeSize(indentation, tree.kids[i]);
|
||||
break;
|
||||
case RENDER_NODE:
|
||||
generateRenderNodeSize(indentation, tree.kids[i]);
|
||||
break;
|
||||
default:
|
||||
panic("unknown node type");
|
||||
}
|
||||
|
@ -170,6 +180,11 @@ void generateOutputNode(int indentation, struct node node) {
|
|||
fprintf(output, "fprintf(out, %s);\n", node.value.text);
|
||||
}
|
||||
|
||||
void generateRenderNode(int indentation, struct node node) {
|
||||
indent(indentation);
|
||||
fprintf(output, "_renderTemplate(out, %s);\n", node.value.text);
|
||||
}
|
||||
|
||||
void parseTree(int indentation, struct tree tree) {
|
||||
for (size_t i = 0; i < tree.kidsno; i++) {
|
||||
switch(tree.kids[i].type) {
|
||||
|
@ -182,6 +197,9 @@ void parseTree(int indentation, struct tree tree) {
|
|||
case OUTPUT_NODE:
|
||||
generateOutputNode(indentation, tree.kids[i]);
|
||||
break;
|
||||
case RENDER_NODE:
|
||||
generateRenderNode(indentation, tree.kids[i]);
|
||||
break;
|
||||
default:
|
||||
panic("unknown node type");
|
||||
}
|
||||
|
|
15
src/parser.y
15
src/parser.y
|
@ -20,6 +20,7 @@ extern struct template result;
|
|||
struct template template;
|
||||
|
||||
char* text;
|
||||
int nodeType;
|
||||
}
|
||||
|
||||
%type <template> metaSection
|
||||
|
@ -38,6 +39,8 @@ extern struct template result;
|
|||
%type <text> texts
|
||||
%type <text> text
|
||||
|
||||
%type <nodeType> structureType
|
||||
|
||||
%token <text> TEXT
|
||||
%token SECTION COMMA END
|
||||
%token PARAMS_BEGIN PARAMS_END
|
||||
|
@ -148,10 +151,22 @@ mainSection: /* empty */
|
|||
}
|
||||
|
||||
$$ = $1;
|
||||
switch($4) {
|
||||
case RENDER_NODE:
|
||||
addNode(&$$, newRenderNode($5));
|
||||
break;
|
||||
default:
|
||||
yyerror("unhandled structure block command (internal error)");
|
||||
YYERROR;
|
||||
}
|
||||
}
|
||||
;
|
||||
|
||||
structureType: RENDER
|
||||
{
|
||||
$$ = RENDER_NODE;
|
||||
}
|
||||
;
|
||||
|
||||
statementHeader: STATEMENT_BEGIN blockStatement STATEMENT_END
|
||||
{
|
||||
|
|
|
@ -50,6 +50,14 @@ struct node newOutputNode(char* text) {
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
struct node newRenderNode(char* arguments) {
|
||||
return (struct node) {
|
||||
.type = RENDER_NODE,
|
||||
.value.text = arguments
|
||||
};
|
||||
}
|
||||
|
||||
struct tree newTree() {
|
||||
return (struct tree) {
|
||||
.kids = NULL,
|
||||
|
@ -184,3 +192,4 @@ bool checkCharset(const char* string, const char* charset) {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ char* combineStr(char*, char*);
|
|||
#define TEXT_NODE (0)
|
||||
#define STATEMENT_NODE (1)
|
||||
#define OUTPUT_NODE (2)
|
||||
#define RENDER_NODE (3)
|
||||
|
||||
struct node {
|
||||
int type;
|
||||
|
@ -27,6 +28,7 @@ struct tree {
|
|||
struct node newTextNode(char*);
|
||||
struct node newStatementNode(char*, struct tree);
|
||||
struct node newOutputNode(char*);
|
||||
struct node newRenderNode(char*);
|
||||
|
||||
struct tree newTree();
|
||||
void addNode(struct tree*, struct node);
|
||||
|
|
Loading…
Reference in a new issue