Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding variable detection #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 85 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function splitRoutinesAndArguments(inputLine) {
}

//Extracts Post-Conditionals from Routines, used by extractRoutines.
function extractPostConditional(inputObject) {
function extractPostConditional(inputObject, varObj, changedObj) {

//Assign to new variable to truncate Routines.
var tmpObject = inputObject;
Expand All @@ -169,6 +169,10 @@ function extractPostConditional(inputObject) {
if (tmpIndex > -1) {
tmpObject[i].mPostConditional = tmpObject[i].mRoutine.substring(tmpIndex + 1);
tmpObject[i].mRoutine = tmpObject[i].mRoutine.substring(0, tmpIndex);

//jrg - extract variables
extractVars(tmpObject[i].mPostConditional,varObj,changedObj);

}
}
}
Expand All @@ -180,9 +184,14 @@ function extractPostConditional(inputObject) {
function extractRoutines(inputLine, inputObject) {

var tmpFunction = {};
var changedObj = {};
var tmpFunctionArray = [];
var varObj = {};
var newedVarObj = {};
var args,idx;

//Divide Routines and Arguments.
inputLine = inputLine.replace(/^\s+/,""); //kill off any leading spaces that slipped through
var splitLine = splitRoutinesAndArguments(inputLine);

//Loop, even is function, odd is arguments.
Expand All @@ -207,6 +216,27 @@ function extractRoutines(inputLine, inputObject) {
}
}

//jrg - extract variables
args = tmpFunction.mArguments;
//if it's a do, the peice before ( is the function. Don't consider that a variable
if (tmpFunction.mRoutine == "d")
{
idx = args.indexOf('(');
if (idx > 0) {
extractVars(args.substring(args.indexOf('(')+1,args.length),varObj,changedObj);
}
}

else if (tmpFunction.mRoutine == "n") {
extractVars(args,newedVarObj,changedObj); //can't actually have changed vars here, we'll prevent the crash in case of typos
}

else {
extractVars(args,varObj,changedObj);
}


tmpFunction.changedVars = changedObj;
tmpFunctionArray.push(tmpFunction);
tmpFunction = {};

Expand All @@ -224,13 +254,66 @@ function extractRoutines(inputLine, inputObject) {
}

//Extract and output Post Conditionals.
var postConditionFunctionArray = extractPostConditional(tmpFunctionArray);
var postConditionFunctionArray = extractPostConditional(tmpFunctionArray,varObj, changedObj);
if (postConditionFunctionArray.length > 0) {
inputObject.lineRoutines = postConditionFunctionArray;
}

inputObject.vars = varObj;
inputObject.newedVars = newedVarObj;
inputObject.changedVars = changedObj;
return inputObject;
}

//jrg - function to add variables

function extractVars(str,obj,changedObj) {
var x, changed, ary, i, j;
str = str.replace(/"(.*?)"/g,""); //kill anything inside of quotes
var regex = /^[a-zA-Z][^^]*$/; //ensure that we don't pick up constants or function calls
if (str.indexOf("=") == -1) {
str=str.substring(str.indexOf('(')+1,str.length); //no equals, so this is a function call. The function itself is not a variable
}
str = str.replace(/[-+*#_<>()!\[\]\\]/g,','); //mod out by all of the seperators except equal

var statements = str.split(',');
for (i in statements) {
changed = false;

x=statements[i].replace(/[@\']/g,""); //pick off negate and indirection

if (x.indexOf('=') > 0) {
ary = x.split('=');
changedObj[ary[0]] = true;

for (j in ary) {
if (regex.test(ary[j])) {
obj[ary[j].replace(':','')] = true; //select statements occasionally produce the trailing colon
}
}

continue;
}

if (x.charAt(0) == '.') {
changed = true;
}

x = x.replace('.',''); //pick off reference
if (regex.test(x)) {
obj[x] = true;
if (changed) {
changedObj[x] = true;
}
}
}

return obj;
}




//Export all routines.
module.exports.extractLabel = extractLabel;
module.exports.extractComment = extractComment;
Expand Down