2009-06-20 23:00:19 +04:00
|
|
|
/*
|
|
|
|
* OpenSCAD (www.openscad.at)
|
|
|
|
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2010-02-01 12:34:18 +03:00
|
|
|
* As a special exception, you have permission to link this program
|
|
|
|
* with the CGAL library and distribute executables, as long as you
|
|
|
|
* follow the requirements of the GNU GPL in regard to all of the
|
|
|
|
* software in the executable aside from CGAL.
|
|
|
|
*
|
2009-06-20 23:00:19 +04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "context.h"
|
|
|
|
#include "expression.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "module.h"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2010-02-23 23:47:54 +03:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
2009-06-20 23:00:19 +04:00
|
|
|
|
2009-06-26 08:32:52 +04:00
|
|
|
Context::Context(const Context *parent)
|
|
|
|
{
|
|
|
|
this->parent = parent;
|
2009-06-30 12:38:31 +04:00
|
|
|
functions_p = NULL;
|
|
|
|
modules_p = NULL;
|
2010-02-28 16:48:04 +03:00
|
|
|
usedlibs_p = NULL;
|
2010-01-13 15:24:24 +03:00
|
|
|
inst_p = NULL;
|
2010-02-23 23:47:54 +03:00
|
|
|
if (parent) document_path = parent->document_path;
|
2009-06-26 08:32:52 +04:00
|
|
|
ctx_stack.append(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Context::~Context()
|
|
|
|
{
|
|
|
|
ctx_stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2009-06-20 23:00:19 +04:00
|
|
|
void Context::args(const QVector<QString> &argnames, const QVector<Expression*> &argexpr,
|
|
|
|
const QVector<QString> &call_argnames, const QVector<Value> &call_argvalues)
|
|
|
|
{
|
|
|
|
for (int i=0; i<argnames.size(); i++) {
|
2009-06-26 08:32:52 +04:00
|
|
|
set_variable(argnames[i], i < argexpr.size() && argexpr[i] ? argexpr[i]->evaluate(this->parent) : Value());
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int posarg = 0;
|
|
|
|
for (int i=0; i<call_argnames.size(); i++) {
|
|
|
|
if (call_argnames[i].isEmpty()) {
|
2009-11-22 12:26:22 +03:00
|
|
|
if (posarg < argnames.size())
|
|
|
|
set_variable(argnames[posarg++], call_argvalues[i]);
|
2009-06-20 23:00:19 +04:00
|
|
|
} else {
|
2009-06-26 08:32:52 +04:00
|
|
|
set_variable(call_argnames[i], call_argvalues[i]);
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-26 08:32:52 +04:00
|
|
|
QVector<const Context*> Context::ctx_stack;
|
|
|
|
|
|
|
|
void Context::set_variable(QString name, Value value)
|
|
|
|
{
|
|
|
|
if (name.startsWith("$"))
|
|
|
|
config_variables[name] = value;
|
|
|
|
else
|
|
|
|
variables[name] = value;
|
|
|
|
}
|
|
|
|
|
2009-07-20 21:45:39 +04:00
|
|
|
Value Context::lookup_variable(QString name, bool silent) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2009-06-26 08:32:52 +04:00
|
|
|
if (name.startsWith("$")) {
|
|
|
|
for (int i = ctx_stack.size()-1; i >= 0; i--) {
|
|
|
|
if (ctx_stack[i]->config_variables.contains(name))
|
|
|
|
return ctx_stack[i]->config_variables[name];
|
|
|
|
}
|
|
|
|
return Value();
|
|
|
|
}
|
2009-06-20 23:00:19 +04:00
|
|
|
if (variables.contains(name))
|
|
|
|
return variables[name];
|
|
|
|
if (parent)
|
2009-07-24 22:18:56 +04:00
|
|
|
return parent->lookup_variable(name, silent);
|
2009-07-20 21:45:39 +04:00
|
|
|
if (!silent)
|
2010-02-10 16:13:10 +03:00
|
|
|
PRINTA("WARNING: Ignoring unknown variable '%1'.", name);
|
2009-06-20 23:00:19 +04:00
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
Value Context::evaluate_function(QString name, const QVector<QString> &argnames, const QVector<Value> &argvalues) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2009-06-30 12:38:31 +04:00
|
|
|
if (functions_p && functions_p->contains(name))
|
2009-06-20 23:00:19 +04:00
|
|
|
return functions_p->value(name)->evaluate(this, argnames, argvalues);
|
2010-02-28 16:48:04 +03:00
|
|
|
if (usedlibs_p) {
|
|
|
|
QHashIterator<QString, Module*> i(*usedlibs_p);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value()->functions.contains(name)) {
|
|
|
|
Module *lib = i.value();
|
|
|
|
Context ctx(parent);
|
|
|
|
ctx.functions_p = &lib->functions;
|
|
|
|
ctx.modules_p = &lib->modules;
|
|
|
|
ctx.usedlibs_p = &lib->usedlibs;
|
|
|
|
for (int j = 0; j < lib->assignments_var.size(); j++) {
|
|
|
|
ctx.set_variable(lib->assignments_var[j], lib->assignments_expr[j]->evaluate(&ctx));
|
|
|
|
}
|
|
|
|
return i.value()->functions.value(name)->evaluate(&ctx, argnames, argvalues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-20 23:00:19 +04:00
|
|
|
if (parent)
|
|
|
|
return parent->evaluate_function(name, argnames, argvalues);
|
2009-06-30 00:30:43 +04:00
|
|
|
PRINTA("WARNING: Ignoring unkown function '%1'.", name);
|
2009-06-20 23:00:19 +04:00
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
AbstractNode *Context::evaluate_module(const ModuleInstantiation *inst) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
if (modules_p && modules_p->contains(inst->modname))
|
|
|
|
return modules_p->value(inst->modname)->evaluate(this, inst);
|
2010-02-28 16:48:04 +03:00
|
|
|
if (usedlibs_p) {
|
|
|
|
QHashIterator<QString, Module*> i(*usedlibs_p);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value()->modules.contains(inst->modname)) {
|
|
|
|
Module *lib = i.value();
|
|
|
|
Context ctx(parent);
|
|
|
|
ctx.functions_p = &lib->functions;
|
|
|
|
ctx.modules_p = &lib->modules;
|
|
|
|
ctx.usedlibs_p = &lib->usedlibs;
|
|
|
|
for (int j = 0; j < lib->assignments_var.size(); j++) {
|
|
|
|
ctx.set_variable(lib->assignments_var[j], lib->assignments_expr[j]->evaluate(&ctx));
|
|
|
|
}
|
|
|
|
return i.value()->modules.value(inst->modname)->evaluate(&ctx, inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-20 23:00:19 +04:00
|
|
|
if (parent)
|
2009-07-01 13:48:25 +04:00
|
|
|
return parent->evaluate_module(inst);
|
|
|
|
PRINTA("WARNING: Ignoring unkown module '%1'.", inst->modname);
|
2009-06-20 23:00:19 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-07 02:48:46 +04:00
|
|
|
/*!
|
|
|
|
Returns the absolute path to the given filename, unless it's empty.
|
|
|
|
*/
|
2010-02-23 23:47:54 +03:00
|
|
|
QString Context::get_absolute_path(const QString &filename) const
|
|
|
|
{
|
2010-07-07 02:48:46 +04:00
|
|
|
if (!filename.isEmpty()) {
|
|
|
|
return QFileInfo(QDir(this->document_path), filename).absoluteFilePath();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return filename;
|
|
|
|
}
|
2010-02-23 23:47:54 +03:00
|
|
|
}
|
|
|
|
|