Skip to content

Commit e5b5fb6

Browse files
committed
std/comptime: add the Tag mehod to comptimeDecl
1 parent de7b1d2 commit e5b5fb6

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

std/comptime/decl.jule

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ impl comptimeDecl {
6868
// Returns count of tags as constant expression.
6969
// Supports only structure fields.
7070
fn Tags(self): int
71+
72+
// Returns the value of the tag corresponding to the key as a constant expression.
73+
// Supports only structure fields.
74+
// Parameter key should be constant.
75+
// Returns empty string if key is not exist in the tags.
76+
fn Tag(self, key: str): str
7177
}
7278

7379
*/

std/comptime/value.jule

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ impl comptimeValue {
2929

3030
// Returns comptimeValue for field access expression.
3131
// Supports only structure types.
32-
// Parameter ident should be constant.
32+
// Parameter name should be constant.
3333
// It allows access to private fields.
34-
fn Field(self, ident: str): comptimeValue
34+
fn Field(self, name: str): comptimeValue
3535

36-
// Same as the Field method, but takes constant index instead of identifier.
36+
// Same as the Field method, but takes constant index instead of name.
3737
fn FieldByIndex(self, index: int): comptimeValue
3838

3939
// Returns comptimeValue for method access expression.
4040
// Supports only structure types.
41-
// Parameter ident should be constant.
41+
// Parameter name should be constant.
4242
// It allows access to private methods.
4343
// It will not use the actual kind, so this method an provide
4444
// access to methods of the any strict type alias.
45-
fn Method(self, ident: str): comptimeValue
45+
fn Method(self, name: str): comptimeValue
4646

4747
// Unwraps expression for runtime execution.
4848
fn Unwrap(self)

std/jule/sema/comptime.jule

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,43 @@ impl comptimeDecl {
20002000
Model: constant,
20012001
}
20022002
|:
2003-
panic("sema: comptimeDecl.Tags: unimplemented declaration kind")
2003+
e.pushErr(fc.Token, log::InvalidTypeForFunc, self.declAsStr(), "Tag")
2004+
ret nil
2005+
}
2006+
}
2007+
2008+
fn _Tag(mut &self, mut &e: &eval, mut &fc: &ast::FuncCallExpr): &Value {
2009+
if len(fc.Args) > 1 {
2010+
e.pushErr(fc.Args[1].Token, log::ArgumentOverflow, "Tag")
2011+
ret nil
2012+
}
2013+
f, field := self.decl.(&Field)
2014+
if !field {
2015+
e.pushErr(fc.Token, log::InvalidTypeForFunc, self.declAsStr(), "Tag")
2016+
ret nil
2017+
}
2018+
mut arg := fc.Args[0]
2019+
mut v := e.evalExpr(arg, evalDefault)
2020+
if v == nil {
2021+
ret nil
2022+
}
2023+
if !v.IsConst() {
2024+
e.pushErr(arg.Token, log::ExprNotConst)
2025+
ret nil
2026+
}
2027+
prim := v.Type.Prim()
2028+
if prim == nil || !prim.IsStr() {
2029+
e.pushErr(arg.Token, log::IncompatibleTypes, str(types::Str), v.Type.Str())
2030+
ret nil
2031+
}
2032+
key := v.Constant.ReadStr()
2033+
value, _ := f.Tags[key]
2034+
mut constant := constant::Const.NewStr(value)
2035+
ret &Value{
2036+
untyped: true,
2037+
Type: primStr,
2038+
Constant: constant,
2039+
Model: constant,
20042040
}
20052041
}
20062042

@@ -2083,6 +2119,13 @@ impl comptimeDecl {
20832119
},
20842120
}
20852121
ret buildAsComptimeMethodData(method)
2122+
| "Tag":
2123+
mut method := &FuncIns{
2124+
caller: fn(mut &e: &eval, mut &fc: &ast::FuncCallExpr, mut &_: &Value): &Value {
2125+
ret self._Tag(e, fc)
2126+
},
2127+
}
2128+
ret buildAsComptimeMethodData(method)
20862129
|:
20872130
ret nil
20882131
}

0 commit comments

Comments
 (0)