From 5f3cb0bf02ed32976370cf6c7f656d000b4d7694 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 15 Jun 2012 11:26:32 +1000 Subject: Re-write heaps of code to work better. --- impl/main.cpp | 84 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 34 deletions(-) (limited to 'impl/main.cpp') diff --git a/impl/main.cpp b/impl/main.cpp index 530556f..b03a4f2 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -1,15 +1,12 @@ #include #include -#include -#include #include -#include - -#include "Operator.hpp" +#include "Complete.hpp" #include "Expression.hpp" -#include "MaxStrategy.hpp" +#include "Operator.hpp" #include "EquationSystem.hpp" -#include "Complete.hpp" +#include "MaxStrategy.hpp" +#include "FixpointAlgorithm.hpp" extern "C" { #include "EquationSystemParser.h" @@ -19,36 +16,35 @@ extern "C" { using namespace std; template -Expression* treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& system) { +Expression& treeToExpression(pANTLR3_BASE_TREE node, ConcreteEquationSystem& system) { ANTLR3_UINT32 num = node->getChildCount(node); - string name = (char*) node->getText(node)->chars; - // leaf node - constant or variable if (num == 0) { + // leaf node -> constant or variable if (name == "inf") { - return system.newExpression(new Constant(infinity())); + return system.constant(infinity()); } else { stringstream stream(name); T output; if (stream >> output) { - return system.newExpression(new Constant(output)); + return system.constant(output); } else { - return system.newExpression(system.newVariable(name)); + return system.variable(name); } } } - // other operators + // anything that's not a constant/variable std::vector*> args; pANTLR3_BASE_TREE childNode; for (unsigned int i = 0; i < num; ++i) { childNode = (pANTLR3_BASE_TREE) node->getChild(node, i); - args.push_back(treeToExpression(childNode, system)); + args.push_back(&treeToExpression(childNode, system)); } if (name == "max") { - return system.newMaxExpression(args); + return system.maxExpression(args); } else { Operator* op = NULL; if (name == "min") { @@ -56,21 +52,24 @@ Expression* treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& syste } else if (name == "+") { op = new Addition(); } else if (name == "-") { - op = new Subtraction(); + if (args.size() == 1) { + op = new Negation(); + } else { + op = new Subtraction(); + } } else if (name == ";") { op = new Comma(); } else if (name == "GUARD") { op = new Guard(); } else { - std::cerr << "Unknown leaf node type: " << name << std::endl; - throw "Unknown leaf node type"; + throw "Parse error: Unknown operator"; } - return system.newExpression(op, args); + return system.expression(op, args); } } template -void treeToSystem(pANTLR3_BASE_TREE node, EquationSystem& system) { +void treeToSystem(pANTLR3_BASE_TREE node, ConcreteEquationSystem& system) { ANTLR3_UINT32 num = node->getChildCount(node); if (num % 2 == 1) @@ -83,12 +82,12 @@ void treeToSystem(pANTLR3_BASE_TREE node, EquationSystem& system) { exprNode = (pANTLR3_BASE_TREE) node->getChild(node, i+1); string varName = (char*) varNode->getText(varNode)->chars; - Variable* var = system.newVariable(varName); + Variable& var = system.variable(varName); vector*> args; - args.push_back(system.newExpression(new Constant(-infinity()))); - args.push_back(treeToExpression(exprNode, system)); - system[*var] = system.newMaxExpression(args); + args.push_back(&system.constant(-infinity())); + args.push_back(&treeToExpression(exprNode, system)); + system[var] = &system.maxExpression(args); } } @@ -96,11 +95,11 @@ typedef Complete ZBar; int main (int argc, char* argv[]) { FixpointAlgorithm* algorithm = NULL; if (argc > 2 && !strcmp(argv[2], "naive")) { - algorithm = new NaiveFixpoint(); + algorithm = new NaiveFixpointAlgorithm(); cout << "Naive fixpoint" << endl; } else { - algorithm = new RecursiveFixpoint(); - cout << "Recursive fixpoint" << endl; + algorithm = new SmartFixpointAlgorithm(); + cout << "Smart fixpoint" << endl; } pANTLR3_INPUT_STREAM input; @@ -115,16 +114,33 @@ int main (int argc, char* argv[]) { EquationSystemParser_equation_system_return ret = parser -> equation_system(parser); - EquationSystem system; + ConcreteEquationSystem system; treeToSystem(ret.tree, system); // DO MORE HERE - VariableAssignment rho = system.minFixpoint(*algorithm); - std::cout << rho << std::endl; + cout << system << endl; + VariableAssignment* prev = NULL; + VariableAssignment* result = system.assignment(-infinity()); + MaxStrategy strategy(system); + do { + if (prev) delete prev; + prev = result; + strategy.improve(*prev); + for (unsigned int i = 0, size = system.variableCount(); i < size; ++i) { + Variable& var = system.variable(i); + cout << var.name() << " = " << (*result)[var] << ", "; + } + cout << endl; + result = algorithm->maxFixpoint(strategy); + } while(!system.equalAssignments(*prev, *result)); + if (prev) delete prev; + + VariableAssignment* rho = result; - const std::vector*> vars = system.vars(); - for (unsigned int i = 0, size = vars.size(); i < size; ++i) { - cout << vars[i]->name() << " = " << rho[*vars[i]] << endl; + cout << endl; + for (unsigned int i = 0, size = system.variableCount(); i < size; ++i) { + Variable& var = system.variable(i); + cout << var.name() << " = " << (*rho)[var] << endl; } parser -> free(parser); -- cgit v1.2.3