Skip to content

Commit

Permalink
std::comptime: add the Key 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 362b49c commit 6607b10
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
56 changes: 33 additions & 23 deletions std/comptime/type.jule
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,37 @@ enum Kind {
// will not executed at runtime.
// fn TypeOf(TYPE || EXPRESSION): comptimeTypeInfo

/* >>> Hint comptimeTypeInfo implementation.

// Private compile-time type information wrapper.
// struct comptimeTypeInfo
//
// Returns Kind of type.
// Returns as constant expression.
// fn Kind(self): Kind
//
// Returns string value of type.
// Returns as constant expression.
// fn Str(self): str
//
// Returns bitsize of type.
// Supports only primitive integer and floating-point types.
// Returns as constant expression.
// fn Bits(self): int
//
// Returns comptimeTypeInfo for element type.
// Supports only pointers (except unsafe pointer), smart pointers, arrays, and slices.
// 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
struct comptimeTypeInfo {}

impl comptimeTypeInfo {
// Returns Kind of type.
// Returns as constant expression.
fn Kind(self): Kind

// Returns string value of type.
// Returns as constant expression.
fn Str(self): str

// Returns bitsize of type.
// Supports only primitive integer and floating-point types.
// Returns as constant expression.
fn Bits(self): int

// Returns comptimeTypeInfo for element type.
// Supports only pointers (except unsafe pointer), smart pointers, arrays, and slices.
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

// Returns type information for key type.
// Supports only map types.
fn Key(self): comptimeTypeInfo
}

*/
16 changes: 16 additions & 0 deletions std/jule/sema/comptime.jule
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ impl comptimeType {
}
}

fn _Key(mut &self, mut &e: &Eval, mut &fc: &ast::FnCallExpr): &Data {
let mut m = self.Base.Map()
if m == nil {
e.pushErr(fc.Token, LogMsg.InvalidTypeForFn, self.Base.Str(), "Key")
ret nil
}
ret buildComptimeTypeInfoData(e.s, m.Key)
}

fn subIdent(mut &self, ident: str): &Data {
match ident {
| "Str":
Expand Down Expand Up @@ -204,6 +213,13 @@ impl comptimeType {
},
}
ret buildAsComptimeMethodData(method)
| "Key":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
ret self._Key(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 6607b10

Please sign in to comment.