Parcourir la source

Refactor module base

Jean Fromentin il y a 8 ans
Parent
commit
a5cd007ad7
8 fichiers modifiés avec 160 ajouts et 67 suppressions
  1. 2 1
      ext/base/Makefile
  2. 19 0
      ext/base/array.cpp
  3. 20 0
      ext/base/array.hpp
  4. 25 45
      ext/base/init.cpp
  5. 27 19
      ext/base/module.cpp
  6. 21 1
      ext/base/module.hpp
  7. 24 1
      ext/base/string.cpp
  8. 22 0
      ext/base/string.hpp

+ 2 - 1
ext/base/Makefile

@@ -7,8 +7,9 @@ all: $(MOD)
 %.o:%.cpp %.hpp
 	$(CPP) -c $< -o $@
 
-$(MOD): init.cpp array.o string.o module.o
+$(MOD): init.cpp array.o kernel.o module.o string.o
 	$(CPP) -shared  $(LDFLAGS) $^ -o $@
 
 clean:
+	-$(RM) *~
 	-$(RM) $(MOD) *.o

+ 19 - 0
ext/base/array.cpp

@@ -1,3 +1,22 @@
+/**
+ * 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 "array.hpp"
 
 void* array_len(void* v){

+ 20 - 0
ext/base/array.hpp

@@ -1,3 +1,23 @@
+/**
+ * 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"
 
+//! Return array size
 void* array_len(void*);

+ 25 - 45
ext/base/init.cpp

@@ -1,55 +1,34 @@
-#include "../../interpreter.hpp"
-#include "array.hpp"
-#include "string.hpp"
-#include "module.hpp"
-
-using namespace std;
-
-Value type(Context&,Value& v){
-  if(v.type==type_symbol) return Value(type_type,((Value*)v.ptr)->type); 
-  else return Value(type_type,v.type);
-}
-
-Value del(Context&,Value& v){
-  ((Value*)v.ptr)->del();  
-  ((Value*)v.ptr)->type=type_void;
-  return Value(type_void,nullptr);
-}
-
-
-
- Value assignment(Context& context,Value& lhs,Value& rhs){
-    Value* rhse=rhs.eval();
-    if(lhs.ptr!=nullptr){
-      //Prevent auto assignement
-      if(((Value*)lhs.ptr)->ptr==rhse->ptr) return rhs;
-      Symbol* symbol=(Symbol*)lhs.ptr;
-      if(symbol->locked) ContextError("The symbol is locked");
-      ((Value*)lhs.ptr)->del();
-    }
-    if(rhs.type==type_symbol){
-      copyValue((Value*)lhs.ptr,rhse);
-    }
-    else{
-      ((Value*)lhs.ptr)->type=rhs.type;
-      ((Value*)lhs.ptr)->ptr=rhs.ptr;
-      rhs.type=type_void;
-      rhs.ptr=nullptr;
-    }
-    return lhs;
-  }
+/**
+ * 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 "init.hpp"
 
 extern "C"{
   
   Gomu::Type types[]={
     TYPE_SENTINEL
   };
-
  
   //--- Functions ---//
   Gomu::Module::Function functions[]={
     {"Integer","len",{"String"},(void*)string_len},
-    //{"Integer","len",{"Array"},(void*)array_len},
+    {"Integer","len",{"Array"},(void*)array_len},
     FUNC_SENTINEL
   };
   
@@ -60,12 +39,13 @@ extern "C"{
     FUNC_SENTINEL
   };
   
-  //--- Meta functions ---//
+  //--- Contextual functions ---//
   Gomu::Module::Function contextual_functions[]={
-    {"Type","type",{"Generic"},(void*)type},
-    {"Generic","operator=",{"Symbol","Generic"},(void*)assignment},
     {"Void","delete",{"Symbol"},(void*)del},
+    //{"Void","load",{"String"},(void*)load},
+    {"Generic","operator=",{"Symbol","Generic"},(void*)assignment},
     {"Array","symbols",{"Type"},(void*)symbols},
+    {"Type","type",{"Generic"},(void*)type},
     FUNC_SENTINEL
   };
 }

+ 27 - 19
ext/base/module.cpp

@@ -1,29 +1,37 @@
+/**
+ * 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"
 #include "../../interpreter.hpp"
 
+//---------------------
+// module_types(void*)
+//---------------------
+
 void* module_types(void* v){
-  Gomu::Module* module=(Gomu::Module*)v;
-  Gomu::ArrayValue *res=new Gomu::ArrayValue(module->ntype);
-  res->type=Gomu::type_string;
+  Module* module=(Module*)v;
+  ArrayValue *res=new ArrayValue(module->ntype);
+  res->type=type_string;
   for(size_t i=0;i<module->ntype;++i){
     res->tab[i]=new string(module->types[i]);
   }
   return res;
 }
 
-Value symbols(Context& context,Value& v){
-  Type* type=(Type*)v.ptr;
-  string str=type->name+'.';
-  deque<string> stack;
-  auto it=context.symbols.lower_bound(str);
-  while(it->first.substr(0,str.size())==str){
-    stack.push_back(it->first);
-    ++it;
-  }
-  ArrayValue* res=new ArrayValue(stack.size());
-  res->type=type_string;
-  for(size_t i=0;i<stack.size();++i){
-    res->tab[i]=new string(stack[i]);
-  }
-  return Value(type_array,res);
-}
+

+ 21 - 1
ext/base/module.hpp

@@ -1,7 +1,27 @@
+/**
+ * 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 <deque>
 #include "../../module.hpp"
 
 using namespace Gomu;
 
+//! Return types defined in a module
 void* module_types(void*);
-Value symbols(Context& context,Value& v);
+

+ 24 - 1
ext/base/string.cpp

@@ -1,6 +1,29 @@
+/**
+ * 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 "string.hpp"
 
+//-------------------
+// string_len(void*)
+//-------------------
+
 void* string_len(void* v){
-  return Gomu::to_integer(((string*)v)->size());
+  return to_integer(((string*)v)->size());
 }
 

+ 22 - 0
ext/base/string.hpp

@@ -1,3 +1,25 @@
+/**
+ * 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"
 
+using namespace Gomu;
+
+//! Return the length of a string
 void* string_len(void*);