Skip to content

Commit

Permalink
add: src
Browse files Browse the repository at this point in the history
  • Loading branch information
117 committed Feb 24, 2020
1 parent 67326f2 commit 0856e62
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/fluorite-7.js
2 changes: 2 additions & 0 deletions compile.bash
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
pegjs fluorite-7.pegjs
23 changes: 23 additions & 0 deletions fl7
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

usage() {
echo "Usage $0 CODE" >&2
exit 1
}

(($# > 0)) || usage
exec=$1
shift

(($# > 0)) && usage

#

cd "$(cd "$(dirname "$0")"; pwd)"

if [ ! -f ./fluorite-7.js ]
then
./compile.bash || exit 2
fi

node -e 'var a = require("./fluorite-7.js"); console.log(a.parse(process.argv[1])[1])' "$exec"
36 changes: 36 additions & 0 deletions fluorite-7.pegjs
@@ -0,0 +1,36 @@
{
function range(start, end) {
return Array.from({length: (end - start) + 1}, (_, i) => start + i);
}
function v_add(a) {
return a.reduce((prev, current, i, arr) => prev + current);
}
}
Root = main:Expression { return [main, eval(main)]; }
Number = main:[0-9]+ { return main.join(""); }
Identifier = main:[a-z_]+ { return main.join(""); }
Variable = main:Identifier { return "v_" + main; }
Bracket = '(' main:Expression ')' { return main; }
Factor = Number / Variable / Bracket
Mul = head:Factor tails:([*~] Factor)* {
tails.map(tail => {
if (tail[0] == "~") {
head = "(range(" + head + ", " + tail[1] + "))";
} else {
head = "(" + head + " " + tail[0] + " " + tail[1] + ")";
}
});
return head;
}
Map = name:Identifier '=' list:Mul ':' content:Map {
return "(" + list + ".map(v_" + name + " => " + content + "))";
}
/ list:Mul ':' content:Map {
return "(" + list + ".map(v__ => " + content + "))";
}
/ Mul
Pipe = head:Map tails:([|] Map)* {
tails.map(tail => head = "(" + tail[1] + "(" + head + "))");
return head;
}
Expression = Pipe

0 comments on commit 0856e62

Please sign in to comment.