Skip to content

Commit

Permalink
added break to while loops (#98)
Browse files Browse the repository at this point in the history
* added break to while loops

* fixed issue with loops and looping property

* added mt19937_64 random number gen to random bool
  • Loading branch information
abbyonstott authored and Metr0Gn0me committed Jun 1, 2019
1 parent ac4298f commit 3ab9e77
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 24 deletions.
3 changes: 2 additions & 1 deletion docs/changelogs/changelog-v1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* moved errors.sts from /usr/bin to /usr/share/stormscript
* Function arguments no longer require you to specify the name
* `for PLACEHOLDER in LIST/STR` creates a foreach loop
* `randomrange` now uses Mersenne Twister generation rather than cpp `rand()` function
* `randomrange and rand` now uses Mersenne Twister generation rather than cpp `rand()` function
* added `break` for loops

## What's Fixed
* Removed snapcraft files
Expand Down
4 changes: 1 addition & 3 deletions example/example.sts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
print "What is your favorite color: ";
in color;
printl "Cool! I like $color too!";
printl random;
3 changes: 3 additions & 0 deletions src/core/errors.sts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ else if arg[1] is "17" {
}
else if arg[1] is "18" {
printl "Error: Function ", arg[2], " requires arguments";
}
else if arg[1] is "19" {
printl "Error: break ouside loop";
}
7 changes: 7 additions & 0 deletions src/include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class sts
//variables
int lineon; //line the parser is on
int function = -1;
/*
* What the function numbers mean:
* -1: Not a function
* -2: A loop
* > 1: The number of the function that is running
*/
bool looping = false;
unsigned int sizeoff = 0; //size of the program
std::vector<string> prg; //unparsed program
std::vector<expression> expressions; // Replacing prs. Same thing with more info
Expand Down
3 changes: 2 additions & 1 deletion src/include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ enum Builtin { // these are built in commands
READ,
RANDOM,
RANDOMRANGE,
LENGTH
LENGTH,
BREAK
};

enum Value { // these are types
Expand Down
3 changes: 2 additions & 1 deletion src/interpreter/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bool evaluateBuiltin(string kwd) {
if ((kwd == "int") || (kwd == "str") || (kwd == "bool") || (kwd == "def") ||(kwd == "list") || (kwd == "func") || (kwd == "class") || (kwd == "mod") || (kwd == "return")) return 1;
else if ((kwd == "print") || (kwd == "printl") || (kwd == "in") || (kwd == "write") || (kwd == "read") || (kwd == "sys") || (kwd == "wait")) return 1;
else if ((kwd == "if") || (kwd == "else") || (kwd == "exit") || (kwd =="for") || (kwd == "foreach") || (kwd == "while")) return 1;
else if ((kwd == "random") || (kwd == "randomrange") || (kwd == "length")) return 1;
else if ((kwd == "random") || (kwd == "randomrange") || (kwd == "length") || (kwd == "break")) return 1;
return 0;
}

Expand Down Expand Up @@ -86,6 +86,7 @@ Builtin getBuiltincmd(string kwd) {
else if (kwd == "random") return RANDOM;
else if (kwd == "randomrange") return RANDOMRANGE;
else if (kwd == "length") return LENGTH;
else if (kwd == "break") return BREAK;
return NONE;
}

Expand Down
7 changes: 7 additions & 0 deletions src/interpreter/interpret.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ void sts::runBuiltin(int *y, std::vector<stsvars> *scpvars, std::vector<stsfunc>
case SYSTEM:
sys(y, scpvars, *functions);
break;
case BREAK:
if (looping) {
scopedown(y, expressions);
looping = false;
}
else error(19, "");
break;
case EXIT:
exit(0);
}
Expand Down
2 changes: 2 additions & 0 deletions src/parser/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ void sts::parse(std::vector<string> prg){

evaluateProgram(x);
}

// TODO: Add error and line parsing here
46 changes: 31 additions & 15 deletions src/stream/loops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,41 @@

void whileloop(sts *script, std::vector<stsvars> *variables, std::vector<stsfunc> functions, int *y) {
*y += 1;
sts s = *script;
int n = *y;
s.looping = true;

while (toBool(s.getval(variables, functions, &n).val)) {
s.newScope(new int(n), variables, &functions);

if (!s.looping) break;

while (condition(script, &n, variables, functions)) {
script->newScope(new int(n), variables, &functions);
n = *y; // set n back to y to repeat
}

while (script->expressions[*y].tktype != OPENCURL) *y += 1;
while (s.expressions[*y].tktype != OPENCURL) *y += 1;
*y += 1;
scopedown(y, script->expressions);

s.looping = false;
scopedown(y, s.expressions);
}


void forloop(sts *script, std::vector<stsvars> *variables, std::vector<stsfunc> functions, int *y) {
*y += 1;
bool foreach = (script->expressions[*y+1].btn == STSIN);

sts s = *script;
bool foreach = (s.expressions[*y+1].btn == STSIN);
s.looping = true;

if (foreach) {
stsvars root;
string name;
int rootsize;
name = script->expressions[*y].contents;
name = s.expressions[*y].contents;

*y += 2;

root = findVar(*variables, script->expressions[*y].contents); // grab variable listed on 3rd argument of for loop
root = findVar(*variables, s.expressions[*y].contents); // grab variable listed on 3rd argument of for loop
*y += 1;

switch (root.type) {
Expand All @@ -37,7 +45,7 @@ void forloop(sts *script, std::vector<stsvars> *variables, std::vector<stsfunc>
break;
case 'i':
case 'b':
script->error(9, root.name);
s.error(9, root.name);
}

for (int i = 0; i < rootsize; i++) {
Expand All @@ -55,23 +63,31 @@ void forloop(sts *script, std::vector<stsvars> *variables, std::vector<stsfunc>

newvars.push_back(placeholder);

script->newScope(new int(*y), &newvars, &functions);
s.newScope(new int(*y), &newvars, &functions);

variables->insert(variables->begin(), newvars.begin(), newvars.end()-1);

if (!s.looping) break;
}

*y += 1;

}
else {
int r = std::stoi(script->getval(variables, functions, y).val);
int r = std::stoi(s.getval(variables, functions, y).val);
*y += 1;

if (r <= 0)
script->error(17, std::to_string(r));
s.error(17, std::to_string(r));

for (int i = 0; i < r; i++)
script->newScope(new int(*y), variables, &functions);
for (int i = 0; i < r; i++) {
s.newScope(new int(*y), variables, &functions);
if (!s.looping) break;
}

*y += 1;
}

scopedown(y, script->expressions);
s.looping = false;
scopedown(y, s.expressions);
}
8 changes: 5 additions & 3 deletions src/values/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ int genrandomintfromrange(sts *s, std::vector<stsvars> *vars, std::vector<stsfun
}

bool randombool() {
long int ut = static_cast<long int> (time(0));
srand(ut);
return rand() % 2;
std::random_device randomd;

std::mt19937_64 generate(randomd());
std::uniform_int_distribution<> dis(0, 1);
return dis(generate);
}

0 comments on commit 3ab9e77

Please sign in to comment.