Skip to content

Commit

Permalink
Merge pull request #120 from stormprograms/stormprograms-1.0
Browse files Browse the repository at this point in the history
fixed issue #100
  • Loading branch information
abbyonstott authored Sep 1, 2019
2 parents be8eac8 + d207a38 commit c1185d7
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 36 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

![GitHub repo size in bytes](https://img.shields.io/github/repo-size/stormprograms/stormscript.svg)

[![Build Status](https://dev.azure.com/alegosdude/stormscript/_apis/build/status/stormprograms.StormScript%20(1)?branchName=dev)](https://dev.azure.com/alegosdude/stormscript/_build/latest?definitionId=2?branchName=dev)

[![Total alerts](https://img.shields.io/lgtm/alerts/g/stormprograms/StormScript.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/stormprograms/StormScript/alerts/)
[![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/stormprograms/StormScript.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/stormprograms/StormScript/context:cpp)

Expand Down
4 changes: 2 additions & 2 deletions docs/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Make a directory called build

**You must have gcc/g++ installed (MinGW for Windows) and CMake**
**You must have gcc-8/g++-8 installed (MinGW for Windows) and CMake**

GDB is also highly recommended for debugging

Expand All @@ -14,7 +14,7 @@ Terminal:
## Windows

Make sure you have MinGW and CMake installed (in PATH)
run `cmake -G "MinGW Makefiles" -D CMAKE_C_COMPILER="gcc" -D CMAKE_CXX_COMPILER="g++"`
run `cmake -G "MinGW Makefiles"`
then run `mingw32-make`

also make sure that you have src/core in PATH for errors to work
Expand Down
14 changes: 12 additions & 2 deletions docs/development/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ This should go without saying, but please comment on your code. I love reading c
# C++ Style
C++ has the strictest rules when it comes to styling as it makes up the majority of the project.

**TABS ARE ALWAYS 4 SPACES**
## Indention
Use the `\t` character

## Includes
**YOU MUST FOLLOW THIS**
Expand All @@ -38,9 +39,18 @@ if (expressions[*y].t == ENDEXPR) *y += 1;
or
```cpp
if (expressions[*y].t == ENDEXPR)
*y += 1;
*y += 1;
```

Otherwise, curly braces go 1 space in front of scope defining statement.

```cpp
switch (expressions[*y].t) {
...
}
```


## If statements and Switches

If you are dealing with enumerations, integers, or characters, you should use switch statements otherwise use if statements.
Expand Down
2 changes: 1 addition & 1 deletion docs/development/the_parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Expressions are "sorted" into 5 different enumerations:

## Characters that are left out

Needless to say, whitespace and empty newlines are left out of the parsed program as they serve no purpose to the actual execution of the program.
Needless to say, whitespace, comments, and empty newlines are left out of the parsed program as they serve no purpose to the actual execution of the program.

Less obviously, parentheses are completely left out of the program (for now) because they currently would only change the look of the program
7 changes: 4 additions & 3 deletions example/example.sts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
socket s => "AF_INET", "127.0.0.1", 9090;

s.connect => "Hello, World";
for 25 {
printl random;
printl randomrange 1 10;
}
26 changes: 13 additions & 13 deletions src/parser/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,54 @@ the interpreter parses the file and calls functions in other files
void parse() {
int y = 0;

while (y!=prg.size()){
while (y!=parserProgram.size()){
int z = 0;
bool inquotes = false;
if (program.expressions.size() > 0)
program.expressions.resize(program.expressions.size()+1);

while (prg[y][0]==' ')
prg[y].erase(prg[y].begin());
while (parserProgram[y][0]==' ')
parserProgram[y].erase(parserProgram[y].begin());


while (z!=prg[y].size()){
if (prg[y][z]=='"'){
while (z!=parserProgram[y].size()){
if (parserProgram[y][z]=='"'){
if (inquotes == false)
inquotes = true;
else
inquotes = false;
}

// this is what checks for chars to remove from prs version
if (((prg[y][z]==' ') || (prg[y][z]=='\n') || (prg[y][z]=='(') || (prg[y][z]==')')) && (inquotes==false)){
if (((parserProgram[y][z]==' ') || (parserProgram[y][z]=='\n') || (parserProgram[y][z]=='(') || (parserProgram[y][z]==')')) && (inquotes==false)){
if ((program.expressions.back().contents.size() != 0))
program.expressions.resize(program.expressions.size()+1);
z++;
program.expressions.back().line = y;
continue;
}
else if ((prg[y][z]=='#')) // line comment
else if ((parserProgram[y][z]=='#')) // line comment
break;
else if (((prg[y][z]==';') || (prg[y][z]=='}') || (prg[y][z]=='{')) && (inquotes==false)) {
else if (((parserProgram[y][z]==';') || (parserProgram[y][z]=='}') || (parserProgram[y][z]=='{')) && (inquotes==false)) {
program.expressions.resize(program.expressions.size() + 1);
program.expressions.back() = string(1,prg[y][z]);
program.expressions.back() = string(1,parserProgram[y][z]);
program.expressions.back().line = y;
break;
}
else if (((prg[y][z]=='+') || (prg[y][z]=='-') || (prg[y][z]=='*') || ((prg[y][z]=='/') && (program.expressions[program.expressions.size()-2].contents!="mod")) || (prg[y][z]=='[') || (prg[y][z]==',') || (prg[y][z]==']') || (prg[y][z] == ':') || (prg[y][z] == '.')) && (inquotes==false)) {
program.expressions.push_back( string(1,prg[y][z]) );
else if (((parserProgram[y][z]=='+') || (parserProgram[y][z]=='-') || (parserProgram[y][z]=='*') || ((parserProgram[y][z]=='/') && (program.expressions[program.expressions.size()-2].contents!="mod")) || (parserProgram[y][z]=='[') || (parserProgram[y][z]==',') || (parserProgram[y][z]==']') || (parserProgram[y][z] == ':') || (parserProgram[y][z] == '.')) && (inquotes==false)) {
program.expressions.push_back( string(1,parserProgram[y][z]) );
program.expressions.back().line = y;
program.expressions.resize(program.expressions.size()+1);
}
else if ((prg[y][z]=='\t') && (inquotes==false)) {
else if ((parserProgram[y][z]=='\t') && (inquotes==false)) {
z++;
program.expressions.back().line = y;
continue;
}
else {
if (program.expressions.size() == 0)
program.expressions.push_back(expression());
program.expressions.back().contents+=prg[y][z];
program.expressions.back().contents+=parserProgram[y][z];
}
program.expressions.back().line = y;
z++;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parseerrors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void parseErrors() {
bool glob = 1;

for (int i = 0; i < program.expressions.size(); i++) {
if ((program.expressions[i].line == prg.size() - 1 && i == program.expressions.size()-1) || program.expressions[i + 1].line != program.expressions[i].line) { // this checks if there is no semicolon at the end of line
if ((program.expressions[i].line == parserProgram.size() - 1 && i == program.expressions.size()-1) || program.expressions[i + 1].line != program.expressions[i].line) { // this checks if there is no semicolon at the end of line
if (program.expressions[i].t != ENDEXPR && program.expressions[i].tktype != CLOSEDCURL && program.expressions[i].tktype != OPENCURL)
error(1, std::to_string(program.expressions[i].line + 1)); // add 1 to line because line gives index, which always starts at 0
}
Expand Down
8 changes: 4 additions & 4 deletions src/parser/read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void stsread(char *argv[], int argc) {
std::ifstream file;
string contents;

prg = {};
parserProgram = {};

int sizeoff = 0;

Expand All @@ -31,20 +31,20 @@ void stsread(char *argv[], int argc) {
} //create sizeof in lines

sizeoff++;
prg.resize(sizeoff); //create vector for lines
parserProgram.resize(sizeoff); //create vector for lines
int loc = 0;
int a = 0;
for (int x = 0; x <= contents.size(); x++) {
if (contents[x] == '\n') {
for (int y = loc; y < x; y++)
prg[a] += contents[y]; //add lines to vector
parserProgram[a] += contents[y]; //add lines to vector

a++;
loc = x + 1;
}
}
for (int x = loc; x <= contents.size(); x++)
prg[prg.size() - 1] += contents[x]; //add last line to vector
parserProgram[parserProgram.size() - 1] += contents[x]; //add last line to vector

interp(sizeoff, argv, argc);
}
2 changes: 1 addition & 1 deletion src/sts_files.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

void stsread(char *argv[], int argc); // read stormscript programs

inline std::vector<string> prg; //unparsed program
inline std::vector<string> parserProgram; //unparsed program

#endif // STS_FILES_H_
2 changes: 1 addition & 1 deletion src/values/getvals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ stsvars getval() {

bool operation = ((program.expressions[program.loc+1].t == TOKEN) &&
(program.expressions[program.loc+1].tktype != COMMA) && (program.expressions[program.loc+1].tktype != COLON) && (program.expressions[program.loc+1].tktype != OPENCURL) && (program.expressions[program.loc+1].tktype != CLOSEDBRACKET) &&
(program.expressions[program.loc+1].tktype != DOT) &&
(program.expressions[program.loc+1].tktype != DOT) && (program.expressions[program.loc].btn != RANDOMRANGE) &&
(program.expressions.size() > program.loc+1));

switch (operation) {
Expand Down
41 changes: 35 additions & 6 deletions src/values/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,60 @@
#include <random>
#include <ctime>

#if (PLATFORM)
#include <chrono>
#endif

int genrandomintfromrange() {
long int ut = static_cast<long int> (time(NULL)); // cast unix epoch to long int
int range[2]; // range will store the min and max values

program.loc++;

std::vector<expression> expressions = program.expressions;
range[0] = std::stoi(getval().val);
program.loc++;
range[1] = std::stoi(getval().val);

#if (!PLATFORM)

std::random_device randomd;

std::mt19937_64 generate(randomd());

range[0] = std::stoi(getval().val);
program.loc++;
range[1] = std::stoi(getval().val);

std::uniform_int_distribution<> dis(range[0], range[1]);

return dis(generate);
#else
/*
* This most likely uses more CPU than the Linux version,
* but it is the best I can do until MinGW fixes mt19937_64
*/
{ // in it's own scope to allow using namespace
// not a bad idea as long as it is in a scope for only how long it is needed
using namespace std::chrono;
nanoseconds timens = duration_cast<nanoseconds>(steady_clock::now().time_since_epoch());
srand(timens.count());
}
return rand() % range[1] + range[0];
#endif

return 0;
}

bool randombool() {
#if (!PLATFORM)
std::random_device randomd;

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

return dis(generate);
#else
{
using namespace std::chrono;
nanoseconds timens = duration_cast<nanoseconds>(steady_clock::now().time_since_epoch());
srand(timens.count());
}
return rand() % 2;
#endif
return 0;
}

0 comments on commit c1185d7

Please sign in to comment.