2018-06-06 03:03:06 +08:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include "emb.h"
|
2018-06-07 04:53:52 +08:00
|
|
|
#include "pybindings.h"
|
|
|
|
#include "ui_mainwindow.h"
|
2018-06-06 03:03:06 +08:00
|
|
|
|
2018-06-11 00:25:23 +08:00
|
|
|
MainWindow::MainWindow(Design *_design, QWidget *parent)
|
|
|
|
: QMainWindow(parent), ui(new Ui::MainWindow), design(_design)
|
2018-06-06 03:03:06 +08:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
PyImport_ImportModule("emb");
|
2018-06-07 04:53:52 +08:00
|
|
|
|
|
|
|
write = [this](std::string s) {
|
2018-06-11 21:34:01 +08:00
|
|
|
ui->plainTextEdit->moveCursor(QTextCursor::End);
|
|
|
|
ui->plainTextEdit->insertPlainText(s.c_str());
|
|
|
|
ui->plainTextEdit->moveCursor(QTextCursor::End);
|
2018-06-06 03:03:06 +08:00
|
|
|
};
|
|
|
|
emb::set_stdout(write);
|
2018-06-11 00:25:23 +08:00
|
|
|
std::string title = "nextpnr-ice40 - " + design->chip.getChipName();
|
|
|
|
setWindowTitle(title.c_str());
|
2018-06-06 03:03:06 +08:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:53:52 +08:00
|
|
|
MainWindow::~MainWindow() { delete ui; }
|
2018-06-06 03:03:06 +08:00
|
|
|
|
2018-06-07 04:53:52 +08:00
|
|
|
void handle_system_exit() { exit(-1); }
|
2018-06-06 03:03:06 +08:00
|
|
|
|
|
|
|
int MainWindow::executePython(std::string command)
|
|
|
|
{
|
|
|
|
PyObject *m, *d, *v;
|
|
|
|
m = PyImport_AddModule("__main__");
|
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
|
|
|
d = PyModule_GetDict(m);
|
2018-06-07 04:53:52 +08:00
|
|
|
v = PyRun_StringFlags(command.c_str(),
|
|
|
|
(command.empty() ? Py_file_input : Py_single_input),
|
|
|
|
d, d, NULL);
|
|
|
|
if (v == NULL) {
|
2018-06-06 03:03:06 +08:00
|
|
|
PyObject *exception, *v, *tb;
|
|
|
|
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
|
|
|
|
handle_system_exit();
|
|
|
|
}
|
|
|
|
PyErr_Fetch(&exception, &v, &tb);
|
|
|
|
if (exception == NULL)
|
|
|
|
return 0;
|
|
|
|
PyErr_NormalizeException(&exception, &v, &tb);
|
|
|
|
if (tb == NULL) {
|
|
|
|
tb = Py_None;
|
|
|
|
Py_INCREF(tb);
|
|
|
|
}
|
|
|
|
PyException_SetTraceback(v, tb);
|
|
|
|
if (exception == NULL)
|
|
|
|
return 0;
|
|
|
|
PyErr_Clear();
|
|
|
|
|
2018-06-07 04:53:52 +08:00
|
|
|
PyObject *objectsRepresentation = PyObject_Str(v);
|
2018-06-11 21:34:01 +08:00
|
|
|
std::string errorStr =
|
|
|
|
PyUnicode_AsUTF8(objectsRepresentation) + std::string("\n");
|
|
|
|
ui->plainTextEdit->moveCursor(QTextCursor::End);
|
|
|
|
ui->plainTextEdit->insertPlainText(errorStr.c_str());
|
|
|
|
ui->plainTextEdit->moveCursor(QTextCursor::End);
|
2018-06-06 03:03:06 +08:00
|
|
|
Py_DECREF(objectsRepresentation);
|
|
|
|
Py_XDECREF(exception);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Py_DECREF(v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_lineEdit_returnPressed()
|
|
|
|
{
|
2018-06-07 04:53:52 +08:00
|
|
|
std::string input = ui->lineEdit->text().toStdString();
|
2018-06-11 21:34:01 +08:00
|
|
|
ui->plainTextEdit->moveCursor(QTextCursor::End);
|
|
|
|
ui->plainTextEdit->insertPlainText(
|
|
|
|
std::string(">>> " + input + "\n").c_str());
|
|
|
|
ui->plainTextEdit->moveCursor(QTextCursor::End);
|
2018-06-06 03:03:06 +08:00
|
|
|
ui->plainTextEdit->update();
|
|
|
|
ui->lineEdit->clear();
|
|
|
|
int error = executePython(input);
|
|
|
|
}
|