Skip to content

Commit 2ea7dbd

Browse files
committed
std/comptime: add the IsTag method to comptimeDecl
1 parent e5b5fb6 commit 2ea7dbd

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

std/comptime/decl.jule

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ impl comptimeDecl {
7474
// Parameter key should be constant.
7575
// Returns empty string if key is not exist in the tags.
7676
fn Tag(self, key: str): str
77+
78+
// Reports whether the key is exist in tags as a constant expression.
79+
// Supports only structure fields.
80+
// Parameter key should be constant.
81+
fn IsTag(self, key: str): bool
7782
}
7883

7984
*/

std/jule/sema/comptime.jule

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,41 @@ impl comptimeDecl {
20402040
}
20412041
}
20422042

2043+
fn _IsTag(mut &self, mut &e: &eval, mut &fc: &ast::FuncCallExpr): &Value {
2044+
if len(fc.Args) > 1 {
2045+
e.pushErr(fc.Args[1].Token, log::ArgumentOverflow, "IsTag")
2046+
ret nil
2047+
}
2048+
f, field := self.decl.(&Field)
2049+
if !field {
2050+
e.pushErr(fc.Token, log::InvalidTypeForFunc, self.declAsStr(), "IsTag")
2051+
ret nil
2052+
}
2053+
mut arg := fc.Args[0]
2054+
mut v := e.evalExpr(arg, evalDefault)
2055+
if v == nil {
2056+
ret nil
2057+
}
2058+
if !v.IsConst() {
2059+
e.pushErr(arg.Token, log::ExprNotConst)
2060+
ret nil
2061+
}
2062+
prim := v.Type.Prim()
2063+
if prim == nil || !prim.IsStr() {
2064+
e.pushErr(arg.Token, log::IncompatibleTypes, str(types::Str), v.Type.Str())
2065+
ret nil
2066+
}
2067+
key := v.Constant.ReadStr()
2068+
_, exist := f.Tags[key]
2069+
mut constant := constant::Const.NewBool(exist)
2070+
ret &Value{
2071+
untyped: true,
2072+
Type: primBool,
2073+
Constant: constant,
2074+
Model: constant,
2075+
}
2076+
}
2077+
20432078
fn subIdent(mut &self, &ident: str): &Value {
20442079
match ident {
20452080
| "Name":
@@ -2126,6 +2161,13 @@ impl comptimeDecl {
21262161
},
21272162
}
21282163
ret buildAsComptimeMethodData(method)
2164+
| "IsTag":
2165+
mut method := &FuncIns{
2166+
caller: fn(mut &e: &eval, mut &fc: &ast::FuncCallExpr, mut &_: &Value): &Value {
2167+
ret self._IsTag(e, fc)
2168+
},
2169+
}
2170+
ret buildAsComptimeMethodData(method)
21292171
|:
21302172
ret nil
21312173
}

0 commit comments

Comments
 (0)