kernel.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. //------------------------------------
  22. // assignment(Context&,Value&,Value&)
  23. //------------------------------------
  24. Value assignment(Context& context,Value& lhs,Value& rhs){
  25. Value* rhse=rhs.eval();
  26. if(lhs.ptr!=nullptr){
  27. //Prevent auto assignement
  28. if(((Value*)lhs.ptr)->ptr==rhse->ptr) return rhs;
  29. Symbol* symbol=(Symbol*)lhs.ptr;
  30. if(symbol->locked) ContextError("The symbol is locked");
  31. ((Value*)lhs.ptr)->del();
  32. }
  33. if(rhs.type==type_symbol){
  34. copyValue((Value*)lhs.ptr,rhse);
  35. }
  36. else{
  37. ((Value*)lhs.ptr)->type=rhs.type;
  38. ((Value*)lhs.ptr)->ptr=rhs.ptr;
  39. rhs.type=type_void;
  40. rhs.ptr=nullptr;
  41. }
  42. return lhs;
  43. }
  44. //----------------------
  45. // del(Context&,Value&)
  46. //----------------------
  47. Value del(Context&,Value& v){
  48. ((Value*)v.ptr)->del();
  49. ((Value*)v.ptr)->type=type_void;
  50. return Value(type_void,nullptr);
  51. }
  52. //---------
  53. // symbols
  54. //---------
  55. Value symbols(Context& context,Value& v){
  56. Type* type=(Type*)v.ptr;
  57. string str=type->name+'.';
  58. deque<string> stack;
  59. auto it=context.symbols.lower_bound(str);
  60. while(it->first.substr(0,str.size())==str){
  61. stack.push_back(it->first);
  62. ++it;
  63. }
  64. ArrayValue* res=new ArrayValue(stack.size());
  65. res->type=type_string;
  66. for(size_t i=0;i<stack.size();++i){
  67. res->tab[i]=new string(stack[i]);
  68. }
  69. return Value(type_array,res);
  70. }
  71. //-----------------------
  72. // type(Context&,Value&)
  73. //-----------------------
  74. Value type(Context&,Value& v){
  75. if(v.type==type_symbol) return Value(type_type,((Value*)v.ptr)->type);
  76. else return Value(type_type,v.type);
  77. }