diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 08bd951..44e699a 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -20,3 +20,7 @@ jobs: run: make - name: run run: ./JunLang + - uses: actions/upload-artifact@v4 + with: + name: JunLang + path: ./JunLang diff --git a/.gitignore b/.gitignore index 427bdb6..28bb9dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ build .idea -cmake-build-debug +cmake-build-debug \ No newline at end of file diff --git a/README.md b/README.md index c93ec78..69ee42b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # JunLang +[![C/C++ CI](https://github.com/Jun-Software/JunLang/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/Jun-Software/JunLang/actions/workflows/c-cpp.yml) [中文版](https://github.com/Jun-Software/JunLang/blob/master/README-zh.md) diff --git a/src/identifiers/addition.hpp b/src/identifiers/addition.hpp index 4392497..bc36d71 100644 --- a/src/identifiers/addition.hpp +++ b/src/identifiers/addition.hpp @@ -3,34 +3,12 @@ * By lemonorangeapple **/ void addition(vector::iterator it, ifstream &file) { - // Check if the variable is already declared - bool undeclared = true; - for (int i = 0; i <= variableCount; i++) { - // If the variable is already declared, add the value - if (variables[i].name == *(it + 1)) { - // Check if the value is an integer - if (isInteger(*(it + 2))) { - // Add the integer value to the variable - variables[i].value += atoi((*(it + 2)).c_str()); - undeclared = false; - break; - } - // If the value is not an integer, check if it is a variable - else { - undeclared = true; - for (int j = 0; j <= variableCount; j++) { - // If the variable is already declared, add the value - if (variables[j].name == *(it + 2)) { - variables[i].value += variables[j].value; - undeclared = false; - break; - } - } - } - } + string next = *(it + 2); + string _this = *(it + 1); + if (isInteger(next)) { + variables[_this] += atoi((next).c_str()); } - // If the variable is not declared, throw an error - if (undeclared) { - cerr << "[ERROR] Variable undeclared. " << endl; + else { + variables[_this] += variables[next]; } } \ No newline at end of file diff --git a/src/identifiers/division.hpp b/src/identifiers/division.hpp index d31ad64..c75f770 100644 --- a/src/identifiers/division.hpp +++ b/src/identifiers/division.hpp @@ -3,43 +3,12 @@ * By lemonorangeapple **/ void division(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through the variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable name matches the one in the expression - if (variables[i].name == *(it + 1)) { - // Check if the value is an integer - if (isInteger(*(it + 2))) { - // Divide the value by the integer - variables[i].value /= atoi((*(it + 2)).c_str()); - // Set undeclared to false - undeclared = false; - // Break out of the loop - break; - } - // If the value is not an integer - else { - // Set undeclared to true - undeclared = true; - // Loop through the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches the one in the expression - if (variables[j].name == *(it + 2)) { - // Divide the value by the value of the other variable - variables[i].value /= variables[j].value; - // Set undeclared to false - undeclared = false; - // Break out of the loop - break; - } - } - } - } + string next = *(it + 2); + string _this = *(it + 1); + if (isInteger(next)) { + variables[_this] /= atoi((next).c_str()); } - // If the variable is undeclared - if (undeclared) { - // Print an error message - cerr << "[ERROR] Variable undeclared. " << endl; + else { + variables[_this] /= variables[next]; } } \ No newline at end of file diff --git a/src/identifiers/end_loop.hpp b/src/identifiers/end_loop.hpp index 4184240..e2d6a63 100644 --- a/src/identifiers/end_loop.hpp +++ b/src/identifiers/end_loop.hpp @@ -7,24 +7,13 @@ void end_loop(vector::iterator it, ifstream &file) { if (!loopFlag.empty()) { // Check if loop flag is true if (loopFlag.top() == true) { - // Check if loop variable name is correct - for (int i = 0; i <= variableCount; i++) { - if (variables[i].name == loopVariableName.top()) { - // Check if loop variable value is 0 - if (variables[i].value == 0) { - // Pop loop flag - loopFlag.pop(); - break; - } - else { - // Check if loop line is empty - if (!loopLine.empty()) { - // Seekg to loop line - file.seekg(int(loopLine.top()) - 2); - } - break; - } - } + if (variables[loopVariableName.top()] == 0) { + // Pop loop flag + loopFlag.pop(); + } + else if (!loopLine.empty()) { + // Seekg to loop line + file.seekg(int(loopLine.top()) - 3); } } else { diff --git a/src/identifiers/equal.hpp b/src/identifiers/equal.hpp index c7a1df5..3191fb6 100644 --- a/src/identifiers/equal.hpp +++ b/src/identifiers/equal.hpp @@ -3,53 +3,14 @@ * By lemonorangeapple **/ void equal(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through the variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable name matches - if (variables[i].name == *(it + 1)) { - // Check if the value is an integer - if (isInteger(*(it + 2))) { - // Loop through the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches - if (variables[j].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value to true if the variable values are equal - variables[j].value = (variables[i].value == atoi((*(it + 2)).c_str())); - break; - } - } - } - // Check if the value is a variable - else { - // Set the undeclared flag to true - undeclared = true; - // Loop through the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches - if (variables[j].name == *(it + 2)) { - // Loop through the variables - for (int k = 0; k <= variableCount; k++) { - // Check if the variable name matches - if (variables[k].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value to true if the variable values are equal - variables[k].value = (variables[i].value == variables[j].value); - break; - } - } - break; - } - } - } - } + string a = *(it + 1), b = *(it + 2), c = *(it + 3); + if (isInteger(b)) { + variables[c] = (variables[a] == atoi(b.c_str())); } - // If the variable is undeclared, print an error message - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; + else if (isInteger(a)){ + variables[c] = (atoi(a.c_str()) == variables[b]); + } + else { + variables[c] = (variables[a] == variables[b]); } } \ No newline at end of file diff --git a/src/identifiers/equal_or_greater.hpp b/src/identifiers/equal_or_greater.hpp index 2c638b2..225dfc2 100644 --- a/src/identifiers/equal_or_greater.hpp +++ b/src/identifiers/equal_or_greater.hpp @@ -3,56 +3,14 @@ * By lemonorangeapple **/ void equal_or_greater(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through all the variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable name matches the first argument - if (variables[i].name == *(it + 1)) { - // Check if the second argument is an integer - if (isInteger(*(it + 2))) { - // Loop through all the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches the third argument - if (variables[j].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value of the third argument to the value of the first argument - variables[j].value = (variables[i].value >= atoi((*(it + 2)).c_str())); - // Break out of the loop - break; - } - } - } - // Check if the second argument is a variable - else { - // Set the undeclared flag to true - undeclared = true; - // Loop through all the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches the second argument - if (variables[j].name == *(it + 2)) { - // Loop through all the variables - for (int k = 0; k <= variableCount; k++) { - // Check if the variable name matches the third argument - if (variables[k].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value of the third argument to the value of the first argument - variables[k].value = (variables[i].value >= variables[j].value); - // Break out of the loop - break; - } - } - // Break out of the inner loop - break; - } - } - } - } + string a = *(it + 1), b = *(it + 2), c = *(it + 3); + if (isInteger(b)) { + variables[c] = (variables[a] >= atoi(b.c_str())); } - // If the variable is undeclared, print an error message - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; + else if (isInteger(a)){ + variables[c] = (atoi(a.c_str()) >= variables[b]); + } + else { + variables[c] = (variables[a] >= variables[b]); } } \ No newline at end of file diff --git a/src/identifiers/equal_or_less.hpp b/src/identifiers/equal_or_less.hpp index 4db1dd9..f62a4f7 100644 --- a/src/identifiers/equal_or_less.hpp +++ b/src/identifiers/equal_or_less.hpp @@ -3,59 +3,14 @@ * By lemonorangeapple **/ void equal_or_less(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through the variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable name matches - if (variables[i].name == *(it + 1)) { - // Check if the second argument is an integer - if (isInteger(*(it + 2))) { - // Loop through the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the third argument matches - if (variables[j].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value of the third argument to the comparison of the first and second arguments - variables[j].value = (variables[i].value <= atoi((*(it + 2)).c_str())); - // Break out of the loop - break; - } - } - // Break out of the loop - break; - } - // If the second argument is not an integer - else { - // Set the undeclared flag to true - undeclared = true; - // Loop through the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the second argument matches - if (variables[j].name == *(it + 2)) { - // Loop through the variables - for (int k = 0; k <= variableCount; k++) { - // Check if the third argument matches - if (variables[k].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value of the third argument to the comparison of the first and second arguments - variables[k].value = (variables[i].value <= variables[j].value); - // Break out of the loop - break; - } - } - // Break out of the loop - break; - } - } - } - } + string a = *(it + 1), b = *(it + 2), c = *(it + 3); + if (isInteger(b)) { + variables[c] = (variables[a] <= atoi(b.c_str())); } - // If the variable is undeclared - if (undeclared) { - // Print an error message - cerr << "[Error] Variable undeclared." << endl; + else if (isInteger(a)){ + variables[c] = (atoi(a.c_str()) <= variables[b]); + } + else { + variables[c] = (variables[a] <= variables[b]); } } \ No newline at end of file diff --git a/src/identifiers/greater.hpp b/src/identifiers/greater.hpp index 1c9fd4b..5893b3e 100644 --- a/src/identifiers/greater.hpp +++ b/src/identifiers/greater.hpp @@ -3,59 +3,14 @@ * By lemonorangeapple **/ void _greater(vector::iterator it, ifstream &file) { - // Check if the first variable is undeclared - bool undeclared = true; - // Loop through all variables - for (int i = 0; i <= variableCount; i++) { - // Check if the first variable is equal to the name of the variable - if (variables[i].name == *(it + 1)) { - // Check if the second variable is an integer - if (isInteger(*(it + 2))) { - // Loop through all variables - for (int j = 0; j <= variableCount; j++) { - // Check if the second variable is equal to the name of the variable - if (variables[j].name == *(it + 3)) { - // Set undeclared to false - undeclared = false; - // Set the value of the second variable to the greater of the first and the second - variables[j].value = (variables[i].value > atoi((*(it + 2)).c_str())); - // Break out of the loop - break; - } - } - // Break out of the loop - break; - } - // If the second variable is not an integer - else { - // Set undeclared to true - undeclared = true; - // Loop through all variables - for (int j = 0; j <= variableCount; j++) { - // Check if the second variable is equal to the name of the variable - if (variables[j].name == *(it + 2)) { - // Loop through all variables - for (int k = 0; k <= variableCount; k++) { - // Check if the third variable is equal to the name of the variable - if (variables[k].name == *(it + 3)) { - // Set undeclared to false - undeclared = false; - // Set the value of the third variable to the greater of the first and the third - variables[k].value = (variables[i].value > variables[j].value); - // Break out of the loop - break; - } - } - // Break out of the loop - break; - } - } - } - } + string a = *(it + 1), b = *(it + 2), c = *(it + 3); + if (isInteger(b)) { + variables[c] = (variables[a] > atoi(b.c_str())); } - // If the first variable is undeclared - if (undeclared) { - // Print an error message - cerr << "[Error] Variable undeclared." << endl; + else if (isInteger(a)){ + variables[c] = (atoi(a.c_str()) > variables[b]); + } + else { + variables[c] = (variables[a] > variables[b]); } } \ No newline at end of file diff --git a/src/identifiers/identifiers.hpp b/src/identifiers/identifiers.hpp index 3a79493..654fbd5 100644 --- a/src/identifiers/identifiers.hpp +++ b/src/identifiers/identifiers.hpp @@ -4,8 +4,6 @@ #include "output.hpp" // Wrap #include "wrap.hpp" -// New -#include "new.hpp" // Set #include "set.hpp" // Input diff --git a/src/identifiers/if.hpp b/src/identifiers/if.hpp index 915b891..456b574 100644 --- a/src/identifiers/if.hpp +++ b/src/identifiers/if.hpp @@ -3,28 +3,12 @@ * By lemonorangeapple **/ void _if(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through the variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable name matches - if (variables[i].name == *(it + 1)) { - // Check if the variable value is 0 - if (variables[i].value == 0) { - // Push false if the variable value is 0 - ifFlag.push(false); - } - else { - // Push true if the variable value is not 0 - ifFlag.push(true); - } - // Break out of the loop - undeclared = false; - break; - } + if (variables[*(it + 1)] == 0) { + // Push false if the variable value is 0 + ifFlag.push(false); } - // If the variable is undeclared, throw an error - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; + else { + // Push true if the variable value is not 0 + ifFlag.push(true); } } \ No newline at end of file diff --git a/src/identifiers/input.hpp b/src/identifiers/input.hpp index cc0ce34..a685213 100644 --- a/src/identifiers/input.hpp +++ b/src/identifiers/input.hpp @@ -3,20 +3,7 @@ * By lemonorangeapple **/ void input(vector::iterator it, ifstream &file) { - // Check if variable is already declared - bool undeclared = true; - for (int i = 0; i <= variableCount; i++) { - // If variable is found, assign value to it - if (variables[i].name == *(it + 1)) { - long double temp; - cin >> temp; - variables[i].value = temp; - undeclared = false; - break; - } - } - // If variable is not found, throw error - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; - } + long double temp; + cin >> temp; + variables[*(it + 1)] = temp; } \ No newline at end of file diff --git a/src/identifiers/less.hpp b/src/identifiers/less.hpp index 3776ee6..ce2e65f 100644 --- a/src/identifiers/less.hpp +++ b/src/identifiers/less.hpp @@ -3,53 +3,14 @@ * By lemonorangeapple **/ void _less(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through all the variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable name matches - if (variables[i].name == *(it + 1)) { - // Check if the value is an integer - if (isInteger(*(it + 2))) { - // Loop through all the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches - if (variables[j].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value of the variable to the comparison - variables[j].value = (variables[i].value < atoi((*(it + 2)).c_str())); - break; - } - } - } - // If the value is not an integer - else { - // Set the undeclared flag to true - undeclared = true; - // Loop through all the variables - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches - if (variables[j].name == *(it + 2)) { - // Loop through all the variables - for (int k = 0; k <= variableCount; k++) { - // Check if the variable name matches - if (variables[k].name == *(it + 3)) { - // Set the undeclared flag to false - undeclared = false; - // Set the value of the variable to the comparison - variables[k].value = (variables[i].value < variables[j].value); - break; - } - } - } - } - } - } + string a = *(it + 1), b = *(it + 2), c = *(it + 3); + if (isInteger(b)) { + variables[c] = (variables[a] < atoi(b.c_str())); } - // If the variable is undeclared - if (undeclared) { - // Print an error message - cerr << "[Error] Variable undeclared." << endl; + else if (isInteger(a)){ + variables[c] = (atoi(a.c_str()) < variables[b]); + } + else { + variables[c] = (variables[a] < variables[b]); } } \ No newline at end of file diff --git a/src/identifiers/loop.hpp b/src/identifiers/loop.hpp index 4299e90..51c5d4f 100644 --- a/src/identifiers/loop.hpp +++ b/src/identifiers/loop.hpp @@ -3,29 +3,16 @@ * By lemonorangeapple **/ void loop(vector::iterator it, ifstream &file) { - // Check if the variable is declared - bool undeclared = true; - for (int i = 0; i <= variableCount; i++) { - if (variables[i].name == *(it + 1)) { - undeclared = false; - // Push the variable name to the loop variable name vector - loopVariableName.push(string(*(it + 1))); - // Check if the variable is assigned - if (variables[i].value == 0) { - // Push false to the loop flag vector - loopFlag.push(false); - } - else { - // Push the current line number to the loop line vector - loopLine.push(int(file.tellg()) - 2); - // Push true to the loop flag vector - loopFlag.push(true); - } - break; - } + string next = *(it + 1); + loopVariableName.push(next); + if (variables[next] == 0) { + // Push false to the loop flag vector + loopFlag.push(false); } - // If the variable is undeclared, throw an error - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; + else { + // Push the current line number to the loop line vector + loopLine.push(int(file.tellg()) - 2); + // Push true to the loop flag vector + loopFlag.push(true); } } \ No newline at end of file diff --git a/src/identifiers/multiplication.hpp b/src/identifiers/multiplication.hpp index 6ee50a0..728946f 100644 --- a/src/identifiers/multiplication.hpp +++ b/src/identifiers/multiplication.hpp @@ -3,37 +3,12 @@ * By lemonorangeapple **/ void multiplication(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through the variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable is the same as the one in the expression - if (variables[i].name == *(it + 1)) { - // Check if the value is an integer - if (isInteger(*(it + 2))) { - // If it is, set undeclared to false and multiply the value - undeclared = false; - variables[i].value *= atoi((*(it + 2)).c_str()); - break; - } - // If it isn't, check if the value is a variable - else { - // If it is, set undeclared to true and loop through the variables - undeclared = true; - for (int j = 0; j <= variableCount; j++) { - // Check if the variable is the same as the one in the expression - if (variables[j].name == *(it + 2)) { - // If it is, set undeclared to false and multiply the value - undeclared = false; - variables[i].value *= variables[j].value; - break; - } - } - } - } + string next = *(it + 2); + string _this = *(it + 1); + if (isInteger(next)) { + variables[_this] *= atoi((next).c_str()); } - // If the variable is undeclared, print an error message - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; + else { + variables[_this] *= variables[next]; } } \ No newline at end of file diff --git a/src/identifiers/new.hpp b/src/identifiers/new.hpp deleted file mode 100644 index d8c24a7..0000000 --- a/src/identifiers/new.hpp +++ /dev/null @@ -1,8 +0,0 @@ -/** - * New function - * By lemonorangeapple -**/ -void _new(vector::iterator it, ifstream &file) { - // Get the name of the variable - variables[variableCount++].name = *(it + 1); -} \ No newline at end of file diff --git a/src/identifiers/not.hpp b/src/identifiers/not.hpp index 4349987..05a04e3 100644 --- a/src/identifiers/not.hpp +++ b/src/identifiers/not.hpp @@ -3,26 +3,6 @@ * By lemonorangeapple **/ void _not(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - for (int i = 0; i <= variableCount; i++) { - // Check if the variable is declared - if (variables[i].name == *(it + 1)) { - undeclared = true; - // Check if the variable to be negated is declared - for (int j = 0; j <= variableCount; j++) { - if (variables[j].name == *(it + 2)) { - // Set the value of the variable to the opposite of the original value - variables[j].value = !((bool)(variables[i].value)); - undeclared = false; - break; - } - } - } - break; - } - // If the variable is undeclared, throw an error - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; - } + string next = *(it + 1); + variables[next] = !(bool(variables[next])); } \ No newline at end of file diff --git a/src/identifiers/output.hpp b/src/identifiers/output.hpp index d9eca0f..bc48a2a 100644 --- a/src/identifiers/output.hpp +++ b/src/identifiers/output.hpp @@ -4,32 +4,20 @@ **/ void output(vector::iterator it, ifstream &file) { // Check if the next token is a variable - if ((*(it + 1))[0] != '"') { - // Check if the variable is undeclared - bool undeclared = true; - // Check if the variable is already declared - for (int i = 0; i <= variableCount; i++) { - if (variables[i].name == *(it + 1)) { - undeclared = false; - printf("%.3Lf", variables[i].value); - break; - } - } - // If the variable is undeclared, print an error message - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; - } + string _this = *(it + 1); + if (_this[0] != '"') { + cout << variables[_this]; } // If the next token is a string, print the string else { - // Remove the quotation marks from the string - string result = string(*(it + 1)).substr(1, strlen((*(it + 1)).c_str()) - 2); + string result = string(_this.substr(1, strlen(_this.c_str()) - 2)); // Replace underscores with spaces - for (int i = 0; i < result.size(); i++) { + for (int i = 0; i <= result.size(); i++) { if (result[i] == '_') { - result[i] = ' '; + cout << ' '; + continue; } + cout << result[i]; } - cout << result; } } \ No newline at end of file diff --git a/src/identifiers/set.hpp b/src/identifiers/set.hpp index 1ae74eb..dd7079d 100644 --- a/src/identifiers/set.hpp +++ b/src/identifiers/set.hpp @@ -3,37 +3,12 @@ * By lemonorangeapple **/ void set(vector::iterator it, ifstream &file) { - // Check if the variable is already declared - bool undeclared = true; - for (int i = 0; i <= variableCount; i++) { - // If the variable is already declared - if (variables[i].name == *(it + 1)) { - // If the value is an integer - if (isInteger(*(it + 2))) { - // Set the value to the integer - undeclared = false; - variables[i].value = atoi((*(it + 2)).c_str()); - break; - } - // If the value is not an integer - else { - // Check if the value is a variable - undeclared = true; - for (int j = 0; j <= variableCount; j++) { - // If the value is a variable - if (variables[j].name == *(it + 2)) { - // Set the value to the variable - undeclared = false; - variables[i].value = variables[j].value; - break; - } - } - } - } + string next = *(it + 2); + string _this = *(it + 1); + if (isInteger(next)) { + variables[_this] = atoi((next).c_str()); } - // If the variable is undeclared - if (undeclared) { - // Print an error message - cerr << "[Error] Variable undeclared." << endl; + else { + variables[_this] = variables[next]; } } \ No newline at end of file diff --git a/src/identifiers/subtraction.hpp b/src/identifiers/subtraction.hpp index e1079b5..31e84b3 100644 --- a/src/identifiers/subtraction.hpp +++ b/src/identifiers/subtraction.hpp @@ -3,37 +3,12 @@ * By lemonorangeapple **/ void subtraction(vector::iterator it, ifstream &file) { - // Check if the variable is undeclared - bool undeclared = true; - // Loop through all variables - for (int i = 0; i <= variableCount; i++) { - // Check if the variable name matches the one in the expression - if (variables[i].name == *(it + 1)) { - // Check if the value is an integer - if (isInteger(*(it + 2))) { - // If it is, set undeclared to false and subtract the value - undeclared = false; - variables[i].value -= atoi((*(it + 2)).c_str()); - break; - } - // If it isn't, check if the value is a variable - else { - // If it is, set undeclared to true and loop through all variables - undeclared = true; - for (int j = 0; j <= variableCount; j++) { - // Check if the variable name matches the one in the expression - if (variables[j].name == *(it + 2)) { - // If it does, set undeclared to false and subtract the value - undeclared = false; - variables[i].value -= variables[j].value; - break; - } - } - } - } + string next = *(it + 2); + string _this = *(it + 1); + if (isInteger(next)) { + variables[_this] -= atoi((next).c_str()); } - // If the variable is undeclared, print an error - if (undeclared) { - cerr << "[Error] Variable undeclared." << endl; + else { + variables[_this] -= variables[next]; } } diff --git a/src/init.hpp b/src/init.hpp index f741f4a..929f061 100644 --- a/src/init.hpp +++ b/src/init.hpp @@ -1,5 +1,5 @@ /** - * Entry function of the program + * Init function of the program * By lemonorangeapple **/ #include @@ -12,17 +12,13 @@ #include #include #include +#include using namespace std; #pragma GCC std("c++11") // Version of the program const string _VERSION_ = "b1.15.00"; // Buffer size const int _BUFFER_SIZE_ = 1024; -// Variable struct -struct Variable { - long double value; - string name; -}; // split function vector split(string str, char delim) { stringstream ss(str); @@ -48,9 +44,7 @@ bool isInteger(string x) { } // Const variables char buffer[_BUFFER_SIZE_]; -Variable variables[_BUFFER_SIZE_]; stack ifFlag; stack loopFlag; stack loopVariableName; -stack loopLine; -int variableCount = 0; \ No newline at end of file +stack loopLine; \ No newline at end of file diff --git a/src/interpreter.hpp b/src/interpreter.hpp index e251bfa..388351c 100644 --- a/src/interpreter.hpp +++ b/src/interpreter.hpp @@ -1,3 +1,7 @@ +/** + * Interpreter function of the program + * By lemonorangeapple +**/ void interpreter(vector vec, ifstream &file) { // Read identifiers. for (vector::iterator it = vec.begin(); it != vec.end(); ++it) { diff --git a/src/main.cpp b/src/main.cpp index 8c9c627..01f2cf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,13 +4,13 @@ **/ #include "init.hpp" map ::iterator, ifstream &)> identifiers; +map variables; #include "identifiers/identifiers.hpp" #include "read.hpp" #include "update.hpp" void load() { identifiers["output"] = output; identifiers["wrap"] = wrap; - identifiers["new"] = _new; identifiers["set"] = set; identifiers["input"] = input; identifiers["addition"] = addition; @@ -20,13 +20,13 @@ void load() { identifiers["equal"] = equal; identifiers["greater"] = _greater; identifiers["less"] = _less; - identifiers["equal_or_greater"] = equal_or_greater; - identifiers["equal_or_less"] = equal_or_less; + identifiers["equal-or-greater"] = equal_or_greater; + identifiers["equal-or-less"] = equal_or_less; identifiers["not"] = _not; identifiers["if"] = _if; - identifiers["end_if"] = end_if; + identifiers["end-if"] = end_if; identifiers["loop"] = loop; - identifiers["end_loop"] = end_loop; + identifiers["end-loop"] = end_loop; identifiers["note"] = note; } int main(int argc, char* argv[]) { diff --git a/src/read.hpp b/src/read.hpp index c15ee33..28f987a 100644 --- a/src/read.hpp +++ b/src/read.hpp @@ -1,3 +1,7 @@ +/** + * Read code function + * By lemonorangeapple +**/ #include "interpreter.hpp" // Read from file. void read(char* argv[]) { diff --git a/src/update.hpp b/src/update.hpp index 9957e01..490b9cf 100644 --- a/src/update.hpp +++ b/src/update.hpp @@ -30,7 +30,7 @@ void update() { string result = execute("curl -s https://junlang.imjcj.eu.org/VERSION_DEVE"); result = result.substr(49); // compare version number with _VERSION_ - if (result != _VERSION_ + '\n') { + if (result != _VERSION_) { cout << "Warning: Please update: \"https://junlang.imjcj.eu.org\"" << endl; } return; diff --git a/tests/test1/ioput.txt b/tests/test1/ioput.txt new file mode 100644 index 0000000..fd3f1bf --- /dev/null +++ b/tests/test1/ioput.txt @@ -0,0 +1,15 @@ +Hello world!Hello world! +Hello world!5010 +155 6 +112 +12 2 +10 +1 +2 +3 +4 +5 +6 +7 +8 +9 diff --git a/tests/test1/test.jun b/tests/test1/test.jun new file mode 100644 index 0000000..47ccf7d --- /dev/null +++ b/tests/test1/test.jun @@ -0,0 +1,31 @@ +output "Hello_world!" +output "Hello_world!" +wrap +output "Hello_world!" +set a 50 +set a 50 +output a +input a +addition a 5 +output a +input a +input b +addition a b +output a +input a +equal a 2 b +output b +input a +input b +equal a b c +if c +output "1" +end-if +set a 0 +set b 1 +loop b +output a +wrap +addition a 1 +less a 10 b +end-loop