Skip to content

Commit

Permalink
Moved microwavebasic from cpp-stuff to here
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthilde committed Aug 22, 2019
1 parent 8c5c32d commit 5f9d17c
Show file tree
Hide file tree
Showing 18 changed files with 910 additions and 0 deletions.
Binary file added bin/microwavebasic
Binary file not shown.
62 changes: 62 additions & 0 deletions commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
MicrowaveBASIC Commands

* The ID are the variables that can contain integers
* The ID$ are the variables that can contain strings

[ P <string expression> ] Print a string expression
10 P "Hello, World!"
// Prints "Hello, World!"

[ p <string expression> ] Same as P but without line return
10 p "Hello, "
20 P "World!"
// Prints "Hello, " without linebreak then prints "World!" with a linebreak
// Showing "Hello, World!" on one line.

[ ?<*/$> <var>] Asks for number (*) or string ($)
10 p "What's your name? "
20 ?$ A
30 P "Hello, " + A

[ * <expression> ] Displays a math expression
10 p "First number : "
20 ?* A
30 p "Second number : "
40 ?* B
50 * A
60 p " + "
70 * B
80 p " = "
90 * A + B
// Example output
// First number : 4
// Second number : 8
// 4 + 8 = 12

[ =<*/$> <var> <expression> ] Set integer (*) or a string <$> to a variable
10 =* A 12+24
20 =* A A+1
30 =$ A "12+24+1="
// String variables are separated from integer variables
40 p A
50 * A
60 P ""
// Output : 12+24+1=37

[ G <line> ] Go to a specified line
10 P "S P A M S P A M S P A M S P A M"
20 G 10
// Do I need to explain how does this code works?

[ I<$/*> <expression> : <line of code>] If condition
10 p "Enter loop : "
20 ?$ A
30 I$ A="loop" : G 50
40 G 80
50 * A
60 =* A A+1
70 I* A<10 G 50
// Example Output
// Enter loop : loop
// 0123456789

3 changes: 3 additions & 0 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "Compiling MicrowaveBASIC Interpreter..."
g++ *.cpp libs/*.cpp -o bin/microwavebasic
echo "Done!"
182 changes: 182 additions & 0 deletions engine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#include <iostream>
#include <string>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>

#include "engine.h"
#include "utils.h"
#include "libs/stringadd.h"
#include "libs/stringop.h"
#include "libs/stringcond.h"
using namespace std;

// This cpp file contains the whole engine to run code.
// Version of the software
string VERSION = "0.1.0-alpha";
string* stringvars = new string[128];
int* intvars = new int[128];

//Boot screen. Displays useless info
void BootScreen() {
cout<<"MicrowaveBASIC " << VERSION << " by h34ting4ppliance \nCopyright (C) Microwave Micro-computers Co."<<endl;
//Display shit lol
}

//Will execute an entire code.
void ExecuteCode(std::string* code) {
int o = 0;
for (int i = 0; i < 99999; ++i) {
if (code[i] != "")
o = ExecuteLine(code[i], i);
if (o == -1) {
cout << "!STOP AT " << to_string(i) << endl;
break;
}

if (o >= 1)
i = o - 1;

o = 0;
}
}

//Will execute only a single line.
int ExecuteLine(std::string line, int pptr) {
if (boost::starts_with(line, "P ")) { // " PRINT command
string o = line.erase(0, 2);
if (o == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
else
cout << evalStringAddition(o, stringvars) << endl;

return 0;
} else if (boost::starts_with(line, "//")) // Comment
return 0;
else if (boost::starts_with(line, "p ")) { // ' PRINT command
string o = line.erase(0, 2);
if (o == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
else
cout << evalStringAddition(o, stringvars);
return 0;
} else if (boost::starts_with(line, "?* ")) {
string o = line.erase(0, 3);
string inp = "";
if (o == "" || !isalpha(o.at(0))) {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
else {
getline(cin, inp);
if (!is_number(inp)) {
cout << "!INPUT ERROR" << endl;
return -1;
}
intvars[toupper(o.at(0)) - 'A'] = evalString(inp, intvars, false);
}
return 0;
} else if (boost::starts_with(line, "?$ ")) {
string o = line.erase(0, 3);
string inp = "";

if (o == "" || !isalpha(o.at(0))) {
cout << "!SYNTAX ERROR" << endl;
return -1;
} else {
getline(cin, inp);
stringvars[toupper(o.at(0)) - 'A'] = inp;
}

return 0;
} else if (boost::starts_with(line, "* ")) {
string o = line.erase(0, 2);

if (o == "" || !isalpha(o.at(0))) {
cout << "!SYNTAX ERROR" << endl;
return -1;
} else {
cout << to_string(intvars[toupper(o.at(0)) - 'A']);
return 0;
}
} else if (boost::starts_with(line, "=* ")) {
string o = line.erase(0, 3);
string oa = line.erase(0, 1);
if (o == "" || !isalpha(o.at(0)) || oa == "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
} else {
intvars[toupper(o.at(0)) - 'A'] = evalString(oa, intvars, false);
return 0;
}
} else if (boost::starts_with(line, "G ")) {
if (pptr == -1) {
cout << "!PROGRAM ERROR" << endl;
return -1;
}
string o = line.erase(0, 2);
if (!is_number(o)) {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
pptr = evalString(o, intvars, false) - 1;
if (pptr <= 0) {
cout << "!SYNTAX ERROR" << endl;
return -1;
}
return pptr;
} else if (boost::starts_with(line, "I* ")) {
string o = line.erase(0, 3);
int sepPos = searchChar(':', o);
if (sepPos == -1) { // If sepPos = -1, that means not any separator has been detected
cout << "!SYNTAX ERROR" << endl;
return -1;
}

string argA = trimString(o, 0, sepPos - 1);
string argB = trimString(o, sepPos + 1, line.size() - 1);

bool condi = evalIntCondition(argA, intvars);
if (condi) {
int a = ExecuteLine(argB, pptr);
if (a == -1)
return -1;
else
return 0;
} else {
return -1;
}
} else if (boost::starts_with(line, "I$ ")) {
string o = line.erase(0, 3);
int sepPos = searchChar(':', o);
if (sepPos == -1) { // If sepPos = -1, that means not any separator has been detected
cout << "!SYNTAX ERROR" << endl;
return -1;
}

string argA = trimString(o, 0, sepPos - 1);
string argB = trimString(o, sepPos + 1, line.size() - 1);

bool condi = evalStrCondition(argA, stringvars);
if (condi) {
int a = ExecuteLine(argB, pptr);
if (a == -1)
return -1;
else
return a;
} else {
return 0;
}
}

else if (line != "") {
cout << "!SYNTAX ERROR" << endl;
return -1;
}

return 0;
}
7 changes: 7 additions & 0 deletions engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef ENGINE_H
#define ENGINE_H

void BootScreen();
void ExecuteCode(std::string*);
int ExecuteLine(std::string, int);
#endif
82 changes: 82 additions & 0 deletions libs/stringadd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <string>
#include <ctype.h>
#include "stringadd.h"
using namespace std;
// This code allows to perform string addition

std::string evalStringAddition(std::string o, const string* vars) {
string* strings = new string[50];
string outputStr = "";

char c; // Current character
int previousType = 0; // Previous type
int ptr = 0; // Array pointer
int endline; // End of array. Variable for optimization purposes.
bool isInQuotes = false;

for (int i = 0; i < o.size(); ++i) {
if (ptr > 48) { // To avoid out of range error
cout << "[stringadd.cpp] Syntax Error! (Pointer out of range)" << endl;
return 0;
}
c = o.at(i);

if (isInQuotes == true) {
if (c == '\"') {
previousType = 1;
isInQuotes = false;
continue;
} else {
strings[ptr] += c;
continue;
}

continue;
}
// The code isn't going further than that as long at there isn't another quote to close it.
if (previousType == 0 || previousType == 3) {
if (c == '\"') {
isInQuotes = true;
continue;
} else if (isalpha(c)) {
strings[ptr] = vars[toupper(c) - 'A'];
previousType = 2;
++ptr;
continue;
} else if (c == '+') {
cout << "[stringadd.cpp] Syntax Error! (Misplaced operator)" << endl;
return "ERR";
} else if (c == ' ') {
continue;
} else {
cout << "[stringadd.cpp] Syntax Error! (Unknown character)" << endl;
return "ERR";
}
} else if (previousType == 1 || previousType == 2) {
if (c == '"') {
cout << "[stringadd.cpp] Syntax Error! (Misplaced quote)" << endl;
return "ERR";
} else if (isalpha(c)) {
cout << "[stringadd.cpp] Syntax Error! (Misplaced variable)" << endl;
return "ERR";
} else if (c == '+') {
previousType = 3;
++ptr;
continue;
} else if (c == ' ') {
continue;
} else {
cout << "[stringadd.cpp] Syntax Error! (Unknown character)" << endl;
return "ERR";
}
}
}

endline = ptr + 1;
for (int i = 0; i < endline; ++i) {
outputStr += strings[i];
}

return outputStr;
}
5 changes: 5 additions & 0 deletions libs/stringadd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifndef STRINGADD_H
#define STRINGADD_H

std::string evalStringAddition(std::string, const std::string*);
#endif
Loading

0 comments on commit 5f9d17c

Please sign in to comment.