m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-20 13:13:57 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-20 13:13:57 -0400
commit71a5cc94a1c0fe2ce035f9c0c26ee5df8bc2953b (patch)
tree3fb8703ef1c75ebcd92e3b7713382dfec9974d8e /src
parent427149c9a4c4e0b389520d67199af01ad3c3f0c2 (diff)
Lex and parse keepHigh
Diffstat (limited to 'src')
-rw-r--r--src/lexer.js1
-rw-r--r--src/parser.js14
2 files changed, 15 insertions, 0 deletions
diff --git a/src/lexer.js b/src/lexer.js
index 85579fb..fd0d0a3 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -25,6 +25,7 @@ newLexemeType('-', '-')
newLexemeType('(', '\\(')
newLexemeType(')', '\\)')
newLexemeType('E', 'E')
+newLexemeType('K', 'K')
newSkippableLexeme('whitespace', '\\s+')
const lex = (expressionString) => {
diff --git a/src/parser.js b/src/parser.js
index 9479abc..5e9960a 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -6,6 +6,8 @@ let throwSyntaxError = () => {
throw new Error('Syntax error: unexpected token')
}
+const dieBindingPower = 30
+
let newSymbol = (type, nud, lbp, led) => {
symbols[type] = {
type,
@@ -33,6 +35,16 @@ newSymbol('(', function(parser) {
newSymbol(')')
+const newDieOperation = (symbol, nud = null) => {
+ newSymbol(symbol, nud, dieBindingPower, (left, parser) => {
+ return {
+ type: symbol,
+ left: left,
+ right: parser.expression(dieBindingPower - 1)
+ }
+ })
+}
+
newSymbol('d', (parser) => {
return {
type: 'd',
@@ -55,6 +67,8 @@ newSymbol('E', null, 30, (left, parser) => {
}
})
+newDieOperation('K')
+
newSymbol('+', null, 20, (left, parser) => {
return {
type: 'add',