Skip to content

Commit

Permalink
feat: add support for with blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Jul 13, 2022
1 parent add2c7e commit 5e1ee93
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 57 deletions.
18 changes: 18 additions & 0 deletions src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ ${SOL_HELPERS_LIBRARY}
return processEachBlock(statement, scope)
}

if (head === "with") {
return processWithBlock(statement, scope)
}

if (head === "if" || head === "unless") {
return processConditionalBlock(statement, scope)
}
Expand Down Expand Up @@ -394,6 +398,20 @@ ${SOL_HELPERS_LIBRARY}
]
}

function processWithBlock(
statement: AST.BlockStatement,
scope: Scope
): Output[] {
const { params, program } = statement
const path = params[0]
if (path.type !== "PathExpression") throw new Error("Unsupported")
const pathExpr = path as AST.PathExpression
const resolvedPath = scope.resolve(pathExpr)
const newScope = scope.dive(resolvedPath)

return processProgram(program, newScope)
}

function processPartialStatement(
statement: AST.PartialStatement,
scope: Scope
Expand Down
8 changes: 8 additions & 0 deletions test/cases/svg/0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/cases/svg/1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/cases/svg/Template.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ contract Template {
'"\n stroke-width="8"\n ></line>\n'
)
);
__result = string(
abi.encodePacked(
__result,
' <line\n x1="0"\n y1="0"\n x2="200"\n y2="200"\n stroke="',
__input.color,
'"\n stroke-width="4"\n ></line>\n'
)
);
for (uint256 __i; __i < __input.words.length; __i++) {
__result = string(
abi.encodePacked(
Expand Down
10 changes: 10 additions & 0 deletions test/cases/svg/template.svg.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
stroke="{{color}}"
stroke-width="8"
></line>
{{#with color}}
<line
x1="0"
y1="0"
x2="200"
y2="200"
stroke="{{.}}"
stroke-width="4"
></line>
{{/with}}
{{#each words}}
<text x="20" y="20">{{this}}</text>
<text x="100" y="20" fill="{{../color}}">{{.}}</text>
Expand Down
4 changes: 1 addition & 3 deletions test/cases/wand/0.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"frame": { "title": "FLOURISHING MISTY WORLD", "level3": true },
"starsSeed": 132413,
"seed": 132413,
"planets": [
{ "x": 10, "y": 154, "visible": true },
{ "x": -39, "y": 157, "visible": true },
Expand All @@ -22,7 +22,6 @@
{ "x1": 0, "y1": 0, "x2": 0, "y2": 0 }
],
"background": {
"hueRotate": 0,
"radial": true,
"color": { "hue": 50, "saturation": 50, "lightness": 50 },
"dark": true
Expand Down Expand Up @@ -129,7 +128,6 @@
"blueExp": 1,
"blueOff": -15,
"rotation": 306,
"seed": 1004123123,
"seasonsAmplitude": 76,
"secondInDay": 41269,
"secondInYear": 14037118
Expand Down
2 changes: 1 addition & 1 deletion test/cases/wand/0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 25 additions & 35 deletions test/cases/wand/Template.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ string constant __constant0 = '%, 0)"/> </radialGradient> <circle style="fill:ur
contract Template {
struct __Input {
Handle handle;
Xp xp;
Background background;
uint256 starsSeed;
Xp xp;
Stone stone;
uint256 seed;
Halo halo;
Planet[8] planets;
Stone stone;
Aspect[8] aspects;
FilterLayer[3] filterLayers;
Frame frame;
Expand Down Expand Up @@ -54,7 +54,6 @@ contract Template {
bool light;
bool dark;
bool linear;
uint16 hueRotate;
Color color;
}

Expand All @@ -79,7 +78,6 @@ contract Template {
int8 blueExp;
int8 blueOff;
uint16 rotation;
uint256 seed;
}

struct Frame {
Expand Down Expand Up @@ -124,8 +122,8 @@ contract Template {

struct Sparkle {
uint8 scale;
int16 tx;
int16 ty;
uint16 tx;
uint16 ty;
}

function render(__Input memory __input)
Expand All @@ -141,7 +139,7 @@ contract Template {
BackgroundLayer.background(__input.background),
BackgroundLayer.xpBar(__input.xp),
BackgroundLayer.stars(__input),
stone(__input.stone),
stone(__input),
FrameLayer.frame(__input.frame),
halo(__input.halo),
HandleLayer.handles(__input.handle),
Expand All @@ -152,7 +150,7 @@ contract Template {
);
}

function stone(Stone memory __input)
function stone(__Input memory __input)
internal
pure
returns (string memory __result)
Expand All @@ -161,38 +159,38 @@ contract Template {
abi.encodePacked(
__result,
'<filter id="s"> <feTurbulence ',
__input.fractalNoise ? 'type="fractalNoise"' : "",
__input.stone.fractalNoise ? 'type="fractalNoise"' : "",
' baseFrequency="',
SolidMustacheHelpers.uintToString(__input.turbFreqX, 3),
SolidMustacheHelpers.uintToString(__input.stone.turbFreqX, 3),
" ",
SolidMustacheHelpers.uintToString(__input.turbFreqY, 3),
SolidMustacheHelpers.uintToString(__input.stone.turbFreqY, 3),
'" numOctaves="',
SolidMustacheHelpers.uintToString(__input.turbOct, 0),
SolidMustacheHelpers.uintToString(__input.stone.turbOct, 0),
'" seed="',
SolidMustacheHelpers.uintToString(__input.seed, 0),
'" /> <feComponentTransfer> <feFuncR type="gamma" amplitude="',
SolidMustacheHelpers.intToString(__input.redAmp, 2),
SolidMustacheHelpers.intToString(__input.stone.redAmp, 2),
'" exponent="',
SolidMustacheHelpers.intToString(__input.redExp, 2),
SolidMustacheHelpers.intToString(__input.stone.redExp, 2),
'" offset="',
SolidMustacheHelpers.intToString(__input.redOff, 2)
SolidMustacheHelpers.intToString(__input.stone.redOff, 2)
)
);
__result = string(
abi.encodePacked(
__result,
'" /> <feFuncG type="gamma" amplitude="',
SolidMustacheHelpers.intToString(__input.greenAmp, 2),
SolidMustacheHelpers.intToString(__input.stone.greenAmp, 2),
'" exponent="',
SolidMustacheHelpers.intToString(__input.greenExp, 2),
SolidMustacheHelpers.intToString(__input.stone.greenExp, 2),
'" offset="',
SolidMustacheHelpers.intToString(__input.greenOff, 2),
SolidMustacheHelpers.intToString(__input.stone.greenOff, 2),
'" /> <feFuncB type="gamma" amplitude="',
SolidMustacheHelpers.intToString(__input.blueAmp, 2),
SolidMustacheHelpers.intToString(__input.stone.blueAmp, 2),
'" exponent="',
SolidMustacheHelpers.intToString(__input.blueExp, 2),
SolidMustacheHelpers.intToString(__input.stone.blueExp, 2),
'" offset="',
SolidMustacheHelpers.intToString(__input.blueOff, 2),
SolidMustacheHelpers.intToString(__input.stone.blueOff, 2),
'" /> <feFuncA type="discrete" tableValues="1"/> </feComponentTransfer> <feComposite operator="in" in2="SourceGraphic" result="tex" /> ',
' <feGaussianBlur in="SourceAlpha" stdDeviation="30" result="glow" /> <feColorMatrix in="glow" result="bgg" type="matrix" values="-1 0 0 0 1 0 -1 0 0 1 0 0 -1 0 1 0 0 0 .8 0 " /> <feMerge> <feMergeNode in="bgg"/> <feMergeNode in="tex"/> </feMerge> </filter> <radialGradient id="ss"> <stop offset="0%" stop-color="hsla(0, 0%, 0%, 0)"/> <stop offset="90%" stop-color="hsla(0, 0%, 0%, .8)"/> </radialGradient> <defs> ',
' <clipPath id="sc"> <circle cx="1000" cy="1060" r="260"/> </clipPath> </defs> ',
Expand All @@ -202,7 +200,7 @@ contract Template {
__result = string(
abi.encodePacked(
__result,
SolidMustacheHelpers.uintToString(__input.rotation, 0),
SolidMustacheHelpers.uintToString(__input.stone.rotation, 0),
', 1000, 1060)" cx="1000" cy="1060" r="260" filter="url(#s)" /> ',
' <circle cx="1200" cy="1060" r="520" fill="url(#ss)" clip-path="url(#sc)" /> <defs> <radialGradient id="sf" cx="606.78" cy="1003.98" fx="606.78" fy="1003.98" r="2" gradientTransform="translate(-187630.67 -88769.1) rotate(-33.42) scale(178.04 178.05)" gradientUnits="userSpaceOnUse" > <stop offset=".05" stop-color="#fff" stop-opacity=".7"/> <stop offset=".26" stop-color="#ececec" stop-opacity=".5"/> <stop offset=".45" stop-color="#c4c4c4" stop-opacity=".5"/> <stop offset=".63" stop-color="#929292" stop-opacity=".5"/> <stop offset=".83" stop-color="#7b7b7b" stop-opacity=".5"/> <stop offset="1" stop-color="#cbcbca" stop-opacity=".5"/> </radialGradient> <radialGradient id="sh" cx="1149" cy="2660" fx="1149" fy="2660" r="76" gradientTransform="translate(312 2546) rotate(-20) scale(1 -.5)" gradientUnits="userSpaceOnUse" > <stop offset="0" stop-color="#fff" stop-opacity=".7"/> <stop offset="1" stop-color="#fff" stop-opacity="0"/> </radialGradient> </defs> <path fill="url(#sf)" d="M1184 876a260 260 0 1 1-368 368 260 260 0 0 1 368-368Z"/> <path fill="url(#sh)" d="M919 857c49-20 96-15 107 11 10 26-21 62-70 82s-97 14-107-12c-10-25 21-62 70-81Z"/>'
)
Expand Down Expand Up @@ -356,9 +354,9 @@ contract Template {
abi.encodePacked(
__result,
' <use width="250" height="377" transform="translate(',
SolidMustacheHelpers.intToString(__input.sparkles[__i].tx, 0),
SolidMustacheHelpers.uintToString(__input.sparkles[__i].tx, 0),
" ",
SolidMustacheHelpers.intToString(__input.sparkles[__i].ty, 0),
SolidMustacheHelpers.uintToString(__input.sparkles[__i].ty, 0),
") scale(",
SolidMustacheHelpers.uintToString(__input.sparkles[__i].scale, 2),
')" href="#sp" /> '
Expand Down Expand Up @@ -550,14 +548,6 @@ library BackgroundLayer {
pure
returns (string memory __result)
{
__result = string(
abi.encodePacked(
__result,
'<g style="filter: hue-rotate(',
SolidMustacheHelpers.uintToString(__input.hueRotate, 0),
'deg);"> '
)
);
if (__input.radial) {
__result = string(abi.encodePacked(__result, " "));
if (__input.light) {
Expand Down Expand Up @@ -641,7 +631,7 @@ library BackgroundLayer {
__result = string(
abi.encodePacked(
__result,
' </g> <path filter="url(#bb)" style="opacity: .5" d="m1000 2435-199 334 195-335-573 212 570-214-892-20 889 18-1123-339 1121 335-1244-713 1243 709-1242-1106L988 2418-133 938 990 2415 101 616l892 1796L423 382l573 2028L801 260l199 2149 199-2149-195 2150 573-2028-569 2030 891-1796-889 1799L2133 938 1012 2418l1244-1102-1243 1106 1243-709-1244 713 1121-335-1123 338 889-17-892 20 570 214-573-212 195 335-199-334z" fill="white" />'
' <path filter="url(#bb)" style="opacity: .5" d="m1000 2435-199 334 195-335-573 212 570-214-892-20 889 18-1123-339 1121 335-1244-713 1243 709-1242-1106L988 2418-133 938 990 2415 101 616l892 1796L423 382l573 2028L801 260l199 2149 199-2149-195 2150 573-2028-569 2030 891-1796-889 1799L2133 938 1012 2418l1244-1102-1243 1106 1243-709-1244 713 1121-335-1123 338 889-17-892 20 570 214-573-212 195 335-199-334z" fill="white" />'
)
);
}
Expand Down Expand Up @@ -675,7 +665,7 @@ library BackgroundLayer {
abi.encodePacked(
__result,
'<defs> <filter id="st"> <feTurbulence baseFrequency=".1" seed="',
SolidMustacheHelpers.uintToString(__input.starsSeed, 0),
SolidMustacheHelpers.uintToString(__input.seed, 0),
'"/> <feColorMatrix values="0 0 0 7 -4 0 0 0 7 -4 0 0 0 7 -4 0 0 0 0 1" /> </filter> </defs> <clipPath id="stc"> <circle cx="1000" cy="1060" r="520"/> </clipPath> <mask id="stm"> <g filter="url(#st)" transform="scale(2)"> <rect width="100%" height="100%"/> </g> </mask> <circle class="bc" cx="1000" cy="1060" r="260"/> <circle class="bc" cx="1000" cy="1060" r="360"/> <circle class="bc" cx="1000" cy="1060" r="440"/> <circle class="bc" cx="1000" cy="1060" r="520"/> <line class="bc" x1="740" y1="610" x2="1260" y2="1510"/> <line class="bc" x1="1260" y1="610" x2="740" y2="1510"/> <line class="bc" x1="1450" y1="800" x2="550" y2="1320"/> <line class="bc" x1="1450" y1="1320" x2="550" y2="800"/> <g style="filter: blur(2px);"> <rect width="100%" height="100%" fill="white" mask="url(#stm)" clip-path="url(#stc)" /> </g>'
)
);
Expand Down
2 changes: 0 additions & 2 deletions test/cases/wand/background.partial.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<g style="filter: hue-rotate({{uint16 hueRotate}}deg);">
{{#if radial}}
{{#if light}}
<path style="fill:hsl({{uint16 color.hue}}, {{uint8 color.saturation}}%, {{uint8 color.lightness}}%)" d="M0 0h2000v3000H0z"/>
Expand Down Expand Up @@ -35,7 +34,6 @@
<rect style="fill:url(#l0)" width="2000" height="3000"/>
{{/if}}
{{/if}}
</g>

<path
filter="url(#bb)"
Expand Down
2 changes: 1 addition & 1 deletion test/cases/wand/sparkle.partial.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
</defs>
<g filter="url(#bb)" style="opacity: .6">
{{#each sparkles}}
<use width="250" height="377" transform="translate({{int16 tx}} {{int16 ty}}) scale({{uint8 scale decimals=2}})" href="#sp" />
<use width="250" height="377" transform="translate({{uint16 tx}} {{uint16 ty}}) scale({{uint8 scale decimals=2}})" href="#sp" />
{{/each}}
</g>
2 changes: 1 addition & 1 deletion test/cases/wand/stars.partial.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<defs>
<filter id="st">
<feTurbulence baseFrequency=".1" seed="{{uint starsSeed}}"/>
<feTurbulence baseFrequency=".1" seed="{{uint seed}}"/>
<feColorMatrix
values="0 0 0 7 -4 0 0 0 7 -4 0 0 0 7 -4 0 0 0 0 1"
/>
Expand Down
26 changes: 13 additions & 13 deletions test/cases/wand/stone.partial.hbs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<filter id="s">
<feTurbulence
{{#if fractalNoise}}type="fractalNoise"{{/if}}
baseFrequency="{{uint8 turbFreqX decimals=3}} {{uint8 turbFreqY decimals=3}}"
numOctaves="{{uint8 turbOct}}"
{{#if stone.fractalNoise}}type="fractalNoise"{{/if}}
baseFrequency="{{uint8 stone.turbFreqX decimals=3}} {{uint8 stone.turbFreqY decimals=3}}"
numOctaves="{{uint8 stone.turbOct}}"
seed="{{uint seed}}"
/>
<feComponentTransfer>
<feFuncR
type="gamma"
amplitude="{{int8 redAmp decimals=2}}"
exponent="{{int8 redExp decimals=2}}"
offset="{{int8 redOff decimals=2}}"
amplitude="{{int8 stone.redAmp decimals=2}}"
exponent="{{int8 stone.redExp decimals=2}}"
offset="{{int8 stone.redOff decimals=2}}"
/>
<feFuncG
type="gamma"
amplitude="{{int8 greenAmp decimals=2}}"
exponent="{{int8 greenExp decimals=2}}"
offset="{{int8 greenOff decimals=2}}"
amplitude="{{int8 stone.greenAmp decimals=2}}"
exponent="{{int8 stone.greenExp decimals=2}}"
offset="{{int8 stone.greenOff decimals=2}}"
/>
<feFuncB
type="gamma"
amplitude="{{int8 blueAmp decimals=2}}"
exponent="{{int8 blueExp decimals=2}}"
offset="{{int8 blueOff decimals=2}}"
amplitude="{{int8 stone.blueAmp decimals=2}}"
exponent="{{int8 stone.blueExp decimals=2}}"
offset="{{int8 stone.blueOff decimals=2}}"
/>
<feFuncA type="discrete" tableValues="1"/>
</feComponentTransfer>
Expand Down Expand Up @@ -66,7 +66,7 @@
</defs>
{{! stone base }}
<circle
transform="rotate({{uint16 rotation}}, 1000, 1060)"
transform="rotate({{uint16 stone.rotation}}, 1000, 1060)"
cx="1000"
cy="1060"
r="260"
Expand Down
2 changes: 1 addition & 1 deletion test/cases/wand/template.svg.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{~>background background extra="BackgroundLayer"~}}
{{~>xpBar xp extra="BackgroundLayer"~}}
{{~>stars extra="BackgroundLayer"~}}
{{~>stone stone~}}
{{~>stone~}}
{{~>frame frame extra="FrameLayer"~}}
{{~>halo halo~}}
{{~>handles handle extra="HandleLayer"~}}
Expand Down

0 comments on commit 5e1ee93

Please sign in to comment.