add a basm code generator
[bosc.git] / src / lexer.l
blob02a292e72331ae4fcc667e2bf3fecec661b833f7
1 %{\r
2 #include <stdio.h>\r
3 #include <string.h>\r
4 #include <ctype.h>\r
5 \r
6 #include "logging.h"\r
7 // hack so compiler doesn't complain\r
8 #define ASTNode void\r
9 #include "parser.tab.hpp"\r
11 int lineno = 1;\r
12 char *curfile = NULL;\r
14 double angular_const = 182;\r
15 double linear_const = 163840;\r
17 %}\r
19 number          ([0-9]+\.?[0-9]*)|([0-9]*\.[0-9]+)\r
21 %%\r
22 ^#[ \t0-9]+\"[a-zA-Z0-9.\-+<>/\\*$#@]+\"[ \t0-9]*$ {       /* GNU cpp line info */\r
23                           char tmp[strlen(yytext)];\r
24                           sscanf(yytext, "# %d %s", &lineno, tmp);\r
25                           --lineno;\r
26                           free(curfile);\r
27                           curfile = strdup(tmp+1);\r
28                           curfile[strlen(curfile)-1] = 0;\r
29                         }\r
30 ^"#pragma linear"[ \t]+[0-9]+ { sscanf(yytext, "#pragma linear %lf", &linear_const); }\r
31 ^"#pragma angular"[ \t]+[0-9]+ { sscanf(yytext, "#pragma angular %lf", &angular_const); }\r
32 ^"#pragma"              { return PRAGMA; }\r
33 ^#.+                    { LOG("unknown directive"); }\r
34 "set-signal-mask"       { return SET_SIGNAL_MASK; }\r
35 "signal"                { return RAISE_SIGNAL; }\r
36 "turn"                  { return DO_TURN; }\r
37 "move"                  { return DO_MOVE; }\r
38 "spin"                  { return DO_SPIN; }\r
39 "accelerate"            { return ACCELERATE; }\r
40 "speed"                 { return SPEED; }\r
41 "now"                   { return SPEED_NOW; }\r
42 "along"                 { return ALONG; }\r
43 "around"                { return AROUND; }\r
44 "to"                    { return TO; }\r
45 "static-var"            { return DECL_STATIC_VAR; }\r
46 "var"                   { return DECL_VAR; }\r
47 "piece"                 { return DECL_PIECE; }\r
48 [xyz]"-axis"            { yylval.number = tolower(*yytext) - 'x'; return AXIS; }\r
49 "start-script"          { return START_SCRIPT; }\r
50 "call-script"           { return CALL_SCRIPT; }\r
51 "explode"               { return EXPLODE; }\r
52 "type"                  { return EXPLODE_TYPE; }\r
53 "emit-sfx"              { return EMIT_SFX; }\r
54 "from"                  { return EMIT_FROM; }\r
55 "get"                   { return COB_GET; }\r
56 "set"                   { return COB_SET; }\r
57 "sleep"                 { return COB_SLEEP; }\r
58 "wait-for-move"         { return WAIT_FOR_MOVE; }\r
59 "wait-for-turn"         { return WAIT_FOR_TURN; }\r
60 "stop-spin"             { return STOP_SPIN; }\r
61 "attach-unit"           { return ATTACH_UNIT; }\r
62 "drop-unit"             { return ATTACH_UNIT; }\r
63 "for"                   { return FOR; }\r
64 "while"                 { return WHILE; }\r
65 "if"                    { return IF; }\r
66 "else"                  { return ELSE; }\r
67 "do"                    { return DO; }\r
68 "rand"                  { return RANDOM_NUMBER; }\r
69 "show"                  { return SHOW; }\r
70 "hide"                  { return HIDE; }\r
71 "cache"                 { return CACHE; }\r
72 "dont-cache"            { return DONT_CACHE; }\r
73 "shade"                 { return SHADE; }\r
74 "dont-shade"            { return DONT_SHADE; }\r
75 "play-sound"            { return PLAY_SOUND; }\r
76 "true"                  { yylval.number = 1; return NUMBER; }\r
77 "false"                 { yylval.number = 0; return NUMBER; }\r
78 "return"                { return RETURN; }\r
79 "continue"              { return CONTINUE; }\r
80 "break"                 { return BREAK; }\r
81 "goto"                  { return GOTO; }\r
82 "not"                   { return '!'; }\r
83 "and"                   { return BOOL_AND; }\r
84 "or"                    { return BOOL_AND; }\r
86 [a-zA-Z_][a-zA-Z0-9_]*  { yylval.string = strdup(yytext);\r
87                           return IDENTIFIER; }\r
88 {number}                { sscanf(yytext, "%lf", &yylval.number);\r
89                           return NUMBER; }\r
90 \<[ \t]*-?[ \t]*{number}+[ \t]*\> {\r
91                           sscanf(yytext+1, "%lf", &yylval.number);\r
92                           yylval.number *= angular_const;\r
93                           return NUMBER;\r
94                         }\r
95 \[[ \t]*-?[ \t]*{number}+[ \t]*\] {\r
96                           sscanf(yytext+1, "%lf", &yylval.number);\r
97                           yylval.number *= linear_const;\r
98                           return NUMBER;\r
99                         }\r
100                           \r
101 "<("                    { return ANGULAR_EXPR_OPEN; }\r
102 ")>"                    { return ANGULAR_EXPR_CLOSE; }\r
103 \{                      { return *yytext; }\r
104 \}                      { return *yytext; }\r
105 \(                      { return *yytext; }\r
106 \)                      { return *yytext; }\r
107 ;                       { return *yytext; }\r
108 ,                       { return *yytext; }\r
109 "++"                    { return INCREMENT; }\r
110 "--"                    { return DECREMENT; }\r
111 "&&"                    { return BOOL_AND; }\r
112 "||"                    { return BOOL_OR; }\r
113 [?|+&\-%*/\^!~]         { return *yytext; }\r
114 [\[\]]                  { return *yytext; }\r
115 "=="                    { return CMP_EQ; }\r
116 \>                      { return *yytext; }\r
117 \<                      { return *yytext; }\r
118 ">="                    { return CMP_GTE; }\r
119 "<="                    { return CMP_LTE; }\r
120 "!="                    { return CMP_NEQ; }\r
121 "<<"                    { return SHIFT_LEFT; }\r
122 ">>"                    { return SHIFT_RIGHT; }\r
123 "="                     { return *yytext; }\r
124 ":"                     { return *yytext; }\r
125 \n                      { ++lineno; }\r
126 [ \t]+                  /* ignore whitespace */;\r
127 .                       { EPRINTF("%s:%d: error: stray \"%s\" in source", curfile, lineno, yytext); exit(1); return 0; }\r
128 %%\r