Skip to content

Commit

Permalink
std::comptime: initialize value support for reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jul 2, 2024
1 parent b88e937 commit ecfa05b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
22 changes: 22 additions & 0 deletions std/comptime/value.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

// Returns compile-time value information.
// Cannot assign to memory, just available in compile-time.
// The expression is evaluated to determine and handle value in compile-time and
// will not executed at runtime.
// fn ValueOf(EXPRESSION): comptimeValue

/* >>> Hint comptimeValue implementation.

// Private compile-time vaue information wrapper.
// Only supports classic expressions.
struct comptimeValue {}

impl comptimeValue {
// Returns type information for value.
fn Type(self): comptimeTypeInfo
}

*/
18 changes: 18 additions & 0 deletions std/jule/sema/builtin.jule
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ fn findBuiltinDefStdComptime(ident: str): any {
| "TypeOf":
static mut f = &FnIns{caller: builtinCallerStdComptimeTypeOf}
ret f
| "ValueOf":
static mut f = &FnIns{caller: builtinCallerStdComptimeValueOf}
ret f
| "Range":
static mut f = &FnIns{caller: builtinCallerStdComptimeRange}
ret f
Expand Down Expand Up @@ -871,6 +874,21 @@ fn builtinCallerStdComptimeTypeOf(mut &e: &Eval, mut &fc: &FnCallExpr, mut &_: &
ret buildComptimeTypeInfoData(e.s, d.Kind)
}

fn builtinCallerStdComptimeValueOf(mut &e: &Eval, mut &fc: &FnCallExpr, mut &_: &Data): &Data {
if len(fc.Args) < 1 {
e.pushErr(fc.Token, LogMsg.MissingExprFor, "expr")
ret nil
}
if len(fc.Args) > 1 {
e.pushErr(fc.Args[1].Token, LogMsg.ArgumentOverflow, "ValueOf")
}
let mut d = e.evalExpr(fc.Args[0])
if d == nil {
ret nil
}
ret buildComptimeValue(d)
}

fn builtinCallerStdComptimeRange(mut &e: &Eval, mut &fc: &FnCallExpr, mut &_: &Data): &Data {
if len(fc.Args) < 1 {
e.pushErr(fc.Token, LogMsg.MissingExprFor, "expr")
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 @@ -1032,6 +1032,36 @@ impl comptimeTypeInfo {
}
}

// Compile-time value information data.
struct comptimeValue {
data: &Data
}

impl Kind for comptimeValue {
fn Str(self): str { ret "comptimeValue" }
fn Equal(&self, other: &TypeKind): bool { ret false }
}

impl comptimeValue {
fn _Type(mut &self, mut &e: &Eval): &Data {
ret buildComptimeTypeInfoData(e.s, self.data.Kind)
}

fn subIdent(mut &self, ident: str): &Data {
match ident {
| "Type":
let mut method = &FnIns{
caller: fn(mut &e: &Eval, mut &fc: &ast::FnCallExpr, mut &_: &Data): &Data {
ret self._Type(e)
},
}
ret buildAsComptimeMethodData(method)
|:
ret nil
}
}
}

fn buildAsComptimeMethodData(mut &f: &FnIns): &Data {
ret &Data{
Kind: &TypeKind{
Expand All @@ -1052,4 +1082,14 @@ fn buildComptimeTypeInfoData(mut &s: &Sema, mut &t: &TypeKind): &Data {
Kind: s.meta.pushComptimeTypeInfo(t),
},
}
}

fn buildComptimeValue(mut &d: &Data): &Data {
ret &Data{
Kind: &TypeKind{
Kind: &comptimeValue{
data: d,
},
},
}
}
3 changes: 3 additions & 0 deletions std/jule/sema/eval.jule
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,9 @@ impl Eval {
| d.Kind.comptimeStatic() != nil:
let mut ct = d.Kind.comptimeStatic()
ret self.comptimeObjSubIdent(ct, d, si)
| d.Kind.comptimeValue() != nil:
let mut ct = d.Kind.comptimeValue()
ret self.comptimeObjSubIdent(ct, d, si)
}

let mut kind = d.Kind
Expand Down
12 changes: 11 additions & 1 deletion std/jule/sema/type.jule
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ impl TypeKind {
self.comptimeStatic() != nil ||
self.comptimeParams() != nil ||
self.comptimeParam() != nil ||
self.comptimeRange() != nil
self.comptimeRange() != nil ||
self.comptimeValue() != nil
}

fn comptimeStructFields(mut self): &comptimeStructFields {
Expand Down Expand Up @@ -479,6 +480,15 @@ impl TypeKind {
ret nil
}
}

fn comptimeValue(mut self): &comptimeValue {
match type self.Kind {
| &comptimeValue:
ret (&comptimeValue)(self.Kind)
|:
ret nil
}
}
}

// Type.
Expand Down

0 comments on commit ecfa05b

Please sign in to comment.