The Sol Programming Language!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tokenizer.lex 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. %{
  2. #define YYSTYPE void *
  3. #include "ast.h"
  4. #include "parser.tab.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. void yyerror(YYLTYPE *, char *);
  9. int yywrap(void);
  10. char *str, *curptr;
  11. int cursz, chars;
  12. #define SZMUL 128
  13. void str_init(void) {
  14. str = malloc(SZMUL);
  15. curptr = str;
  16. cursz = SZMUL;
  17. chars = 0;
  18. }
  19. void str_putc(char c) {
  20. *curptr++ = c;
  21. chars++;
  22. if(chars >= cursz) {
  23. str = realloc(str, cursz + SZMUL);
  24. curptr = str + chars;
  25. cursz += SZMUL;
  26. }
  27. }
  28. /* http://stackoverflow.com/questions/656703/how-does-flex-support-bison-location-exactly */
  29. /* Many thanks to hugomg and David Elson! */
  30. static void update_loc(YYLTYPE *yylloc, char *yytext){
  31. static int curr_line = 1;
  32. static int curr_col = 1;
  33. yylloc->first_line = curr_line;
  34. yylloc->first_column = curr_col;
  35. {char * s; for(s = yytext; *s != '\0'; s++){
  36. if(*s == '\n'){
  37. curr_line++;
  38. curr_col = 1;
  39. }else{
  40. curr_col++;
  41. }
  42. }}
  43. yylloc->last_line = curr_line;
  44. yylloc->last_column = curr_col-1;
  45. }
  46. #define YY_USER_ACTION update_loc(yylloc, yytext);
  47. %}
  48. DIGIT [0-9]
  49. HEXDIGIT [0-9a-fA-F]
  50. ALPHA [a-zA-Z]
  51. IDENT [a-zA-Z_][a-zA-Z0-9_]*
  52. /* This is the right way to do it, but it keeps generating token $undefined.
  53. %x STRING
  54. \" { str_init(); BEGIN STRING; }
  55. <STRING>\\n { str_putc('\n'); }
  56. <STRING>\\t { str_putc('\t'); }
  57. <STRING>\\b { str_putc('\b'); }
  58. <STRING>\\r { str_putc('\r'); }
  59. <STRING>\\x{HEXDIGIT}{HEXDIGIT} { str_putc(strtol(yytext+2, NULL, 16)); }
  60. <STRING>\\\" { str_putc('"'); }
  61. <STRING>\" { str_putc('\0'); yylval = str; BEGIN 0; return STRING; }
  62. <STRING>. { str_putc(*yytext); }
  63. */
  64. %option bison-bridge bison-locations
  65. %%
  66. {DIGIT}+"."{DIGIT}* { *yylval = malloc(sizeof(double)); *((double *) *yylval) = atof(yytext); return FLOAT; }
  67. {DIGIT}+ { *yylval = malloc(sizeof(long)); *((long *) *yylval) = atol(yytext); return INT; }
  68. \"[^"]*\" { *yylval = strdup(yytext+1); ((char *) *yylval)[yyleng-2] = 0; return STRING; }
  69. \'[^']*\' { *yylval = strdup(yytext+1); ((char *) *yylval)[yyleng-2] = 0; return STRING; }
  70. if { return IF; }
  71. then { return THEN; }
  72. else { return ELSE; }
  73. while { return WHILE; }
  74. for { return FOR; }
  75. in { return IN; }
  76. do { return DO; }
  77. func { return FUNC; }
  78. lambda { return LAMBDA; }
  79. return { return RETURN; }
  80. break { return BREAK; }
  81. continue { return CONTINUE; }
  82. end { return END; }
  83. None { return NONE; }
  84. "+" { return PLUS; }
  85. "-" { return MINUS; }
  86. "*" { return STAR; }
  87. "/" { return SLASH; }
  88. "%" { return PERCENT; }
  89. "mod" { return PERCENT; }
  90. "**" { return DSTAR; }
  91. "&" { return BAND; }
  92. "|" { return BOR; }
  93. "^" { return BXOR; }
  94. "~" { return BNOT; }
  95. "&&" { return LAND; }
  96. "and" { return LAND; }
  97. "||" { return LOR; }
  98. "or" { return LOR; }
  99. "!" { return LNOT; }
  100. "not" { return LNOT; }
  101. "true" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 1; return INT; }
  102. "True" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 1; return INT; }
  103. "false" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 0; return INT; }
  104. "False" { *yylval = malloc(sizeof(long)); *((long *) *yylval) = 0; return INT; }
  105. "=" { return ASSIGN; }
  106. "+=" { return ASSIGNPLUS; }
  107. "-=" { return ASSIGNMINUS; }
  108. "*=" { return ASSIGNSTAR; }
  109. "/=" { return ASSIGNSLASH; }
  110. "**=" { return ASSIGNDSTAR; }
  111. "&=" { return ASSIGNBAND; }
  112. "|=" { return ASSIGNBOR; }
  113. "^=" { return ASSIGNBXOR; }
  114. "==" { return EQUAL; }
  115. "!=" { return NEQUAL; }
  116. "<" { return LESS; }
  117. ">" { return GREATER; }
  118. "<=" { return LESSEQ; }
  119. ">=" { return GREATEREQ; }
  120. ">>" { return RSHIFT; }
  121. "<<" { return LSHIFT; }
  122. "{" { return LBRACE; }
  123. "}" { return RBRACE; }
  124. "[" { return LBRACKET; }
  125. "]" { return RBRACKET; }
  126. "(" { return LPAREN; }
  127. ")" { return RPAREN; }
  128. "." { return DOT; }
  129. ":" { return COLON; }
  130. ";" { return SEMICOLON; }
  131. "," { return COMMA; }
  132. "#" { return POUND; }
  133. "!!!" { return TBANG; }
  134. {IDENT} { *yylval = (void *) strdup(yytext); return IDENT; }
  135. --[^\n]*\n /* Skip comments */
  136. [ \t\n]+ /* Skip whitespace */
  137. %%
  138. int yywrap(void) {
  139. return 1;
  140. }
  141. void yyerror(YYLTYPE *locp, char *err) {
  142. puts(err);
  143. printf("(at lines %d-%d, cols %d-%d)\n", locp->first_line, locp->last_line, locp->first_column, locp->last_column);
  144. }
  145. stmt_node *sol_compile(const char *prgstr) {
  146. stmt_node *program = NULL;
  147. YY_BUFFER_STATE buf = yy_scan_string(prgstr);
  148. yyparse(&program);
  149. yy_delete_buffer(buf);
  150. return program;
  151. }
  152. stmt_node *sol_compile_file(FILE *prgfile) {
  153. stmt_node *program = NULL;
  154. YY_BUFFER_STATE buf = yy_create_buffer(prgfile, YY_BUF_SIZE);
  155. yy_switch_to_buffer(buf);
  156. yyparse(&program);
  157. yy_delete_buffer(buf);
  158. return program;
  159. }