├── avm ├── src ├── Open.o ├── main.o ├── Parser.o ├── Execute.o ├── run_avm.o ├── Operations.o ├── specOperand.o ├── createOperand.o ├── operandCreator.o ├── virtualMachine.o ├── main.cpp ├── specOperand.cpp ├── operandCreator.cpp ├── Open.cpp ├── virtualMachine.cpp ├── run_avm.cpp ├── createOperand.cpp ├── Operations.cpp ├── Execute.cpp └── Parser.cpp ├── include ├── .#virtualMachine.hpp ├── specOperand.hh ├── IOperand.hh ├── Open.hh ├── virtualMachine.hpp ├── Execute.hh ├── operandCreator.hpp ├── Exceptions.hpp ├── Parser.hh └── Operand.hpp ├── .gitattributes ├── .gitignore └── Makefile /avm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/avm -------------------------------------------------------------------------------- /src/Open.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/Open.o -------------------------------------------------------------------------------- /src/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/main.o -------------------------------------------------------------------------------- /src/Parser.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/Parser.o -------------------------------------------------------------------------------- /src/Execute.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/Execute.o -------------------------------------------------------------------------------- /src/run_avm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/run_avm.o -------------------------------------------------------------------------------- /src/Operations.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/Operations.o -------------------------------------------------------------------------------- /src/specOperand.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/specOperand.o -------------------------------------------------------------------------------- /src/createOperand.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/createOperand.o -------------------------------------------------------------------------------- /src/operandCreator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/operandCreator.o -------------------------------------------------------------------------------- /src/virtualMachine.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/AbstractVM/master/src/virtualMachine.o -------------------------------------------------------------------------------- /include/.#virtualMachine.hpp: -------------------------------------------------------------------------------- 1 | IntxLNKabgral_f@pc-abgral_f.6798:1393948137 -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // main.cpp for main.cpp in /home/abgral_f/dev/avm/src 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Wed Feb 12 14:49:47 2014 abgral_f 8 | // Last update Thu Feb 27 18:58:44 2014 abgral_f 9 | // 10 | 11 | #include 12 | #include 13 | #include "Exceptions.hpp" 14 | #include "Parser.hh" 15 | #include "Open.hh" 16 | #include "Execute.hh" 17 | 18 | int main(int argc, char **argv) 19 | { 20 | catch_ex(argc, argv); 21 | return (EXIT_SUCCESS); 22 | } 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /include/specOperand.hh: -------------------------------------------------------------------------------- 1 | // 2 | // specOperand.hh for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/include 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Tue Feb 25 10:18:36 2014 Julien ANDRE 8 | // Last update Tue Feb 25 17:14:25 2014 Julien ANDRE 9 | // 10 | 11 | #ifndef SPECOPERAND_HH_ 12 | # define SPECOPERAND_HH_ 13 | 14 | # include "Operand.hpp" 15 | 16 | class Int8 : public Operand 17 | { 18 | public: 19 | Int8(char value); 20 | virtual ~Int8(); 21 | }; 22 | 23 | class Int16 : public Operand 24 | { 25 | public: 26 | Int16(int value); 27 | virtual ~Int16(); 28 | }; 29 | 30 | class Int32 : public Operand 31 | { 32 | public: 33 | Int32(long value); 34 | virtual ~Int32(); 35 | }; 36 | 37 | class Float : public Operand 38 | { 39 | public: 40 | Float(float value); 41 | virtual ~Float(); 42 | }; 43 | 44 | class Double : public Operand 45 | { 46 | public: 47 | Double(double value); 48 | virtual ~Double(); 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /src/specOperand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // specOperand.cpp for AbstractVm in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/src 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Tue Feb 25 10:22:05 2014 Julien ANDRE 8 | // Last update Thu Feb 27 18:02:44 2014 Julien ANDRE 9 | // 10 | 11 | #include "specOperand.hh" 12 | 13 | Int8::Int8(char value) : Operand(INT8, value) 14 | { 15 | this->_value = value; 16 | } 17 | 18 | Int8::~Int8() 19 | { 20 | } 21 | 22 | Int16::Int16(int value) : Operand(INT16, value, 1) 23 | { 24 | this->_value = value; 25 | } 26 | 27 | Int16::~Int16() 28 | { 29 | } 30 | 31 | Int32::Int32(long value) : Operand(INT32, value, 2) 32 | { 33 | this->_value = value; 34 | } 35 | 36 | Int32::~Int32() 37 | { 38 | } 39 | 40 | Float::Float(float value) : Operand(FLOAT, value, 6) 41 | { 42 | this->_value = value; 43 | } 44 | 45 | Float::~Float() 46 | { 47 | } 48 | 49 | Double::Double(double value) : Operand(DOUBLE, value, 12) 50 | { 51 | this->_value = value; 52 | } 53 | 54 | Double::~Double() 55 | { 56 | } 57 | -------------------------------------------------------------------------------- /include/IOperand.hh: -------------------------------------------------------------------------------- 1 | // 2 | // IOperand.hh for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/include 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Wed Feb 12 11:19:30 2014 Julien ANDRE 8 | // Last update Wed Feb 26 13:18:38 2014 abgral_f 9 | // 10 | 11 | #ifndef IOPERAND_HH_ 12 | # define IOPERAND_HH_ 13 | 14 | # include 15 | # include 16 | 17 | typedef enum 18 | { 19 | INT8, 20 | INT16, 21 | INT32, 22 | FLOAT, 23 | DOUBLE, 24 | UNKNOWN 25 | } eOperandType; 26 | 27 | class IOperand 28 | { 29 | public: 30 | virtual ~IOperand() {} 31 | 32 | virtual int getPrecision() const = 0; 33 | virtual eOperandType getType() const = 0; 34 | 35 | virtual std::string const &toString() const = 0; 36 | 37 | virtual IOperand *operator+(const IOperand &rhs) const = 0; 38 | virtual IOperand *operator-(const IOperand &rhs) const = 0; 39 | virtual IOperand *operator*(const IOperand &rhs) const = 0; 40 | virtual IOperand *operator/(const IOperand &rhs) const = 0; 41 | virtual IOperand *operator%(const IOperand &rhs) const = 0; 42 | }; 43 | 44 | #endif // IOPERAND_HH_ 45 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Makefile for Makefile in /home/abgral_f/dev/avm 3 | ## 4 | ## Made by abgral_f 5 | ## Login 6 | ## 7 | ## Started on Wed Feb 12 14:31:08 2014 abgral_f 8 | ## Last update Thu Feb 27 18:29:44 2014 abgral_f 9 | ## 10 | 11 | RM = rm -f 12 | 13 | CC = g++ 14 | 15 | NAME = avm 16 | 17 | PATH1 = include/ 18 | 19 | PATH2 = src/ 20 | 21 | SRC = $(PATH2)main.cpp \ 22 | $(PATH2)Parser.cpp \ 23 | $(PATH2)Open.cpp \ 24 | $(PATH2)Execute.cpp \ 25 | $(PATH2)createOperand.cpp \ 26 | $(PATH2)Operations.cpp \ 27 | $(PATH2)specOperand.cpp \ 28 | $(PATH2)virtualMachine.cpp \ 29 | $(PATH2)operandCreator.cpp \ 30 | $(PATH2)run_avm.cpp \ 31 | 32 | OBJS = $(SRC:.cpp=.o) 33 | 34 | CPPFLAGS= -W -Wall -Werror -Wextra -I $(PATH1) -g 35 | 36 | all: $(NAME) 37 | 38 | $(NAME): $(OBJS) 39 | $(CC) -o $(NAME) $(OBJS) 40 | @echo -en "\t\033[32m> " $(NAME) " compiled\033[0m\n" 41 | 42 | clean: 43 | $(RM) $(OBJS) 44 | @echo -en "\t\033[35m> " $(NAME) " cleaned\033[0m\n" 45 | 46 | fclean: clean 47 | $(RM) $(NAME) 48 | @echo -en "\t\033[31m> " $(NAME) " executable removed\033[0m\n" 49 | 50 | re: fclean all 51 | -------------------------------------------------------------------------------- /include/Open.hh: -------------------------------------------------------------------------------- 1 | // 2 | // Open.hh for Open.hh in /home/abgral_f/Dropbox/AVM/avm/include 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Mon Feb 24 16:12:31 2014 abgral_f 8 | // Last update Tue Feb 25 19:12:38 2014 abgral_f 9 | // 10 | 11 | #ifndef OPEN_HH 12 | # define OPEN_HH 13 | 14 | #include 15 | #include 16 | #include 17 | #include "Exceptions.hpp" 18 | 19 | /* Extension */ 20 | 21 | #define SIZE_EXTENSION 4 22 | #define EXTENSION ".avm" 23 | 24 | /* Parsing arguments */ 25 | 26 | #define AVM "AVM : " 27 | #define USAGE "USAGE : ./avm [FILE.avm]." 28 | #define WRONG_EXTENSION " => The extension must be '.avm'." 29 | #define INVALID_FILE " => This file doesn't exist." 30 | 31 | class Open 32 | { 33 | public: 34 | Open(int, char **); 35 | ~Open(); 36 | Open(Open const &); 37 | Open &operator=(Open const &); 38 | void startOpen(); 39 | void openFile(); 40 | void checkExtension(); 41 | bool isFile() const; 42 | std::ifstream *getOpenFile() const; 43 | private: 44 | std::ifstream *_openFile; 45 | int _argc; 46 | char **_argv; 47 | std::string _file; 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /include/virtualMachine.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // virtualMachine.hh for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/include 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Wed Feb 12 10:47:20 2014 Julien ANDRE 8 | // Last update Fri Feb 28 15:07:49 2014 abgral_f 9 | // 10 | 11 | #ifndef VIRTUALMACHINE_HPP_ 12 | # define VIRTUALMACHINE_HPP_ 13 | 14 | #include 15 | #include 16 | 17 | #include "IOperand.hh" 18 | #include "Exceptions.hpp" 19 | #include "specOperand.hh" 20 | #include "operandCreator.hpp" 21 | 22 | /* Errors */ 23 | 24 | #define NO_ELEMS "Not enough element for the operation." 25 | #define POP_ERR "Pop on an empty stack." 26 | #define EMPTY_STACK "Stack is empty." 27 | #define ASSERT_ERR "Assertion is not correct." 28 | #define PRINT_ERR "Element from print is not an int8." 29 | #define DUMP_ERR "Dump on an empty stack." 30 | 31 | class virtualMachine 32 | { 33 | private: 34 | std::list _stack; 35 | operandCreator *_operandCreator; 36 | public: 37 | virtualMachine(); 38 | ~virtualMachine(); 39 | virtualMachine(const virtualMachine &); 40 | virtualMachine *operator=(const virtualMachine ©); 41 | 42 | void push(eOperandType type, const std::string &value); 43 | void push(IOperand *toPush); 44 | void pop(); 45 | void dump(); 46 | void assert(eOperandType type, const std::string &value) const; 47 | 48 | void add(); 49 | void sub(); 50 | void mul(); 51 | void div(); 52 | void mod(); 53 | void print(); 54 | }; 55 | 56 | #endif // VIRTUALMACHINE_HH_ 57 | -------------------------------------------------------------------------------- /include/Execute.hh: -------------------------------------------------------------------------------- 1 | // 2 | // Execute.hh for Execute.hh in /home/abgral_f/Dropbox/AVM/avm/include 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Mon Feb 24 18:31:12 2014 abgral_f 8 | // Last update Fri Feb 28 15:15:53 2014 abgral_f 9 | // 10 | 11 | #ifndef EXECUTE_HH 12 | # define EXECUTE_HH 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include "virtualMachine.hpp" 19 | #include "Parser.hh" 20 | 21 | #define SIZE_TAB 8 22 | 23 | class Execute 24 | { 25 | public: 26 | Execute(); 27 | ~Execute(); 28 | Execute(Execute const &); 29 | Execute &operator=(Execute const &); 30 | void startExecute(std::vector >); 31 | void getIntegerValue(const std::string, const std::string); 32 | void getNonIntegerValue(const std::string, const std::string); 33 | void getType(const std::string); 34 | void executeAdvancedInstruc(const std::vector); 35 | void executeBasicInstruc(const std::vector); 36 | void epureValue(std::string &value); 37 | private: 38 | eOperandType _type; 39 | std::string _value; 40 | virtualMachine _vm; 41 | }; 42 | 43 | 44 | typedef void (virtualMachine::*ptr_op)(void); 45 | 46 | struct s_basic_instruc 47 | { 48 | std::string name; 49 | void (virtualMachine::*ptr_op)(void); 50 | }; 51 | 52 | 53 | typedef void (Execute::*ptr_ex)(std::string, std::string); 54 | 55 | struct s_type_ex 56 | { 57 | std::string name; 58 | eOperandType type; 59 | void (Execute::*ptr_ex)(std::string, std::string); 60 | }; 61 | 62 | void catch_ex(int argc, char **argv); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /include/operandCreator.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // operandCreator.hpp for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/AVM/include 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Wed Feb 26 18:47:34 2014 Julien ANDRE 8 | // Last update Fri Feb 28 16:25:35 2014 abgral_f 9 | // 10 | 11 | #ifndef OPERANDCREATOR_HPP_ 12 | # define OPERANDCREATOR_HPP_ 13 | 14 | # include 15 | 16 | # include "specOperand.hh" 17 | # include "Exceptions.hpp" 18 | 19 | # define NBR_TYPE 5 20 | 21 | # define INT8_UNDER "int8 underflow." 22 | # define INT8_OVER "int8 overflow." 23 | # define INT16_UNDER "int16 underflow." 24 | # define INT16_OVER "int16 overflow." 25 | # define INT32_UNDER "int32 underflow." 26 | # define INT32_OVER "int32 overflow." 27 | # define FLOAT_UNDER "float underflow." 28 | # define FLOAT_OVER "float overflow." 29 | # define DOUBLE_UNDER "double underflow." 30 | # define DOUBLE_OVER "double overflow." 31 | 32 | # define UNKN_TYPE "This type isn't known." 33 | 34 | class operandCreator 35 | { 36 | private: 37 | IOperand *createInt8(const std::string &value); 38 | IOperand *createInt16(const std::string &value); 39 | IOperand *createInt32(const std::string &value); 40 | IOperand *createFloat(const std::string &value); 41 | IOperand *createDouble(const std::string &value); 42 | 43 | public: 44 | operandCreator(); 45 | operandCreator(const operandCreator ©); 46 | ~operandCreator(); 47 | 48 | operandCreator *operator=(const operandCreator ©); 49 | 50 | template 51 | bool checkUnder(const long double &value) 52 | { 53 | if (value < std::numeric_limits::min()) 54 | return true; 55 | return false; 56 | } 57 | 58 | 59 | template 60 | bool checkOver(const long double &value) 61 | { 62 | if (value > std::numeric_limits::max()) 63 | return true; 64 | return false; 65 | } 66 | 67 | IOperand *createOperand(eOperandType type, const std::string &value); 68 | 69 | struct operandType 70 | { 71 | eOperandType type; 72 | IOperand *(operandCreator::*ptr)(const std::string &value); 73 | }; 74 | 75 | operandType _operandType[NBR_TYPE]; 76 | }; 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/operandCreator.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // operandCreator.cpp for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/AVM/include 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Wed Feb 26 19:19:00 2014 Julien ANDRE 8 | // Last update Thu Feb 27 17:18:54 2014 Julien ANDRE 9 | // 10 | 11 | #include "operandCreator.hpp" 12 | 13 | operandCreator::operandCreator() 14 | { 15 | _operandType[0].type = INT8; 16 | _operandType[0].ptr = &operandCreator::createInt8; 17 | _operandType[1].type = INT16; 18 | _operandType[1].ptr = &operandCreator::createInt16; 19 | _operandType[2].type = INT32; 20 | _operandType[2].ptr = &operandCreator::createInt32; 21 | _operandType[3].type = FLOAT; 22 | _operandType[3].ptr = &operandCreator::createFloat; 23 | _operandType[4].type = DOUBLE; 24 | _operandType[4].ptr = &operandCreator::createDouble; 25 | } 26 | 27 | operandCreator::operandCreator(const operandCreator ©) 28 | { 29 | _operandType[0].type = copy._operandType[0].type; 30 | _operandType[0].ptr = copy._operandType[0].ptr; 31 | _operandType[1].type = copy._operandType[1].type; 32 | _operandType[1].ptr = copy._operandType[1].ptr; 33 | _operandType[2].type = copy._operandType[2].type; 34 | _operandType[2].ptr = copy._operandType[2].ptr; 35 | _operandType[3].type = copy._operandType[3].type; 36 | _operandType[3].ptr = copy._operandType[3].ptr; 37 | _operandType[4].type = copy._operandType[4].type; 38 | _operandType[4].ptr = copy._operandType[4].ptr; 39 | } 40 | 41 | operandCreator::~operandCreator() 42 | { 43 | } 44 | 45 | operandCreator *operandCreator::operator=(const operandCreator ©) 46 | { 47 | _operandType[0].type = copy._operandType[0].type; 48 | _operandType[0].ptr = copy._operandType[0].ptr; 49 | _operandType[1].type = copy._operandType[1].type; 50 | _operandType[1].ptr = copy._operandType[1].ptr; 51 | _operandType[2].type = copy._operandType[2].type; 52 | _operandType[2].ptr = copy._operandType[2].ptr; 53 | _operandType[3].type = copy._operandType[3].type; 54 | _operandType[3].ptr = copy._operandType[3].ptr; 55 | _operandType[4].type = copy._operandType[4].type; 56 | _operandType[4].ptr = copy._operandType[4].ptr; 57 | return this; 58 | } 59 | -------------------------------------------------------------------------------- /src/Open.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Open.cpp for Open.cpp in /home/abgral_f/Dropbox/AVM/avm/src 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Mon Feb 24 16:16:23 2014 abgral_f 8 | // Last update Wed Feb 26 14:44:25 2014 abgral_f 9 | // 10 | 11 | #include 12 | #include "Open.hh" 13 | 14 | Open::Open(int argc, char **argv) 15 | { 16 | this->_argc = argc; 17 | this->_argv = argv; 18 | this->_file = ""; 19 | this->_openFile = NULL; 20 | } 21 | 22 | Open::~Open() 23 | { 24 | 25 | } 26 | 27 | Open::Open(Open const & cpy) 28 | { 29 | this->_file = cpy._file; 30 | this->_argc = cpy._argc; 31 | this->_argv = cpy._argv; 32 | this->_openFile = cpy._openFile; 33 | } 34 | 35 | Open &Open::operator=(Open const & cpy) 36 | { 37 | this->_file = cpy._file; 38 | this->_argc = cpy._argc; 39 | this->_argv = cpy._argv; 40 | this->_openFile = cpy._openFile; 41 | return (*this); 42 | } 43 | 44 | std::ifstream *Open::getOpenFile() const 45 | { 46 | return (this->_openFile); 47 | } 48 | 49 | void Open::openFile() 50 | { 51 | this->_openFile = new std::ifstream(); 52 | this->_openFile->open(this->_file.c_str(), std::ifstream::in); 53 | if (!(*this->_openFile)) 54 | throw invalidArgument(INVALID_FILE, this->_file); 55 | } 56 | 57 | bool Open::isFile() const 58 | { 59 | if (this->_argc == 1) 60 | return (false); 61 | else if (this->_argc == 2) 62 | return (true); 63 | else 64 | throw invalidArgument(USAGE, ""); 65 | return (false); 66 | } 67 | 68 | void Open::checkExtension() 69 | { 70 | size_t str_size; 71 | size_t pos_occur; 72 | 73 | str_size = this->_file.size(); 74 | pos_occur = this->_file.rfind(EXTENSION); 75 | if (pos_occur != str_size - SIZE_EXTENSION 76 | || pos_occur == std::string::npos 77 | || pos_occur == 0) 78 | throw invalidArgument(WRONG_EXTENSION, this->_file); 79 | } 80 | 81 | void Open::startOpen() 82 | { 83 | if (this->isFile() != false) 84 | { 85 | this->_file = this->_argv[1]; 86 | this->checkExtension(); 87 | this->openFile(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/virtualMachine.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // virtualMachine.cpp for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/src 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Wed Feb 12 15:05:36 2014 Julien ANDRE 8 | // Last update Fri Feb 28 15:07:56 2014 abgral_f 9 | // 10 | 11 | #include 12 | #include "virtualMachine.hpp" 13 | 14 | #define HIGHER(x, y) (x < y ? y : x) 15 | 16 | virtualMachine::virtualMachine() 17 | { 18 | this->_operandCreator = new operandCreator; 19 | } 20 | 21 | virtualMachine::virtualMachine(const virtualMachine ©) 22 | { 23 | this->_operandCreator = copy._operandCreator; 24 | this->_stack = copy._stack; 25 | } 26 | 27 | virtualMachine::~virtualMachine() 28 | { 29 | } 30 | 31 | virtualMachine *virtualMachine::operator=(const virtualMachine ©) 32 | { 33 | this->_operandCreator = copy._operandCreator; 34 | this->_stack = copy._stack; 35 | return this; 36 | } 37 | 38 | void virtualMachine::push(eOperandType type, const std::string &value) 39 | { 40 | this->_stack.push_front(this->_operandCreator->createOperand(type, value)); 41 | } 42 | 43 | void virtualMachine::push(IOperand *toPush) 44 | { 45 | this->_stack.push_front(toPush); 46 | } 47 | 48 | void virtualMachine::pop() 49 | { 50 | if (this->_stack.size() == 0) 51 | throw popOnAnEmptyStack(POP_ERR); 52 | this->_stack.pop_front(); 53 | } 54 | 55 | void virtualMachine::dump() 56 | { 57 | std::list::iterator it = this->_stack.begin(); 58 | 59 | if (this->_stack.size() == 0) 60 | throw std::runtime_error(DUMP_ERR); 61 | while (it != this->_stack.end()) 62 | { 63 | std::cout << std::setprecision((*it)->getPrecision()) << (*it)->toString() << std::endl; 64 | ++it; 65 | } 66 | } 67 | 68 | void virtualMachine::assert(eOperandType type, const std::string &value) const 69 | { 70 | if (this->_stack.size() == 0) 71 | throw wrongAssert(EMPTY_STACK); 72 | std::list::const_iterator it = this->_stack.begin(); 73 | 74 | if ((*it)->getType() != type) 75 | throw wrongAssert(ASSERT_ERR); 76 | if ((*it)->toString() != value) 77 | throw wrongAssert(ASSERT_ERR); 78 | } 79 | 80 | void virtualMachine::print() 81 | { 82 | if (this->_stack.size() == 0) 83 | throw wrongAssert(EMPTY_STACK); 84 | std::list::const_iterator it = this->_stack.begin(); 85 | 86 | if ((*it)->getType() != INT8) 87 | throw printError(PRINT_ERR); 88 | std::cout << stringToValue((*it)->toString()) << std::endl; 89 | } 90 | -------------------------------------------------------------------------------- /include/Exceptions.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Exceptions.hpp for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/include 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Wed Feb 12 22:49:19 2014 Julien ANDRE 8 | // Last update Thu Feb 27 19:07:31 2014 abgral_f 9 | // 10 | 11 | #ifndef EXCEPTIONS_HPP_ 12 | # define EXCEPTIONS_HPP_ 13 | 14 | #include 15 | #include 16 | 17 | class invalidArgument : public std::invalid_argument 18 | { 19 | public: 20 | invalidArgument(std::string const &msg, std::string const &str) : std::invalid_argument(msg) {this->str = str;} 21 | ~invalidArgument() throw() {} 22 | const std::string &getStr() const {return (this->str);} 23 | private: 24 | std::string str; 25 | }; 26 | 27 | class logicError : public std::logic_error 28 | { 29 | public: 30 | logicError(std::string const &msg, std::string const &str) : std::logic_error(msg) {this->str = str;} 31 | ~logicError() throw() {} 32 | const std::string &getStr() const {return (this->str);} 33 | private: 34 | std::string str; 35 | }; 36 | 37 | 38 | class intValueOverflow : public std::overflow_error 39 | { 40 | public: 41 | intValueOverflow(std::string const msg) : std::overflow_error(msg) {} 42 | }; 43 | 44 | class intValueUnderflow : public std::underflow_error 45 | { 46 | public: 47 | intValueUnderflow(std::string const msg) : std::underflow_error(msg) {} 48 | }; 49 | 50 | class floatRangeError : public std::range_error 51 | { 52 | public: 53 | floatRangeError(std::string const msg) : std::range_error(msg) {} 54 | }; 55 | 56 | class wrongAssert : public std::runtime_error 57 | { 58 | public: 59 | wrongAssert(std::string const msg) : std::runtime_error(msg) {} 60 | }; 61 | 62 | class popOnAnEmptyStack : public std::runtime_error 63 | { 64 | public: 65 | popOnAnEmptyStack(std::string const msg) : std::runtime_error(msg) {} 66 | }; 67 | 68 | class tooFewElem : public std::runtime_error 69 | { 70 | public: 71 | tooFewElem(std::string const msg) : std::runtime_error(msg) {} 72 | }; 73 | 74 | class printError : public std::runtime_error 75 | { 76 | public: 77 | printError(std::string const msg) : std::runtime_error(msg) {} 78 | }; 79 | 80 | class unknownType : public std::runtime_error 81 | { 82 | public: 83 | unknownType(const std::string msg) : std::runtime_error(msg) {} 84 | }; 85 | 86 | class divideByZero : public std::runtime_error 87 | { 88 | public: 89 | divideByZero(const std::string msg) : std::runtime_error(msg) {} 90 | }; 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /src/run_avm.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // catch.cpp for catch.cpp in /home/abgral_f/rendu/dev_AVM/src 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Thu Feb 27 18:20:35 2014 abgral_f 8 | // Last update Mon Sep 15 10:00:35 2014 abgral_f 9 | // 10 | 11 | #include 12 | #include "Exceptions.hpp" 13 | #include "Parser.hh" 14 | #include "Open.hh" 15 | #include "Execute.hh" 16 | 17 | void run_avm(int argc, char **argv) 18 | { 19 | Open file(argc, argv); 20 | Parser parsing; 21 | Execute exec; 22 | std::istream *stream; 23 | 24 | file.startOpen(); 25 | if (file.getOpenFile() != NULL) 26 | { 27 | stream = file.getOpenFile(); 28 | parsing.startParsing(*stream, false); 29 | if (parsing.getError() == false) 30 | exec.startExecute(parsing.getLineTab()); 31 | } 32 | else 33 | { 34 | parsing.startParsing(std::cin, true); 35 | if (parsing.getError() == false) 36 | exec.startExecute(parsing.getLineTab()); 37 | } 38 | } 39 | 40 | void catch_ex(int argc, char **argv) 41 | { 42 | try 43 | { 44 | run_avm(argc, argv); 45 | } 46 | catch (const invalidArgument & e) 47 | { 48 | std::cout << AVM << e.what() << std::endl; 49 | } 50 | catch (const intValueOverflow & e) 51 | { 52 | std::cout << AVM << e.what() << std::endl; 53 | } 54 | catch (const intValueUnderflow & e) 55 | { 56 | std::cout << AVM << e.what() << std::endl; 57 | } 58 | catch (const floatRangeError & e) 59 | { 60 | std::cout << AVM << e.what() << std::endl; 61 | } 62 | catch (const wrongAssert & e) 63 | { 64 | std::cout << AVM << e.what() << std::endl; 65 | } 66 | catch (const popOnAnEmptyStack & e) 67 | { 68 | std::cout << AVM << e.what() << std::endl; 69 | } 70 | catch (const tooFewElem & e) 71 | { 72 | std::cout << AVM << e.what() << std::endl; 73 | } 74 | catch (const printError & e) 75 | { 76 | std::cout << AVM << e.what() << std::endl; 77 | } 78 | catch (const unknownType & e) 79 | { 80 | std::cout << AVM << e.what() << std::endl; 81 | } 82 | catch (const divideByZero & e) 83 | { 84 | std::cout << AVM << e.what() << std::endl; 85 | } 86 | catch (const std::logic_error & e) 87 | { 88 | std::cout << AVM << e.what() << std::endl; 89 | } 90 | catch (const std::exception & e) 91 | { 92 | std::cout << AVM << e.what() << std::endl; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /include/Parser.hh: -------------------------------------------------------------------------------- 1 | // 2 | // Parser.hh for Parser.hh in /home/abgral_f/dev/avm/include 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Wed Feb 12 16:58:50 2014 abgral_f 8 | // Last update Thu Feb 27 19:10:17 2014 abgral_f 9 | // 10 | 11 | #ifndef PARSER_HH 12 | # define PARSER_HH 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include "IOperand.hh" 19 | #include "Exceptions.hpp" 20 | 21 | /* Instruction */ 22 | 23 | #define ASSERT "assert" 24 | #define PUSH "push" 25 | #define EXIT "exit" 26 | #define SECOND_EXIT ";;" 27 | #define COMMENT ";" 28 | 29 | /* Synthaxe error */ 30 | 31 | #define ERROR_LINE "avm: line " 32 | #define SYNTHAXE ": Synthaxe error => " 33 | #define ADVANCED_SYNTH " : Advanced instruction must have this synthaxe => INSTRUCTION TYPE(VALUE)" 34 | #define WRONG_TYPE " : Wrong type. Type must be : int8 | int16 | int32 | float | double" 35 | #define WRONG_VALUE " : This type must recieve an integer value." 36 | #define WRONG_VALUE2 " : This type must recieve an integer or decimal value." 37 | #define INVALID_INSTRUC " : This instruction does not exist." 38 | #define INVALID_EXIT " : Instructions 'exit' and ';;' don't take argument." 39 | #define EXIT_ABS "The file must end by the instruction 'exit'." 40 | 41 | class Parser 42 | { 43 | public: 44 | Parser(); 45 | ~Parser(); 46 | Parser(Parser const &); 47 | Parser &operator=(Parser const &); 48 | void startParsing(std::istream&, bool); 49 | void parsIntegerValue(const std::string, const std::string); 50 | void parsNonIntegerValue(const std::string, const std::string); 51 | std::vector stringToWordtab(const std::string); 52 | bool checkType(const std::string); 53 | void checkAdvancedInstruc(const std::vector); 54 | void checkBasicInstruc(const std::vector) const; 55 | bool checkProgramEnd(const std::vector); 56 | void commentLine(std::string &); 57 | const std::vector > &getLineTab() const; 58 | bool getError() const; 59 | private: 60 | std::string _line; 61 | unsigned int _line_num; 62 | eOperandType _type; 63 | std::vector > _lineTab; 64 | bool _error; 65 | bool _readInputMode; 66 | bool _exit; 67 | }; 68 | 69 | typedef void (Parser::*ptr)(std::string, std::string); 70 | 71 | struct s_type 72 | { 73 | std::string name; 74 | eOperandType type; 75 | void (Parser::*ptr)(std::string, std::string); 76 | }; 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/createOperand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // createOperand.cpp for AbstractVM in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/src 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Mon Feb 24 20:12:52 2014 Julien ANDRE 8 | // Last update Fri Feb 28 16:26:11 2014 abgral_f 9 | // 10 | 11 | #include "operandCreator.hpp" 12 | 13 | template<> 14 | bool operandCreator::checkUnder(const long double &value) 15 | { 16 | if (value < -std::numeric_limits::max()) 17 | return true; 18 | return false; 19 | } 20 | 21 | template<> 22 | bool operandCreator::checkUnder(const long double &value) 23 | { 24 | if (value < -std::numeric_limits::max()) 25 | return true; 26 | return false; 27 | } 28 | 29 | IOperand *operandCreator::createInt8(const std::string &value) 30 | { 31 | long double numValue = stringToValue(value); 32 | 33 | if (this->checkUnder(numValue) == true) 34 | throw intValueUnderflow(INT8_UNDER); 35 | else if (this->checkOver(numValue) == true) 36 | throw intValueOverflow(INT8_OVER); 37 | return new class Int8(stringToValue(value)); 38 | } 39 | 40 | IOperand *operandCreator::createInt16(const std::string &value) 41 | { 42 | long double numValue = stringToValue(value); 43 | 44 | if (this->checkUnder(numValue) == true) 45 | throw intValueUnderflow(INT16_UNDER); 46 | else if (this->checkOver(numValue) == true) 47 | throw intValueOverflow(INT16_OVER); 48 | return new class Int16(stringToValue(value)); 49 | } 50 | 51 | IOperand *operandCreator::createInt32(const std::string &value) 52 | { 53 | long double numValue = stringToValue(value); 54 | 55 | if (this->checkUnder(numValue) == true) 56 | throw intValueUnderflow(INT32_UNDER); 57 | else if (this->checkOver(numValue) == true) 58 | throw intValueOverflow(INT32_OVER); 59 | return new class Int32(stringToValue(value)); 60 | } 61 | 62 | IOperand *operandCreator::createFloat(const std::string &value) 63 | { 64 | long double numValue = stringToValue(value); 65 | 66 | if (this->checkUnder(numValue) == true) 67 | throw floatRangeError(FLOAT_UNDER); 68 | else if (this->checkOver(numValue) == true) 69 | throw floatRangeError(FLOAT_OVER); 70 | return new class Float(stringToValue(value)); 71 | } 72 | 73 | IOperand *operandCreator::createDouble(const std::string &value) 74 | { 75 | long double numValue = stringToValue(value); 76 | 77 | if (this->checkUnder(numValue) == true) 78 | throw floatRangeError(DOUBLE_UNDER); 79 | else if (this->checkOver(numValue) == true) 80 | throw floatRangeError(DOUBLE_OVER); 81 | return new class Double(stringToValue(value)); 82 | } 83 | 84 | IOperand *operandCreator::createOperand(eOperandType type, const std::string &value) 85 | { 86 | int count = 0; 87 | 88 | while (count != NBR_TYPE) 89 | { 90 | if (this->_operandType[count].type == type) 91 | return (this->*(this->_operandType[count].ptr))(value); 92 | ++count; 93 | } 94 | throw unknownType(UNKN_TYPE); 95 | return (NULL); 96 | } 97 | -------------------------------------------------------------------------------- /src/Operations.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Operations.cpp for AbstractVm in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/src 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Tue Feb 25 17:17:55 2014 Julien ANDRE 8 | // Last update Thu Feb 27 18:58:08 2014 abgral_f 9 | // 10 | 11 | #include "virtualMachine.hpp" 12 | #include "specOperand.hh" 13 | 14 | void virtualMachine::add() 15 | { 16 | IOperand *newOperand; 17 | 18 | if (this->_stack.size() < 2) 19 | throw tooFewElem(NO_ELEMS); 20 | std::list::const_iterator first = this->_stack.begin(); 21 | std::list::iterator second = this->_stack.begin(); 22 | ++second; 23 | if ((*first)->getPrecision() > (*second)->getPrecision()) 24 | newOperand = *(*first) + *(*second); 25 | else 26 | newOperand = *(*second) + *(*first); 27 | this->pop(); 28 | this->pop(); 29 | this->push(newOperand); 30 | } 31 | 32 | void virtualMachine::sub() 33 | { 34 | IOperand *newOperand; 35 | 36 | if (this->_stack.size() < 2) 37 | throw tooFewElem(NO_ELEMS); 38 | std::list::const_iterator first = this->_stack.begin(); 39 | std::list::iterator second = this->_stack.begin(); 40 | ++second; 41 | if ((*first)->getPrecision() > (*second)->getPrecision()) 42 | { 43 | IOperand *tmp = _operandCreator->createOperand((*first)->getType(), (*second)->toString()); 44 | newOperand = *(tmp) - *(*first); 45 | delete tmp; 46 | } 47 | else 48 | newOperand = *(*second) - *(*first); 49 | this->pop(); 50 | this->pop(); 51 | this->push(newOperand); 52 | } 53 | 54 | void virtualMachine::mul() 55 | { 56 | IOperand *newOperand; 57 | 58 | if (this->_stack.size() < 2) 59 | throw tooFewElem(NO_ELEMS); 60 | std::list::const_iterator first = this->_stack.begin(); 61 | std::list::iterator second = this->_stack.begin(); 62 | ++second; 63 | if ((*first)->getPrecision() > (*second)->getPrecision()) 64 | newOperand = *(*first) * *(*second); 65 | else 66 | newOperand = *(*second) * *(*first); 67 | this->pop(); 68 | this->pop(); 69 | this->push(newOperand); 70 | } 71 | 72 | void virtualMachine::div() 73 | { 74 | IOperand *newOperand; 75 | 76 | if (this->_stack.size() < 2) 77 | throw tooFewElem(NO_ELEMS); 78 | std::list::const_iterator first = this->_stack.begin(); 79 | std::list::iterator second = this->_stack.begin(); 80 | ++second; 81 | if ((*first)->getPrecision() > (*second)->getPrecision()) 82 | { 83 | IOperand *tmp = _operandCreator->createOperand((*first)->getType(), (*second)->toString()); 84 | newOperand = *tmp / *(*first); 85 | delete tmp; 86 | } 87 | else 88 | newOperand = *(*second) / *(*first); 89 | this->pop(); 90 | this->pop(); 91 | this->push(newOperand); 92 | } 93 | 94 | void virtualMachine::mod() 95 | { 96 | IOperand *newOperand; 97 | 98 | if (this->_stack.size() < 2) 99 | throw tooFewElem(NO_ELEMS); 100 | std::list::const_iterator first = this->_stack.begin(); 101 | std::list::iterator second = this->_stack.begin(); 102 | ++second; 103 | if ((*first)->getPrecision() > (*second)->getPrecision()) 104 | { 105 | IOperand *tmp = _operandCreator->createOperand((*first)->getType(), (*second)->toString()); 106 | newOperand = *tmp % *(*first); 107 | delete tmp; 108 | } 109 | else 110 | newOperand = *(*second) % *(*first); 111 | this->pop(); 112 | this->pop(); 113 | this->push(newOperand); 114 | } 115 | -------------------------------------------------------------------------------- /include/Operand.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Operand.hpp for AbstractVm in /home/andre_j/Projects/C++/AbstractVM/dev_AVM/include 3 | // 4 | // Made by Julien ANDRE 5 | // Login 6 | // 7 | // Started on Tue Feb 25 10:01:45 2014 Julien ANDRE 8 | // Last update Fri Feb 28 14:15:33 2014 Julien ANDRE 9 | // 10 | 11 | #ifndef OPERAND_HPP_ 12 | # define OPERAND_HPP_ 13 | 14 | #include 15 | 16 | #include "Exceptions.hpp" 17 | #include "IOperand.hh" 18 | 19 | /* Errors */ 20 | 21 | #define DIV_ERR "Divsion by zero." 22 | #define MOD_ERR "Modulo by zero." 23 | 24 | template 25 | T stringToValue(const std::string &value) 26 | { 27 | std::istringstream iss(value); 28 | long double tmp; 29 | T res; 30 | 31 | iss >> tmp; 32 | res = tmp; 33 | return res; 34 | } 35 | 36 | template 37 | class Operand : public IOperand 38 | { 39 | protected: 40 | eOperandType _type; 41 | T _value; 42 | std::string _descrp; 43 | int _precision; 44 | 45 | public: 46 | Operand(eOperandType type, T value, int precision) 47 | { 48 | std::ostringstream oss; 49 | 50 | this->_type = type; 51 | this->_value = value; 52 | oss << this->_value; 53 | this->_descrp = oss.str(); 54 | this->_precision = precision; 55 | } 56 | 57 | Operand(eOperandType type, char value) 58 | { 59 | std::ostringstream oss; 60 | 61 | this->_type = type; 62 | this->_value = value; 63 | oss << static_cast(this->_value); 64 | this->_descrp = oss.str(); 65 | this->_precision = 0; 66 | } 67 | 68 | Operand(const IOperand ©) 69 | { 70 | this->_type = copy.getType(); 71 | this->_value = stringToValue(copy.toString()); 72 | this->_descrp = copy.toString(); 73 | this->_precision = copy.getPrecision(); 74 | } 75 | 76 | virtual ~Operand() {} 77 | 78 | virtual int getPrecision() const 79 | { 80 | return this->_precision; 81 | } 82 | 83 | virtual eOperandType getType() const 84 | { 85 | return this->_type; 86 | } 87 | 88 | virtual std::string const &toString() const 89 | { 90 | return this->_descrp; 91 | } 92 | 93 | virtual IOperand *operator+(const IOperand &rhs) const 94 | { 95 | return new Operand(this->_type, this->_value + stringToValue(rhs.toString()), this->_precision); 96 | } 97 | 98 | virtual IOperand *operator-(const IOperand &rhs) const 99 | { 100 | return new Operand(this->_type, this->_value - stringToValue(rhs.toString()), this->_precision); 101 | } 102 | 103 | virtual IOperand *operator*(const IOperand &rhs) const 104 | { 105 | return new Operand(this->_type, this->_value * stringToValue(rhs.toString()), this->_precision); 106 | } 107 | 108 | virtual IOperand *operator/(const IOperand &rhs) const 109 | { 110 | if (stringToValue(rhs.toString()) == 0) 111 | throw divideByZero(DIV_ERR); 112 | return new Operand(this->_type, this->_value / stringToValue(rhs.toString()), this->_precision); 113 | } 114 | 115 | virtual IOperand *operator%(const IOperand &rhs) const 116 | { 117 | if (stringToValue(rhs.toString()) == 0) 118 | throw divideByZero(MOD_ERR); 119 | return new Operand(this->_type, static_cast(this->_value) % static_cast(stringToValue(rhs.toString())), this->_precision); 120 | } 121 | 122 | IOperand *operator=(const IOperand ©) 123 | { 124 | this->_type = copy.getType(); 125 | this->_value = stringToValue(copy.toString()); 126 | this->_precision = copy.getPrecision(); 127 | return this; 128 | } 129 | }; 130 | 131 | #endif 132 | -------------------------------------------------------------------------------- /src/Execute.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Execute.cpp for Execute.cpp in /home/abgral_f/Dropbox/AVM/avm/src 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Mon Feb 24 19:04:58 2014 abgral_f 8 | // Last update Sun Mar 2 20:47:49 2014 abgral_f 9 | // 10 | 11 | #include 12 | #include "Execute.hh" 13 | 14 | static s_type_ex type[] = 15 | { 16 | {"int8", INT8, &Execute::getIntegerValue}, 17 | {"int16", INT16, &Execute::getIntegerValue}, 18 | {"int32", INT32, &Execute::getIntegerValue}, 19 | {"float", FLOAT, &Execute::getNonIntegerValue}, 20 | {"double", DOUBLE, &Execute::getNonIntegerValue}, 21 | {"", UNKNOWN, NULL} 22 | }; 23 | 24 | 25 | static s_basic_instruc basic[] = 26 | { 27 | {"add", &virtualMachine::add}, 28 | {"sub", &virtualMachine::sub}, 29 | {"mul", &virtualMachine::mul}, 30 | {"div", &virtualMachine::div}, 31 | {"mod", &virtualMachine::mod}, 32 | {"pop", &virtualMachine::pop}, 33 | {"dump", &virtualMachine::dump}, 34 | {"print", &virtualMachine::print}, 35 | }; 36 | 37 | Execute::Execute() 38 | { 39 | this->_value = ""; 40 | } 41 | 42 | Execute::~Execute() 43 | { 44 | 45 | } 46 | 47 | Execute::Execute(Execute const & cpy) 48 | { 49 | this->_type = cpy._type; 50 | this->_value = cpy._value; 51 | } 52 | 53 | Execute &Execute::operator=(Execute const & cpy) 54 | { 55 | this->_type = cpy._type; 56 | this->_value = cpy._value; 57 | return (*this); 58 | } 59 | 60 | void Execute::getIntegerValue(const std::string type, const std::string word) 61 | { 62 | size_t count; 63 | char to_add[2]; 64 | 65 | to_add[1] = '\0'; 66 | this->_value = ""; 67 | count = type.size() + 1; 68 | for (int i = count ; word[i] != ')' ; i++) 69 | { 70 | to_add[0] = word[i]; 71 | this->_value += &to_add[0]; 72 | } 73 | } 74 | 75 | void Execute::getNonIntegerValue(const std::string type, const std::string word) 76 | { 77 | size_t count; 78 | char to_add[2]; 79 | 80 | to_add[1] = '\0'; 81 | this->_value = ""; 82 | count = type.size() + 1; 83 | for (int i = count ; word[i] != ')' ; i++) 84 | { 85 | to_add[0] = word[i]; 86 | this->_value += &to_add[0]; 87 | } 88 | } 89 | 90 | void Execute::getType(const std::string word) 91 | { 92 | for (int i = 0 ; type[i].ptr_ex != NULL ; i++) 93 | { 94 | if (word.find(type[i].name) == 0) 95 | { 96 | (this->*(type[i].ptr_ex))(type[i].name, word); 97 | this->_type = type[i].type; 98 | } 99 | } 100 | } 101 | 102 | void Execute::executeAdvancedInstruc(const std::vector string_tab) 103 | { 104 | this->getType(string_tab[1]); 105 | epureValue(this->_value); 106 | if (string_tab[0] == PUSH) 107 | this->_vm.push(this->_type, this->_value); 108 | else 109 | this->_vm.assert(this->_type, this->_value); 110 | } 111 | 112 | void Execute::executeBasicInstruc(const std::vector string_tab) 113 | { 114 | for (int i = 0 ; i != SIZE_TAB ; i++) 115 | { 116 | if (string_tab[0] == basic[i].name) 117 | (this->_vm.*(basic[i].ptr_op))(); 118 | } 119 | } 120 | 121 | void Execute::startExecute(const std::vector > lineTab) 122 | { 123 | for(size_t i = 0 ; i != lineTab.size() ; i++) 124 | { 125 | if (lineTab[i].size() != 0) 126 | { 127 | if (lineTab[i][0] == ASSERT || lineTab[i][0] == PUSH) 128 | executeAdvancedInstruc(lineTab[i]); 129 | else if (lineTab[i][0] == EXIT) 130 | return ; 131 | else if (lineTab[i].size() == 1) 132 | executeBasicInstruc(lineTab[i]); 133 | } 134 | } 135 | } 136 | 137 | void Execute::epureValue(std::string &value) 138 | { 139 | size_t count = 0; 140 | size_t ret; 141 | 142 | if (value[count] == '-') 143 | ++count; 144 | while (count != value.size() && value[count] == '0') 145 | { 146 | if (value.size() == 1 || value[count + 1] == '.') 147 | ++count; 148 | else 149 | value.erase(count, 1); 150 | } 151 | if ((ret = value.find(".", 0)) != std::string::npos) 152 | { 153 | count = value.size() - 1; 154 | while (count != ret && value[count] == '0') 155 | value.erase(count--, 1); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/Parser.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Parser.cpp for Parser.cpp in /home/abgral_f/dev/avm/src 3 | // 4 | // Made by abgral_f 5 | // Login 6 | // 7 | // Started on Wed Feb 12 17:00:51 2014 abgral_f 8 | // Last update Thu Feb 27 18:48:45 2014 abgral_f 9 | // 10 | 11 | #include 12 | #include "Parser.hh" 13 | 14 | static s_type type[] = 15 | { 16 | {"int8", INT8, &Parser::parsIntegerValue}, 17 | {"int16", INT16, &Parser::parsIntegerValue}, 18 | {"int32", INT32, &Parser::parsIntegerValue}, 19 | {"float", FLOAT, &Parser::parsNonIntegerValue}, 20 | {"double", DOUBLE, &Parser::parsNonIntegerValue}, 21 | {"", UNKNOWN, NULL} 22 | }; 23 | 24 | static std::string basic[] = 25 | { 26 | "add", 27 | "sub", 28 | "mul", 29 | "div", 30 | "mod", 31 | "pop", 32 | "dump", 33 | "print", 34 | "exit", 35 | "" 36 | }; 37 | 38 | Parser::Parser() 39 | { 40 | this->_line_num = 0; 41 | this->_error = false; 42 | } 43 | 44 | Parser::~Parser() 45 | { 46 | 47 | } 48 | 49 | Parser::Parser(Parser const & cpy) 50 | { 51 | this->_line_num = cpy._line_num; 52 | this->_type = cpy._type; 53 | this->_lineTab = cpy._lineTab; 54 | this->_error = cpy._error; 55 | this->_readInputMode = cpy._readInputMode; 56 | } 57 | 58 | Parser &Parser::operator=(Parser const & cpy) 59 | { 60 | this->_line_num = cpy._line_num; 61 | this->_type = cpy._type; 62 | this->_lineTab = cpy._lineTab; 63 | this->_error = cpy._error; 64 | this->_readInputMode = cpy._readInputMode; 65 | return (*this); 66 | } 67 | 68 | bool Parser::getError() const 69 | { 70 | return (this->_error); 71 | } 72 | 73 | const std::vector > &Parser::getLineTab() const 74 | { 75 | return (this->_lineTab); 76 | } 77 | 78 | std::vector Parser::stringToWordtab(const std::string line) 79 | { 80 | std::vector string_tab; 81 | std::string str; 82 | char c[2]; 83 | int i = 0; 84 | 85 | c[1] = '\0'; 86 | while (line[i] != '\0') 87 | { 88 | str = ""; 89 | if (line[i] != '\t' && line[i] != ' ' && line[i] != '\n') 90 | { 91 | while (line[i] != '\t' && line[i] != ' ' && line[i] != '\0' && line[i] != '\n') 92 | { 93 | c[0] = line[i]; 94 | str += &c[0]; 95 | i++; 96 | } 97 | string_tab.push_back(str); 98 | } 99 | if (line[i] != '\0') 100 | i++; 101 | } 102 | return (string_tab); 103 | } 104 | 105 | void Parser::parsIntegerValue(const std::string type, const std::string word) 106 | { 107 | size_t count; 108 | 109 | count = type.size(); 110 | if (word[count] != '(') 111 | throw logicError(ADVANCED_SYNTH, word); 112 | else 113 | count++; 114 | if (word[count] == '-') 115 | count++; 116 | if (word[count] == ')') 117 | throw logicError(ADVANCED_SYNTH, word); 118 | while (word[count] != '\0' && word[count] != ')') 119 | { 120 | if (word[count] < 48 || word[count] > 57) 121 | throw logicError(WRONG_VALUE, word); 122 | count++; 123 | } 124 | if (word[count] == '\0' || (word[count] == ')' && word[count + 1] != '\0')) 125 | throw logicError(ADVANCED_SYNTH, word); 126 | } 127 | 128 | void Parser::parsNonIntegerValue(const std::string type, const std::string word) 129 | { 130 | size_t count; 131 | bool check = false; 132 | 133 | 134 | count = type.size(); 135 | if (word[count] != '(') 136 | throw logicError(ADVANCED_SYNTH, word); 137 | else 138 | count++; 139 | if (word[count] == '-') 140 | count++; 141 | if (word[count] == ')') 142 | throw logicError(ADVANCED_SYNTH, word); 143 | while (word[count] != '\0' && word[count] != ')') 144 | { 145 | if (((word[count] < 48 || word[count] > 57) && word[count] != '.') 146 | || (word[count] == '.' && check == true)) 147 | throw logicError(WRONG_VALUE2, word); 148 | if (word[count] == '.') 149 | { 150 | if (word[count - 1] < 48 || word[count - 1] > 57 151 | || word[count + 1] < 48 || word[count + 1] > 57) 152 | throw logicError(WRONG_VALUE2, word); 153 | check = true; 154 | } 155 | count++; 156 | } 157 | if (word[count] == '\0' || (word[count] == ')' && word[count + 1] != '\0')) 158 | throw logicError(ADVANCED_SYNTH, word); 159 | } 160 | 161 | bool Parser::checkType(const std::string word) 162 | { 163 | for (int i = 0 ; type[i].ptr != NULL ; i++) 164 | { 165 | if (word.find(type[i].name) == 0) 166 | { 167 | (this->*(type[i].ptr))(type[i].name, word); 168 | return (true); 169 | } 170 | } 171 | return (false); 172 | } 173 | 174 | void Parser::checkAdvancedInstruc(const std::vector string_tab) 175 | { 176 | if (string_tab.size() != 2) 177 | throw logicError(ADVANCED_SYNTH, this->_line); 178 | if (this->checkType(string_tab[1]) == false) 179 | throw logicError(WRONG_TYPE, string_tab[1]); 180 | } 181 | 182 | void Parser::checkBasicInstruc(const std::vector string_tab) const 183 | { 184 | for (int i = 0 ; basic[i] != "" ; i++) 185 | { 186 | if (string_tab[0] == basic[i]) 187 | return ; 188 | } 189 | throw logicError(INVALID_INSTRUC, this->_line); 190 | } 191 | 192 | bool Parser::checkProgramEnd(const std::vector string_tab) 193 | { 194 | if (string_tab.size() != 1) 195 | throw logicError(INVALID_EXIT, string_tab[0]); 196 | if (string_tab[0] == SECOND_EXIT 197 | && this->_readInputMode == true) 198 | { 199 | if (this->_exit == false) 200 | throw std::runtime_error(EXIT_ABS); 201 | return (true); 202 | } 203 | else if (string_tab[0] == EXIT 204 | && this->_readInputMode == false) 205 | return (true); 206 | else 207 | { 208 | this->_exit = true; 209 | return (false); 210 | } 211 | } 212 | 213 | void Parser::commentLine(std::string & line) 214 | { 215 | int i = 0; 216 | 217 | while (line[i] != '\0') 218 | { 219 | if (line[i] == ';' && line[i + 1] == ';') 220 | return ; 221 | if (line[i] == ';' && line[i + 1] != ';') 222 | { 223 | line[i] = '\0'; 224 | i--; 225 | } 226 | i++; 227 | } 228 | } 229 | 230 | void Parser::startParsing(std::istream& is, bool _readInputMode) 231 | { 232 | std::string line; 233 | std::vector string_tab; 234 | 235 | this->_readInputMode = _readInputMode; 236 | while (std::getline(is, line)) 237 | { 238 | this->_line = line; 239 | try 240 | { 241 | commentLine(line); 242 | this->_line_num++; 243 | string_tab = this->stringToWordtab(line); 244 | this->_lineTab.push_back(string_tab); 245 | if (string_tab.size() != 0) 246 | { 247 | if (string_tab[0] == ASSERT || string_tab[0] == PUSH) 248 | checkAdvancedInstruc(string_tab); 249 | else if (string_tab[0] == EXIT || string_tab[0] == SECOND_EXIT) 250 | { 251 | if (checkProgramEnd(string_tab) == true) 252 | return ; 253 | } 254 | else if (string_tab.size() == 1) 255 | checkBasicInstruc(string_tab); 256 | else 257 | throw logicError(INVALID_INSTRUC, line); 258 | } 259 | } 260 | catch (const logicError & e) 261 | { 262 | this->_error = true; 263 | std::cout << ERROR_LINE << this->_line_num << SYNTHAXE << e.getStr() << e.what() << std::endl; 264 | if (this->_readInputMode == true) 265 | return ; 266 | } 267 | } 268 | if (this->_readInputMode == false && string_tab[0] != EXIT) 269 | throw std::runtime_error(EXIT_ABS); 270 | } 271 | --------------------------------------------------------------------------------