module.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * This file is part of Gomu.
  3. *
  4. * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
  5. *
  6. * Gomu is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Gomu is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <fstream>
  20. #include "module.hpp"
  21. #include "../../interpreter.hpp"
  22. using namespace Gomu;
  23. //-----------------------------
  24. // module_check(Context,Value)
  25. //-----------------------------
  26. Value module_check(Context& context,Value& value){
  27. Module* module=(Module*)value.ptr;
  28. string filename="ext/"+module->name+"/check";
  29. ifstream fs;
  30. fs.open(filename.c_str(),fstream::in);
  31. if(!fs) RuntimeError("File "+filename+" does not exist");
  32. string cmd;
  33. Value* v;
  34. bool error=false;
  35. size_t line=1;
  36. while(not error and getline(fs,cmd)){
  37. if(not cmd.empty() and cmd[0]!='#'){
  38. try{
  39. v=context.interpreter->eval_basic(cmd,context);
  40. }
  41. catch(Error err){
  42. error=true;
  43. context.interpreter->purge_tree();
  44. cout<<"[\033[34m"<<filename<<"\033[0m:\033[32m"<<line<<"\033[0m] ";
  45. err.disp(cout,cmd);
  46. cout<<endl;
  47. return Value(type_void,nullptr);
  48. }
  49. if(not error){
  50. if(v->type==type_boolean){
  51. bool flag=*(bool*)v->ptr;
  52. if(not flag){
  53. cout<<"\033[31mfailed\033[0m line "<<line<<"\033[0m"<<endl;
  54. return Value(type_void,nullptr);
  55. }
  56. }
  57. v->pdel();
  58. }
  59. }
  60. ++line;
  61. }
  62. cout<<"\033[32mpassed\033[0m"<<endl;
  63. return Value(type_void,nullptr);
  64. }
  65. //---------------------
  66. // module_types(void*)
  67. //---------------------
  68. void* module_types(void* v){
  69. Module* module=(Module*)v;
  70. ArrayValue *res=new ArrayValue(module->ntype);
  71. res->type=type_string;
  72. for(size_t i=0;i<module->ntype;++i){
  73. res->tab[i]=new string(module->types[i]);
  74. }
  75. return res;
  76. }