Skip to content

Commit

Permalink
parse now always returns the module pointer to give the dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
nophead committed Mar 1, 2017
1 parent 433076b commit 8352592
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/MainWindow.h
Expand Up @@ -42,7 +42,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindow

ModuleContext top_ctx;
FileModule *root_module; // Result of parsing
FileModule *last_good_module; // Last successful parse for include list
FileModule *parsed_module; // Last parse for include list
ModuleInstantiation root_inst; // Top level instance
AbstractNode *absolute_root_node; // Result of tree evaluation
AbstractNode *root_node; // Root if the root modifier (!) is used
Expand Down
14 changes: 5 additions & 9 deletions src/ModuleCache.cc
Expand Up @@ -60,7 +60,7 @@ time_t ModuleCache::evaluate(const std::string &filename, FileModule *&module)
// Initialize entry, if new
if (!found) {
entry.module = nullptr;
entry.last_good_module = nullptr;
entry.parsed_module = nullptr;
entry.cache_id = cache_id;
entry.includes_mtime = st.st_mtime;
}
Expand All @@ -72,8 +72,8 @@ time_t ModuleCache::evaluate(const std::string &filename, FileModule *&module)
if (entry.cache_id == cache_id) {
shouldCompile = false;
// Recompile if includes changed
if (entry.last_good_module) {
time_t mtime = entry.last_good_module->includesChanged();
if (entry.parsed_module) {
time_t mtime = entry.parsed_module->includesChanged();
if (mtime > entry.includes_mtime) {
entry.includes_mtime = mtime;
shouldCompile = true;
Expand Down Expand Up @@ -112,13 +112,9 @@ time_t ModuleCache::evaluate(const std::string &filename, FileModule *&module)
print_messages_push();

fs::path pathname = fs::path(filename);
lib_mod = parse(textbuf.str().c_str(), pathname, false);
delete entry.parsed_module;
lib_mod = parse(entry.parsed_module, textbuf.str().c_str(), pathname, false) ? entry.parsed_module : nullptr;
PRINTDB(" compiled module: %p", lib_mod);

if (lib_mod) { // parse successful
delete entry.last_good_module;
entry.last_good_module = lib_mod;
}
entry.module = lib_mod;
entry.cache_id = cache_id;

Expand Down
2 changes: 1 addition & 1 deletion src/ModuleCache.h
Expand Up @@ -24,7 +24,7 @@ class ModuleCache

struct cache_entry {
class FileModule *module;
class FileModule *last_good_module; // the last version that parsed correctly for include list
class FileModule *parsed_module; // the last version parsed for the include list
std::string cache_id;
time_t mtime; // time file last modified
time_t includes_mtime; // time the includes last changed
Expand Down
19 changes: 8 additions & 11 deletions src/mainwin.cc
Expand Up @@ -237,7 +237,7 @@ MainWindow::MainWindow(const QString &filename)
top_ctx.registerBuiltin();

root_module = NULL;
last_good_module = NULL;
parsed_module = NULL;
absolute_root_node = NULL;
#ifdef ENABLE_CGAL
this->cgalRenderer = NULL;
Expand Down Expand Up @@ -733,9 +733,9 @@ void MainWindow::updateReorderMode(bool reorderMode)

MainWindow::~MainWindow()
{
// If root_module is not null then it will be the same as last_good_module,
// If root_module is not null then it will be the same as parsed_module,
// so no need to delete it.
delete last_good_module;
delete parsed_module;
delete root_node;
#ifdef ENABLE_CGAL
this->root_geom.reset();
Expand Down Expand Up @@ -972,8 +972,8 @@ void MainWindow::compile(bool reload, bool forcedone)
shouldcompiletoplevel = true;
}

if (!shouldcompiletoplevel && this->last_good_module) {
time_t mtime = this->last_good_module->includesChanged();
if (!shouldcompiletoplevel && this->parsed_module) {
time_t mtime = this->parsed_module->includesChanged();
if (mtime > this->includes_mtime) {
this->includes_mtime = mtime;
shouldcompiletoplevel = true;
Expand Down Expand Up @@ -1761,12 +1761,9 @@ void MainWindow::compileTopLevelDocument()

auto fnameba = this->fileName.toLocal8Bit();
const char* fname = this->fileName.isEmpty() ? "" : fnameba;
this->root_module = parse(fulltext.c_str(), fs::path(fname), false);
if (this->root_module) {
delete this->last_good_module;
this->last_good_module = this->root_module;
}

delete this->parsed_module;
this->root_module = parse(this->parsed_module, fulltext.c_str(), fs::path(fname), false) ? this->parsed_module : NULL;

if (Feature::ExperimentalCustomizer.is_enabled()) {
if (this->root_module!=NULL) {
//add parameters as annotation in AST
Expand Down
5 changes: 4 additions & 1 deletion src/openscad.cc
Expand Up @@ -397,7 +397,10 @@ int cmdline(const char *deps_output_file, const std::string &filename, Camera &c
std::string text((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
text += "\n" + commandline_commands;
fs::path abspath = fs::absolute(filename);
root_module = parse(text.c_str(), abspath, false);
if(!parse(root_module, text.c_str(), abspath, false)) {
delete root_module; // parse failed
root_module = NULL;
}
if (!root_module) {
PRINTB("Can't parse file '%s'!\n", filename.c_str());
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/openscad.h
Expand Up @@ -28,7 +28,7 @@

#include <boost/filesystem.hpp>

extern class FileModule *parse(const char *text, const boost::filesystem::path &filename, int debug);
extern bool parse(class FileModule *&module, const char *text, const boost::filesystem::path &filename, int debug);

#include <string>
extern std::string commandline_commands;
Expand Down
7 changes: 4 additions & 3 deletions src/parser.y
Expand Up @@ -628,7 +628,7 @@ void yyerror (char const *s)
sourcefile() % lexerget_lineno() % s);
}

FileModule *parse(const char *text, const fs::path &filename, int debug)
bool parse(FileModule *&module, const char *text, const fs::path &filename, int debug)
{
lexerin = NULL;
parser_error_pos = -1;
Expand All @@ -645,9 +645,10 @@ FileModule *parse(const char *text, const fs::path &filename, int debug)
lexerdestroy();
lexerlex_destroy();

if (parserretval != 0) return NULL;
module = rootmodule;
if (parserretval != 0) return false;

parser_error_pos = -1;
scope_stack.pop();
return rootmodule;
return true;
}
5 changes: 4 additions & 1 deletion test-code/exportdxf.cc
Expand Up @@ -151,7 +151,10 @@ int main(int argc, char **argv)
text += buffer;
}
fclose(fp);
root_module = parse((text+commandline_commands).toAscii().data(), fileInfo.absolutePath().toLocal8Bit(), false);
if(!parse(root_module, (text+commandline_commands).toAscii().data(), fileInfo.absolutePath().toLocal8Bit(), false)) {
delete root_module; // parse failed
root_module = NULL;
}
if (!root_module) {
exit(1);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/tests-common.cc
Expand Up @@ -30,7 +30,10 @@ FileModule *parsefile(const char *filename, const char *fakepath)
std::string pathname;
if (fakepath) pathname = fakepath;
else pathname = fs::path(filename).parent_path().generic_string();
root_module = parse(text.c_str(), pathname.c_str(), false);
if(!parse(root_module, text.c_str(), pathname.c_str(), false)) {
delete root_module; // parse failed
root_module = NULL;
}
if (root_module) {
root_module->handleDependencies();
}
Expand Down

0 comments on commit 8352592

Please sign in to comment.