anydice/tokens.lex

97 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

2024-08-16 10:16:51 -04:00
%{
#include <string.h> // strncmp
#include "rust.h" // bst_* functions
#define YYSTYPE BSTNode
#include "grammar.tab.h" // PLUS MINUS etc.
extern YYSTYPE yylval;
extern Frame *current_frame;
#include <readline/readline.h>
#include <readline/history.h>
#define YY_INPUT(buf,result,max_size) result = mygetinput(buf, max_size);
static int mygetinput(char *buf, int size) {
char *line;
if (feof(yyin))
return YY_NULL;
line = readline("> ");
if(line == NULL)
return YY_NULL;
size_t len = strnlen(line, size);
if (len > size-2)
return YY_NULL;
snprintf(buf, size, "%s\n", line);
add_history(line);
free(line);
return strlen(buf);
}
%}
%option noyywrap
white [ \t]+
integer [0-9]+
ident [a-zA-Z][a-zA-Z0-9_]*
%%
{white} { ; }
{integer}"d"{integer} {
yylval = bst_dice(yytext);
return DICE;
}
{integer} {
yylval = bst_number(yytext);
return NUMBER;
}
{ident} {
// check for keywords
// note to self: don't pass yyleng as the max length,
// because then things like "ret" would pass as "return"
if (strncmp(yytext, "return", 6) == 0) {
return RETURN;
}
if (strncmp(yytext, "function", 8) == 0) {
return FUNCTION;
}
if (strncmp(yytext, "drop", 4) == 0) {
return DROP;
}
yylval = bst_variable(yytext);
return IDENT;
}
"+" return PLUS;
"-" return MINUS;
"**" return FLATMUL;
"*" return MUL;
"/" return DIV;
"^" return POWER;
"(" return LEFT;
")" return RIGHT;
"!" return FACT;
"|" return BAR;
"\n" return END;
"<=" return LESSEQ;
"<" return LESS;
">=" return GREATEREQ;
">" return GREATER;
"==" return EQ;
"=" return ASSIGN;
"," return ARGSEP;
"{" return BLOCKSTART;
"}" return BLOCKEND;
%%