-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add raw pointers and pointer types. #30
Comments
Generally references (i.e. disguised pointers but with less free semantics) are a necessity for a general purpose language. I think pointers (i.e. the free semantics as pointer arithmetic etc.) should definitely be disallowed by default like Rust, V, and other modern langs do. And only allowed in some sort of
By default (say outside of
Yep, why not. But only in the |
I'm kind of morally opposed to If it wasn't clear though, the predominant approach towards reference semantics and memory management will be through safe, garbage-collected reference types - I've created a new issue for those, and the intent is that they'll be the recommended kind of pointer type for most workloads. |
Ah, ok. This reminds me of Nim's references. Now it's clear that it should be easy to judge about the source code whether we're dealing with "dangerous pointers" or "safe pointers". That could be enough for me as a linter or some compiler option (akin to |
Basil compiles directly to native code, so it should be relatively straightforward to support unsafe pointer types and pointer arithmetic, at least from a codegen perspective. While the primary means of passing around reference types should be through garbage-collected pointers, supporting raw pointers would allow Basil to more easily express low-level function prototypes - useful for interacting with foreign functions.
I propose we introduce a new primitive type kind, "Pointer", that has a single parameter - the type it points to. We'll tentatively denote the type of a pointer to some type
T
asT ptr
. Pointer types can be coerced generically to pointer types that point to a generic type likeAny
or a type variable. Besides this, pointer types support no other implicit coercions.Pointers should, at minimum, support a few primary operations:
deref : T? ptr -> T?
: dereferences a pointer and returns the value it points to.addr : T? -> T? ptr
: computes the address of a value and returns it as a pointer value. The parameter toaddr
must be an lvalue!T? ptr as U? ptr
: converts one pointer type to another. This is unchecked - unsafe pointer coercion is not a runtime or compile error!T? ptr as Int
: converts a pointer to an integer value.Int as T? ptr
: converts an integer to a pointer value. Between this and the previous conversion, rudimentary pointer arithmetic is achievable.A few open questions:
&val
and*ptr
syntax with a few new tokens. One less-invasive alternative would be Zig-style dot syntax:val.&
andptr.*
would be easily expressed in the current Basil semantics.Int
? Maybe it could be type-based:ptr + Int
could add the size of anInt
to the address contained inptr
.The text was updated successfully, but these errors were encountered: