diff --git a/Parser/FunctionManager.cpp b/Parser/FunctionManager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c9dc8e713546606eb41d3642476132be982de7d --- /dev/null +++ b/Parser/FunctionManager.cpp @@ -0,0 +1,84 @@ +#include "FunctionManager.h" +#include <stdio.h> +#include <stack> +#include <map> + +struct ltstr +{ + bool operator()(const char* s1, const char* s2) const + { + return strcmp(s1, s2) < 0; + } +}; + +class File_Position +{ + public : + fpos_t position; + FILE *file; +}; + +class mystack +{ +public: + std::stack<File_Position> s; +}; +class mymap +{ +public : + std::map<char*,File_Position,ltstr> m; +}; + +FunctionManager *FunctionManager::instance = 0; + +FunctionManager::FunctionManager() +{ + functions = new mymap; + calls = new mystack; +} + +FunctionManager* FunctionManager::Instance() +{ + if(!instance) + { + instance = new FunctionManager; + } + return instance; +} + +bool FunctionManager::enterFunction(char *name, FILE **f) const +{ + if(functions->m.find(name) == functions->m.end())return false; + File_Position fpold; + fpold.file = *f; + fgetpos(fpold.file,&fpold.position); + calls->s.push(fpold); + File_Position fp = (functions->m)[name]; + fsetpos(fp.file,&fp.position); + *f = fp.file; + return true; +} + +bool FunctionManager::leaveFunction(FILE **f) +{ + if(!calls->s.size())return false; + File_Position fp; + fp = calls->s.top(); + calls->s.pop(); + fsetpos(fp.file,&fp.position); + *f = fp.file; + return true; +} + +bool FunctionManager::createFunction(char *name, FILE *f) +{ + File_Position fp; + fp.file = f; + fgetpos(fp.file,&fp.position); + (functions->m)[name] = fp; + return true; +} + + + + diff --git a/Parser/FunctionManager.h b/Parser/FunctionManager.h new file mode 100644 index 0000000000000000000000000000000000000000..6961f6dd17051dc55728412a901eab3923eba890 --- /dev/null +++ b/Parser/FunctionManager.h @@ -0,0 +1,25 @@ +#ifndef _FUNCTION_MANAGER_H_ +#define _FUNCTION_MANAGER_H_ + +class mystack; +class mymap; +#include <stdio.h> +/* + Singleton, one function manager for + all parsers. +*/ + +class FunctionManager +{ + mymap *functions; + mystack *calls; + FunctionManager (); + static FunctionManager *instance; + public : + static FunctionManager* Instance(); + bool enterFunction (char *name, FILE **f) const; + bool createFunction (char *name, FILE *f); + bool leaveFunction (FILE **f); +}; + +#endif diff --git a/examples/function.geo b/examples/function.geo new file mode 100644 index 0000000000000000000000000000000000000000..000e7e6b64eb65a66d0ede9c7b401e6ef45c58cf --- /dev/null +++ b/examples/function.geo @@ -0,0 +1,57 @@ +x = 0; +y = 0; +r = 1; +theloop = 0; + +Function myCircle + p1 = newp; + Point (p1) = {x,y,0,0.2}; + p2 = newp; + Point (p2) = {r+x,y,0,0.2}; + p3 = newp; + Point (p3) = {x,r+y,0,0.2}; + p4 = newp; + Point (p4) = {-r+x,y,0,0.2}; + p5 = newp; + Point (p5) = {x,-r+y,0,0.2}; + c1 = newreg; + Circle (c1) = {p2,p1,p3}; + c2 = newreg; + Circle (c2) = {p3,p1,p4}; + c3 = newreg; + Circle (c3) = {p4,p1,p5}; + c4 = newreg; + Circle (c4) = {p5,p1,p2}; + theloop = newreg; + Line Loop (theloop) = {c1,c2,c3,c4}; +Return + +x = 2; +y = 2; +Call myCircle; +/*loop,x,y and r should be parameters*/ +loop1 = theloop; +x = -2; +y = 2; +Call myCircle; +loop2 = theloop; +x = 2; +y = -2; +Call myCircle; +loop3 = theloop; +x = -2; +y = -2; +Call myCircle; +loop4 = theloop; + +r = 5; +x = 0; +y = 0; +Call myCircle; +loop5 = theloop; + +Plane Surface(newreg) = {loop5,loop4,loop3,loop2,loop1}; +Line(10000) = {6,11}; +Attractor Line {10000} = {1,.03,1}; + +Mesh.Algorithm = 2 ; // This is the new 2D anisotropic algorithm