Skip to content

Commit

Permalink
julefmt: add type declaration support
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 21, 2024
1 parent 159202e commit 954957e
Showing 1 changed file with 100 additions and 1 deletion.
101 changes: 100 additions & 1 deletion src/format.jule
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Formatter {
} else if p.reference {
self.buf += "_: "
}
// PARSE_TYPE
self.buf += TypeFormatter.format(p.kind)
if i + 1 < d.params.len {
self.buf += ", "
}
Expand Down Expand Up @@ -150,3 +150,102 @@ impl Formatter {
ret strings::trim(self.buf, " \n\r\v\t")
}
}

struct TypeFormatter {
mut buf: str
}

impl TypeFormatter {
static fn format(&t: &ast::TypeDecl): str {
let tf = TypeFormatter{}
tf.format(t.kind)
ret tf.buf
}

fn generics(self, g: []&ast::TypeDecl) {
if g.len == 0 {
ret
}
self.buf += "["
for i, t in g {
self.format(t.kind)
if i + 1 < g.len {
self.buf += ","
}
}
self.buf += "]"
}

fn ident(self, id: &ast::IdentTypeDecl) {
if id.cpp_linked {
self.buf += "cpp."
}
self.buf += id.ident
self.generics(id.generics)
}

fn namespace(self, ns: &ast::NamespaceTypeDecl) {
for _, id in ns.idents {
self.buf += id.kind
self.buf += "::"
}
self.ident(ns.kind)
}

fn sptr(self, sptr: &ast::SptrTypeDecl) {
self.buf += "&"
self.format(sptr.elem.kind)
}

fn slc(self, slc: &ast::SlcTypeDecl) {
self.buf += "[]"
self.format(slc.elem.kind)
}

fn ptr(self, ptr: &ast::PtrTypeDecl) {
self.buf += "*"
if ptr.is_unsafe() {
self.buf += "unsafe"
ret
}
self.format(ptr.elem.kind)
}

fn arr(self, arr: &ast::ArrTypeDecl) {
self.buf += "["
if arr.auto_sized() {
self.buf += "..."
} else {
// EXPR
}
self.buf += "]"
self.format(arr.elem.kind)
}

fn map(self, map: &ast::MapTypeDecl) {
self.buf += "["
self.format(map.key.kind)
self.buf += ":"
self.format(map.val.kind)
self.buf += "]"
}

fn format(self, &kind: any) {
match type kind {
| &ast::IdentTypeDecl:
self.ident((&ast::IdentTypeDecl)(kind))
| &ast::NamespaceTypeDecl:
self.namespace((&ast::NamespaceTypeDecl)(kind))
| &ast::SptrTypeDecl:
self.sptr((&ast::SptrTypeDecl)(kind))
| &ast::SlcTypeDecl:
self.slc((&ast::SlcTypeDecl)(kind))
| &ast::PtrTypeDecl:
self.ptr((&ast::PtrTypeDecl)(kind))
| &ast::ArrTypeDecl:
self.arr((&ast::ArrTypeDecl)(kind))
| &ast::MapTypeDecl:
self.map((&ast::MapTypeDecl)(kind))
}
}
}

0 comments on commit 954957e

Please sign in to comment.