Added scope to paramter passes

This commit is contained in:
Samuel D. Crow
2021-04-15 12:40:57 -05:00
parent d37eb38ad1
commit ca08646b93
5 changed files with 49 additions and 40 deletions

View File

@@ -72,7 +72,7 @@ const string CODETYPES[]={
enum COMPILE_ERRORS errorLevel=E_OK; enum COMPILE_ERRORS errorLevel=E_OK;
unsigned int indentLevel=0; unsigned int indentLevel=0;
bool scopeGlobal=true; bool scopeGlobal=true;
unsigned int currentFunc=0; fn *currentFunc=nullptr;
bool COMPILE=false; bool COMPILE=false;
bool DUMP=false; bool DUMP=false;
@@ -336,9 +336,7 @@ void testFunc()
func=fn::declare(name, T_FLOATFUNC, o); func=fn::declare(name, T_FLOATFUNC, o);
logger("funkBeat"); logger("funkBeat");
name=string("radius"); name=string("radius");
v=variableType::getOrCreateVar(name, T_FLOATVAR); func->addParameter(name, T_FLOATVAR);
logger("param made");
func->addParameter(v);
logger("param added"); logger("param added");
e=new expression(new expression(v), O_MULTIPLY, new expression(v)); e=new expression(new expression(v), O_MULTIPLY, new expression(v));
logger("expression made"); logger("expression made");

View File

@@ -71,7 +71,7 @@ const string CODETYPES[]={
enum COMPILE_ERRORS errorLevel=E_OK; enum COMPILE_ERRORS errorLevel=E_OK;
unsigned int indentLevel=0; unsigned int indentLevel=0;
bool scopeGlobal=true; bool scopeGlobal=true;
unsigned int currentFunc=0; fn *currentFunc=nullptr;
bool COMPILE=false; bool COMPILE=false;
bool DUMP=false; bool DUMP=false;

View File

@@ -10,6 +10,7 @@
*/ */
#include <string> #include <string>
#include <list> #include <list>
#include <vector>
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
#include <iostream> #include <iostream>
@@ -23,6 +24,7 @@ using namespace std;
#define VER_RELEASE 1 #define VER_RELEASE 1
class variableType; class variableType;
class fn;
extern ofstream output_cpp; extern ofstream output_cpp;
extern ofstream funcs_h; extern ofstream funcs_h;
extern ofstream heap_h; extern ofstream heap_h;
@@ -60,9 +62,9 @@ enum COMPILE_ERRORS:unsigned int
extern enum COMPILE_ERRORS errorLevel; extern enum COMPILE_ERRORS errorLevel;
extern unsigned int indentLevel; extern unsigned int indentLevel;
/*TODO: Replace scopeGlobal with currentFunc==0*/ /*TODO: Replace scopeGlobal with currentFunc==nullptr*/
extern bool scopeGlobal; extern bool scopeGlobal;
extern unsigned int currentFunc; extern fn *currentFunc;
/* flags used internally by the compiler */ /* flags used internally by the compiler */
extern bool COMPILE; extern bool COMPILE;
@@ -111,13 +113,6 @@ enum CODES:unsigned int
T_UNKNOWNFUNC T_UNKNOWNFUNC
}; };
typedef union
{
double d;
int i;
string *s;
}boxTypes;
/* subtypes of the T_PRINTSEPARATOR type */ /* subtypes of the T_PRINTSEPARATOR type */
enum SEPARATORS:unsigned int enum SEPARATORS:unsigned int
{ {
@@ -131,7 +126,8 @@ enum SCOPES:unsigned int
S_UNKNOWN, S_UNKNOWN,
S_LOCAL, S_LOCAL,
S_STATIC, S_STATIC,
S_GLOBAL S_GLOBAL,
S_PARAMETER
}; };
enum OPERATORS:unsigned int enum OPERATORS:unsigned int
@@ -169,11 +165,10 @@ class operands
enum TYPES type; enum TYPES type;
unsigned int id; unsigned int id;
static unsigned int nextID; static unsigned int nextID;
/* private constructor for parameter passing only */
explicit operands(unsigned int id, enum TYPES t);
protected: protected:
void generateBox(enum SCOPES s); void generateBox(enum SCOPES s);
/* constructor for parameter passing */
explicit operands(unsigned int id, enum TYPES t);
explicit operands(enum TYPES t); explicit operands(enum TYPES t);
virtual ~operands() virtual ~operands()
{} {}
@@ -359,14 +354,15 @@ public:
class variableType:public operands class variableType:public operands
{ {
enum SCOPES myScope; enum SCOPES myScope;
unsigned int localID; fn *handle;
public: public:
static variableType *getOrCreateVar(string &name, enum TYPES t); static variableType *getOrCreateVar(string &name, enum TYPES t);
static variableType *cloneAttributes(variableType *v);
virtual string boxName(); virtual string boxName();
void assignment(expression *value); void assignment(expression *value);
/* always call generateBox() after new variableType() */ /* always call generateBox() after new variableType() */
variableType(enum SCOPES s, string &name, enum TYPES t, unsigned int fnID); variableType(enum SCOPES s, string &name, enum TYPES t, fn *fnHandle);
variableType(); variableType();
~variableType() ~variableType()
{} {}
@@ -408,7 +404,8 @@ class fn
{ {
static unordered_map<string, unique_ptr<fn> > functions; static unordered_map<string, unique_ptr<fn> > functions;
static unsigned int nextID; static unsigned int nextID;
list<variableType * >params; unordered_map<string, variableType *>parameters;
vector<variableType *>params;
unsigned int id; unsigned int id;
enum CODES type; enum CODES type;
enum TYPES kind; enum TYPES kind;
@@ -426,7 +423,8 @@ public:
enum CODES getType() const {return this->type;} enum CODES getType() const {return this->type;}
unsigned int getID() const {return this->id;} unsigned int getID() const {return this->id;}
size_t getNumParams() const {return this->params.size();} size_t getNumParams() const {return this->params.size();}
void addParameter(variableType *); variableType *getLocalVar(string &name);
void addParameter(string &name, enum TYPES t);
operands *generateCall(string &name, list<operands *>&paramList); operands *generateCall(string &name, list<operands *>&paramList);
/* standard return is for call */ /* standard return is for call */

View File

@@ -471,15 +471,14 @@ expression::~expression()
} }
/* variable definitions */ /* variable definitions */
variableType::variableType(enum SCOPES s, string &name, enum TYPES t, unsigned int fnID):operands(t) variableType::variableType(enum SCOPES s, string &name, enum TYPES t, fn *fnHandle):operands(t)
{ {
this->myScope=s; this->myScope=s;
this->localID=fnID; this->handle=fnHandle;
switch (s) switch (s)
{ {
case S_LOCAL: case S_LOCAL:
if(locals.find(name)!=locals.end() || if(handle->getLocalVar(name)) error(E_DUPLICATE_SYMBOL);
statics.find(name)!=statics.end() ) error(E_DUPLICATE_SYMBOL);
locals[name]=unique_ptr<variableType>(this); locals[name]=unique_ptr<variableType>(this);
break; break;
case S_GLOBAL: case S_GLOBAL:
@@ -487,10 +486,13 @@ variableType::variableType(enum SCOPES s, string &name, enum TYPES t, unsigned i
globals[name]=unique_ptr<variableType>(this); globals[name]=unique_ptr<variableType>(this);
break; break;
case S_STATIC: case S_STATIC:
if(locals.find(name)!=locals.end() || if(handle->getLocalVar(name)) error(E_DUPLICATE_SYMBOL);
statics.find(name)!=statics.end() ) error(E_DUPLICATE_SYMBOL);
statics[name]=unique_ptr<variableType>(this); statics[name]=unique_ptr<variableType>(this);
break; break;
case S_PARAMETER:
if (handle->getLocalVar(name)) error(E_DUPLICATE_SYMBOL);
/* parameter is added to function list by addParamter */
break;
default: default:
error(E_INTERNAL); error(E_INTERNAL);
} }
@@ -501,7 +503,7 @@ string variableType::boxName()
ostringstream ss; ostringstream ss;
if (myScope==S_LOCAL) if (myScope==S_LOCAL)
{ {
ss << "sub" << this->localID << "->v" << this->getID(); ss << "sub" << this->handle->getID() << "->v" << this->getID();
return ss.str(); return ss.str();
} }
ss << "v" << this->getID(); ss << "v" << this->getID();
@@ -510,18 +512,17 @@ string variableType::boxName()
variableType *variableType::getOrCreateVar(string &name, enum TYPES t) variableType *variableType::getOrCreateVar(string &name, enum TYPES t)
{ {
if (!scopeGlobal) variableType *v;
if (currentFunc!=nullptr)
{ {
auto i=locals.find(name); v=currentFunc->getLocalVar(name);
if(i!=locals.end())return i->second.get(); if (v!=nullptr) return v;
i=statics.find(name);
if(i!=statics.end())return i->second.get();
} }
if (globals.find(name)!=globals.end())return globals[name].get(); if (globals.find(name)!=globals.end())return globals[name].get();
variableType *v;
if (scopeGlobal) if (scopeGlobal)
{ {
v=new variableType(S_GLOBAL, name, t, 0); v=new variableType(S_GLOBAL, name, t, nullptr);
} }
else else
{ {

View File

@@ -53,8 +53,21 @@ fn *fn::getSub(string &name)
return iter->second.get(); return iter->second.get();
} }
void fn::addParameter(variableType *v) variableType *fn::getLocalVar(string &name)
{ {
auto iter=this->parameters.find(name);
if (iter!=parameters.end()) return iter->second;
auto i=locals.find(name);
if (i!=locals.end()) return i->second.get();
i=statics.find(name);
if (i!=statics.end()) return i->second.get();
return nullptr;
}
void fn::addParameter(string &name, enum TYPES t)
{
variableType *v=new variableType(S_PARAMETER, name, t, this);
this->parameters[name]=v;
this->params.push_back(v); this->params.push_back(v);
} }
@@ -205,7 +218,7 @@ void fn::close()
} }
locals.clear(); locals.clear();
statics.clear(); statics.clear();
currentFunc=0; currentFunc=nullptr;
scopeGlobal=true; scopeGlobal=true;
} }
@@ -221,7 +234,7 @@ fn *fn::declare(string &s, enum CODES t, operands *returnCode)
fn::functions.insert({s,unique_ptr<fn>(self)}); fn::functions.insert({s,unique_ptr<fn>(self)});
logger("fn inserted"); logger("fn inserted");
/* initiate local scope */ /* initiate local scope */
currentFunc=self->getID(); currentFunc=self;
scopeGlobal=false; scopeGlobal=false;
return self; return self;
} }
@@ -243,6 +256,5 @@ fn::fn(enum CODES t, operands *returnCode)
fn::~fn() fn::~fn()
{ {
this->params.clear();
delete startAddr; delete startAddr;
} }