FreeBASIC  0.91.0
parser-expr-constant.bas
Go to the documentation of this file.
1 '' atom constants and literals parsing
2 ''
3 '' chng: sep/2004 written [v1ctor]
4 
5 #include once "fb.bi"
6 #include once "fbint.bi"
7 #include once "parser.bi"
8 #include once "ast.bi"
9 
10 function cConstant( byval sym as FBSYMBOL ptr ) as ASTNODE ptr
11  '' check visibility
12  if( symbCheckAccess( sym ) = FALSE ) then
13  errReport( FB_ERRMSG_ILLEGALMEMBERACCESS )
14  end if
15 
16  '' ID
17  lexSkipToken( )
18 
19  function = astBuildConst( sym )
20 end function
21 
22 '':::::
23 '' LitString = STR_LITERAL STR_LITERAL* .
24 ''
25 function cStrLiteral( byval skiptoken as integer ) as ASTNODE ptr
26  dim as FBSYMBOL ptr sym = any
27  dim as integer lgt = any, isunicode = any
28  dim as zstring ptr zs = any
29  dim as wstring ptr ws = any
30 
31  dim as ASTNODE ptr expr = NULL
32 
33  do
34  lgt = lexGetTextLen( )
35 
36  if( lexGetType( ) <> FB_DATATYPE_WCHAR ) then
37  '' escaped? convert to internal format..
38  if( lexGetToken( ) = FB_TK_STRLIT_ESC ) then
39  zs = hReEscape( lexGetText( ), lgt, isunicode )
40  else
41  zs = lexGetText( )
42 
43  '' any '\'?
44  if( lexGetHasSlash( ) ) then
45  if( fbPdCheckIsSet( FB_PDCHECK_ESCSEQ ) ) then
46  if( lexGetToken( ) <> FB_TK_STRLIT_NOESC ) then
47  if( hHasEscape( zs ) ) then
48  errReportWarn( FB_WARNINGMSG_POSSIBLEESCSEQ, _
49  zs, _
50  FB_ERRMSGOPT_ADDCOLON or FB_ERRMSGOPT_ADDQUOTES )
51  end if
52  end if
53  end if
54  end if
55 
56  isunicode = FALSE
57  end if
58 
59  if( isunicode = FALSE ) then
60  sym = symbAllocStrConst( zs, lgt )
61  '' convert to unicode..
62  else
63  sym = symbAllocWstrConst( wstr( *zs ), lgt )
64  end if
65 
66  else
67  '' escaped? convert to internal format..
68  if( lexGetToken( ) = FB_TK_STRLIT_ESC ) then
69  ws = hReEscapeW( lexGetTextW( ), lgt )
70  else
71  ws = lexGetTextW( )
72 
73  '' any '\'?
74  if( lexGetHasSlash( ) ) then
75  if( fbPdCheckIsSet( FB_PDCHECK_ESCSEQ ) ) then
76  if( lexGetToken( ) <> FB_TK_STRLIT_NOESC ) then
77  if( hHasEscapeW( ws ) ) then
78  errReportWarn( FB_WARNINGMSG_POSSIBLEESCSEQ )
79  end if
80  end if
81  end if
82  end if
83  end if
84 
85  sym = symbAllocWstrConst( ws, lgt )
86  end if
87 
88  if( expr = NULL ) then
89  expr = astNewVAR( sym )
90  else
91  expr = astNewBOP( AST_OP_ADD, expr, astNewVAR( sym ) )
92  end if
93 
94  if( skiptoken ) then
95  lexSkipToken( )
96 
97  '' not another literal string?
98  if( lexGetClass( ) <> FB_TKCLASS_STRLITERAL ) then
99  exit do
100  end if
101 
102  else
103  exit do
104  end if
105  loop
106 
107  function = expr
108 end function
109 
110 function cNumLiteral( byval skiptoken as integer ) as ASTNODE ptr
111  dim as integer dtype = any
112 
113  dtype = lexGetType( )
114 
115  select case( dtype )
116  case FB_DATATYPE_DOUBLE
117  function = astNewCONSTf( val( *lexGetText( ) ), dtype )
118 
119  case FB_DATATYPE_SINGLE
120  dim fval as single = val( *lexGetText( ) )
121  function = astNewCONSTf( fval , dtype )
122 
123  case else
124  if( typeIsSigned( dtype ) ) then
125  function = astNewCONSTi( vallng( *lexGetText( ) ), dtype )
126  else
127  function = astNewCONSTi( valulng( *lexGetText( ) ), dtype )
128  end if
129  end select
130 
131  if( skiptoken ) then
132  lexSkipToken( )
133  end if
134 end function
135