Pointer support #80

Closed
opened 2023-01-11 10:26:38 +00:00 by deavmi · 19 comments
Owner

What is to be done?

To add support for pointers, so we will need to add support for doing:

int* myPtr;
int myInt;

myPtr = &myInt;
### What is to be done? To add support for pointers, so we will need to add support for doing: ```d int* myPtr; int myInt; myPtr = &myInt; ```
deavmi self-assigned this 2023-01-11 10:30:04 +00:00
deavmi added this to the Parser project 2023-01-11 10:30:06 +00:00
deavmi added this to the Basics milestone 2023-01-11 10:30:10 +00:00
deavmi added the
parser
label 2023-01-11 10:30:18 +00:00
deavmi added reference pointers 2023-01-11 10:30:30 +00:00
Author
Owner

Marked duplicate of #17

Marked duplicate of #17
deavmi reopened this issue 2023-01-14 10:20:35 +00:00
Author
Owner

We need support for *deref as expression and not *deref = (ASSIGN).

We need to indicate this somehow.

We need support for `*deref` as expression and not `*deref =` (ASSIGN). We need to indicate this somehow.
deavmi added the due date 2023-01-14 2023-01-14 10:21:56 +00:00
Author
Owner

Todo

  • Dereferencing
    • Okay it looks like (*ptr)+1 works but not *ptr+1 (it gets confused a little.)
    • Completed the basic dereferencing in commit 6b3fccfc159cee77e8113ac084d27f522e7fa49c on pointers branch
  • Arithmetic
    • Offset a pointer (will complain type wise), we need to add coercion
  • Casting support
    • Seems a little broken cast(int*) fails - gets choked up by the fact there is a * present.
  • Assignment type checking
    • Assigning int* to byte* for example, should require an explicit cast
  • Add Pointer support for isSameType(Type, Type)
    • Currently it compares if both are integers, same size and signed/unsignedness and returns true then which is wrong as we are not checking referred type
## Todo - [x] Dereferencing * Okay it looks like `(*ptr)+1` works but not `*ptr+1` (it gets confused a little.) * Completed the basic dereferencing in commit `6b3fccfc159cee77e8113ac084d27f522e7fa49c` on `pointers` branch - [x] Arithmetic * Offset a pointer (will complain type wise), we need to add coercion - [x] Casting support * Seems a little broken `cast(int*)` fails - gets choked up by the fact there is a `*` present. - [x] Assignment type checking * Assigning int* to byte* for example, should require an explicit cast - [x] Add `Pointer` support for `isSameType(Type, Type)` * Currently it compares if both are integers, same size and signed/unsignedness and returns `true` then which is wrong as we are not checking referred type
deavmi added the
typing
label 2023-01-14 10:43:29 +00:00
deavmi modified the project from Parser to Dependency tree, type-checking and codegen 2023-01-14 10:43:33 +00:00
deavmi added a new dependency 2023-02-11 12:06:45 +00:00
Author
Owner

I need to work on adding arithmetic support this week - as it still isn't added.

I need to work on adding arithmetic support this week - as it still isn't added.
Author
Owner

Todo

  • Dereferencing
    • Okay it looks like (*ptr)+1 works but not *ptr+1 (it gets confused a little.)
      • Completed the basic dereferencing in commit 6b3fccfc159cee77e8113ac084d27f522e7fa49c on pointers branch
  • Arithmetic
    • Offset a pointer (will complain type wise), we need to add casting
  • Casting support
    • Seems a little broken cast(int*) fails - gets choked up by the fact there is a * present.

Tomorrow I will finish up my attemptPointerAriehmeticCoercion(Value, Value) method for implementing coercion of pointer arithmetic operands to support the arithmetic stuff.

> ## Todo > > - [x] Dereferencing > * Okay it looks like `(*ptr)+1` works but not `*ptr+1` (it gets confused a little.) > * Completed the basic dereferencing in commit `6b3fccfc159cee77e8113ac084d27f522e7fa49c` on `pointers` branch > - [ ] Arithmetic > * Offset a pointer (will complain type wise), we need to add casting > - [ ] Casting support > * Seems a little broken `cast(int*)` fails - gets choked up by the fact there is a `*` present. > Tomorrow I will finish up my `attemptPointerAriehmeticCoercion(Value, Value)` method for implementing coercion of pointer arithmetic operands to support the arithmetic stuff.
deavmi modified the due date from 2023-01-14 to 2023-04-12 2023-04-12 07:38:02 +01:00
Author
Owner

Todo

  • Dereferencing
    • Okay it looks like (*ptr)+1 works but not *ptr+1 (it gets confused a little.)
      • Completed the basic dereferencing in commit 6b3fccfc159cee77e8113ac084d27f522e7fa49c on pointers branch
  • Arithmetic
    • Offset a pointer (will complain type wise), we need to add casting
  • Casting support
    • Seems a little broken cast(int*) fails - gets choked up by the fact there is a * present.

Arithmetic is almost done, added coercion but also I have updated the DGen to emit *(<expr>) instead of *<expr> as then one could have <expr> := <expr1>+<expr2> which wouldn't work out well as you would have *<expr1>+<expr2> which wouldn't deref the sum but rather the left hand side, then add the dereferenced value to the right hand side.

> ## Todo > > - [x] Dereferencing > * Okay it looks like `(*ptr)+1` works but not `*ptr+1` (it gets confused a little.) > * Completed the basic dereferencing in commit `6b3fccfc159cee77e8113ac084d27f522e7fa49c` on `pointers` branch > - [ ] Arithmetic > * Offset a pointer (will complain type wise), we need to add casting > - [ ] Casting support > * Seems a little broken `cast(int*)` fails - gets choked up by the fact there is a `*` present. > Arithmetic is almost done, added coercion but also I have updated the `DGen` to emit `*(<expr>)` instead of `*<expr>` as then one could have `<expr> := <expr1>+<expr2>` which wouldn't work out well as you would have `*<expr1>+<expr2>` which wouldn't deref the sum but rather the left hand side, then add the dereferenced value to the right hand side.
Author
Owner

Arithmetic

Offset a pointer (will complain type wise), we need to add coercion

We fixed it in the manner described above. Coercion was in b5e8ba0b31c6cd7a2faea45b74a9274b545710e4 (tagged as pointers_coerce) and DGen emit in d58e5aad4d3eb1091960d8e13b7562e99e47bc75.

## Arithmetic ✅ >Offset a pointer (will complain type wise), we need to add coercion We fixed it in the manner described above. Coercion was in `b5e8ba0b31c6cd7a2faea45b74a9274b545710e4` (tagged as `pointers_coerce`) and `DGen` emit in `d58e5aad4d3eb1091960d8e13b7562e99e47bc75`.
deavmi modified the due date from 2023-04-12 to 2023-04-23 2023-04-14 09:22:01 +01:00
deavmi added the
emit
label 2023-04-14 09:23:21 +01:00
deavmi added a new dependency 2023-04-14 12:38:59 +01:00
Author
Owner

Testing it now on simple_pointer_cast_le.t and it looks like it works, see commit 87ebae209ef01126bf6c7ce8634055b197715754 (tag: pointer_cast_merge).

This is good for casting support.


All that remains is the newly added "Assignment type checking".

Testing it now on `simple_pointer_cast_le.t` and it looks like it works, see commit `87ebae209ef01126bf6c7ce8634055b197715754` (tag: `pointer_cast_merge`). This is good for casting support. --- All that remains is the newly added "Assignment type checking".
Author
Owner

Todo

  • Dereferencing
    • Okay it looks like (*ptr)+1 works but not *ptr+1 (it gets confused a little.)
      • Completed the basic dereferencing in commit 6b3fccfc159cee77e8113ac084d27f522e7fa49c on pointers branch
  • Arithmetic
    • Offset a pointer (will complain type wise), we need to add coercion
  • Casting support
    • Seems a little broken cast(int*) fails - gets choked up by the fact there is a * present.
  • Assignment type checking
    • Assigning int* to byte* for example, should require an explicit cast
  • Add Pointer support for isSameType(Type, Type)
    • Currently it compares if both are integers, same size and signed/unsignedness and returns true then which is wrong as we are not checking referred type

I have updated isSameType(Type t1, Type t2) to now handle pointer comparisons explicitly:

   /**
    * There are several types and comparing them differs
    */
    private bool isSameType(Type type1, Type type2)
    {
        bool same = false;

        /* Handling for pointers */
        if(typeid(type1) == typeid(type2) && cast(Pointer)type1 !is null)
        {
            Pointer p1 = cast(Pointer)type1, p2 = cast(Pointer)type2;

            /* Now check that both of their referred types are the same */
            return isSameType(p1.getReferredType(), p2.getReferredType());
        }
        
        ...
> ## Todo > > - [x] Dereferencing > * Okay it looks like `(*ptr)+1` works but not `*ptr+1` (it gets confused a little.) > * Completed the basic dereferencing in commit `6b3fccfc159cee77e8113ac084d27f522e7fa49c` on `pointers` branch > - [x] Arithmetic > * Offset a pointer (will complain type wise), we need to add coercion > - [x] Casting support > * Seems a little broken `cast(int*)` fails - gets choked up by the fact there is a `*` present. > - [ ] Assignment type checking > * Assigning int* to byte* for example, should require an explicit cast > - [ ] Add `Pointer` support for `isSameType(Type, Type)` > * Currently it compares if both are integers, same size and signed/unsignedness and returns `true` then which is wrong as we are not checking referred type I have updated `isSameType(Type t1, Type t2)` to now handle pointer comparisons explicitly: ```d /** * There are several types and comparing them differs */ private bool isSameType(Type type1, Type type2) { bool same = false; /* Handling for pointers */ if(typeid(type1) == typeid(type2) && cast(Pointer)type1 !is null) { Pointer p1 = cast(Pointer)type1, p2 = cast(Pointer)type2; /* Now check that both of their referred types are the same */ return isSameType(p1.getReferredType(), p2.getReferredType()); } ... ```
Author
Owner

Todo

  • Dereferencing
    • Okay it looks like (*ptr)+1 works but not *ptr+1 (it gets confused a little.)
      • Completed the basic dereferencing in commit 6b3fccfc159cee77e8113ac084d27f522e7fa49c on pointers branch
  • Arithmetic
    • Offset a pointer (will complain type wise), we need to add coercion
  • Casting support
    • Seems a little broken cast(int*) fails - gets choked up by the fact there is a * present.
  • Assignment type checking
    • Assigning int* to byte* for example, should require an explicit cast
  • Add Pointer support for isSameType(Type, Type)
    • Currently it compares if both are integers, same size and signed/unsignedness and returns true then which is wrong as we are not checking referred type

Assignment type checking seems to actually be working, most likely as the isSameType(Type t1, Type t2) is updated.

Look:

module simple_pointer;

int j;

int function(int* ptr)
{
    *(ptr+0) = 2+2;
    return (*ptr)+1*2;
}

int thing()
{
    int discardExpr = function(&j);
    int** l;

    int* lV = l;

    return discardExpr;
}

Here it correctly errors out:

[INFO] Pointer to 'int'
[ERROR] isSameType(int,int*): false
[INFO] VibeCheck?
[ERROR] Type mismatch between type int* and int**: Not coercible (lacking integral var type)
> ## Todo > > - [x] Dereferencing > * Okay it looks like `(*ptr)+1` works but not `*ptr+1` (it gets confused a little.) > * Completed the basic dereferencing in commit `6b3fccfc159cee77e8113ac084d27f522e7fa49c` on `pointers` branch > - [x] Arithmetic > * Offset a pointer (will complain type wise), we need to add coercion > - [x] Casting support > * Seems a little broken `cast(int*)` fails - gets choked up by the fact there is a `*` present. > - [ ] Assignment type checking > * Assigning int* to byte* for example, should require an explicit cast > - [x] Add `Pointer` support for `isSameType(Type, Type)` > * Currently it compares if both are integers, same size and signed/unsignedness and returns `true` then which is wrong as we are not checking referred type Assignment type checking seems to actually be working, most likely as the `isSameType(Type t1, Type t2)` is updated. Look: ```d module simple_pointer; int j; int function(int* ptr) { *(ptr+0) = 2+2; return (*ptr)+1*2; } int thing() { int discardExpr = function(&j); int** l; int* lV = l; return discardExpr; } ``` Here it **correctly** errors out: ``` [INFO] Pointer to 'int' [ERROR] isSameType(int,int*): false [INFO] VibeCheck? [ERROR] Type mismatch between type int* and int**: Not coercible (lacking integral var type) ```
Author
Owner

All looks good, I will merge this soon. I should just finish #111 first and then upstream it into here - test again - and then we can merge this.

All looks good, I will merge this soon. I should just finish #111 first and then upstream it into here - test again - and then we can merge this.
Author
Owner

All looks good, I will merge this soon. I should just finish #111 first and then upstream it into here - test again - and then we can merge this.

This is now since fixed, waiting on CI for merge.

> All looks good, I will merge this soon. I should just finish #111 first and then upstream it into here - test again - and then we can merge this. This is now since fixed, waiting on CI for merge.
Author
Owner

- [ ] I should now go and test function calls that have int[] in them

~~- [ ] I should now go and test function calls that have `int[]` in them~~
Author
Owner

Looks like it broke something, that is, merging in #111

Looks like it broke something, that is, merging in #111
Author
Owner

Looks like it broke something, that is, merging in #111

Nvm, had to compile - no wodner CI passed ;)

> Looks like it broke something, that is, merging in #111 Nvm, had to compile - no wodner CI passed ;)
Author
Owner
  • I should now go and test function calls that have int[] in them

It is struggling with this:

int function(int[] ptr)
{
    *(ptr+0) = 2+2;
    return (*ptr)+1*2;
}

As we are getting this error:

[INFO] ParseTypedDec: DecisionBtwn FuncDef/VarDef: (
[INFO] ParseTypedDec: SymbolType=LBRACE
[WARNING] parseFuncDef(): Enter
[WARNING] parseTypedDeclaration(): Enter
[ERROR] Syntax error: Expected IDENT_TYPE but got UNKNOWN, see [ at (5, 17)

Solution (misunderstanding)

IT was a misunderstanding, support for []-based pointer syntax (the so-called "array syntax") is in the arrays branch.

> - [ ] I should now go and test function calls that have `int[]` in them It is struggling with this: ```d int function(int[] ptr) { *(ptr+0) = 2+2; return (*ptr)+1*2; } ``` As we are getting this error: ``` [INFO] ParseTypedDec: DecisionBtwn FuncDef/VarDef: ( [INFO] ParseTypedDec: SymbolType=LBRACE [WARNING] parseFuncDef(): Enter [WARNING] parseTypedDeclaration(): Enter [ERROR] Syntax error: Expected IDENT_TYPE but got UNKNOWN, see [ at (5, 17) ``` --- ## Solution ✅ (misunderstanding) IT was a misunderstanding, support for `[]`-based pointer syntax (the so-called _"array syntax"_) is in the `arrays` branch.
Author
Owner

I should be able to close this soon.

I should be able to close this soon.
Author
Owner

Merging now, waiting CI/CD on vardec_varass_dependency

Merging now, waiting CI/CD on `vardec_varass_dependency`
Author
Owner

CI/CD is all green

CI/CD is all green
deavmi removed a dependency 2023-04-17 15:53:28 +01:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

2023-04-23

Reference: tlang/tlang#80
No description provided.