Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoft-UK committed Jan 8, 2025
1 parent 1d6400c commit 99971bd
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 21 deletions.
5 changes: 5 additions & 0 deletions examples/test.pp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
end;
end;

#define SEVEN 7
PreCalc()
begin
var ppl_numbers = \[320 / #2d];
Expand All @@ -219,6 +220,10 @@
#if __VERSION >= 202
var new = \ 2 [ 1 + pi * 4 / 2 & 2 % 7 ];
#endif
#if __VERSION >= 304
var new = \` 1 + pi * 4 / 2 & 2 % SEVEN `;
LOCAL new = \` 10.0 + SEVEN `;
#endif
end;

#PPL
Expand Down
46 changes: 42 additions & 4 deletions src/calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ static bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^');
}

static bool isExpresionValid(const std::string& expression) {
std::regex re;

re = R"([\d+\-*\/ πe%&|()]+)";
return regex_match(expression, re);
}

// Function to get the precedence of an operator
static int precedence(char op) {
if (op == '+' || op == '-') return 1;
Expand Down Expand Up @@ -268,7 +275,7 @@ static std::string convertPPLIntegerNumberToBase10(const std::string& str) {
}

// Function to convert a string with PPL-style integer number to a plain base 10 number
static void convertPPLStyleNumberToBase10(std::string& str) {
static void convertPPLStyleNumbersToBase10(std::string& str) {
std::regex re;
std::smatch match;
std::string s;
Expand All @@ -286,20 +293,51 @@ static void convertPPLStyleNumberToBase10(std::string& str) {
}


// MARK: -
// MARK: - Public Methods

bool Calc::evaluateMathExpression(std::string& str)
{
std::regex re;
std::smatch match;

if (!isExpresionValid(str)) return false;

std::string expression = str;
convertPPLStyleNumbersToBase10(expression);

expression = regex_replace(expression, std::regex(R"(e)"), "2.71828182845904523536028747135266250");
expression = regex_replace(expression, std::regex(R"(π|pi)"), "3.14159265358979323846264338327950288");

strip(expression);

expression = separateExpression(expression);
double result = evaluateExpression(expression);

std::stringstream ss;
ss << std::fixed << std::setprecision(10) << result;
expression = ss.str();
expression.erase ( expression.find_last_not_of('0') + 1, std::string::npos );
if (expression.at(expression.length() - 1) == '.') {
expression.resize(expression.length() - 1);
}
str = expression;

return true;
}

bool Calc::parse(std::string& str)
{
std::regex re;
std::smatch match;


re = R"(\\( *\d{1,2})?\[(.*)\])";

re = R"(\\( *\d{1,2})?(?:\[|`)(.*)(?:\]|`))";
while (regex_search(str, match, re)) {

std::string matched = match.str();

convertPPLStyleNumberToBase10(matched);
convertPPLStyleNumbersToBase10(matched);

std::string expression;
int scale = 0;
Expand Down
1 change: 1 addition & 0 deletions src/calc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
namespace pp {
class Calc {
public:
static bool evaluateMathExpression(std::string& str);
static bool parse(std::string& str);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bool Def::parse(std::string& str) {
}

if (match[1] == "dictionary") {
re = R"(([a-zA-Z]\w*(?:(?:[a-zA-Z_]\w*)|(?:::)|\.)*)(?:(\[#?[\dA-F]+(?::\d{0,2}[bodh])?(?:,#?[\dA-F]+(?::\d{0,2}[bodh])?)*\])|(?:=(#?[\dA-F]+(?::\d{0,2}[bodh])?)))?)";
re = R"(([a-zA-Z]\w*(?:(?:[a-zA-Z_]\w*)|(?:::)|\.)*)(?:(\[#?[\dA-F]+(?::-?\d{0,2}[bodh])?(?:,#?[\dA-F]+(?::-?\d{0,2}[bodh])?)*\])|(?:=(#?[\dA-F]+(?::-?\d{0,2}[bodh])?)))?)";
s = match[3];
for (auto it = std::sregex_iterator(s.begin(), s.end(), re); it != std::sregex_iterator(); it++) {
identity.identifier = match[4].str() + "." + it->str(1);
Expand Down
20 changes: 10 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,14 @@ uint32_t utf8_to_utf16(const char *str) {
std::string removeWhitespaceAroundOperators(const std::string& str) {
// Regular expression pattern to match spaces around the specified operators
// Operators: {}[]()≤≥≠<>=*/+-▶.,;:!^
std::regex re(R"(\s*([{}[\]()≤≥≠<>=*\/+\-▶.,;:!^&|%])\s*)");
std::regex re(R"(\s*([{}[\]()≤≥≠<>=*\/+\-▶.,;:!^&|%`])\s*)");

// Replace matches with the operator and no surrounding spaces
std::string result = std::regex_replace(str, re, "$1");

return result;
}




// MARK: - P+ To PPL Translater...
void reformatPPLLine(std::string& str) {
std::regex re;
Expand Down Expand Up @@ -188,6 +185,7 @@ void reformatPPLLine(std::string& str) {
}

str = regex_replace(str, std::regex(R"( +:)"), " :");
str = regex_replace(str, std::regex(R"(FROM)"), ":=");

strings.restoreStrings(str);
}
Expand Down Expand Up @@ -377,7 +375,8 @@ void translatePPlusLine(std::string& ln, std::ofstream& outfile) {


IFTE::parse(ln);
ln = regex_replace(ln, std::regex(R"(\.{3})"), " TO ");
ln = regex_replace(ln, std::regex(R"(\.{3}|…)"), " TO ");
ln = regex_replace(ln, std::regex(R"(\bdown TO\b)"), "DOWNTO");
}


Expand Down Expand Up @@ -488,6 +487,11 @@ void translatePPlusToPPL(const std::string& pathname, std::ofstream& outfile) {
if (!infile.is_open()) exit(2);

while (getline(infile, utf8)) {
if (preprocessor.disregard == true) {
preprocessor.parse(utf8);
continue;
}

if (isPythonBlock(utf8)) {
writePythonBlock(infile, outfile);
continue;
Expand All @@ -498,13 +502,9 @@ void translatePPlusToPPL(const std::string& pathname, std::ofstream& outfile) {
continue;
}

if (preprocessor.disregard == true) {
preprocessor.parse(utf8);
continue;
}

re = R"(\#pragma mode *\(.*\)$)";
if (std::regex_match(utf8, re)) {
writeUTF16Line(utf8 + "\n", outfile);
continue;
}

Expand Down
4 changes: 4 additions & 0 deletions src/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "preprocessor.hpp"
#include "singleton.hpp"
#include "common.hpp"
#include "calc.hpp"

#include <regex>
#include <sstream>
Expand Down Expand Up @@ -103,6 +104,9 @@ bool Preprocessor::parse(std::string& str) {
identity.scope = Aliases::Scope::Global;
identity.type = Aliases::Type::Macro;

identity.real = _singleton->aliases.resolveAllAliasesInText(identity.real);
Calc::evaluateMathExpression(identity.real);

_singleton->aliases.append(identity);
if (verbose) std::cout << MessageType::Verbose << "#define: " << identity.identifier << '\n';
return true;
Expand Down
10 changes: 5 additions & 5 deletions src/version_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#define VERSION_NUMBER "3.0.3"
#define VERSION_CODE "C0D33-25A6"
#define NUMERIC_BUILD 30333
#define INTERNAL_BUILD_CODE "C0D33"
#define DATE "2025 January 07"
#define VERSION_NUMBER "3.0.4"
#define VERSION_CODE "C0E16-25A"
#define NUMERIC_BUILD 30416
#define INTERNAL_BUILD_CODE "C0E16"
#define DATE "2025 January 08"
#define YEAR "2025"
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30333
30416

0 comments on commit 99971bd

Please sign in to comment.