diff --git a/interpreter_lab.cpp b/interpreter_lab.cpp new file mode 100644 index 0000000..656dedc --- /dev/null +++ b/interpreter_lab.cpp @@ -0,0 +1,41 @@ +#include +#include "process_stack.h" +#include "lexer.h" +using namespace std; +using namespace nasal; + +int main() +{ + + _unit.line=1; + _unit.type="function"; + _unit.Name="__main()"; + _unit.unitdata.Type="string"; + _unit.unitdata.data=new std::string; + *((std::string *)_unit.unitdata.data)="in function main."; + _unit.global=true; + _test.stack_append(_unit); + + _unit.line=2; + _unit.type="int"; + _unit.Name="NewInt"; + _unit.global=true; + _unit.unitdata.Type="int"; + _unit.unitdata.data=new int; + *((int *)_unit.unitdata.data)=2147483647; + _test.stack_append(_unit); + + _unit.line=3; + _unit.type="int"; + _unit.Name="ptr"; + _unit.global=true; + _unit.unitdata.Type="int"; + *((int *)_unit.unitdata.data)=1073741824; + _test.stack_append(_unit); + //_test.stack_print(true); + _test.stack_print(false); + + + CommandProcess(); + return 0; +} diff --git a/lexer.h b/lexer.h new file mode 100644 index 0000000..d5cb091 --- /dev/null +++ b/lexer.h @@ -0,0 +1,183 @@ +#ifndef __LEXER_H__ +#define __LEXER_H__ + + +#include +#include +#include +#include "nasal.h" +#include "process_stack.h" + +using namespace nasal; + +std::string text; +process_stack _test; +process_stack_unit _unit; + +void PrintProcess(std::string content) +{ + std::string Sentence=""; + int len=(int)content.length(); + for(int i=0;i> "; + std::getline(std::cin,text); + int len=(int)text.length(); + + int sharpDetected=0; + for(int i=len-1;i>=0;--i) + { + if(text[i]=='#') + { + //ignore sharp + len=i; + sharpDetected=i; + } + if(text[i]==';') + { + len=i+1; + //find the real end of the sentence + if(sharpDetected) + { + for(int j=sharpDetected-1;j>=len;--j) + if(text[j]!=' ') + { + len=j+1; + break; + } + } + else + { + for(int j=(int)text.length()-1;j>=len;--j) + if(text[j]!=' ') + { + len=j+1; + break; + } + } + break; + } + } + + if(text[len-1]==';') + { + for(int i=0;i=0;--j) + if(text[j]!=' ') + { + string_end=j; + break; + } + if(text[string_end]!=')') + { + std::cout<Type=="int") delete (int *)temp->data; - if(temp->Type=="float") + else if(temp->Type=="float") delete (float *)temp->data; - if(temp->Type=="double") + else if(temp->Type=="double") delete (double *)temp->data; - if(temp->Type=="char") + else if(temp->Type=="char") delete (char *)temp->data; - if(temp->Type=="long long int") + else if(temp->Type=="long long int") delete (long long int *)temp->data; - if(temp->Type=="string") + else if(temp->Type=="string") delete (std::string *)temp->data; - if(temp->Type=="array") + else if(temp->Type=="array") delete (NasalList *)temp->data; - if(temp->Type=="hash") + else if(temp->Type=="hash") delete (NasalHash *)temp->data; } delete temp; @@ -819,6 +819,51 @@ NasalList NasalList::Sort(const int SortType,const int _cmp) return TempList; } +var::var(var &p) +{ + Type=p.Type; + if(Type=="int") + { + data=new int; + *((int *)data)=*((int *)p.data); + } + else if(Type=="float") + { + data=new float; + *((float *)data)=*((float *)p.data); + } + else if(Type=="double") + { + data=new double; + *((double *)data)=*((double *)p.data); + } + else if(Type=="char") + { + data=new char; + *((char *)data)=*((char *)p.data); + } + else if(Type=="long long int") + { + data=new long long int; + *((long long int *)data)=*((long long int *)p.data); + } + else if(Type=="string") + { + data=new std::string; + *((std::string *)data)=*((std::string *)p.data); + } + else if(Type=="array") + { + data=new NasalList; + *((NasalList *)data)=*((NasalList *)p.data); + } + else if(Type=="hash") + { + data=new NasalHash; + *((NasalHash *)data)=*((NasalHash *)p.data); + } +} + var::~var() { if(data) @@ -842,6 +887,91 @@ var::~var() } } +var& var::operator=(const var &p) +{ + if(data) + { + if(Type=="int") + delete (int *)data; + else if(Type=="float") + delete (float *)data; + else if(Type=="double") + delete (double *)data; + else if(Type=="char") + delete (char *)data; + else if(Type=="long long int") + delete (long long int *)data; + else if(Type=="string") + delete (std::string *)data; + else if(Type=="array") + delete (NasalList *)data; + else if(Type=="hash") + delete (NasalHash *)data; + } + Type=p.Type; + if(Type=="int") + { + data=new int; + *((int *)data)=*((int *)p.data); + } + else if(Type=="float") + { + data=new float; + *((float *)data)=*((float *)p.data); + } + else if(Type=="double") + { + data=new double; + *((double *)data)=*((double *)p.data); + } + else if(Type=="char") + { + data=new char; + *((char *)data)=*((char *)p.data); + } + else if(Type=="long long int") + { + data=new long long int; + *((long long int *)data)=*((long long int *)p.data); + } + else if(Type=="string") + { + data=new std::string; + *((std::string *)data)=*((std::string *)p.data); + } + else if(Type=="array") + { + data=new NasalList; + *((NasalList *)data)=*((NasalList *)p.data); + } + else if(Type=="hash") + { + data=new NasalHash; + *((NasalHash *)data)=*((NasalHash *)p.data); + } + return *this; +} + +void var::Print() +{ + if(Type=="int") + std::cout<<*((int *)data); + else if(Type=="float") + std::cout<<*((float *)data); + else if(Type=="double") + std::cout<<*((double *)data); + else if(Type=="char") + std::cout<<*((char *)data); + else if(Type=="long long int") + std::cout<<*((long long int *)data); + else if(Type=="string") + std::cout<<*((std::string *)data); + else if(Type=="array") + (*((NasalList *)data)).PrintList(); + else if(Type=="hash") + (*((NasalHash *)data)).PrintHash(); +} + } #endif diff --git a/nasal_list.h b/nasal_list.h index 06df4db..bc2ef3b 100644 --- a/nasal_list.h +++ b/nasal_list.h @@ -47,7 +47,10 @@ class var { data=NULL; } + var(var &); ~var(); + var& operator=(const var &); + void Print(); }; } diff --git a/process_stack.h b/process_stack.h index 0a43628..5c14f97 100644 --- a/process_stack.h +++ b/process_stack.h @@ -11,8 +11,10 @@ namespace nasal struct process_stack_unit { int line; //place the unit first appear - std::string content;//content of the unit or name of the var/class/function + std::string Name;//content of the unit or name of the var/class/function std::string type; //var class function + var unitdata; + bool global; process_stack_unit *next; process_stack_unit *last; }; @@ -25,8 +27,15 @@ class process_stack public: process_stack() { + head=new process_stack_unit; head->line=0; - head->content="# Nasal language for FlightGear."; + head->Name="InterpreterInfo"; + head->global=false; + head->type="information"; + head->unitdata.Type="string"; + head->unitdata.data=new std::string; + *((std::string *)head->unitdata.data)="# Nasal language for FlightGear."; + head->last=NULL; head->next=NULL; ptr=NULL; @@ -48,18 +57,67 @@ class process_stack process_stack_unit *last_node; while(temp->next) { - if(!temp->next->next) - last_node=temp; temp=temp->next; } - if(temp==head) - last_node=head; - temp=new process_stack_unit; + temp->next=new process_stack_unit; + + last_node=temp; + temp=temp->next; + temp->last=last_node; + last_node->next=temp; temp->next=NULL; - temp->content=p.content; - temp->line=p.last; + + temp->Name=p.Name; + temp->line=p.line; temp->type=p.type; + temp->global=p.global; + temp->unitdata=p.unitdata; + + return; + } + void stack_print(bool reverse_mode_used) + { + process_stack_unit *temp=head; + std::cout<<"In stack: "<line<<": |"<type<<"| \""<Name<<"\""<next) + { + temp=temp->next; + std::cout<line<<": |"<type<<"| \""<Name<<"\""<last) + { + std::cout<line<<": |"<type<<"| \""<Name<<"\""<last; + } + std::cout<<"End."<next) + { + temp=temp->next; + if(temp->Name==ElementName) + return true; + } + return false; + } + void stack_content_print(std::string &ElementName) + { + process_stack_unit *temp=head; + while(temp->next) + { + temp=temp->next; + if(temp->Name==ElementName) + { + temp->unitdata.Print(); + return; + } + } return; } };