Skip to content

Commit

Permalink
comptime: add the Bits method to the comptimeTypeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jun 23, 2024
1 parent dc8588f commit 15f2286
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
7 changes: 6 additions & 1 deletion std/comptime/type.jule
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ enum Kind {
//
// Returns string value of type.
// Returns as constant expression.
// fn Str(self): str
// fn Str(self): str
//
// Returns bitsize of type.
// Supports only primitive integer and floating-point types.
// Returns as constant expression.
// fn Bits(self): int
46 changes: 38 additions & 8 deletions std/jule/sema/comptime.jule
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// license that can be found in the LICENSE file.

use ast for std::jule::ast
use std::jule::build::{LogMsg}
use std::jule::constant::{Const}
use types for std::jule::types

// All comptime-structure methods starts with underscore (_).

Expand Down Expand Up @@ -110,6 +112,27 @@ impl comptimeType {
ret evalEnumStatic(enm, item)
}

fn _Bits(mut &self, mut &e: &Eval, mut &fc: &ast::FnCallExpr): &Data {
let prim = self.Base.Prim()
if prim == nil {
e.pushErr(fc.Token, LogMsg.InvalidTypeForFn, self.Base.Str(), "Bits")
ret nil
}
let n = types::BitsizeOf(prim.Kind)
if n == -1 {
e.pushErr(fc.Token, LogMsg.InvalidTypeForFn, self.Base.Str(), "Bits")
ret nil
}
let mut constant = Const.NewI64(i64(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 @@ -118,28 +141,35 @@ impl comptimeType {
ret self._Str()
},
}
ret &Data{
Kind: &TypeKind{
Kind: method,
ret buildAsComptimeMethodData(method)
| "Bits":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
ret self._Bits(e, fc)
},
}
ret buildAsComptimeMethodData(method)
| "Kind":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
ret self._Kind(e)
},
}
ret &Data{
Kind: &TypeKind{
Kind: method,
},
}
ret buildAsComptimeMethodData(method)
|:
ret nil
}
}
}

fn buildAsComptimeMethodData(mut &f: &FnIns): &Data {
ret &Data{
Kind: &TypeKind{
Kind: f,
},
}
}

fn findComptimePackage(mut &s: &Sema): &ImportInfo {
ret s.SelectPackage(fn(pkg: &ImportInfo): bool {
ret pkg.LinkPath == "std::comptime"
Expand Down

0 comments on commit 15f2286

Please sign in to comment.