Skip to content

Commit fc53370

Browse files
committed
std/comptime: add IncludeStr (#128)
1 parent 5d144b6 commit fc53370

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

std/comptime/embed.jule

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@
66
// The file path is resolved relative to the file path of the package in which
77
// the function is called. In other words, the file is searched for in the
88
// directory of the package that contains the source file calling the function.
9-
// fn IncludeBytes(path: str): []byte
9+
// fn IncludeBytes(path: str): []byte
10+
11+
// Returns the contents of the file at path as constant string.
12+
// The file path is resolved relative to the file path of the package in which
13+
// the function is called. In other words, the file is searched for in the
14+
// directory of the package that contains the source file calling the function.
15+
// fn IncludeStr(path: str): str

std/jule/sema/builtin.jule

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,58 @@ fn builtinCallerStdComptimeIncludeBytes(mut e: &eval, mut fc: &ast::CallExpr, _:
13571357
}
13581358
}
13591359

1360+
fn builtinCallerStdComptimeIncludeStr(mut e: &eval, mut fc: &ast::CallExpr, _: &Value): &Value {
1361+
if len(fc.Args) > 1 {
1362+
e.pushErr(fc.Token, "passed more argument than expected to IncludeStr")
1363+
e.pushSuggestion("call like; IncludeStr(Path)")
1364+
ret nil
1365+
}
1366+
if len(fc.Args) == 0 {
1367+
e.pushErr(fc.Token, "identifier and type is missing for IncludeStr")
1368+
e.pushSuggestion("call like; IncludeStr(Path)")
1369+
ret nil
1370+
}
1371+
mut v := e.eval(fc.Args[0], evalDefault)
1372+
if v == nil {
1373+
ret nil
1374+
}
1375+
if !v.IsConst() || !v.Constant.IsStr() {
1376+
e.pushErr(fc.Args[0].Token, "expression must be constant and string")
1377+
ret nil
1378+
}
1379+
mut path := v.Constant.ReadStr()
1380+
if len(path) == 0 {
1381+
e.pushErr(fc.Args[0].Token, "empty path for IncludeStr")
1382+
ret nil
1383+
}
1384+
if !filepath::IsAbs(path) {
1385+
packagefile := e.s.getCurrentFile()
1386+
packagedir := filepath::Dir(packagefile.File.Path)
1387+
path = filepath::Join(packagedir, path)
1388+
path = filepath::Abs(path) else {
1389+
e.pushErr(fc.Args[0].Token, "path handling failed: "+fmt::Sprint(error))
1390+
ret nil
1391+
}
1392+
}
1393+
mut file := e.s.importer.GetFile(path)
1394+
if file == nil {
1395+
file = e.s.importer.IncludeFile(path)
1396+
if file == nil {
1397+
e.pushErr(fc.Args[0].Token, "IncludeStr cannot read file: "+conv::Quote(path))
1398+
ret nil
1399+
}
1400+
e.s.importer.IncludedFile(file)
1401+
}
1402+
data := file.Text()
1403+
mut cs := constant::NewStr(data)
1404+
ret &Value{
1405+
Type: primStr,
1406+
Constant: cs,
1407+
Model: cs,
1408+
untyped: true,
1409+
}
1410+
}
1411+
13601412
fn builtinCallerStdMemSizeOf(mut e: &eval, mut fc: &ast::CallExpr, _: &Value): &Value {
13611413
mut result := &Value{
13621414
Type: primUint,
@@ -1564,6 +1616,7 @@ fn init() {
15641616
builtinFuncsStdComptime["Files"] = &FuncIns{caller: builtinCallerStdComptimeFiles}
15651617
builtinFuncsStdComptime["TypeAlias"] = &FuncIns{caller: builtinCallerStdComptimeTypeAlias}
15661618
builtinFuncsStdComptime["IncludeBytes"] = &FuncIns{caller: builtinCallerStdComptimeIncludeBytes}
1619+
builtinFuncsStdComptime["IncludeStr"] = &FuncIns{caller: builtinCallerStdComptimeIncludeStr}
15671620

15681621
// Initialize built-in functions of the "std/integ" package.
15691622
builtinFuncsStdInteg["Emit"] = &FuncIns{

0 commit comments

Comments
 (0)