commit 9c75d045d714e428f2a33b50fb59d589139e3529 from: Omar Polo date: Mon Oct 03 08:51:59 2022 UTC template: add for loops commit - c8e1e0443e5715c08703da02a0673f29c1d74bf2 commit + 9c75d045d714e428f2a33b50fb59d589139e3529 blob - b2f21c0a91042ef4a2e55b52475a4ba54ebbacb7 blob + 38f801e710c7cd79e1ad80f0252443b807fa3a0f --- template/parse.y +++ template/parse.y @@ -84,7 +84,7 @@ typedef struct { %} -%token DEFINE ELSE END ERROR FINALLY IF INCLUDE PRINTF +%token DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF %token RENDER TQFOREACH UNSAFE URLESCAPE %token STRING %type string @@ -252,7 +252,13 @@ else : '{' ELSE '}' { } ; -loop : '{' TQFOREACH STRING STRING STRING '}' { +loop : '{' FOR stringy '}' { + printf("for (%s) {\n", $3); + free($3); + } body end { + puts("}"); + } + | '{' TQFOREACH STRING STRING STRING '}' { printf("TAILQ_FOREACH(%s, %s, %s) {\n", $3, $4, $5); free($3); @@ -333,6 +339,7 @@ lookup(char *s) { "else", ELSE }, { "end", END }, { "finally", FINALLY }, + { "for", FOR }, { "if", IF }, { "include", INCLUDE }, { "printf", PRINTF }, blob - 0a1daeb5d04c95c9bfaa13fa6bd319e71f0c231e blob + dd96702b6b6d728f77177f953de0319b5fc8c5a8 --- template/regress/05-loop.tmpl +++ template/regress/05-loop.tmpl @@ -17,7 +17,10 @@ int list(struct template *, struct tailhead *); {{ end }} {{ define list(struct template *tp, struct tailhead *head) }} - {! struct entry *np; !} +{! + struct entry *np; + int i; +!} {{ if !TAILQ_EMPTY(head) }}

items:

    @@ -28,4 +31,11 @@ int list(struct template *, struct tailhead *); {{ else }}

    no items

    {{ end }} + +

    + {{ for i = 0; i < 3; ++i }} + hello{{ " " }} + {{ end }} + world! +

    {{ end }} blob - 1c17d8e4f2fb97529887c0f02f9ee8e6d8d77c08 blob + d4c20d67eeee5e6e0217d8abd4c19f2d440ba6d9 --- template/regress/05.expected +++ template/regress/05.expected @@ -1,2 +1,2 @@ -

    items:

    • 1
    • 2
    -

    no items

    +

    items:

    • 1
    • 2

    hello hello hello world!

    +

    no items

    hello hello hello world!