Skip to content

Commit

Permalink
std::comptime: add the Size 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 b6b7229 commit 362b49c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion std/comptime/type.jule
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ enum Kind {
//
// Returns comptimeTypeInfo for element type.
// Supports only pointers (except unsafe pointer), smart pointers, arrays, and slices.
// fn Elem(self): comptimeTypeInfo
// fn Elem(self): comptimeTypeInfo
//
// Returns size of array.
// Returns as constant expression.
// Returns zero if array type is auto-sized declaration.
// fn Size(self): int
23 changes: 23 additions & 0 deletions std/jule/sema/comptime.jule
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,22 @@ impl comptimeType {
ret nil
}

fn _Size(mut &self, mut &e: &Eval, mut &fc: &ast::FnCallExpr): &Data {
let mut arr = self.Base.Arr()
if arr == nil {
e.pushErr(fc.Token, LogMsg.InvalidTypeForFn, self.Base.Str(), "Size")
ret nil
}
let mut constant = Const.NewI64(i64(arr.N))
ret &Data{
Kind: &TypeKind{
Kind: buildPrimType(PrimKind.Int),
},
Constant: constant,
Model: constant,
}
}

fn subIdent(mut &self, ident: str): &Data {
match ident {
| "Str":
Expand All @@ -181,6 +197,13 @@ impl comptimeType {
},
}
ret buildAsComptimeMethodData(method)
| "Size":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
ret self._Size(e, fc)
},
}
ret buildAsComptimeMethodData(method)
| "Kind":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
Expand Down

0 comments on commit 362b49c

Please sign in to comment.