123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /**
- * This file is part of Gomu.
- *
- * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
- *
- * Gomu is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Gomu is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "module.hpp"
- namespace Gomu{
- //**************
- //* ArrayValue *
- //**************
-
- ArrayValue::ArrayValue(size_t s){
- size=s;
- type=nullptr;
- if(s==0) tab=nullptr;
- else tab=new void*[size];
- }
- //*********
- //* Error *
- //*********
-
- void
- Error::disp(ostream& os,const string& cmd) const{
- switch(type){
- case errBug:
- os<<"\033[31mBug\033[0m : "<<msg;
- os<<" (\033[34m"<<file<<"\033[0m:\033[32m"<<line<<"\033[0m)";
- return;
- case errContext:
- os<<"\033[31mContext error\033[0m : "<<msg;
- os<<" (\033[34m"<<file<<"\033[0m:\033[32m"<<line<<"\033[0m)";
- return;
- case errRuntime:
- os<<"\033[31mRuntime error\033[0m : "<<msg;
- return;
- case errSyntax:
- os<<"\033[31mSyntax error\033[0m : "<<msg;
- os<<"(\033[34m"<<file<<"\033[0m:\033[32m"<<line<<"\033[0m)";
- break;
- default:
- os<<"\033[31mError\033[0m : ";
- break;
- }
- os<<endl<<"@ ";
- if(first>0) os<<cmd.substr(0,first);
- os<<"\033[32m"<<cmd.substr(first,last-first+1)<<"\033[0m";
- if(last+1<cmd.length()) os<<cmd.substr(last+1,string::npos);
- return;
- }
-
- //**********
- //* Module *
- //**********
- //------------------
- // Module::Module()
- //------------------
-
- Module::Module(){
- types=nullptr;
- functions=nullptr;
- member_functions=nullptr;
- contextual_functions=nullptr;
- }
-
- //---------------------
- // Module::init(Type*)
- //---------------------
-
- void Module::init(Type* types){
- size_t i=0;
- if(types==nullptr) ntype=0;
- else{
- while(types[i].disp!=nullptr) ++i;
- ntype=i;
- i=0;
- }
- if(functions==nullptr) nfunc=0;
- else{
- while(functions[i].ptr!=nullptr) ++i;
- nfunc=i;
- i=0;
- }
- if(member_functions==nullptr) nmfunc=0;
- else{
- while(member_functions[i].ptr!=nullptr) ++i;
- nmfunc=i;
- i=0;
- }
- if(contextual_functions==nullptr) ncfunc=0;
- else{
- while(contextual_functions[i].ptr!=nullptr) ++i;
- ncfunc=i;
- i=0;
- }
- if(symbols==nullptr) nsymb=0;
- else{
- while(symbols[i].ptr!=nullptr) ++i;
- nsymb=i;
- }
- }
- //********
- //* Type *
- //********
-
- //-------------------------------------------------------
- // Type::Type(string,DispFunc,DelFunc,CopyFunc,CompFunc)
- //-------------------------------------------------------
-
- Type::Type(string _name,DispFunc _disp,DelFunc _del,CopyFunc _copy,CompFunc _comp){
- name=_name;
- disp=_disp;
- del=_del;
- copy=_copy;
- comp=_comp;
- }
- //-------------------------
- // Type::Type(const Type&)
- //-------------------------
-
- Type::Type(const Type& t){
- name=t.name;
- disp=t.disp;
- del=t.del;
- copy=t.copy;
- comp=t.comp;
- }
- //---------------------------------
- // Type::Type(const Module::Type&)
- //---------------------------------
-
- Type::Type(const Module::Type& t){
- name=t.name;
- disp=t.disp;
- del=t.del;
- copy=t.copy;
- comp=t.comp;
- }
-
- //**************
- //* TupleValue *
- //**************
-
- TupleValue::TupleValue(size_t s){
- size=s;
- tab=(s==0)?nullptr:new Value[s];
- }
-
- //*********
- //* Value *
- //*********
- //---------------
- // Value::copy()
- //---------------
-
- Value
- Value::copy(){
- Value res;
- res.type=type;
- res.ptr=type->copy(ptr);
- return res;
- }
- //---------------
- // Value::disp()
- //---------------
-
- string
- Value::disp(){
- if(type==type_symbol) return ((Value*)ptr)->disp();
- if(ptr!=nullptr and type!=nullptr) return type->disp(ptr);
- else return "";
- }
- //***********************
- //* Auxiliary functions *
- //***********************
- //------------------
- // get_slong(void*)
- //------------------
-
- int64
- get_slong(void* v){
- fmpz* z=(fmpz*)v;
- if(fmpz_fits_si(z)) return fmpz_get_si(z);
- else RuntimeError("Integer too huge to fit slong");
- }
- //------------------
- // to_boolean(bool)
- //------------------
-
- void*
- to_boolean(bool b){
- char* res=new char;
- *res=(b?1:0);
- return res;
- }
- //-------------------
- // to_integer(slong)
- //-------------------
-
- void*
- to_integer(slong n){
- fmpz* z=new fmpz;
- fmpz_init(z);
- fmpz_set_si(z,n);
- return z;
- }
- //----------------
- // no_copy(void*)
- //----------------
-
- void*
- no_copy(void*){
- ContextError("Copy is undefined for this type");
- }
- //----------------------
- // no_comp(void*,void*)
- //----------------------
- int
- no_comp(void*,void*){
- ContextError("Comparison is undefined for this type");
- }
- }
|