root/src/ss_lexical.l

Revision f6b8679f96ca75d2bf72c8c1eebf66f6fa14095d, 8.0 kB (checked in by redbrain <redbrain@…>, 2 years ago)

removing print as keyword

  • Property mode set to 100644
Line 
1%{
2/**
3 * ss_lexical.l -> Part of Crules Programming language
4 *
5 * Crules is the legal property of its developers. Please refer to the
6 * COPYRIGHT file distributed with this source distribution.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22/**
23 * The lexer the lexical tokens which are important get parsed here!
24 *
25 * IMPORTANT: We now requite Flex to generate the lexer now!
26 **/
27
28#ifdef HAVE_CONFIG_H
29# include "config.h"
30#else
31# define CMAKE 1
32# include "config.h.cmake"
33#endif
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include <gmp.h>
40#include <mpfr.h>
41
42#include <crules/crules.h>
43#include <crules/opcodes.h>
44#include <crules/symbols.h>
45#include <crules/objects.h>
46#include <crules/backend.h>
47#include <crules/runtime.h>
48#include <crules/garbage.h>
49#include <crules/operators.h>
50
51#ifndef CMAKE
52# include "libparser_a-ss_parser.h"
53#else
54# include "ss_parser.h"
55#endif
56
57#ifdef HAVE_LIBREADLINE
58#  if defined(HAVE_READLINE_READLINE_H)
59#    include <readline/readline.h>
60#  elif defined(HAVE_READLINE_H)
61#    include <readline.h>
62#  else /* !defined(HAVE_READLINE_H) */
63extern char *readline ();
64#  endif /* !defined(HAVE_READLINE_H) */
65char *cmdline = NULL;
66#else /* !defined(HAVE_READLINE_READLINE_H) */
67  /* no readline */
68#endif /* HAVE_LIBREADLINE */
69
70#ifdef HAVE_READLINE_HISTORY
71#  if defined(HAVE_READLINE_HISTORY_H)
72#    include <readline/history.h>
73#  elif defined(HAVE_HISTORY_H)
74#    include <history.h>
75#  else /* !defined(HAVE_HISTORY_H) */
76extern void add_history ();
77extern int write_history ();
78extern int read_history ();
79#  endif /* defined(HAVE_READLINE_HISTORY_H) */
80  /* no history */
81#endif /* HAVE_READLINE_HISTORY */
82
83static bool crl_yy_stdin= false;
84bool crl_interactive= false;
85
86crl_stack_t *crl_lex_stack;
87extern int yyparse( );
88%}
89
90%option yylineno
91
92
93DIGIT    [0-9]
94ID       [_a-zA-Z][a-zA_Z0-9_$]*
95qstring  \"[^\"\n]*[\"\n]
96
97%%
98
99#.*                     ; /* comment */
100
101object                  {  return OBJECT; }
102defun                   {  return DEFUN; }
103
104break                   {  return BREAK; }
105continue                {  return CONTINUE; }
106return                  {  return RETURN; }
107for                     {  return FOR; }
108while                   {  return WHILE; }
109new                     {  return NEW; }
110
111if                      {  return IF; }
112elif                    {  return ELIF; }
113else                    {  return ELSE; }
114
115true                    {  return TRUE;  }
116false                   {  return FALSE; }
117nil                     {  return NIL; }
118
119\[                      {  return '['; }
120\]                      {  return ']'; }
121\(                      {  return '('; }
122\)                      {  return ')'; }
123\{                      {  return '{'; }
124\}                      {  return '}'; }
125
126","                     {  return ','; }
127";"                     {  return ';'; }
128"."                     {  return '.'; }
129
130"="                     { return '='; }
131"+"                     { return '+'; }
132"-"                     { return '-'; }
133"/"                     { return '/'; }
134"*"                     { return '*'; }
135"^"                     { return '^'; }
136
137"Int"                   { return INT_T;      }
138"Float"                 { return FLOAT_T;    }
139"Char"                  { return CHAR_T;     }
140"String"                { return STRING_T;   }
141"List"                  { return LIST_T;     }
142"Object"                { return OBJECT_T;   }
143
144"Callback"              { return CALLBACK_T; }
145"..."                   { return VARADIC_T;  }
146
147"!"                     { return '!'; }
148"||"                    { return OR; }
149"&&"                    { return AND; }
150
151"++"                    { return PLUSPLUS; }
152"--"                    { return MINMIN; }
153"=="                    { return EQUAL; }
154"+="                    { return PLUS_EQUAL; }
155"*="                    { return MULTIPLY_EQUAL; }
156"-="                    { return MINUS_EQUAL; }
157"/="                    { return DIVIDE_EQUAL; }
158
159"!="                    { return NOT_EQUAL; }
160"<"                     { return LESS; }
161"<="                    { return LESS_EQUAL; }
162">"                     { return GREATER; }
163">="                    { return GREATER_EQUAL; }
164
165\'.\'                   {
166                          const char* str= crl_strdup( yytext );
167                          yylval.character= str[ 1 ];
168                          return CHAR;
169                        }
170
171{qstring}               {
172                          yylval.string= crl_strdup( (yytext+1) );
173                          if( yylval.string[ yyleng-2 ] != '\"' ) {
174                            crl_error("Un-termintated character string!\n");
175                          }
176                          else {
177                            yylval.string[yyleng-2] = '\0';
178                          }
179                          return STRING;
180                        }
181
182{DIGIT}+                {
183                          mpfr_t x; // folding the constants to avoid overflow
184                          mpfr_init2( x, SIZEOF_INT*8 );
185                          if( mpfr_set_str( x, yytext, 10, GMP_RNDU) )
186                            crl_fatal("error initilizing integer value <%s>!\n", yytext );
187                          yylval.integer = mpfr_get_si( x, GMP_RNDU );
188                          mpfr_clear( x );
189                          return INTEGER;
190                        }
191
192{DIGIT}+"."{DIGIT}*     {
193                          mpfr_t x; // folding the constants to avoid overflow
194                          mpfr_init2( x, SIZEOF_DOUBLE*8 );
195                          if( mpfr_set_str( x, yytext, 10, GMP_RNDU) )
196                            crl_fatal("error initilizing floating point value <%s>!\n", yytext );
197                          yylval.floating_point= mpfr_get_ld( x, GMP_RNDU );
198                          mpfr_clear( x );
199                          return DOUBLE;
200                        }
201
202{ID}                    {
203                          yylval.string= crl_strdup( yytext );
204                          return IDENTIFIER;
205                        }
206
207[\n]                    ;
208[^f\n]                  ;
209
210%%
211
212int yywrap( )
213{
214  return 1;
215}
216
217static
218void crl_lexer_mark_string( char * string )
219{
220  if( string )
221    {
222      if( crl_lex_stack )
223        {
224          crl_dd_stack_push( crl_lex_stack, string );
225        }
226      else
227        {
228          crl_lex_stack= (crl_stack_t *)
229            crl_malloc( sizeof(crl_stack_t) );
230          crl_dd_stack_init( &crl_lex_stack );
231          crl_dd_stack_push( crl_lex_stack, string );
232        }
233    }
234}
235
236void crl_destroy_lexer( void )
237{
238#ifdef HAVE_READLINE_HISTORY
239  if( crl_interactive ) {
240    clear_history( );
241  }
242#endif
243  // finally destroy the lexer!
244  yylex_destroy( );
245}
246
247int crl_parse( FILE* fin )
248{
249  int err= 0;
250  if( fin == stdin )
251    {
252      crl_yy_stdin= true;
253      crl_interactive= true;
254
255      YY_BUFFER_STATE bp; const char * prompt= ">>> ";
256#ifdef HAVE_LIBREADLINE
257      char *line= NULL;
258      while( 1 )
259        {
260          line= readline( prompt );
261          crl_lexer_mark_string( line );
262
263          if( line == 0 )
264            break;
265          else if( crl_strlen( line ) > 0 )
266            {
267#ifdef HAVE_READLINE_HISTORY
268              add_history( line );
269#endif
270              bp= yy_scan_string( line );
271              yy_switch_to_buffer( bp );
272              err= yyparse( );
273              yy_delete_buffer( bp );
274            }
275        }
276#else
277      char line[LINE_MAX];
278      fprintf( stdout, "%s", prompt );
279      while( fgets( line, LINE_MAX, stdin ) != NULL )
280        {
281          bp= yy_scan_string( line );
282          yy_switch_to_buffer( bp );
283          err= yyparse( );
284          yy_delete_buffer( bp );
285          fprintf( stdout, "%s", prompt );
286        }
287#endif
288    }
289  else
290    {
291      yyin = fin;
292      err = yyparse();
293    }
294
295  return err;
296}
Note: See TracBrowser for help on using the browser.