From e0fc8bd2b18b74208db2420c90213b7774e22768 Mon Sep 17 00:00:00 2001 From: ElementG9 Date: Mon, 14 Jan 2019 10:47:43 -0700 Subject: [PATCH] Comment the code for ShrubBoi. --- classes.js | 2 ++ parser.js | 12 ++++++++++++ test.pivot | 11 ++++++----- tokenizer.js | 28 ++++++++++++++++++---------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/classes.js b/classes.js index f91b1ed..e5df2f0 100644 --- a/classes.js +++ b/classes.js @@ -1,7 +1,9 @@ +// This is the most basic type of token. var token = function (type, value) { this.type = type; this.value = value; }; +// This is a group of tokens. var group = function (type, tokens) { this.type = "Group"; this.value = type; diff --git a/parser.js b/parser.js index 3426fd6..e163023 100644 --- a/parser.js +++ b/parser.js @@ -1,8 +1,15 @@ +// Import the group class. const group = require("./classes.js").group; + +// Create the parser function. +// parse() takes an array of tokens in, and outputs an +// Abstract Syntax Tree (a structured array of tokens). module.exports = tokens => { + // Variables for later. var layer = 0; var delimiterCount = 0; var deepestLayer = 0; + // Give each token a layer number based on delimiters. for (var i = 0; i < tokens.length; i++) { if (tokens[i].type == "Left Delimiter") { layer++; @@ -16,6 +23,7 @@ module.exports = tokens => { layer--; } } + // Lower the layer of delimiters. for (var i = 0; i < tokens.length; i++) { if ((tokens[i].type == "Left Delimiter") || (tokens[i].type == "Right Delimiter")) { tokens[i].layer--; @@ -24,7 +32,9 @@ module.exports = tokens => { if (layer > 0) { // Unclosed delimiter. } else if (layer < 0) { // Overclosed delimiter. } + // Reset layer for structuring. layer = 0; + /* - - - DO NOT TOUCH THIS - - - */ for(var i=deepestLayer;i>=0;i--) { var temp = []; var firstIndex; @@ -43,5 +53,7 @@ module.exports = tokens => { } } } + /* - - - OK YOU CAN TOUCH AGAIN - - - */ + // Return the structured tokens. return tokens; }; diff --git a/test.pivot b/test.pivot index b2d039c..98f93a2 100644 --- a/test.pivot +++ b/test.pivot @@ -1,5 +1,6 @@ -x = 4 -y = 3 * x -asdf = (a) {return(a++)} -a = asdf(2) -log(asdf(a(x,0))) \ No newline at end of file +// Example Pivot code. +// This is a comment. +x = 4 // This creates a variable x with the value of 4. +y = 3 * x // This creates a variable y with the value of the output of (3 * x). +asdf = (a) {return(a++)} // This creates a function asdf that takes a number and returns that number + 1/ +a = asdf(2) // This creates a variable a with the value of the output of asdf(2). diff --git a/tokenizer.js b/tokenizer.js index 9b3edf7..b1ba7f9 100644 --- a/tokenizer.js +++ b/tokenizer.js @@ -1,5 +1,10 @@ +// Import the token class. const token = require("./classes.js").token; + +// Create the tokenizer function. +// tokenize() takes Pivot code in, and outputs an array of tokens. module.exports = exp => { + // Check functions for different token types. var isDigit = char => { return /\d/.test(char); }; @@ -21,16 +26,17 @@ module.exports = exp => { var isPeriod = char => { return (char === "."); }; - var result = []; - var nb = []; - var lb = []; - var ob = []; - var sb = []; - var inString = false; - var stringType; - exp = exp.split(""); - for (var i = 0; i < exp.length; i++) { - var char = exp[i]; + var result = []; // The final array of tokens. + var nb = []; // Number buffer. Allows for multiple digits to be one number. + var lb = []; // Letter buffer. Allows for multiple letters to be one variable / function. + var ob = []; // Operator buffer. Allows for multi-character operators. E.g. ++ or ==. + var sb = []; // String buffer. Allows for multi-character strings. + var inString = false; // Keep track of whether in string or not. + var stringType; // Keep track of what type of string. E.g. "" or ''. + exp = exp.split(""); // Split the expression into an array of characters. + /* - - - DO NOT TOUCH THIS - - - */ + for (var i = 0; i < exp.length; i++) { // Loop through all of the characters. + var char = exp[i]; // Create a quick reference to the current char. if (i >= 1) { if (exp[i - 1] == "\\") { exp.splice(i - 1, 2, `\\${char}`); @@ -44,6 +50,8 @@ module.exports = exp => { } } } + /* - - - OK YOU CAN TOUCH AGAIN - - - */ + // Nevermind, just don't mess with any of this file. for (var i = 0; i < exp.length; i++) { var char = exp[i]; if (inString) {