From 6eb15466705ac943cbd508c788036b7bdbfb2a06 Mon Sep 17 00:00:00 2001 From: Sarbbottam Bandyopadhyay Date: Thu, 31 Mar 2016 14:31:39 -0700 Subject: [PATCH 1/7] not familiar instead of familiar --- super-tiny-compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/super-tiny-compiler.js b/super-tiny-compiler.js index 1181a6c..a4d23d7 100644 --- a/super-tiny-compiler.js +++ b/super-tiny-compiler.js @@ -81,7 +81,7 @@ * We're going to compile some lisp-like function calls into some C-like * function calls. * - * If you are familiar with one or the other. I'll just give you a quick intro. + * If you are not familiar with one or the other. I'll just give you a quick intro. * * If we had two functions `add` and `subtract` they would be written like this: * From 4efa9f85227f9f8d9c39c9503c01f9eac0d92f8a Mon Sep 17 00:00:00 2001 From: Sarbbottam Bandyopadhyay Date: Thu, 31 Mar 2016 16:35:31 -0700 Subject: [PATCH 2/7] updated LETTERS regex --- super-tiny-compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/super-tiny-compiler.js b/super-tiny-compiler.js index e37f1a4..953b83d 100644 --- a/super-tiny-compiler.js +++ b/super-tiny-compiler.js @@ -446,7 +446,7 @@ function tokenizer(input) { // ^^^ // Name token // - var LETTERS = /[a-zA-Z]/; + var LETTERS = /[a-z]i/; if (LETTERS.test(char)) { var value = ''; From 8ab8a42fc05b24fd5578c0bb6c21cc605960636f Mon Sep 17 00:00:00 2001 From: James Kyle Date: Thu, 31 Mar 2016 16:44:54 -0700 Subject: [PATCH 3/7] Update super-tiny-compiler-unannotated.js --- super-tiny-compiler-unannotated.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/super-tiny-compiler-unannotated.js b/super-tiny-compiler-unannotated.js index 6fb0a9c..af26cc9 100644 --- a/super-tiny-compiler-unannotated.js +++ b/super-tiny-compiler-unannotated.js @@ -46,7 +46,7 @@ function tokenizer(input) { continue; } - var LETTERS = /[a-zA-Z]/; + var LETTERS = /[a-z]i/; if (LETTERS.test(char)) { var value = ''; From 974ca6ad29c4fa049e0f70474babbc74ebe514ed Mon Sep 17 00:00:00 2001 From: James Kyle Date: Thu, 31 Mar 2016 16:54:04 -0700 Subject: [PATCH 4/7] Delete super-tiny-compiler-unannotated.js --- super-tiny-compiler-unannotated.js | 252 ----------------------------- 1 file changed, 252 deletions(-) delete mode 100644 super-tiny-compiler-unannotated.js diff --git a/super-tiny-compiler-unannotated.js b/super-tiny-compiler-unannotated.js deleted file mode 100644 index af26cc9..0000000 --- a/super-tiny-compiler-unannotated.js +++ /dev/null @@ -1,252 +0,0 @@ -function tokenizer(input) { - var current = 0; - var tokens = []; - - while (current < input.length) { - var char = input[current]; - - if (char === '(') { - tokens.push({ - type: 'paren', - value: '(' - }); - current++; - continue; - } - - if (char === ')') { - tokens.push({ - type: 'paren', - value: ')' - }); - current++; - continue; - } - - var WHITESPACE = /\s/; - if (WHITESPACE.test(char)) { - current++; - continue; - } - - var NUMBERS = /[0-9]/; - if (NUMBERS.test(char)) { - var value = ''; - - while (NUMBERS.test(char)) { - value += char; - char = input[++current]; - } - - tokens.push({ - type: 'number', - value: value - }); - - continue; - } - - var LETTERS = /[a-z]i/; - if (LETTERS.test(char)) { - var value = ''; - - while (LETTERS.test(char)) { - value += char; - char = input[++current]; - } - - tokens.push({ - type: 'name', - value: value - }); - - continue; - } - - throw new TypeError('I dont know what this character is: ' + char); - } - - return tokens; -} - -function parser(tokens) { - var current = 0; - - function walk() { - var token = tokens[current]; - - if (token.type === 'number') { - current++; - - return { - type: 'NumberLiteral', - value: token.value - }; - } - - if ( - token.type === 'paren' && - token.value === '(' - ) { - token = tokens[++current]; - - var node = { - type: 'CallExpression', - name: token.value, - params: [] - }; - - token = tokens[++current]; - - while ( - (token.type !== 'paren') || - (token.type === 'paren' && token.value !== ')') - ) { - node.params.push(walk()); - token = tokens[current]; - } - - current++; - - return node; - } - - throw new TypeError(token.type); - } - - var ast = { - type: 'Program', - body: [] - }; - - while (current < tokens.length) { - ast.body.push(walk()); - } - - return ast; -} - -function traverser(ast, visitor) { - function traverseArray(array, parent) { - array.forEach(function(child) { - traverseNode(child, parent); - }); - } - - function traverseNode(node, parent) { - var method = visitor[node.type]; - - if (method) { - method(node, parent); - } - - switch (node.type) { - case 'Program': - traverseArray(node.body, node); - break; - - case 'CallExpression': - traverseArray(node.params, node); - break; - - case 'NumberLiteral': - break; - - default: - throw new TypeError(node.type); - } - } - - traverseNode(ast, null); -} - -function transformer(ast) { - var newAst = { - type: 'Program', - body: [] - }; - - ast._context = newAst.body; - - traverser(ast, { - NumberLiteral: function(node, parent) { - parent._context.push({ - type: 'NumberLiteral', - value: node.value - }); - }, - - CallExpression: function(node, parent) { - var expression = { - type: 'CallExpression', - callee: { - type: 'Identifier', - name: node.name - }, - arguments: [] - }; - - node._context = expression.arguments; - - if (parent.type !== 'CallExpression') { - expression = { - type: 'ExpressionStatement', - expression: expression - }; - } - - parent._context.push(expression); - } - }); - - return newAst; -} - -function codeGenerator(node) { - switch (node.type) { - case 'Program': - return node.body.map(codeGenerator) - .join('\n'); - - case 'ExpressionStatement': - return ( - codeGenerator(node.expression) + - ';' - ); - - case 'CallExpression': - return ( - codeGenerator(node.callee) + - '(' + - node.arguments.map(codeGenerator) - .join(', ') + - ')' - ); - - case 'Identifier': - return node.name; - - case 'NumberLiteral': - return node.value; - - default: - throw new TypeError(node.type); - } -} - -function compiler(input) { - var tokens = tokenizer(input); - var ast = parser(tokens); - var newAst = transformer(ast); - var output = codeGenerator(newAst); - - return output; -} - -module.exports = { - tokenizer: tokenizer, - parser: parser, - transformer: transformer, - codeGenerator: codeGenerator, - compiler: compiler -}; From 93572febb7ba6fbcfdc5c186f36cd0ddcd0fbc54 Mon Sep 17 00:00:00 2001 From: James Kyle Date: Thu, 31 Mar 2016 17:06:15 -0700 Subject: [PATCH 5/7] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 7958f5d..27cdc65 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,6 @@ work from end to end. ### [Want to jump into the code? Click here](super-tiny-compiler.js) -[(Or if you would prefer to read it without annotations, click here.)](super-tiny-compiler-unannotated.js) - --- ### Why should I care? From d8cafdb00216c4b6231baddd2c6b53d4e2a9ef32 Mon Sep 17 00:00:00 2001 From: Sarbbottam Bandyopadhyay Date: Thu, 31 Mar 2016 18:10:54 -0700 Subject: [PATCH 6/7] fixed regex `/[a-z]i/` to `/[a-z]/i` fixed #25 --- super-tiny-compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/super-tiny-compiler.js b/super-tiny-compiler.js index 953b83d..d7bd38a 100644 --- a/super-tiny-compiler.js +++ b/super-tiny-compiler.js @@ -446,7 +446,7 @@ function tokenizer(input) { // ^^^ // Name token // - var LETTERS = /[a-z]i/; + var LETTERS = /[a-z]/i; if (LETTERS.test(char)) { var value = ''; From bd11625f5cfeed3e36eb190b26f40a19b2949d9a Mon Sep 17 00:00:00 2001 From: Vishal Telangre Date: Fri, 1 Apr 2016 11:56:51 +0530 Subject: [PATCH 7/7] Fix grammar --- super-tiny-compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/super-tiny-compiler.js b/super-tiny-compiler.js index d7bd38a..e4e214d 100644 --- a/super-tiny-compiler.js +++ b/super-tiny-compiler.js @@ -74,7 +74,7 @@ */ /** - * Today we're going write a compiler together. But not just any compiler... A + * Today we're going to write a compiler together. But not just any compiler... A * super duper teeny tiny compiler! A compiler that is so small that if you * remove all the comments this file would only be ~200 lines of actual code. *