Last active
July 10, 2023 13:54
-
-
Save kenwebb/c22cb0b3d44fd87d6ba2ce6328c74ea5 to your computer and use it in GitHub Desktop.
nearley - lambda
This file contains hidden or 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
# lambda | |
# | |
# see my Michaelson book | |
# nearley | |
#nearleyc lambda.ne -o lambda.js | |
#nearley-test -q -i '' lambda.js | |
# https://github.com/kach/nearley/blob/master/builtin/whitespace.ne | |
# Whitespace: `_` is optional, `__` is mandatory. | |
#_ -> wschar:* {% function(d) {return null;} %} | |
#__ -> wschar:+ {% function(d) {return null;} %} | |
#wschar -> [ \t\n\v\f] {% id %} | |
@builtin "whitespace.ne" # `_` means arbitrary amount of whitespace | |
# based on: https://medium.com/@gajus/parsing-absolutely-anything-in-javascript-using-earley-algorithm-886edcc31e5e | |
@{% | |
const flatten = d => { | |
return d.reduce( | |
(a, b) => { | |
return a.concat(b); | |
}, | |
[] | |
); | |
} | |
// this doesn't seem to do anything | |
const flatten2 = d => { | |
return flatten(flatten(d)); | |
} | |
const filter = d => { | |
return d.filter((token) => { | |
return token !== null; | |
}); | |
} | |
const selectorBody = (d, i, reject) => { | |
const tokens = filter(d); | |
if (!tokens.length) { | |
return reject; | |
} | |
return flatten(tokens); | |
} | |
%} | |
# 2.2 Syntax | |
expression -> name | function | application {% selectorBody %} | |
function -> LAMBDA name DOT body {% selectorBody %} | |
body -> expression {% selectorBody %} #{% function(d) {return d } %} | |
application -> "(" function_expression __ argument_expression ")" | |
function_expression -> expression | |
argument_expression -> expression | |
name -> [a-zA-Z0-9-_+]:+ #{% function(d) {return d } %} | |
LAMBDA -> "λ" {% function(d) {return "" } %} | |
DOT -> "." {% function(d) {return " => " } %} | |
__ -> " " {% function(d) {return " " } %} | |
# KSW added | |
#ParenthOpen -> [ "(" ] {% function(d) {return null;} %} | |
#ParenthClose -> [ ")" ] {% function(d) {return null;} %} |
This file contains hidden or 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, Mon Jul 10 2023 09:54:16 GMT-0400 (GMT-04:00)--> | |
<XholonWorkbook> | |
<Notes><![CDATA[ | |
Xholon | |
------ | |
Title: nearley - lambda | |
Description: | |
Url: http://www.primordion.com/Xholon/gwt/ | |
InternalName: c22cb0b3d44fd87d6ba2ce6328c74ea5 | |
Keywords: | |
My Notes | |
-------- | |
10 July 2023 | |
Demonstrate the use of nearley in a browser and in Xholon. | |
Use my new nearley lambda grammar, that works with nodejs. | |
http://0.0.0.0:3001/wb/editwb.html | |
http://0.0.0.0:3001/wb/editwb.html?app=nearley+-+lambda&src=lstr | |
http://0.0.0.0:3001/Xholon.html?app=nearley+-+lambda&src=lstr&gui=clsc&jslib=nearley/nearley,nearley/lambdaXH01 | |
// does not work | |
https://www.primordion.com/Xholon/gwt/Xholon.html?app=nearley+-+lambda&src=lstr&gui=clsc | |
###The lambda grammar | |
The lambda grammar is not yet complete. | |
It works for some simple cases. | |
###The version of nearley | |
I already had nearley.js in my local Xholon src/public/lib/nearley folder | |
nearley github site has a different .js file (newer/older ???) | |
- I have included this in Xholon src/public/lib/nearley as nearleyGHlatest.js | |
###Dev Tools code: | |
(() => { | |
const example = [ | |
"λx.x", | |
"λx.λy.λz.x", | |
"λfirst.λsecond.first", | |
"λs.(s s)" | |
]; | |
// Create a Parser object from our grammar. | |
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)); | |
// Parse something! | |
parser.feed(example[2]); | |
// parser.results is an array of possible parsings. | |
const arr = parser.results.flat(Infinity).join(''); | |
console.log(`const funk = ${arr}`); // const funk = first => second => first | |
})() | |
###To run locally | |
~/gwtspace/Xholon/Xholon/war$ python3 -m http.server 3001 | |
Serving HTTP on 0.0.0.0 port 3001 (http://0.0.0.0:3001/) ... | |
References | |
---------- | |
() Greg Michaelson, An Introduction to Functional Programming Through Lambda Calculus, Dover, 2011 | |
() https://nearley.js.org/ | |
() https://omrelli.ug/nearley-playground/ | |
() https://github.com/kach/nearley | |
() my folders | |
~/gwtspace/Xholon/Xholon/nearley/lambda | |
~/nodespace/nearleyKSW | |
~/gwtspace/Xholon/Xholon/src/org/public/lib/nearley | |
() my notebook | |
9 July 2023 | |
]]></Notes> | |
<_-.XholonClass> | |
<PhysicalSystem/> | |
<Lambda/> | |
</_-.XholonClass> | |
<xholonClassDetails> | |
<!--<Block> | |
<port name="height" connector="Height"/> | |
</Block>--> | |
</xholonClassDetails> | |
<PhysicalSystem> | |
<Lambda/> | |
</PhysicalSystem> | |
<Lambdabehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[ | |
const example = [ | |
"λx.x", | |
"λx.λy.λz.x", | |
"λfirst.λsecond.first", | |
"λs.(s s)" | |
]; | |
// Create a Parser object from our grammar. | |
const parser = new $wnd.nearley.Parser($wnd.nearley.Grammar.fromCompiled($wnd.grammar)); | |
// Parse something! | |
parser.feed(example[2]); | |
// parser.results is an array of possible parsings. | |
const arr = parser.results.flat(Infinity).join(''); | |
console.log(`const funk = ${arr}`); // const funk = first => second => first | |
// this works | |
$wnd.xh.root().println(`const funk = ${arr}`); | |
// this works | |
//# sourceURL=Lambdabehavior.js | |
]]></Lambdabehavior> | |
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml, | |
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg"> | |
<g> | |
<title>Lambda</title> | |
<rect id="PhysicalSystem/Lambda" fill="#98FB98" height="50" width="50" x="25" y="0"/> | |
<g> | |
<title>Lambda</title> | |
<rect id="PhysicalSystem/Lambda" 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