conlang: Introduce as casting

Arbitrary primitive type conversion

Currently implemented as jankily as possible in the interpreter, but it works alright™️
This commit is contained in:
2024-07-26 05:26:08 -05:00
parent a8b8a91c79
commit e43847bbd4
10 changed files with 100 additions and 5 deletions

View File

@@ -99,6 +99,7 @@ pub enum Parsing {
BinaryKind,
Unary,
UnaryKind,
Cast,
Index,
Structor,
Fielder,
@@ -204,6 +205,7 @@ impl Display for Parsing {
Parsing::BinaryKind => "a binary operator",
Parsing::Unary => "a unary expression",
Parsing::UnaryKind => "a unary operator",
Parsing::Cast => "an `as`-casting expression",
Parsing::Index => "an indexing expression",
Parsing::Structor => "a struct constructor expression",
Parsing::Fielder => "a struct field expression",

View File

@@ -948,6 +948,17 @@ impl<'t> Parser<'t> {
continue;
}
if let Punct::As = op {
let before = Precedence::Cast.level();
if before < power {
break;
}
self.consume_peeked();
let ty = self.ty()?;
head = Cast { head: head.into(), ty }.into();
continue;
}
if let Punct::Eq = op {
let (before, after) = Precedence::Assign
.infix()
@@ -961,6 +972,7 @@ impl<'t> Parser<'t> {
}
break;
}
Ok(head)
}
@@ -1213,13 +1225,14 @@ pub enum Precedence {
Factor,
Term,
Unary,
Cast,
Member, // left-associative
Call,
}
impl Precedence {
#[inline]
pub fn level(self) -> u8 {
pub const fn level(self) -> u8 {
(self as u8) << 1
}
pub fn prefix(self) -> Option<((), u8)> {