diff --git a/std/comptime/value.jule b/std/comptime/value.jule new file mode 100644 index 000000000..98b1335e0 --- /dev/null +++ b/std/comptime/value.jule @@ -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 +} + +*/ \ No newline at end of file diff --git a/std/jule/sema/builtin.jule b/std/jule/sema/builtin.jule index ce84dc63e..1424e069e 100644 --- a/std/jule/sema/builtin.jule +++ b/std/jule/sema/builtin.jule @@ -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 @@ -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") diff --git a/std/jule/sema/comptime.jule b/std/jule/sema/comptime.jule index feecf6ef3..bd250f269 100644 --- a/std/jule/sema/comptime.jule +++ b/std/jule/sema/comptime.jule @@ -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{ @@ -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, + }, + }, + } } \ No newline at end of file diff --git a/std/jule/sema/eval.jule b/std/jule/sema/eval.jule index f7f03c1d4..5fd0b8544 100644 --- a/std/jule/sema/eval.jule +++ b/std/jule/sema/eval.jule @@ -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 diff --git a/std/jule/sema/type.jule b/std/jule/sema/type.jule index b3c31dad8..5666f8405 100644 --- a/std/jule/sema/type.jule +++ b/std/jule/sema/type.jule @@ -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 { @@ -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.