Skip to content
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

Builtin types #19

Open
12 of 28 tasks
jdpage opened this issue Dec 11, 2017 · 2 comments
Open
12 of 28 tasks

Builtin types #19

jdpage opened this issue Dec 11, 2017 · 2 comments

Comments

@jdpage
Copy link
Collaborator

jdpage commented Dec 11, 2017

Unsigned integers

Types u8, u16, u24, and u32 represent 1-byte, 2-byte, 3-byte, and 4-byte unsigned integers, respectively. (3-byte makes sense because the C64 is memory-constrained and the 6502 doesn't really do alignment.)

Unsigned integer arithmetic is well-defined as wrapping on over/underflow.

Shorter types can be coerced to longer types, but not the other way around.

  • add unsigned types to the spec
  • add unsigned types to the type system
  • write tests for unsigned arithmetic behaviour

Signed integers

Types i8, i16, i24, and i32 represent 1-byte, 2-byte, 3-byte, and 4-byte signed two's-complement integers, respectively.

Signed integer arithmetic is well-defined as wrapping on over/underflow.

Shorter types can be coerced to longer types, but not the other way around.

  • add signed types to the spec
  • add signed types to the type system
  • write tests for unsigned arithmetic behaviour

Booleans

Type bool represents a boolean value (represented in machine code as a 1-byte value). Values are true and false (or yes and no?)

Booleans cannot be coerced to anything.

  • decide on true/false vs. yes/no
  • add booleans to the spec
  • add booleans to the type system

Ranges

Types range and irange are 2-byte types indicating a range of u8s and a range of i8s, respectively. The first byte is the inclusive lower bound, and the second byte is the exclusive upper bound. These are written using ... to ... notation.

  • decide if ranges should be actual types/values or not
  • add ranges to the spec
  • add ranges to the type system

Pointers

Pointer types are created by prefixing any other type with & or &mut. The only valid operation for a pointer is to dereference it. Dereferencing a pointer always succeeds, due to the C64 having a completely flat memory model. Because of this, there is no such thing as a null pointer.

(Alternatively, we could use 0x0000 for nullable pointers should we need them, and provide an intrinsic for accessing that memory offset.)

  • add pointers to the spec
  • add pointers to the type system

Arrays

Array types are of the form [T; n to m] or [T; n to m, p to q, ...], where T is any type and (m - n) * (q - p) * ... is in the range 0 through 255. The index range of the array is part of the type. As a shorthand, 0 to n may simply be written n in an array type; these are considered strictly identical.

Arrays can be initialized using [ ... ] syntax or "..." syntax; the latter always results in an array of u8s.

Internally, arrays are always passed by reference. In addition, an array type can be coerced to another array type with the same element type and same dimension but a range which is a subrange.

The only valid operations on an array are indexing, creating a slice by taking a reference, and creating a slice by indexing with a range.

  • add arrays to the spec
  • add arrays to the type system

Slices

A slice of an array is written &[T] or &mut [T] where T is any type. Slices know their range but it isn't part of the type. They are 4 bytes long (2 for the address, 2 for the range).

The only valid operations on a slice are indexing and creating another slice by indexing with a range.

(I'm not 100% sure about slices--they're useful, but you either have to make them 4 bytes long to store the upper and lower bounds, or 3 bytes, but you have to do pointer arithmetic when they're created. In either case, they really abstract over the "pass in a pointer and a length" pattern one sees in C.)

  • add slices to the spec
  • add slices to the type system

ISR type

Written &isr. The type of names bound using an isr ... endisr block; pass it to library functions for registering ISRs. 2 bytes long.

  • add ISRs to the spec
  • add ISRs to the type system

Function types

Written &fun(...) and &fun(...) -> .... The type of names bound using a fun ... endfun block. 2 bytes long. They can be called like functions.

  • add functions to the spec
  • add functions to the type system
  • add function types to the parser
  • add calling conventions to the function type

Mystery pointer, mystery array, and mystery function

Unnamed 2-byte types which can be coerced to and from any pointer type, any array type, and any function type (!), respectively. Used in the "type signatures" of certain intrinsic/library "functions" provided for purposes of poking holes in the type system when necessary.

Bindings cannot be given these types, even using type inference (see #18); a real type must be given.

  • add mystery pointer to the type system
  • add mystery array to the type system
  • add mystery function to the type system
  • add mystery types to the spec
@jdpage jdpage added this to the Hello world! milestone Dec 12, 2017
@woodrowbarlow
Copy link
Collaborator

Arrays can be initialized using [ ... ] syntax or "..." syntax; the latter always results in an array of u8s.

wait, quotation marks? what's going on here? why would we have special syntax for arrays of type u8, especially since the type is always required in the signature?

everything else makes sense and sounds good to me. i'm on the same boat as you w.r.t. slices, and i propose we consider them after we have an MVP.

@jdpage
Copy link
Collaborator Author

jdpage commented Dec 19, 2017

wait, quotation marks? what's going on here? why would we have special syntax for arrays of type u8, especially since the type is always required in the signature?

Sorry, the wording was unclear; since there's no separate string type, a string literal (a.k.a. "..." syntax) assigns to a [u8; ...].

@jdpage jdpage removed this from the Hello world! milestone Aug 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants