kernel.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "kernel.hpp"
  20. #include "../../interpreter.hpp"
  21. #include <fstream>
  22. //------------------------------------
  23. // assignment(Context&,Value&,Value&)
  24. //------------------------------------
  25. Value assignment(Context& context,Value& lhs,Value& rhs){
  26. Value* rhse=rhs.eval();
  27. if(lhs.ptr!=nullptr){
  28. //Prevent auto assignement
  29. if(((Value*)lhs.ptr)->ptr==rhse->ptr) return rhs;
  30. Symbol* symbol=(Symbol*)lhs.ptr;
  31. if(symbol->locked) ContextError("The symbol is locked");
  32. ((Value*)lhs.ptr)->del();
  33. }
  34. if(rhs.type==type_symbol){
  35. copyValue((Value*)lhs.ptr,rhse);
  36. }
  37. else{
  38. ((Value*)lhs.ptr)->type=rhs.type;
  39. ((Value*)lhs.ptr)->ptr=rhs.ptr;
  40. rhs.type=type_void;
  41. rhs.ptr=nullptr;
  42. }
  43. return lhs;
  44. }
  45. //----------------------
  46. // del(Context&,Value&)
  47. //----------------------
  48. Value del(Context&,Value& v){
  49. ((Value*)v.ptr)->del();
  50. ((Value*)v.ptr)->type=type_void;
  51. return Value(type_void,nullptr);
  52. }
  53. //--------------------------
  54. // execute(Context&,Value&)
  55. //--------------------------
  56. Value execute(Context& context,Value& file){
  57. const string &filename="worksheet/"+*((string*)file.ptr);
  58. ifstream fs;
  59. fs.open(filename.c_str(),fstream::in);
  60. string cmd;
  61. size_t line=1;
  62. while(getline(fs,cmd)){
  63. context.interpreter->eval(cmd,context,false);
  64. }
  65. fs.close();
  66. return Value(type_void,nullptr);
  67. }
  68. //---------
  69. // symbols
  70. //---------
  71. Value symbols(Context& context,Value& v){
  72. Type* type=(Type*)v.ptr;
  73. string str=type->name+'.';
  74. deque<string> stack;
  75. auto it=context.symbols.lower_bound(str);
  76. while(it->first.substr(0,str.size())==str){
  77. stack.push_back(it->first);
  78. ++it;
  79. }
  80. ArrayValue* res=new ArrayValue(stack.size());
  81. res->type=type_string;
  82. for(size_t i=0;i<stack.size();++i){
  83. res->tab[i]=new string(stack[i]);
  84. }
  85. return Value(type_array,res);
  86. }
  87. //-----------------------
  88. // type(Context&,Value&)
  89. //-----------------------
  90. Value type(Context&,Value& v){
  91. if(v.type==type_symbol) return Value(type_type,((Value*)v.ptr)->type);
  92. else return Value(type_type,v.type);
  93. }