Last active
June 21, 2017 16:05
-
-
Save kenwebb/7746de2abd4576de67e9a76eebc5fe6b to your computer and use it in GitHub Desktop.
PEG.js arithmetics
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
<?xml version="1.0" encoding="UTF-8"?> | |
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Wed Jun 21 2017 12:05:38 GMT-0400 (EDT)--> | |
<XholonWorkbook> | |
<Notes><![CDATA[ | |
Xholon | |
------ | |
Title: PEG.js arithmetics | |
Description: | |
Url: http://www.primordion.com/Xholon/gwt/ | |
InternalName: 7746de2abd4576de67e9a76eebc5fe6b | |
Keywords: | |
My Notes | |
-------- | |
June 21, 2017 | |
Investigate the use of PEG.js to generate parsers. | |
To install PEG.js (from a terminal) | |
----------------- | |
sudo npm install -g pegjs | |
To test it (from a terminal) | |
---------- | |
pegjs -h | |
pegjs --format globals --export-var PEG arithmetics.pegjs | |
- this generates arithmetics.js | |
arithmetics.pegjs | |
----------------- | |
// Simple Arithmetics Grammar | |
// ========================== | |
// | |
// Accepts expressions like "2 * (3 + 4)" and computes their value. | |
Expression | |
= head:Term tail:(_ ("+" / "-") _ Term)* { | |
return tail.reduce(function(result, element) { | |
if (element[1] === "+") { return result + element[3]; } | |
if (element[1] === "-") { return result - element[3]; } | |
}, head); | |
} | |
Term | |
= head:Factor tail:(_ ("*" / "/") _ Factor)* { | |
return tail.reduce(function(result, element) { | |
if (element[1] === "*") { return result * element[3]; } | |
if (element[1] === "/") { return result / element[3]; } | |
}, head); | |
} | |
Factor | |
= "(" _ expr:Expression _ ")" { return expr; } | |
/ Integer | |
Integer "integer" | |
= _ [0-9]+ { return parseInt(text(), 10); } | |
_ "whitespace" | |
= [ \t\n\r]* | |
References | |
---------- | |
(1) https://github.com/pegjs/pegjs | |
(2) http://pegjs.org/ | |
]]></Notes> | |
<_-.XholonClass> | |
<PhysicalSystem/> | |
<RoundPeg/> | |
</_-.XholonClass> | |
<xholonClassDetails> | |
</xholonClassDetails> | |
<PhysicalSystem> | |
<RoundPeg/> | |
</PhysicalSystem> | |
<RoundPegbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[ | |
var me, beh = { | |
postConfigure: function() { | |
me = this.cnode.parent(); | |
$wnd.xh.param("TimeStepInterval","1000"); | |
$wnd.xh.require("pegjs/arithmetics"); | |
}, | |
act: function() { | |
if ($wnd.PEG) { | |
me.println($wnd.PEG.parse("5 + 7")); // 12 | |
me.println($wnd.PEG.parse("(5 + 7) - 3")); // 9 | |
} | |
} | |
} | |
]]></RoundPegbehavior> | |
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml, | |
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg"> | |
<g> | |
<title>Round Peg</title> | |
<rect id="PhysicalSystem/RoundPeg" fill="#98FB98" height="50" width="50" x="25" y="0"/> | |
<g> | |
<title>Round Peg again</title> | |
<rect id="PhysicalSystem/RoundPeg" fill="#6AB06A" height="50" width="10" x="80" y="0"/> | |
</g> | |
</g> | |
</svg> | |
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient> | |
</XholonWorkbook> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment