Skip to content

Commit

Permalink
comptime: add the Elem method to the comptimeTypeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jun 24, 2024
1 parent ae4cbf5 commit b6b7229
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
6 changes: 5 additions & 1 deletion std/comptime/type.jule
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ enum Kind {
// Returns bitsize of type.
// Supports only primitive integer and floating-point types.
// Returns as constant expression.
// fn Bits(self): int
// fn Bits(self): int
//
// Returns comptimeTypeInfo for element type.
// Supports only pointers (except unsafe pointer), smart pointers, arrays, and slices.
// fn Elem(self): comptimeTypeInfo
7 changes: 1 addition & 6 deletions std/jule/sema/builtin.jule
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,7 @@ fn builtinCallerStdComptimeTypeOf(mut &e: &Eval, mut &fc: &FnCallExpr, mut &_: &
e.pushErr(fc.Token, LogMsg.InvalidTypeForTypeOf, d.Kind.Str())
ret nil
}

ret &Data{
Kind: &TypeKind{
Kind: e.s.meta.pushComptimeType(d.Kind)
},
}
ret buildComptimeTypeInfoData(e.s, d.Kind)
}

fn builtinCallerStdMemSizeOf(mut &e: &Eval, mut &fc: &FnCallExpr, mut &_: &Data): &Data {
Expand Down
40 changes: 40 additions & 0 deletions std/jule/sema/comptime.jule
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ impl comptimeType {
}
}

fn _Elem(mut &self, mut &e: &Eval, mut &fc: &ast::FnCallExpr): &Data {
let mut ptr = self.Base.Ptr()
if ptr != nil {
if ptr.IsUnsafe() {
e.pushErr(fc.Token, LogMsg.InvalidTypeForFn, self.Base.Str(), "Elem")
ret nil
}
ret buildComptimeTypeInfoData(e.s, ptr.Elem)
}
let mut sptr = self.Base.Sptr()
if sptr != nil {
ret buildComptimeTypeInfoData(e.s, sptr.Elem)
}
let mut slice = self.Base.Slc()
if slice != nil {
ret buildComptimeTypeInfoData(e.s, slice.Elem)
}
let mut array = self.Base.Arr()
if array != nil {
ret buildComptimeTypeInfoData(e.s, array.Elem)
}
e.pushErr(fc.Token, LogMsg.InvalidTypeForFn, self.Base.Str(), "Elem")
ret nil
}

fn subIdent(mut &self, ident: str): &Data {
match ident {
| "Str":
Expand All @@ -149,6 +174,13 @@ impl comptimeType {
},
}
ret buildAsComptimeMethodData(method)
| "Elem":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
ret self._Elem(e, fc)
},
}
ret buildAsComptimeMethodData(method)
| "Kind":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
Expand All @@ -174,4 +206,12 @@ fn findComptimePackage(mut &s: &Sema): &ImportInfo {
ret s.SelectPackage(fn(pkg: &ImportInfo): bool {
ret pkg.LinkPath == "std::comptime"
})
}

fn buildComptimeTypeInfoData(mut &s: &Sema, mut &t: &TypeKind): &Data {
ret &Data{
Kind: &TypeKind{
Kind: s.meta.pushComptimeType(t)
},
}
}

0 comments on commit b6b7229

Please sign in to comment.