ast: add gens for ty and impl, raw ptr types, make fn return value non-optional

This commit is contained in:
2025-07-18 05:25:35 -04:00
parent 6ba62ac1c4
commit 148ef34a01
19 changed files with 242 additions and 147 deletions

View File

@@ -273,7 +273,7 @@ pub mod clangify {
fn print(&self, y: &mut CLangifier) {
let Self { name, gens: _, sign, bind, body } = self;
let TyFn { args, rety } = sign;
let types = match args.as_ref() {
let types = match &args.kind {
TyKind::Tuple(TyTuple { types }) => types.as_slice(),
TyKind::Empty => &[],
_ => panic!("Unsupported function args: {args}"),
@@ -282,13 +282,7 @@ pub mod clangify {
Pattern::Tuple(tup) => tup.as_slice(),
_ => panic!("Unsupported function binders: {args}"),
};
match rety {
Some(ty) => y.p(ty),
None => y.p("void"),
}
.p(" ")
.p(name)
.p(" (");
y.p(rety).p(" ").p(name).p(" (");
for (idx, (bind, ty)) in bind.iter().zip(types).enumerate() {
if idx > 0 {
y.p(", ");
@@ -347,8 +341,8 @@ pub mod clangify {
}
impl CLangify for Impl {
fn print(&self, y: &mut CLangifier) {
let Self { target, body } = self;
y.nest("/* TODO: impl ").p(target).p(" { */ ");
let Self { gens, target, body } = self;
y.nest("/* TODO: impl ").p(gens).p(target).p(" { */ ");
y.p(body);
y.p("/* } // impl ").p(target).p(" */ ");
}
@@ -477,7 +471,7 @@ pub mod clangify {
}
TyKind::Fn(TyFn { args, rety }) => {
y.nest("(").p(rety).p(" *").p(mutable).p(name).p(")(");
match args.as_ref() {
match &args.kind {
TyKind::Empty => {}
TyKind::Tuple(TyTuple { types }) => {
for (idx, ty) in types.iter().enumerate() {
@@ -688,14 +682,14 @@ pub mod clangify {
impl CLangify for ArrayRep {
fn print(&self, y: &mut CLangifier) {
let Self { value, repeat } = self;
let ExprKind::Literal(Literal::Int(repeat)) = &repeat.kind else {
eprintln!("Constant needs folding: {repeat}");
return;
};
{
let mut y = y.nest("{");
y.endl();
for idx in 0..*repeat {
if idx > 0 {
y.p(", ");
}
y.p(value);
for _ in 0..*repeat {
y.endl().p(value).p(",");
}
}
y.endl().p("}");
@@ -797,7 +791,7 @@ pub mod clangify {
}
impl CLangify for Ty {
fn print(&self, y: &mut CLangifier) {
let Self { span: _, kind } = self;
let Self { span: _, kind, gens: _ } = self;
y.p(kind);
}
}
@@ -806,10 +800,11 @@ pub mod clangify {
match self {
TyKind::Never => y.p("Never"),
TyKind::Empty => y.p("Empty"),
TyKind::Infer => y.p("Any"),
TyKind::Infer => y.p("auto"),
TyKind::Path(t) => y.p(t),
TyKind::Tuple(t) => y.p(t),
TyKind::Ref(t) => y.p(t),
TyKind::Ptr(t) => y.p(t),
TyKind::Fn(t) => y.p(t),
TyKind::Slice(t) => y.p(t),
TyKind::Array(t) => y.p(t),
@@ -873,12 +868,18 @@ pub mod clangify {
}
}
}
impl CLangify for TyPtr {
fn print(&self, y: &mut CLangifier) {
let Self { to } = self;
y.p(to).p("*");
}
}
impl CLangify for TyFn {
fn print(&self, y: &mut CLangifier) {
let Self { args, rety } = self;
// TODO: function pointer syntax
y.nest("(").p(rety).p(" *)(");
match args.as_ref() {
match &args.kind {
TyKind::Empty => y,
TyKind::Tuple(TyTuple { types }) => {
for (idx, ty) in types.iter().enumerate() {

View File

@@ -83,7 +83,7 @@ pub mod yamler {
pub fn key(&mut self, name: impl Yamlify) -> Section {
println!();
self.print_indentation(&mut std::io::stdout().lock());
print!("- ");
print!(" ");
name.yaml(self);
print!(":");
self.indent()
@@ -103,8 +103,10 @@ pub mod yamler {
}
pub fn list<D: Yamlify>(&mut self, list: &[D]) -> &mut Self {
for (idx, value) in list.iter().enumerate() {
self.pair(idx, value);
for value in list {
println!();
self.print_indentation(&mut std::io::stdout().lock());
self.yaml(&"- ").yaml(value);
}
self
}
@@ -302,8 +304,11 @@ pub mod yamlify {
}
impl Yamlify for Impl {
fn yaml(&self, y: &mut Yamler) {
let Self { target, body } = self;
y.key("Impl").pair("target", target).pair("body", body);
let Self { gens, target, body } = self;
y.key("Impl")
.pair("gens", gens)
.pair("target", target)
.pair("body", body);
}
}
impl Yamlify for ImplKind {
@@ -536,7 +541,10 @@ pub mod yamlify {
impl Yamlify for Index {
fn yaml(&self, y: &mut Yamler) {
let Self { head, indices } = self;
y.key("Index").pair("head", head).list(indices);
y.key("Index")
.pair("head", head)
.key("indices")
.list(indices);
}
}
impl Yamlify for Structor {
@@ -589,7 +597,7 @@ pub mod yamlify {
impl Yamlify for Else {
fn yaml(&self, y: &mut Yamler) {
let Self { body } = self;
y.key("Else").yaml(body);
y.key("fail").yaml(body);
}
}
impl Yamlify for If {
@@ -633,8 +641,8 @@ pub mod yamlify {
}
impl Yamlify for Ty {
fn yaml(&self, y: &mut Yamler) {
let Self { span: _, kind } = self;
y.key("Ty").yaml(kind);
let Self { span: _, kind, gens } = self;
y.key("Ty").yaml(kind).yaml(gens);
}
}
impl Yamlify for TyKind {
@@ -646,6 +654,7 @@ pub mod yamlify {
TyKind::Path(t) => y.yaml(t),
TyKind::Tuple(t) => y.yaml(t),
TyKind::Ref(t) => y.yaml(t),
TyKind::Ptr(t) => y.yaml(t),
TyKind::Fn(t) => y.yaml(t),
TyKind::Slice(t) => y.yaml(t),
TyKind::Array(t) => y.yaml(t),
@@ -659,9 +668,7 @@ pub mod yamlify {
if *absolute {
y.pair("absolute", absolute);
}
for part in parts {
y.pair("part", part);
}
y.yaml(parts);
}
}
impl Yamlify for PathPart {
@@ -703,6 +710,13 @@ pub mod yamlify {
.pair("to", to);
}
}
impl Yamlify for TyPtr {
fn yaml(&self, y: &mut Yamler) {
let Self { to } = self;
y.key("TyPtr")
.pair("to", to);
}
}
impl Yamlify for TyFn {
fn yaml(&self, y: &mut Yamler) {
let Self { args, rety } = self;
@@ -726,9 +740,7 @@ pub mod yamlify {
}
impl<T: Yamlify> Yamlify for Vec<T> {
fn yaml(&self, y: &mut Yamler) {
for thing in self {
y.yaml(thing);
}
y.list(self);
}
}
impl Yamlify for () {