From ea05c9c5fa30b8822f618e861d12a09df1f8f017 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Mon, 28 May 2012 13:00:50 +1000 Subject: Fix memory error and x = max(-inf, expr) stuff. --- impl/main.cpp | 58 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'impl/main.cpp') diff --git a/impl/main.cpp b/impl/main.cpp index c5a0be6..abd27ba 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -9,6 +9,7 @@ #include "Expression.hpp" #include "MaxStrategy.hpp" #include "EquationSystem.hpp" +#include "Complete.hpp" extern "C" { #include "EquationSystemParser.h" @@ -17,26 +18,24 @@ extern "C" { using namespace std; -typedef std::vector*> Args; -typedef Expression E; -typedef Constant C; -typedef Addition A; -typedef Minimum Min; - template -Expression* treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& system) { +Expression* treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& system, bool topLevel=true) { ANTLR3_UINT32 num = node->getChildCount(node); string name = (char*) node->getText(node)->chars; // leaf node - constant or variable if (num == 0) { - stringstream stream(name); - T output; - if (stream >> output) { - return system.newExpression(new Constant(output), vector*>()); + if (name == "inf") { + return system.newExpression(new Constant(infinity())); } else { - return system.newExpression(system.newVariable(name), vector*>()); + stringstream stream(name); + T output; + if (stream >> output) { + return system.newExpression(new Constant(output)); + } else { + return system.newExpression(system.newVariable(name)); + } } } @@ -45,7 +44,7 @@ Expression* treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& syste 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, false)); } if (name == "max") { @@ -58,6 +57,13 @@ Expression* treeToExpression(pANTLR3_BASE_TREE node, EquationSystem& syste op = new Addition(); } else if (name == "-") { 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"; } return system.newExpression(op, args); } @@ -79,12 +85,24 @@ void treeToSystem(pANTLR3_BASE_TREE node, EquationSystem& system) { string varName = (char*) varNode->getText(varNode)->chars; Variable* var = system.newVariable(varName); - system[*var] = treeToExpression(exprNode, system); + vector*> args; + args.push_back(system.newExpression(new Constant(-infinity()))); + args.push_back(treeToExpression(exprNode, system)); + system[*var] = system.newMaxExpression(args); } } -std::vector< Expression* > empty; +typedef Complete ZBar; int main (int argc, char* argv[]) { + FixpointAlgorithm* algorithm = NULL; + if (argc > 2 && !strcmp(argv[2], "naive")) { + algorithm = new NaiveFixpoint(); + cout << "Naive fixpoint" << endl; + } else { + algorithm = new RecursiveFixpoint(); + cout << "Recursive fixpoint" << endl; + } + pANTLR3_INPUT_STREAM input; pEquationSystemLexer lex; pANTLR3_COMMON_TOKEN_STREAM tokens; @@ -97,12 +115,14 @@ int main (int argc, char* argv[]) { EquationSystemParser_equation_system_return ret = parser -> equation_system(parser); - EquationSystem system; - treeToSystem(ret.tree, system); + EquationSystem system; + treeToSystem(ret.tree, system); // DO MORE HERE - VariableAssignment rho = system.minFixpoint(); - const std::vector*> vars = system.vars(); + VariableAssignment rho = system.minFixpoint(*algorithm); + std::cout << rho << std::endl; + + 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; } -- cgit v1.2.3