Hello, I spent a little bit of time throwing together a README for a programming language that I am working on after repeatedly abandoning previous projects. For this project, I really wanted to start by focusing on the type system and I thought of some cool ideas. I have a lot of the implementation details worked out, but I wanted to know if anyone had thoughts!
(repost because I didn't know I couldn't upload GitHub links)
Below is the README pasted from my GitHub, you can also view it at ephf/boxc for better Markdown parsing.
boxc
boxc is a C-like language transpiled into C.
Development
[!IMPORTANT] This language is in active development and has no current releases, below are some ideas / dev logs and current progress. Below code examples, you will see checklists on what is implemented and what is not.
Although it may not look like I have done much in the development of this language, I have made a couple of other unfinished languages that I have learned from. See,
Tuples
The main idea for this first round of development is focusing on the type system, generics, and overloading. In order to do this, I am trying to build out a tuple type, similar to C++.
struct tuple<...>;
This code declares a tuple structure without a body, it cannot be used in this state to define a variable,
tuple<> x; // error: tuple<> has no body
The tuple struct will have two main overloads that will create a linked-list structure in compile time.
struct tuple<<First, Items...>> {
First value;
tuple<Items> next;
}
struct tuple<> {}
[!NOTE] Defining a tuple using <<>> instead of <> is a shorthand which will create an overload. Normally overloading requires type arguments and a generics declaration, like the following:
This can also be used to mix and match type arguments and generics,
Below is an example of what the compiled code would look like,
tuple<i32, char, usize> x;
struct tuple {};
struct tuple0 {
size_t value;
struct tuple next;
};
struct tuple1 {
char value;
struct tuple0 next;
};
struct tuple2 {
int32_t value;
struct tuple1 next;
};
struct tuple2 x;
You could implement a function similar to std::get<size_t> in C++, however there are a couple of things I wanted to implement that would make this easier.
Here I have defined a trait for the field_access operation or .
namespace std::operator {
trait field_access<Accessor> {
Field field_access<Field>(self, Accessor);
Field field_access<Field, Accessor accessor>(self);
}
}
The field_access trait has two functions that can be overridden. The first one accepts any expression, and the second one, which takes precedence, takes any compiler constant expression.
We can implement this trait in the tuple type to make it easier to select specific tuple values.
struct tuple<First, Items><Index : usize, First, Items...>
: std::operator::field_access<Index>
{
First value;
tuple<Items> next;
inline auto field_access<First, index><Index index>(self)
index ? next.field_access<auto, index - 1>() : value;
}
This will allow us to use the . operator to get a specific element in our tuples at compile-time,
tuple<i32, u32> x;
auto y = x.0;
auto z = z.1;
assert typeof y == i32;
assert typeof z == u32;
Overall, I think one of the main things I want in this language is for a lot of the language features to be implementable from within the language itself instead of relying on syntactic sugar.
Optionals
Similar to JavaScript's ?. operator, I wanted to create an easy way to interface with nullable values and optionals. Below is a definition for an operator override using ? as an operator.
namespace std {
trait nullable_coalesce<NonNull> {
std::tuple<bool, NonNull> nullable_coalesce(self);
}
}
If we have a value that implements this trait, we can use the ? operator to conditionally search through a hierarchy of values.
[!NOTE] You can differentiate the ? nullish coalescing operator from the ? ternary operator because the nullish coalescing operator needs to be followed by a postfix operator like . or ().
struct MyStruct {
int field;
}
int? get_field<T : std::nullable_coalesce<MyStruct>>(T maybe) {
return maybe?.field;
}
MyStruct* not_null = &{5}; // referencing an rvalue will stack allocate its value
auto x = get_field(not_null);
auto y = get_field(null); // null is a keyword similar to nullptr, but it can also be used as a type
assert *x == 5;
assert !y;
int? is syntactic sugar for std::option<int>. Before we write option, there are a couple of traits it needs to override,
namespace std::operator {
trait dereference<Type> {
Type dereference(self);
}
}
namespace std::trans {
trait from {
Self from<From>(From);
}
trait into {
Into into<Into>(self);
}
}
Now we can write option,
namespace std {
struct option<Type>
: operator::dereference<Type>, trans::from, trans::into
{
Type || null data;
inline Type dereference(self)
self.data.(Type);
inline Self from<Type>(Type data)
Self { data };
inline Self from<null>(null)
Self { null };
inline bool into(self)
self.data.is<Type>();
};
}
Overall, even for syntactic sugar, there will be definitions inside of std that are being referenced.