kernel.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "kernel.hpp"
  21. #include "../../interpreter.hpp"
  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)->pdel();
  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)->pdel();
  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. bool error=false;
  63. Value* res;
  64. while(not error and getline(fs,cmd)){
  65. try{
  66. res=context.interpreter->eval_basic(cmd,context);
  67. }
  68. catch(Error err){
  69. error=true;
  70. context.interpreter->purge_tree();
  71. cout<<"Line "<<line<<" : ";
  72. err.disp(cout,cmd);
  73. cout<<endl;
  74. }
  75. if(not error){
  76. res->pdel();
  77. }
  78. }
  79. fs.close();
  80. return Value(type_void,nullptr);
  81. }
  82. //---------
  83. // symbols
  84. //---------
  85. Value member_symbols(Context& context,Value& v){
  86. Type* type=(Type*)v.ptr;
  87. string str=type->name+'.';
  88. deque<string> stack;
  89. auto it=context.symbols.lower_bound(str);
  90. while(it->first.substr(0,str.size())==str){
  91. stack.push_back(it->first);
  92. ++it;
  93. }
  94. ArrayValue* res=new ArrayValue(stack.size());
  95. res->type=type_string;
  96. for(size_t i=0;i<stack.size();++i){
  97. res->tab[i]=new string(stack[i]);
  98. }
  99. return Value(type_array,res);
  100. }
  101. //-----------------------
  102. // type(Context&,Value&)
  103. //-----------------------
  104. Value type(Context&,Value& v){
  105. if(v.type==type_symbol) return Value(type_type,((Value*)v.ptr)->type);
  106. else return Value(type_type,v.type);
  107. }
  108. //-------------------------------
  109. // equal(Context&,Value&,Value&)
  110. //-------------------------------
  111. Value equality(Context&,Value& v1,Value& v2){
  112. Value res;
  113. res.type=type_boolean;
  114. Value* a=v1.eval();
  115. Value* b=v2.eval();
  116. if(a->type!=b->type){
  117. res.ptr=to_boolean(false);
  118. return res;
  119. }
  120. Type* type=a->type;
  121. res.ptr=to_boolean(type->comp(a->ptr,b->ptr)==0);
  122. return res;
  123. }