Skip to content

WIP : First draft for #31 #41

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

Open
wants to merge 3 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
46 changes: 40 additions & 6 deletions js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const LBRACK = '[';
const RBRACK = ']';
const COMMA = ',';
const COLON = ':';
const TILDEN = '~';

const DIGITS = '0123456789';
const ALPHA = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Expand Down Expand Up @@ -293,7 +294,7 @@ class PhraseLexer extends Lexer {
this.skipWhitespace();
continue;
}
while (this.currentChar !== null && ALPHANUM.indexOf(this.currentChar) !== -1) {
while (this.currentChar !== null && (ALPHANUM.indexOf(this.currentChar) !== -1 || this.currentChar === TILDEN)) {
result += this.currentChar;
this.advance();
}
Expand Down Expand Up @@ -939,12 +940,39 @@ class Interpreter {
return evalPhrase(phraseBook, phrase, globalMemory, localMemory, this.t, this.level + 1, this.startTime);
}

visitNamedKey(node) {
const name = `${node.key}:${node.name}`;
if (!this.globalMemory[name]) {
this.globalMemory[name] = this.visitKey(node);
visitNamedKey(node) {
if (node.name.indexOf(TILDEN) === -1) {
const name = `${node.key}:${node.name}`;
if (!this.globalMemory[name]) {
this.globalMemory[name] = this.visitKey(node);
}
return this.globalMemory[name];
} else {
if ( !this.phraseBook[node.key].randomOrder ) {
//Fisher-Yates Shuffle
let tempRandOrder = Array.from(this.phraseBook[node.key]);

let currentIndex = tempRandOrder.length;
while (currentIndex !== 0) {
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

let temporaryValue = tempRandOrder[currentIndex];
tempRandOrder[currentIndex] = tempRandOrder[randomIndex];
tempRandOrder[randomIndex] = temporaryValue;
}
this.phraseBook[node.key].randomOrder = tempRandOrder;
}

let randomOrder = this.phraseBook[node.key].randomOrder;
let nodeKey = this.phraseBook[node.key];
let counter = nodeKey.count;

if(counter === (randomOrder.length-1)) nodeKey.count = 0;
else nodeKey.count += 1;

return randomOrder[counter].text;
}
return this.globalMemory[name];
}

visitRepeatFilter(node) {
Expand Down Expand Up @@ -1197,6 +1225,12 @@ async function parsePhraseBook(s, loadSketch) {
phraseBook[phrase.key].parameters = phrase.parameters;
}
}

//to help keep track of how many times has a reference been referred to
for(k in phraseBook){
phraseBook[k].count = 0;
}

phraseBook['%preamble'] = preamble;
phraseBook['%imports'] = imports;
return phraseBook;
Expand Down