chaos-lang / chaos

The Chaos Programming Language
https://chaos-lang.org
GNU General Public License v3.0
281 stars 17 forks source link

[RFC] Implement Pointer Data Type #87

Closed mertyildiran closed 3 years ago

mertyildiran commented 3 years ago

The development of fs core library showed that we need a primitive data type to store C pointers. The keyword for this data type will be ptr. It will be a generic pointer such that every pointer can be assigned to it. That probably means it will be a void* ptr. Right now we are not sure if it encompasses function pointers too. Support of more pointer types can be added to the language's source according to the need later on.

We will add void* ptr to this union such that it will look like this:

union Value {
    bool b;
    long long i;
    char* s;
    long double f;
    void* ptr;
} value;

We know that void pointers encompasses file pointers because the two examples below produce the same output:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp = fopen("files/readme.txt", "rb");

    if (fp == NULL)
        return 1;

    fseek(fp, 0, SEEK_END);
    long fsize = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *text = malloc(fsize + 1);
    fread(text, 1, fsize, fp);
    fclose(fp);

    text[fsize] = 0;

    printf("%s", text);
}
#include <stdio.h>
#include <stdlib.h>

int main()
{
    void *fp = fopen("files/readme.txt", "rb");

    if (fp == NULL)
        return 1;

    fseek(fp, 0, SEEK_END);
    long fsize = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *text = malloc(fsize + 1);
    fread(text, 1, fsize, fp);
    fclose(fp);

    text[fsize] = 0;

    printf("%s", text);
}

ptr data type will be used by the fs library like this:

import fs

ptr file = fs.open('readme.txt')
fs.write(file, 'Chaos language is awesome!\n')
fs.close(file)

ptr data type should be able to assignable to hexadecimal values like 0x7fffb1d7ec80 (memory address) because if you print the pointer, you will see a similar value:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    void *fp = fopen("files/readme.txt", "rb");

    if (fp == NULL)
        return 1;

    fseek(fp, 0, SEEK_END);
    long fsize = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *text = malloc(fsize + 1);
    fread(text, 1, fsize, fp);
    fclose(fp);

    text[fsize] = 0;

    printf("%s", text);
    printf("%p\n", &fp);
}
$ ./a.out
You read me!
0x7fff107966e0

So the Chaos code for the pointer assignment will look like this:

ptr my_ptr = 0x7fffb1d7ec80

print and echo statements should be able to print it. Type casting to num and str data types should be implemented in the types library.

ptr data type should support bitwise operators. It can be implemented using type casting to uintptr_t in C:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main()
{
    void *fp = fopen("files/readme.txt", "rb");

    if (fp == NULL)
        return 1;

    fseek(fp, 0, SEEK_END);
    long fsize = ftell(fp);
    fseek(fp, 0, SEEK_SET);

    char *text = malloc(fsize + 1);
    fread(text, 1, fsize, fp);
    fclose(fp);

    text[fsize] = 0;

    printf("%s", text);
    printf("%p\n", &fp);
    void *ptr2 = (void *) ((uintptr_t) fp & (uintptr_t) 0xfff);
    printf("%p\n", &ptr2);
}
$ ./a.out
You read me!
0x7ffc91920c58
0x7ffc91920c60

Roadmap to Implement the ptr data type:

mertyildiran commented 3 years ago

I've started to implement this feature in feature/ptr branch.

mertyildiran commented 3 years ago

@naltun I've realized that num data type can be used to achieve pointer functionality. Therefore there is no need to introduce a new data type into the language.

These are the commits that introducing fs.open() and fs.close() into the fs library and they are returning the file descriptors in the form of num data type: