Skip to content

Commit

Permalink
bugfixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
treefrogframework committed Apr 14, 2024
1 parent b761242 commit fe3095b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
3 changes: 1 addition & 2 deletions codegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ static QString modifyCode(const QString &code, bool safeCode = false)

QString CodeGenerator::generateMainFunc(bool safety) const
{
const QRegularExpression re(" main *(.*)");
QString src;

if (_code.contains(re)) {
if (_code.contains(QRegularExpression(" main\\s*\\("))) {
src += _headers;
src += "\n";
src += _code;
Expand Down
46 changes: 27 additions & 19 deletions compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
using namespace cpi;


const QMap<QString, QString> requiredOptions = {
{"gcc", "-xc"},
{"g++", "-xc++"},
{"clang", "-xc"},
{"clang++", "-xc++"},
{"cl.exe", "/nologo /EHsc"},
{"cl", "/nologo /EHsc"},
const QMap<QString, QStringList> requiredOptions = {
{"gcc", {"-xc"}},
{"g++", {"-xc++"}},
{"clang", {"-xc"}},
{"clang++", {"-xc++"}},
{"cl.exe", {"-nologo", "-EHsc"}},
{"cl", {"-nologo", "-EHsc"}},
};


Expand Down Expand Up @@ -112,7 +112,7 @@ bool Compiler::compile(const QString &cc, const QStringList &options, const QStr
temp.write(qPrintable(_sourceCode));
temp.close();
}
ccOptions << "/Fo" + objtemp.fileName();
ccOptions << "-Fo" + objtemp.fileName();
ccOptions << temp.fileName();
#endif

Expand All @@ -134,14 +134,16 @@ bool Compiler::compile(const QString &cc, const QStringList &options, const QStr
compileProc.waitForBytesWritten();
compileProc.closeWriteChannel();
#endif

compileProc.waitForFinished();
_compileError = QString::fromLocal8Bit(compileProc.readAllStandardError());

#ifdef Q_CC_MSVC
_compileError = QString::fromLocal8Bit(compileProc.readAllStandardOutput());
objtemp.remove();
temp.remove();
#else
_compileError = QString::fromLocal8Bit(compileProc.readAllStandardError());
#endif
//qDebug() << "#" << _compileError << "#";

return (compileProc.exitStatus() == QProcess::NormalExit && compileProc.exitCode() == 0);
}
Expand All @@ -165,12 +167,12 @@ int Compiler::compileAndExecute(const QString &cc, const QStringList &options, c
}
}

QString ccopt = requiredOptions.value(QFileInfo(cc).fileName());
QStringList ccopt = requiredOptions.value(QFileInfo(cc).fileName());
if (!ccopt.isEmpty()) {
ccOpts << ccopt;
}
#ifdef Q_CC_MSVC
ccOpts << "/Fe:" + aoutName();
ccOpts << "-Fe:" + aoutName();
#else
ccOpts << "-o";
ccOpts << aoutName();
Expand All @@ -186,20 +188,21 @@ int Compiler::compileAndExecute(const QString &cc, const QStringList &options, c
exe.start(aoutName(), cppsArgs);
exe.waitForStarted();

auto readfunc = [&]() {
auto readStdInput = [&]() {
// read and write to the process
std::string s;
if (std::getline(std::cin, s)) {
QString line = QString::fromStdString(s) + "\n";
exe.write(line.toLocal8Bit());
auto line = QByteArray::fromStdString(s);
line += "\n";
exe.write(line.data());
} else {
exe.closeWriteChannel();
}
};

#ifndef Q_OS_WIN
QSocketNotifier notifier(fileno(stdin), QSocketNotifier::Read);
QObject::connect(&notifier, &QSocketNotifier::activated, readfunc);
QObject::connect(&notifier, &QSocketNotifier::activated, readStdInput);
#endif

while (!exe.waitForFinished(50)) {
Expand All @@ -212,7 +215,7 @@ int Compiler::compileAndExecute(const QString &cc, const QStringList &options, c
#ifdef Q_OS_WIN
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
if (WaitForSingleObject(h, 50) == WAIT_OBJECT_0) {
readfunc();
readStdInput();
}
#endif
if (exe.state() != QProcess::Running) {
Expand Down Expand Up @@ -300,6 +303,7 @@ void Compiler::printLastCompilationError() const
void Compiler::printContextCompilationError() const
{
static auto printMessage = [](const QString &msg) {
// output after the first colon
int idx = msg.indexOf(": ");
if (idx > 0) {
auto s = msg.mid(idx + 1);
Expand All @@ -311,11 +315,15 @@ void Compiler::printContextCompilationError() const

if (_sourceCode.endsWith(';') || _sourceCode.endsWith('}')) {
// print error
#ifdef Q_CC_MSVC
printMessage(_compileError);
#else
auto errs = _compileError.split("\n");
if (!errs.value(0).contains("int main()")) {
printMessage(errs.value(0));
printMessage(errs.value(0) + errs.value(1));
} else {
printMessage(errs.value(1));
printMessage(errs.value(1) + errs.value(2));
}
#endif
}
}
31 changes: 17 additions & 14 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ constexpr auto CPI_VERSION_STR = "2.1.0";
#ifdef Q_CC_MSVC
constexpr auto DEFAULT_CONFIG = "[General]\n"
"CXX=cl.exe\n"
"CXXFLAGS=/std:c++20\n"
"CXXFLAGS=-std:c++20\n"
"LDFLAGS=\n"
"COMMON_INCLUDES=\n";
#else
Expand Down Expand Up @@ -254,9 +254,11 @@ static void compile()

if (cpl) {
compiler.printContextCompilationError();
// delete last line
if (lastLineNumber > 0) {
deleteLine(lastLineNumber);
if (!code.join("\n").contains(QRegularExpression(" main\\s*\\("))) {
// delete last line
if (lastLineNumber > 0) {
deleteLine(lastLineNumber);
}
}
}
};
Expand Down Expand Up @@ -304,7 +306,8 @@ static int interpreter()
return;
}

if (line == ".quit" || line == ".q") {
QString cmd = line.trimmed();
if (cmd == ".quit" || cmd == ".q") {
end = true;
return;
}
Expand All @@ -317,25 +320,25 @@ static int interpreter()
QByteArray prompt {"cpi> "};
} promptOut;

if (line == ".help" || line == "?") { // shows help
if (cmd == ".help" || cmd == "?") { // shows help
showHelp();
return;
}

if (line == ".show" || line == ".code") { // shows code
if (cmd == ".show" || cmd == ".code") { // shows code
showCode();
return;
}

if (line == ".conf") { // shows configs
if (cmd == ".conf") { // shows configs
showConfigs(*conf);
return;
}

if (line.startsWith(".del ") || line.startsWith(".rm ")) { // Deletes code
int n = line.indexOf(' ');
line.remove(0, n + 1);
const QStringList list = line.split(QRegularExpression("[,\\s]"), SkipEmptyParts);
if (cmd.startsWith(".del ") || cmd.startsWith(".rm ")) { // Deletes code
int n = cmd.indexOf(' ');
cmd.remove(0, n + 1);
const QStringList list = cmd.split(QRegularExpression("[,\\s]"), SkipEmptyParts);

std::list<int> numbers; // line-numbers
for (auto &s : list) {
Expand All @@ -349,7 +352,7 @@ static int interpreter()
return;
}

if (line == ".clear") {
if (cmd == ".clear") {
headers.clear();
code.clear();
lastLineNumber = 0;
Expand All @@ -366,7 +369,7 @@ static int interpreter()
}
}

if (waitForReadyStdInputRead(20)) {
if (waitForReadyStdInputRead(40)) {
// continue reading
promptOut.off();
return;
Expand Down

0 comments on commit fe3095b

Please sign in to comment.