xxxxxxxxxx
/*
createVector()
ellipse()
for
while
rect()
mouseIsPressed
keyPressed
random
vertex
line()
*/
var textArr1, textArr2, textArr3;
var keywordArr = ['createVector', 'ellipse', 'for', 'while', 'rect', 'mouseIsPressed', 'keyPressed', 'random', 'vertex', 'line'];
//var keywords = {createVector:0, ellipse:0, for:0, while:0, rect:0, mouseIsPressed:0, keyPressed:0, random:0, vertex:0, line:0};
//var keywordNumArr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var keywordNumArr1 = [], keywordNumArr2 = [], keywordNumArr3 = [];
var newTextArr1, newTextArr2, newTextArr3;
function preload(){
textArr1 = loadStrings("code_perlin_noise.txt");
textArr2 = loadStrings("code_colorful_flow_IV.txt");
textArr3 = loadStrings("code_teleporting_dots.txt");
console.log(textArr2);
}
// Note: we could delete users' comments and all the strings in the code
// find things between /**/ and the line starts with //, delete them
// find things between "" and ''
// does user specified variables matter? perhaps the amount matters in the future?
function setup(){
newTextArr2 = cleanText(textArr2);
console.log(newTextArr2);
// create an array storing amount of each keyword
for (var i=0; i<keywordArr.length; i++){
keywordNumArr2.push(0);
}
for (var j=0; j<keywordArr.length; j++){
var keywordCounter = findKeywordAmount(newTextArr2, keywordArr[j]);
keywordNumArr2[i] = keywordCounter;
}
console.log(keywordNumArr2);
}
function cleanText(textArr){
var newTextArr = [];
// iterate through text array
for (var i=0; i<textArr.length; i++){
var rowStr = textArr[i];
// find "//"
var commentIndex = rowStr.search("//");
// if "//" is found in this row,
// slice everything from "//" to the end of this row,
// add commentless row to new text array
if (commentIndex !== -1){
var commentStr = rowStr.slice(commentIndex, rowStr.length);
newTextArr.push(rowStr - commentStr);
}
// if "//" if not found in this row, add this row to new text array
else{
newTextArr.push(rowStr);
}
// find /* and */
var multiCommentStartIndex = rowStr.search("/*");
var multiCommentEndIndex = rowStr.search("*/");
/* two possibilities:
comments within the same line
comments expands multiple lines */
// same line, both /* and */ are found
if ((multiCommentStartIndex !== -1) && (multiCommentEndIndex !== -1)){
var multiCommentStr = rowStr.slice(multiCommentStartIndex, multiCommentEndIndex);
newTextArr.push(rowStr - multiCommentStr);
}
else if (multiCommentStartIndex !== -1){
var multiCommentStr = rowStr.slice(multiCommentStartIndex, rowStr.length);
newTextArr.push(rowStr - multiCommentStr);
}
else if (multiCommentEndIndex !== -1){
var multiCommentStr = rowStr.slice(0, multiCommentEndIndex);
newTextArr.push(rowStr - multiCommentStr);
}
}
return newTextArr;
}
/*
// find ''
// variable set to false
var foundSingleQ = false;
var singleQIndex = rowStr.search("'");
// find ""
*/
function findKeywordAmount(textArr, keyword){
var keywordCounter = 0;
// iterate through text array
for (var i=0; i<textArr.length; i++){
if (textArr[i]){
var rowStr = textArr[i];
}
var keywordFound = rowStr.search(keyword);
if (keywordFound !== -1){
keywordCounter ++; // this code is not perfect, what if there's two of the same keyword in the same row
}
}
return keywordCounter;
}