Add ByteArray to the prelude

The compiler needs support for circular types (e.g. A depending on B and
B depending on A). The way ByteArray was set up in std::byte_array meant
this could not be done with multiple passes, as the assignment of the
`ByteArray` constant would require full type-inference too early in the
compilation process. Moving ByteArray to the prelude and setting it up
while bootstrapping simplifies this, and makes it easier for the user to
use byte arrays.
parent 7a990558
Pipeline #102439977 passed with stages
in 30 minutes and 32 seconds
......@@ -21,6 +21,7 @@ let Integer = _INKOC.get_integer_prototype
let Float = _INKOC.get_float_prototype
let Block = _INKOC.get_block_prototype
let Array = _INKOC.get_array_prototype
let ByteArray = _INKOC.get_byte_array_prototype
_INKOC.set_object_name(self, 'Inko')
_INKOC.set_object_name(Boolean, 'Boolean')
......@@ -33,6 +34,7 @@ _INKOC.set_object_name(Integer, 'Integer')
_INKOC.set_object_name(Float, 'Float')
_INKOC.set_object_name(Array, 'Array')
_INKOC.set_object_name(Block, 'Block')
_INKOC.set_object_name(ByteArray, 'ByteArray')
# Now that our core objects are set up we can start defining more of the
# building blocks of Inko, such as "Object.new" and the bits necessary to allow
......
......@@ -12,3 +12,4 @@ let Integer = Inko::Integer
let Float = Inko::Float
let Block = Inko::Block
let Array = Inko::Array
let ByteArray = Inko::ByteArray
......@@ -16,11 +16,6 @@ import std::iterator::(self, Iterator)
import std::length::Length
import std::operators::Equal
# A mutable sequence of bytes, similar to an `Array`.
let ByteArray = _INKOC.get_byte_array_prototype
_INKOC.set_object_name(ByteArray, 'ByteArray')
# A type that can be converted to a `ByteArray`.
trait ToByteArray {
# Converts `self` to a `ByteArray`.
......@@ -37,14 +32,10 @@ impl ByteArray {
#
# Creating an empty `ByteArray`:
#
# import std::byte_array::ByteArray
#
# ByteArray.new
#
# Creating a `ByteArray` with values:
#
# import std::byte_array::ByteArray
#
# ByteArray.new(10, 20, 30)
static def new(*bytes: Integer) -> Self {
_INKOC.byte_array_from_array(bytes)
......@@ -56,8 +47,6 @@ impl ByteArray {
#
# Removing all values:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(10, 20, 30)
#
# bytes.clear
......@@ -73,8 +62,6 @@ impl ByteArray {
#
# Pushing a value into a `ByteArray`:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new
#
# bytes.push(10) # => 10
......@@ -92,8 +79,6 @@ impl ByteArray {
#
# Popping an existing value:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(10)
#
# bytes.pop # => 10
......@@ -101,8 +86,6 @@ impl ByteArray {
#
# Popping a value when the `ByteArray` is empty:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new
#
# bytes.pop # => Nil
......@@ -118,8 +101,6 @@ impl ByteArray {
#
# Removing an existing value:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(10)
#
# bytes.remove_at(0) # => 10
......@@ -127,8 +108,6 @@ impl ByteArray {
#
# Removing a non-existing value:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new
#
# bytes.remove_at(0) # => Nil
......@@ -148,7 +127,6 @@ impl ByteArray {
# Iterating over all the values in a `ByteArray`:
#
# import std::stdio::stdout
# import std::byte_array::ByteArray
#
# ByteArray.new(10, 20, 30).each do (byte) {
# stdout.print(byte)
......@@ -176,7 +154,6 @@ impl ByteArray {
# Iterating over the values of an `ByteArray` and their indexes:
#
# import std::stdio::stdout
# import std::byte_array::ByteArray
#
# ByteArray.new(10, 20, 30).each_with_index do (byte, index) {
# stdout.print(index) # => 0, 1, 2
......@@ -201,8 +178,6 @@ impl ByteArray {
#
# Draining a `ByteArray` into a `String`:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(105, 110, 107, 111)
#
# bytes.drain_to_string # => 'inko'
......@@ -221,8 +196,6 @@ impl ByteArray {
#
# Slicing a `ByteArray`:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(1, 2, 3, 4)
# let sliced = bytes.slice(start: 1, length: 2)
#
......@@ -256,16 +229,12 @@ impl Index!(Integer, Integer) for ByteArray {
#
# Retrieving an existing byte:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(10, 20)
#
# bytes[0] # => 10
#
# Using an out of bounds index:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(10, 20)
#
# bytes[5] # => Nil
......@@ -289,8 +258,6 @@ impl SetIndex!(Integer, Integer) for ByteArray {
#
# Setting the value of an existing index:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(10, 20)
#
# bytes[0] = 30 # => 30
......@@ -310,8 +277,6 @@ impl ToString for ByteArray {
#
# Converting a `ByteArray` into a `String`:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(105, 110, 107, 111)
#
# bytes.to_string # => 'inko'
......@@ -331,8 +296,6 @@ impl ToArray!(Integer) for ByteArray {
#
# Converting a `ByteArray`:
#
# import std::byte_array::ByteArray
#
# let bytes = ByteArray.new(105, 110, 107, 111)
#
# bytes.to_array # => Array.new(105, 110, 107, 111)
......@@ -363,8 +326,6 @@ impl Equal for ByteArray {
#
# Comparing two `ByteArray` objects:
#
# import std::byte_array::ByteArray
#
# ByteArray.new(10) == ByteArray.new(10) # => True
# ByteArray.new(10) == ByteArray.new(20) # => False
def ==(other: ByteArray) -> Boolean {
......@@ -379,8 +340,6 @@ impl Length for ByteArray {
#
# Obtaining the length of a `ByteArray`
#
# import std::byte_array::ByteArray
#
# ByteArray.new.length # => 0
# ByteArray.new(10).length # => 1
def length -> Integer {
......
......@@ -11,7 +11,6 @@
# Since this code is based on generated code, changing it may produce
# unexpected results. For example, the various lookup tables in this module
# should not have their order changed.
import std::byte_array::ByteArray
# The associated values for the first and second bytes in a keyword.
let ASSOCIATED_VALUES = Array.new(
......
......@@ -2,7 +2,7 @@
#
# The types and methods of this module are not part of the public API at this
# time, meaning they can change at any time.
import std::byte_array::(ByteArray, ToByteArray)
import std::byte_array::ToByteArray
import std::compiler::keywords
import std::compiler::source_location::SourceLocation
import std::compiler::token::*
......
......@@ -100,7 +100,6 @@
# let errno = libc.variable('errno')
#
# errno.read(types.i32) # => 0
import std::byte_array::ByteArray
import std::ffi::types::Type
import std::index::(Index, SetIndex)
import std::io::Close
......
......@@ -8,7 +8,6 @@
# file is opened in. For example, when opening a file in read-only mode you
# will be given a `ReadOnlyFile` object while opening a file in write-only mode
# produces a `WriteOnlyFile` object.
import std::byte_array::ByteArray
import std::conversion::ToString
import std::fs::path::(Path, ToPath)
import std::fs::raw::(self, READ_ONLY)
......
......@@ -2,7 +2,6 @@
#
# The methods in this module should not be used directly, instead they serve as
# building blocks for other modules such as `std::fs::file`.
import std::byte_array::ByteArray
import std::io::(Error as IOError)
import std::process
import std::time::SystemTime
......
......@@ -3,7 +3,6 @@
# The IO module provides the basic building blocks for IO operations such as
# reading from and writing to a file.
import std::byte_array::ByteArray
import std::conversion::ToString
import std::error::StandardError
import std::os::NEWLINE
......
......@@ -2,7 +2,6 @@
#
# This module should not be used directly, instead one should use
# `std::net::socket` and `std::net::unix`.
import std::byte_array::ByteArray
import std::io::(Error as IoError)
# A marker trait used for values that can be directly sent across a socket.
......
import std::byte_array::(ByteArray, ToByteArray)
import std::byte_array::ToByteArray
import std::conversion::ToString
import std::error::Error
import std::operators::Equal
......
# Networking types for TCP/UDP communication.
import std::byte_array::ByteArray
import std::conversion::(ToFloat, ToString)
import std::io::(Close, Error as IoError, Read, Write)
import std::net::bits::(
......@@ -208,7 +207,6 @@ object Socket {
#
# Sending a message to ourselves and receiving it:
#
# import std::byte_array::ByteArray
# import std::net::socket::(Socket, IPV4, DGRAM)
#
# let socket = try! Socket.new(domain: IPV4, kind: DGRAM)
......
# Networking types for Unix domain socket communication.
import std::byte_array::ByteArray
import std::conversion::ToString
import std::fs::path::(Path, ToPath)
import std::io::(Close, Error as IoError, Read, Write)
......@@ -269,7 +268,6 @@ object Socket {
#
# Sending a message to ourselves and receiving it:
#
# import std::byte_array::ByteArray
# import std::net::unix::(Socket, DGRAM)
#
# let socket = try! Socket.new(DGRAM)
......
# Generating of random values.
#
# This module provides methods for generating random numbers and bytes.
import std::byte_array::ByteArray
import std::conversion::(ToFloat, ToInteger)
# Returns a random `Integer`.
......
......@@ -12,7 +12,6 @@
# STDERR more convenient as in almost all (if not all) cases a developer
# doesn't care if a write to STDERR succeeds or not.
import std::byte_array::ByteArray
import std::conversion::ToString
import std::io::Write
import std::os::NEWLINE
......
......@@ -2,7 +2,6 @@
#
# This module provides various methods for reading data from STDIN.
import std::byte_array::ByteArray
import std::io::(Read, Error as IOError)
import std::process
......
......@@ -12,7 +12,6 @@
# STDOUT more convenient as in almost all (if not all) cases a developer
# doesn't care if a write to STDOUT succeeds or not.
import std::byte_array::ByteArray
import std::conversion::ToString
import std::io::Write
import std::os::NEWLINE
......
# Extensions for the `String` type that can only be defined later on in the
# bootstrapping process.
import std::byte_array::(ByteArray, ToByteArray)
import std::byte_array::ToByteArray
import std::format::(Formatter, Inspect)
impl Inspect for String {
......
import std::byte_array::ByteArray
import std::compiler::lexer::Lexer
import std::compiler::token::Token
import std::test
......
import std::byte_array::ByteArray
import std::env
import std::fs
import std::fs::file::(self, ReadOnlyFile, ReadWriteFile, Remove, WriteOnlyFile)
......
import std::byte_array::ByteArray
import std::conversion::ToString
import std::fs::path::Path
import std::fs::raw::(READ_APPEND, READ_ONLY, READ_WRITE)
......
import std::byte_array::ByteArray
import std::net::ip::Ipv4Address
import std::net::bits::MAXIMUM_LISTEN_BACKLOG
import std::net::socket::(
......
import std::byte_array::ByteArray
import std::fs::file
import std::fs::path::Path
import std::net::bits::MAXIMUM_LISTEN_BACKLOG
......
import std::byte_array::ByteArray
import std::format::DefaultFormatter
import std::test
import std::test::assert
......
import std::byte_array::ByteArray
import std::ffi::(
self, Function, Layout, LayoutBuilder, Library, Member, Pointer, Struct
)
......
import std::byte_array::ByteArray
import std::io::(Read, Write)
import std::os::NEWLINE
import std::test
......
import std::byte_array::ByteArray
import std::fs::path::Path
import std::map::DefaultHasher
import std::test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment