Skip to content

Commit

Permalink
✨ Added fraction
Browse files Browse the repository at this point in the history
  • Loading branch information
mationai committed Sep 21, 2022
1 parent a0c1772 commit 6d2ed60
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.tabCompletion": "on"
}
8 changes: 7 additions & 1 deletion dist/index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -4384,6 +4384,10 @@ parse$1.numericLiteral = (sourceString) => {
const n = Number(sourceString.replace("%", ""));
return round(n * 0.01);
}
if (sourceString.includes("/")) {
const [numer, denom] = sourceString.split("/");
return numer / denom;
}
return Number(sourceString);
};
parse$1.literal = (node = {}) => {
Expand Down Expand Up @@ -4608,11 +4612,12 @@ var grammar = `mationGrammar {
signedInteger = "+" decimalDigit* -- positive
| "-" decimalDigit* -- negative
| decimalDigit+ -- noSign
naturalNum = nonZeroDigit decimalDigit*
// Note that the ordering of hexNum and decimalNum is reversed w.r.t. the spec
// This is intentional: the order decimalNum | hexNum will parse
// "0x..." as a decimal literal "0" followed by "x..."
numericLiteral = "-"? (percentNum | octalNum | hexNum | decimalNum)
numericLiteral = "-"? (fraction | percentNum | octalNum | hexNum | decimalNum)
decimalNum = integer "." decimalDigit* exponentPart -- bothParts
| "." decimalDigit+ exponentPart -- decimalsOnly
| integer exponentPart -- integerOnly
Expand All @@ -4621,6 +4626,7 @@ var grammar = `mationGrammar {
hexNum = "0x" hexDigit+
| "0X" hexDigit+
percentNum = decimalNum "%"
fraction = naturalNum "/" naturalNum
// hexDigit defined in Ohm's built-in rules (otherwise: hexDigit = "0".."9" | "a".."f" | "A".."F")
octalNum = "0" octalDigit+
Expand Down
6 changes: 4 additions & 2 deletions dist/index.umd.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/mationGrammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ mationGrammar {
signedInteger = "+" decimalDigit* -- positive
| "-" decimalDigit* -- negative
| decimalDigit+ -- noSign
naturalNum = nonZeroDigit decimalDigit*

// Note that the ordering of hexNum and decimalNum is reversed w.r.t. the spec
// This is intentional: the order decimalNum | hexNum will parse
// "0x..." as a decimal literal "0" followed by "x..."
numericLiteral = "-"? (percentNum | octalNum | hexNum | decimalNum)
numericLiteral = "-"? (fraction | percentNum | octalNum | hexNum | decimalNum)
decimalNum = integer "." decimalDigit* exponentPart -- bothParts
| "." decimalDigit+ exponentPart -- decimalsOnly
| integer exponentPart -- integerOnly
Expand All @@ -123,6 +124,7 @@ mationGrammar {
hexNum = "0x" hexDigit+
| "0X" hexDigit+
percentNum = decimalNum "%"
fraction = naturalNum "/" naturalNum
// hexDigit defined in Ohm's built-in rules (otherwise: hexDigit = "0".."9" | "a".."f" | "A".."F")
octalNum = "0" octalDigit+

Expand Down
4 changes: 4 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ parse.numericLiteral = (sourceString) => {
const n = Number(sourceString.replace('%', ''))
return round(n * .01)
}
if (sourceString.includes('/')) {
const [numer, denom] = sourceString.split('/')
return numer / denom
}
return Number(sourceString)
}

Expand Down
2 changes: 2 additions & 0 deletions test/commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ test.only(`Commands - Mixed strings and values args`, () => {
expect(parse(`do: [
turn left -1.23 right 2;
move [1] 12.3% [2] -.5% beyond:[1 2];
move [1] 1/3 forward 11/111 back;
]`).result)
.toStrictEqual({ do: [
['turn', 'left', -1.23, 'right', 2],
['move', [1], .123, [2], -.005, { beyond: [1, 2] }],
['move', [1], 0.3333333333333333, 'forward', 0.0990990990990991, 'back'],
]})
})

0 comments on commit 6d2ed60

Please sign in to comment.