Skip to content

Commit

Permalink
Simplify control expression and modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
redlizard committed Apr 14, 2021
1 parent 384de5f commit 2ca8d5e
Show file tree
Hide file tree
Showing 31 changed files with 313 additions and 459 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ set(CORE_SOURCES
src/degree_trig.cc
src/dxfdata.cc
src/dxfdim.cc
src/evalcontext.cc
src/evaluationsession.cc
src/export.cc
src/export_3mf.cc
Expand Down
2 changes: 0 additions & 2 deletions openscad.pro
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ HEADERS += src/version_check.h \
src/contextframe.h \
src/builtincontext.h \
src/modcontext.h \
src/evalcontext.h \
src/arguments.h \
src/children.h \
src/parameters.h \
Expand Down Expand Up @@ -449,7 +448,6 @@ SOURCES += \
src/contextframe.cc \
src/builtincontext.cc \
src/modcontext.cc \
src/evalcontext.cc \
src/arguments.cc \
src/children.cc \
src/parameters.cc \
Expand Down
17 changes: 17 additions & 0 deletions src/Assignment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,20 @@ void Assignment::print(std::ostream &stream, const std::string &indent) const
}
stream << indent << this->name << " = " << *this->expr << ";\n";
}

std::ostream &operator <<(std::ostream &stream, const AssignmentList& assignments)
{
bool first = true;
for (const auto& assignment : assignments) {
if (first) {
first = false;
} else {
stream << ", ";
}
if (!assignment->getName().empty()) {
stream << assignment->getName() << " = ";
}
stream << *assignment->getExpr();
}
return stream;
}
3 changes: 3 additions & 0 deletions src/Assignment.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <ostream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -39,3 +40,5 @@ template<class... Args> shared_ptr<Assignment> assignment(Args... args) {

typedef std::vector<shared_ptr<Assignment>> AssignmentList;
typedef std::unordered_map<std::string, const Expression*> AssignmentMap;

std::ostream &operator <<(std::ostream &stream, const AssignmentList& assignments);
19 changes: 6 additions & 13 deletions src/ModuleInstantiation.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "compiler_specific.h"
#include "context.h"
#include "ModuleInstantiation.h"
#include "evalcontext.h"
#include "expression.h"
#include "exceptions.h"
#include "printutils.h"
Expand Down Expand Up @@ -71,26 +71,19 @@ static void NOINLINE print_trace(const ModuleInstantiation *mod, const std::shar
LOG(message_group::Trace,mod->location(),ctx->documentRoot(),"called by '%1$s'",mod->name());
}

AbstractNode *ModuleInstantiation::evaluate(const std::shared_ptr<Context> ctx) const
AbstractNode *ModuleInstantiation::evaluate(const std::shared_ptr<Context> context) const
{

boost::optional<InstantiableModule> module = ctx->lookup_module(this->name(), this->loc);
boost::optional<InstantiableModule> module = context->lookup_module(this->name(), this->loc);
if (!module) {
return nullptr;
}

ContextHandle<EvalContext> c{Context::create<EvalContext>(ctx, this->arguments, this->loc, &this->scope)};
#if 0 && DEBUG
LOG(message_group::None,Location::NONE,"","New eval ctx:");
c.dump(nullptr, this);
#endif


try{
AbstractNode *node = module->module->instantiate(module->defining_context, this, c.ctx);
AbstractNode *node = module->module->instantiate(module->defining_context, this, context);
return node;
}catch(EvaluationException &e){
if(e.traceDepth>0){
print_trace(this, ctx);
print_trace(this, context);
e.traceDepth--;
}
throw;
Expand Down
2 changes: 1 addition & 1 deletion src/ModuleInstantiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ModuleInstantiation : public ASTNode

virtual void print(std::ostream &stream, const std::string &indent, const bool inlined) const;
void print(std::ostream &stream, const std::string &indent) const override { print(stream, indent, false); };
class AbstractNode *evaluate(const std::shared_ptr<Context> ctx) const;
class AbstractNode *evaluate(const std::shared_ptr<Context> context) const;

const std::string &name() const { return this->modname; }
bool isBackground() const { return this->tag_background; }
Expand Down
1 change: 0 additions & 1 deletion src/SourceFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "modcontext.h"
#include "parsersettings.h"
#include "StatCache.h"
#include "evalcontext.h"
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include "boost-utils.h"
Expand Down
11 changes: 5 additions & 6 deletions src/UserModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "UserModule.h"
#include "ModuleInstantiation.h"
#include "node.h"
#include "evalcontext.h"
#include "exceptions.h"
#include "stackcheck.h"
#include "modcontext.h"
Expand All @@ -43,21 +42,21 @@ static void NOINLINE print_err(std::string name, const Location &loc,const std::
LOG(message_group::Error,loc,ctx->documentRoot(),"Recursion detected calling module '%1$s'",name);
}

AbstractNode *UserModule::instantiate(const std::shared_ptr<Context>& ctx, const ModuleInstantiation *inst, const std::shared_ptr<EvalContext>& evalctx) const
AbstractNode* UserModule::instantiate(const std::shared_ptr<Context>& defining_context, const ModuleInstantiation *inst, const std::shared_ptr<Context>& context) const
{
if (StackCheck::inst().check()) {
print_err(inst->name(),loc,ctx);
print_err(inst->name(),loc,context);
throw RecursionException::create("module", inst->name(),loc);
return nullptr;
}

StaticModuleNameStack name{inst->name()}; // push on static stack, pop at end of method!
ContextHandle<UserModuleContext> module_context{Context::create<UserModuleContext>(
ctx,
defining_context,
this,
inst->location(),
Arguments(inst->arguments, evalctx->get_shared_ptr()),
Children(&inst->scope, evalctx->get_shared_ptr())
Arguments(inst->arguments, context),
Children(&inst->scope, context)
)};
#if 0 && DEBUG
c.dump(this, inst);
Expand Down
2 changes: 1 addition & 1 deletion src/UserModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserModule : public AbstractModule, public ASTNode
UserModule(const char *name, const class Feature& feature, const Location &loc) : AbstractModule(feature), ASTNode(loc), name(name) { }
~UserModule() {}

AbstractNode *instantiate(const std::shared_ptr<Context>& ctx, const ModuleInstantiation *inst, const std::shared_ptr<EvalContext>& evalctx) const override;
AbstractNode* instantiate(const std::shared_ptr<Context>& defining_context, const ModuleInstantiation *inst, const std::shared_ptr<Context>& context) const override;
void print(std::ostream &stream, const std::string &indent) const override;
static const std::string& stack_element(int n) { return StaticModuleNameStack::at(n); };
static int stack_size() { return StaticModuleNameStack::size(); };
Expand Down
23 changes: 23 additions & 0 deletions src/arguments.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ Arguments::Arguments(const AssignmentList& argument_expressions, const std::shar
);
}
}

std::ostream &operator<<(std::ostream &stream, const Argument& argument)
{
if (argument.name) {
stream << *argument.name << " = ";
}
stream << argument.value.toEchoString();
return stream;
}

std::ostream &operator<<(std::ostream &stream, const Arguments& arguments)
{
bool first = true;
for (const auto& argument : arguments) {
if (first) {
first = false;
} else {
stream << ", ";
}
stream << argument;
}
return stream;
}
4 changes: 4 additions & 0 deletions src/arguments.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <ostream>
#include <vector>
#include <boost/optional.hpp>

Expand Down Expand Up @@ -33,3 +34,6 @@ class Arguments : public std::vector<Argument>
private:
EvaluationSession* evaluation_session;
};

std::ostream &operator<<(std::ostream &stream, const Argument& argument);
std::ostream &operator<<(std::ostream &stream, const Arguments& arguments);
3 changes: 1 addition & 2 deletions src/builtincontext.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#define _USE_MATH_DEFINES
#include <cmath>

#include "builtincontext.h"
#include "builtin.h"
#include "builtincontext.h"
#include "expression.h"
#include "function.h"
#include "ModuleInstantiation.h"
#include "printutils.h"
#include "evalcontext.h"
#include "boost-utils.h"

BuiltinContext::BuiltinContext(EvaluationSession* session) : Context(session)
Expand Down
23 changes: 15 additions & 8 deletions src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,28 @@ const Children* Context::user_module_children() const
}
}

const Value& Context::lookup_variable(const std::string &name, bool silent, const Location &loc) const
boost::optional<const Value&> Context::try_lookup_variable(const std::string &name) const
{
if (is_config_variable(name)) {
return session()->lookup_special_variable(name, silent, loc);
return session()->try_lookup_special_variable(name);
}
for (const Context* context = this; context != nullptr; context = context->getParent().get()) {
boost::optional<const Value&> value = context->lookup_local_variable(name);
if (value) {
return *value;
boost::optional<const Value&> result = context->lookup_local_variable(name);
if (result) {
return result;
}
}
if (!silent) {
return boost::none;
}

const Value& Context::lookup_variable(const std::string &name, const Location &loc) const
{
boost::optional<const Value&> result = try_lookup_variable(name);
if (!result) {
LOG(message_group::Warning,loc,documentRoot(),"Ignoring unknown variable '%1$s'",name);
return Value::undefined;
}
return Value::undefined;
return *result;
}

boost::optional<CallableFunction> Context::lookup_function(const std::string &name, const Location &loc) const
Expand Down Expand Up @@ -117,7 +124,7 @@ std::string Context::dump(const AbstractModule *mod, const ModuleInstantiation *
if (m) {
s << " module parameters:";
for(const auto &parameter: m->parameters) {
s << boost::format(" %s = %s\n") % parameter->getName() % lookup_variable(parameter->getName());
s << boost::format(" %s = %s\n") % parameter->getName() % lookup_variable(parameter->getName(), Location::NONE);
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ class Context : public ContextFrame, public std::enable_shared_from_this<Context
virtual void init() { }

std::shared_ptr<Context> get_shared_ptr() const { return const_cast<Context*>(this)->shared_from_this(); }
const std::shared_ptr<Context> &getParent() const { return this->parent; }
virtual const class Children* user_module_children() const;

const Value& lookup_variable(const std::string &name, bool silent = false, const Location &loc=Location::NONE) const;
boost::optional<const Value&> try_lookup_variable(const std::string &name) const;
const Value& lookup_variable(const std::string &name, const Location &loc) const;
boost::optional<CallableFunction> lookup_function(const std::string &name, const Location &loc) const;
boost::optional<InstantiableModule> lookup_module(const std::string &name, const Location &loc) const;

const std::shared_ptr<Context> &getParent() const { return this->parent; }
// This modifies the semantics of the context in an error-prone way. Use with caution.
void setParent(const std::shared_ptr<Context>& parent) { this->parent = parent; }

protected:
const std::shared_ptr<Context> parent;
std::shared_ptr<Context> parent;

public:
#ifdef DEBUG
Expand Down

0 comments on commit 2ca8d5e

Please sign in to comment.