LittleC icon indicating copy to clipboard operation
LittleC copied to clipboard

About LittleC

Open pacosecu opened this issue 1 year ago • 2 comments

Hello: Do you know how to add pointer support in LittleC? Thanks in advance, paco

pacosecu avatar Jan 04 '24 14:01 pacosecu

The advice given in the textbook (4th ed.) on page 786 is:

Supporting pointers is no more difficult than supporting any other data type. However, you will need to add support for the pointer operators to the expression parser.

Add a pointer type to var_type and go from there.

pmachapman avatar Jan 08 '24 20:01 pmachapman

Hello. Thanks for answering. I think that in parser.c you need to add or modify a production rule, taking into account the precedence of the * and & pointer operators

Page 58 C The Complete Reference 4th Edition Precedence Summary Table 2-8 lists the precedence of all operators defined by C. Note that all operators, except the unary operators and ?, associate from left to right. The unary operators (*, &, –) and ? associate from right to left.

Expressions Operators, constants, functions, and variables are the constituents of expressions. An expression in C is any valid combination of these elements. Because most expressions tend to follow the general rules of algebra, they are often taken for granted. However, a few aspects of expressions relate specifically to C.

Highest ( ) [ ] –>. ! ~++ –– – (type) * & sizeof */%

  • – << >> < <= > >= == != & ^ | && | | ?: = += –= *= /= etc. Lowest, Table 2 -8. Precedence of C Operators

I believe that as seen in the Operator Precedence table, the * operator and the & operator have the same level of precedence as the NOT (!) and unary minus (-) operator.

I have added support to Littlec for NOT(!) , AND(&&), OR(||). I think support for the *, & pointer operators could be done in the same evalexp() as support for NOT(!) and unary minus (-)

But I do not know where to start...

pacosecu avatar Jan 16 '24 18:01 pacosecu