-
-
Save Abdur-rahmaanJ/9984f438686c890f19f4910d95239d4f to your computer and use it in GitHub Desktop.
transform function for sbn compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function transformer (ast) { | |
var svg_ast = { | |
tag : 'svg', | |
attr: { | |
width: 100, height: 100, viewBox: '0 0 100 100', | |
xmlns: 'http://www.w3.org/2000/svg', version: '1.1' | |
}, | |
body:[] | |
} | |
var pen_color = 100 // default pen color is black | |
// Extract a call expression at a time as `node`. Loop until we are out of expressions in body. | |
while (ast.body.length > 0) { | |
var node = ast.body.shift() | |
switch (node.name) { | |
case 'Paper' : | |
var paper_color = 100 - node.arguments[0].value | |
svg_ast.body.push({ // add rect element information to svg_ast's body | |
tag : 'rect', | |
attr : { | |
x: 0, y: 0, | |
width: 100, height:100, | |
fill: 'rgb(' + paper_color + '%,' + paper_color + '%,' + paper_color + '%)' | |
} | |
}) | |
break | |
case 'Pen': | |
pen_color = 100 - node.arguments[0].value // keep current pen color in `pen_color` variable | |
break | |
case 'Line': | |
... | |
} | |
} | |
return svg_ast | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment