PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
FunctionMap.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/base/FunctionMap.cpp
4// Purpose: Structure that holds all user-defined and library function
5// used during execution
6// Author: Shane T. Mueller, Ph.D.
7// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
8// License: GPL 2
9//
10//
11//
12// This file is part of the PEBL project.
13//
14// PEBL is free software; you can redistribute it and/or modify
15// it under the terms of the GNU General Public License as published by
16// the Free Software Foundation; either version 2 of the License, or
17// (at your option) any later version.
18//
19// PEBL is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License
25// along with PEBL; if not, write to the Free Software
26// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28
29#include "FunctionMap.h"
30#include "PNode.h"
31#include "../utility/PEBLUtility.h"
32#include "../utility/PError.h"
33
34#include <iostream>
35#include <map>
36#include <string>
37#include <functional>
38
39using std::cout;
40using std::cerr;
41using std::endl;
42using std::flush;
43using std::string;
44using std::map;
45using std::pair;
46
47
52
54{
55 //Initiate anything necessary here.
56}
57
58
60{
61
62 //cout << "Detelitng functionmap\n";
63 //Delete mFunctionMap if necessary.
64 //go through each function and delete the opnode, because
65 //it is a pointer that is held nowhere else.
66 Destroy();
67}
68
70{
71
72 //Delete mFunctionMap if necessary.
73 //go through each function and delete the opnode, because
74 //it is a pointer that is held nowhere else.
75 std::map<std::string, PNode*>::iterator i = mFunctionMap.begin();
76
77 while(i != mFunctionMap.end())
78 {
79 if(i->second)
80 {
81 i->second->DestroyChildren();
82 delete i->second;
83 i->second = NULL;
84 }
85 i++;
86 }
87 mFunctionMap.clear();
88}
89
94void FunctionMap::AddFunction(std::string funcname, OpNode * node)
95{
96
97 //Get the uppercase version of the funcname.
98
99 std::string upperfuncname = PEBLUtility::ToUpper(funcname);
100 //std::cout << "Adding " << upperfuncname << endl;
101 map<string, PNode*>::iterator p;
102 p = mFunctionMap.find(upperfuncname);
103
104 //If a function is in there already, just signal an error.
105 // Maybe we should clean up the PNode tree that
106 //is there and add the new one in its place.
107 if(p!=mFunctionMap.end())
108 {
109 cerr << "Error in function '" << upperfuncname << "'. Function already exists.\n";
110 }
111 else
112 {
113 //variable isn't there yet, so add the new value into map
114 mFunctionMap.insert(pair<string, PNode* >(upperfuncname, node));
115 }
116}
117
120
121// void FunctionMap::AddFunction(OpNode * node)
122// {
123
124// //Get the left node, which is a FunctionName.
125// DataNode * node1 = (DataNode*)(node->GetLeft());
126// Variant v = node1->GetValue();
127
128// OpNode * node2 = (OpNode*)(node->GetRight());
129
130// //Add the name/node pair using previous method.
131// AddFunction(v,node2);
132
133// }
134
135
140PNode * FunctionMap::GetFunction(const string & lcasefuncname)
141{
142 // Convert the function name to uppercase, so that it will find
143 // a match when searching through the upper-case functionmap.
144
145 std::string funcname = PEBLUtility::ToUpper(lcasefuncname);
146 map<string,PNode*>::iterator p;
147
148 //Get a the function
149
150 p = mFunctionMap.find(funcname);
151
152 if(p == mFunctionMap.end())
153 {
154 //This should probably signal an error.
155 PError::SignalFatalError(string("Function Name [")+ funcname+string("] not found.\n"));
156 DumpValues();
157 return NULL;//new DataNode(Variant(PEBL_DATA_NODE),"PEBL SELF-GENERATED OBJECT",-1);
158 }
159 else
160 {
161
162 //Otherwise, just return a pointer to the node.
163 //cast it to the appropriate type.
164 if((p->second)->GetType() == PEBL_OP_NODE)
165 return (OpNode*)(p->second);
166
167 else if((p->second)->GetType() == PEBL_DATA_NODE)
168 return (DataNode*)(p->second);
169
170 else
171 return p->second;
172 }
173
174}
175
176
177
182bool FunctionMap::IsFunction(const string & funcname)
183{
184
185 std::string upperfuncname = PEBLUtility::ToUpper(funcname);
186 //Get a the function from the map
187 map<string,PNode*>::iterator p;
188 p = mFunctionMap.find(upperfuncname);
189
190 //If it isn't in there, return false, otherwise true.
191 if(p == mFunctionMap.end())
192 return false;
193 else
194 return true;
195}
196
197
200void FunctionMap::Erase(const string & funcname)
201{
202 std::string upperfuncname = PEBLUtility::ToUpper(funcname);
203 mFunctionMap.erase(upperfuncname);
204}
205
206
207
213{
214 map<string,PNode *>::iterator p;
215 cerr << "---------------------------\n Function Map:\n";
216 for(p= mFunctionMap.begin(); p!=mFunctionMap.end(); p++)
217 {
218 cerr << "Function Name: [" << std::flush;
219 cerr << p->first << ":";
220 cerr << ":" << *(p->second) << "]\n" ;
221
222 }
223
224}
#define NULL
Definition BinReloc.cpp:317
@ PEBL_OP_NODE
Definition PNode.h:35
@ PEBL_DATA_NODE
Definition PNode.h:36
~FunctionMap()
The Standard destructor.
FunctionMap()
The Standard constructor.
bool IsFunction(const std::string &funcname)
void DumpValues()
void AddFunction(std::string funcname, OpNode *node)
PNode * GetFunction(const std::string &funcname)
void Erase(const std::string &funcname)
Definition PNode.h:45
std::string ToUpper(const std::string &text)
void SignalFatalError(const std::string &message)