4522fab16aa6e74dc5982267bcb9378adeb37c9f
[fw/sdcc] / sim / ucsim / cmd.src / cmdlex.l
1 %{
2 #include "cmdlexcl.h"
3 #include "cmdpars.h"
4
5 static YY_cl_ucsim_parser_STYPE *yylval;
6
7 int yywrap(void);
8
9 #define tok(x) (YY_cl_ucsim_parser_CLASS::x)
10 #define lexer_obj (dynamic_cast<class cl_ucsim_lexer *>(this))
11
12 %}
13
14 %%
15
16 [ \t]*                  ;
17
18 "+"                     return(tok(PTOK_PLUS));
19 "-"                     return(tok(PTOK_MINUS));
20 "*"                     return(tok(PTOK_ASTERIX));
21 "/"                     return(tok(PTOK_SLASH));
22 "("                     return(tok(PTOK_LEFT_PAREN));
23 ")"                     return(tok(PTOK_RIGHT_PAREN));
24 "["                     return(tok(PTOK_LEFT_BRACKET));
25 "]"                     return(tok(PTOK_RIGHT_BRACKET));
26 "="                     return(tok(PTOK_EQUAL));
27 "."                     return(tok(PTOK_DOT));
28 "&"                     return(tok(PTOK_AMPERSAND));
29
30 ([0-9]+)|(0x[0-9a-fA-F]+) {
31   yylval->number= strtol(yytext, 0, 0);
32   return(YY_cl_ucsim_parser_CLASS::PTOK_NUMBER);
33 }
34
35 [a-zA-Z_][0-9a-zA-Z_]*  return(lexer_obj->check_id(yytext));
36
37 .                       ;
38
39 %%
40
41 #undef lexer_obj
42
43 int
44 yywrap(void)
45 {
46   return(1);
47 }
48
49 #include "cmdlexcl.h"
50 #include "globals.h"
51
52 int
53 cl_ucsim_lexer::LexerInput(char *buf, int max_size)
54 {
55   if (!string_to_parse)
56     return(yyFlexLexer::LexerInput(buf, max_size));
57   int lrem= strlen(string_ptr);
58   int n= max_size;
59   if (lrem < max_size)
60     n= lrem;
61   strncpy(buf, string_ptr, n);
62   string_ptr+= n;
63   return(n);
64 }
65
66 void
67 cl_ucsim_lexer::activate_lexer_to_parse_into(void *yylv)
68 {
69   yylval= (YY_cl_ucsim_parser_STYPE *)yylv;
70 }
71
72 int
73 cl_ucsim_lexer::check_id(char *token)
74 {
75   class cl_uc *uc= application->get_uc();
76   //printf("checking id=\"%s\"\n",token);
77
78   if (uc)
79     {
80       class cl_memory *mem= uc->memory(token);
81       if (mem)
82         {
83           yylval->memory_object= mem;
84           return(tok(PTOK_MEMORY_OBJECT));
85         }
86       
87       t_addr addr;
88       bool found= uc->symbol2address(yytext, uc->sfr_tbl(), &addr);
89       if (found)
90         {
91           /*yylval->number= addr;
92             return(tok(PTOK_NUMBER));*/
93           yylval->memory.memory= uc->address_space(MEM_SFR_ID);
94           yylval->memory.address= addr;
95           return(tok(PTOK_MEMORY));
96         }
97
98       found= uc->symbol2address(yytext, uc->bit_tbl(), &addr);
99       if (found)
100         {
101           t_addr memaddr;
102           t_mem mask;
103           yylval->bit.memory= uc->bit2mem(addr, &memaddr, &mask);
104           yylval->bit.mem_address= memaddr;
105           yylval->bit.bit_address= addr;
106           yylval->bit.mask= mask;
107           return(tok(PTOK_BIT));
108         }
109     }
110
111   return(0);
112 }