FreeBASIC  0.91.0
parser-quirk-peekpoke.bas
Go to the documentation of this file.
1 '' quirk pointer statements (PEEK and POKE) parsing
2 ''
3 '' chng: sep/2004 written [v1ctor]
4 
5 
6 #include once "fb.bi"
7 #include once "fbint.bi"
8 #include once "parser.bi"
9 #include once "ast.bi"
10 
11 ''
12 '' (SymbolType ',')? Expression
13 ''
15  ( _
16  byref dtype as integer, _
17  byref subtype as FBSYMBOL ptr _
18  ) as ASTNODE ptr
19 
20  dim as ASTNODE ptr expr = any
21 
22  expr = cTypeOrExpression( FALSE, dtype, subtype, 0 )
23  if( expr = NULL ) then
24  '' SymbolType
25 
26  '' check for types invalid for PEEK/POKE
27  select case( dtype )
28  case FB_DATATYPE_VOID, FB_DATATYPE_FIXSTR
29  errReport( FB_ERRMSG_INVALIDDATATYPES, TRUE )
30  '' error recovery: fake a type
31  dtype = FB_DATATYPE_UBYTE
32  subtype = NULL
33  end select
34 
35  '' ','
36  hMatchCOMMA( )
37 
38  '' Expression
39  expr = cExpression( )
40  if( expr = NULL ) then
41  errReport( FB_ERRMSG_EXPECTEDEXPRESSION )
42  '' error recovery: fake an expr
43  expr = astNewCONSTi( 0 )
44  end if
45  else
46  '' Expression (without a SymbolType in front of it)
47  dtype = FB_DATATYPE_UBYTE
48  subtype = NULL
49  end if
50 
51  function = expr
52 end function
53 
54 ''
55 '' PokeStmt = POKE (SymbolType ',')? Expression ',' Expression .
56 ''
57 function cPokeStmt( ) as integer
58  dim as ASTNODE ptr expr1 = any, expr2 = any
59  dim as integer poketype = any
60  dim as FBSYMBOL ptr subtype = any
61 
62  function = FALSE
63 
64  '' POKE
65  lexSkipToken( )
66 
67  '' (SymbolType ',')? Expression
68  expr1 = hOptionalTypeAndFirstExpr( poketype, subtype )
69 
70  '' ','
71  hMatchCOMMA( )
72 
73  hMatchExpressionEx( expr2, FB_DATATYPE_INTEGER )
74 
75  select case astGetDataClass( expr1 )
76  case FB_DATACLASS_STRING
77  errReport( FB_ERRMSG_INVALIDDATATYPES )
78  '' no error recovery: stmt was already parsed
79  astDelTree( expr1 )
80  exit function
81 
82  case FB_DATACLASS_FPOINT
83  expr1 = astNewCONV( FB_DATATYPE_UINT, NULL, expr1 )
84 
85  case else
86  if( typeGetSize( astGetDataType( expr1 ) ) <> env.pointersize ) then
87  errReport( FB_ERRMSG_INVALIDDATATYPES )
88  '' no error recovery: ditto
89  astDelTree( expr1 )
90  exit function
91  end if
92  end select
93 
94  expr1 = astNewDEREF( expr1, poketype, subtype )
95 
96  expr1 = astNewASSIGN( expr1, expr2 )
97  if( expr1 = NULL ) then
98  errReport( FB_ERRMSG_INVALIDDATATYPES )
99  else
100  astAdd( expr1 )
101  end if
102 
103  function = TRUE
104 
105 end function
106 
107 ''
108 '' PeekFunct = PEEK '(' (SymbolType ',')? Expression ')' .
109 ''
110 function cPeekFunct( ) as ASTNODE ptr
111  dim as ASTNODE ptr expr = any
112  dim as integer dtype = any
113  dim as FBSYMBOL ptr subtype = any
114 
115  function = NULL
116 
117  '' PEEK
118  lexSkipToken( )
119 
120  '' '('
121  hMatchLPRNT( )
122 
123  '' (SymbolType ',')? Expression
124  expr = hOptionalTypeAndFirstExpr( dtype, subtype )
125 
126  ' ')'
127  hMatchRPRNT( )
128 
129  select case astGetDataClass( expr )
130  case FB_DATACLASS_STRING
131  errReport( FB_ERRMSG_INVALIDDATATYPES )
132  '' error recovery: fake an expr
133  astDelTree( expr )
134  expr = NULL
135 
136  case FB_DATACLASS_FPOINT
137  expr = astNewCONV( FB_DATATYPE_UINT, NULL, expr )
138 
139  case else
140  if( typeGetSize( astGetDataType( expr ) ) <> env.pointersize ) then
141  errReport( FB_ERRMSG_INVALIDDATATYPES )
142  '' error recovery: fake an expr
143  astDelTree( expr )
144  expr = NULL
145  end if
146  end select
147 
148  if( expr = NULL ) then
149  expr = astNewCONSTi( 0 )
150  end if
151 
152  '' ('.' UdtMember)?
153  if( lexGetToken( ) = CHAR_DOT ) then
154  select case dtype
155  case FB_DATATYPE_STRUCT ', FB_DATATYPE_CLASS
156 
157  case else
158  errReport( FB_ERRMSG_EXPECTEDUDT, TRUE )
159  hSkipStmt( )
160  exit function
161  end select
162 
163  lexSkipToken( LEXCHECK_NOPERIOD )
164  function = cUdtMember( dtype, subtype, expr, TRUE )
165  else
166  function = astNewDEREF( expr, dtype, subtype )
167  end if
168 end function
169