init.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "../../interpreter.hpp"
  2. #include "array.hpp"
  3. #include "string.hpp"
  4. #include "module.hpp"
  5. using namespace std;
  6. Value type(Context&,Value& v){
  7. if(v.type==type_symbol) return Value(type_type,((Value*)v.ptr)->type);
  8. else return Value(type_type,v.type);
  9. }
  10. Value del(Context&,Value& v){
  11. ((Value*)v.ptr)->del();
  12. ((Value*)v.ptr)->type=type_void;
  13. return Value(type_void,nullptr);
  14. }
  15. Value assignment(Context& context,Value& lhs,Value& rhs){
  16. Value* rhse=rhs.eval();
  17. if(lhs.ptr!=nullptr){
  18. //Prevent auto assignement
  19. if(((Value*)lhs.ptr)->ptr==rhse->ptr) return rhs;
  20. Symbol* symbol=(Symbol*)lhs.ptr;
  21. if(symbol->locked) ContextError("The symbol is locked");
  22. ((Value*)lhs.ptr)->del();
  23. }
  24. if(rhs.type==type_symbol){
  25. copyValue((Value*)lhs.ptr,rhse);
  26. }
  27. else{
  28. ((Value*)lhs.ptr)->type=rhs.type;
  29. ((Value*)lhs.ptr)->ptr=rhs.ptr;
  30. rhs.type=type_void;
  31. rhs.ptr=nullptr;
  32. }
  33. return lhs;
  34. }
  35. extern "C"{
  36. Gomu::Type types[]={
  37. TYPE_SENTINEL
  38. };
  39. //--- Functions ---//
  40. Gomu::Module::Function functions[]={
  41. {"Integer","len",{"String"},(void*)string_len},
  42. //{"Integer","len",{"Array"},(void*)array_len},
  43. FUNC_SENTINEL
  44. };
  45. //--- Member functions ---//
  46. Gomu::Module::Function member_functions[]={
  47. {"Integer","len",{"String"},(void*)string_len},
  48. {"Array","types",{"Module"},(void*)module_types},
  49. FUNC_SENTINEL
  50. };
  51. //--- Meta functions ---//
  52. Gomu::Module::Function contextual_functions[]={
  53. {"Type","type",{"Generic"},(void*)type},
  54. {"Generic","operator=",{"Symbol","Generic"},(void*)assignment},
  55. {"Void","delete",{"Symbol"},(void*)del},
  56. {"Array","symbols",{"Type"},(void*)symbols},
  57. FUNC_SENTINEL
  58. };
  59. }