/Lexer.cpp

https://bitbucket.org/hardtack/resoup · C++ · 65 lines · 62 code · 2 blank · 1 comment · 4 complexity · cfa8584e5c7704fb72291a4faeaa28aa MD5 · raw file

  1. #include "Lexer.h"
  2. #include "LispObject.h"
  3. #include <string.h>
  4. #include "Functions.h"
  5. using namespace Functions;
  6. CHARQUEUE* Lexer::lexing (const char *str) throw(StringNotClosed &){
  7. int len = strlen(str);
  8. CHARQUEUE* qu = new CHARQUEUE();
  9. for(int idx = 0; idx < len; idx++){
  10. char c = str[idx];
  11. switch(c){
  12. case '(' :
  13. qu->push(stringcpy("("));
  14. break;
  15. case ')':
  16. qu->push(stringcpy(")"));
  17. break;
  18. case '\'':
  19. qu->push(stringcpy("'"));
  20. break;
  21. case '\n':
  22. case ' ':
  23. case '\t':
  24. break;
  25. /*String*/
  26. case '\"':{
  27. char *string = new char[2];
  28. string[0] = c;
  29. string[1] = '\0';
  30. do{
  31. char *tmp = string;
  32. int len = strlen(tmp)+2;
  33. string = new char[len];
  34. strcpy(string,tmp);
  35. string[strlen(tmp)] = str[++idx];
  36. string[strlen(tmp) + 1] = '\0';
  37. delete[] tmp;
  38. } while(str[idx] != '"');
  39. qu->push(string);
  40. break;
  41. }
  42. default:{
  43. char *string = new char[2];
  44. string[0] = c;
  45. string[1] = '\0';
  46. while(str[idx + 1] != ' ' && str[idx + 1] != ')' && str[idx + 1] != '('
  47. && str[idx + 1] != '\"' && str[idx + 1] != '\0' && str[idx + 1] != '\n'){
  48. char *tmp = string;
  49. int len = strlen(tmp)+2;
  50. string = new char[len];
  51. strcpy(string,tmp);
  52. string[strlen(tmp)] = str[++idx];
  53. string[strlen(tmp) + 1] = '\0';
  54. delete[] tmp;
  55. }
  56. qu->push(string);
  57. break;
  58. }
  59. }
  60. }
  61. return qu;
  62. }