Skip to content

Commit eff76b5

Browse files
authored
Merge pull request #121 from certik/ltypes
Implement initial ltypes.py
2 parents 2e497f7 + 6665057 commit eff76b5

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

integration_tests/expr_05.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from ltypes import i32
2+
13
def test_multiply(a: i32, b: i32) -> i32:
24
return a*b
35

integration_tests/run_tests.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,9 @@ def main():
3535
if r != 0:
3636
print("Command '%s' failed." % cmd)
3737
sys.exit(1)
38-
pysrc = open("integration_tests/%s" % pyfile).read()
39-
pysrc = """\
40-
i32 = None
41-
i64 = None
42-
f32 = None
43-
f64 = None
44-
""" + pysrc
45-
open("integration_tests/py_%s" % pyfile, "w").write(pysrc)
46-
47-
cmd = "python integration_tests/py_%s" % (pyfile)
38+
python_path="src/runtime/ltypes"
39+
cmd = "PYTHONPATH=%s python integration_tests/%s" % (python_path,
40+
pyfile)
4841
print("+ " + cmd)
4942
r = os.system(cmd)
5043
if r != 0:

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ LFortran::Result<LFortran::Python::AST::ast_t*> parse_python_file(Allocator &al,
4646
return ast;
4747
}
4848

49+
// Does a CPython style lookup for a module:
50+
// * First the current directory (this is incorrect, we need to do it relative to the current file)
51+
// * Then the LPython runtime directory
4952
LFortran::Result<std::string> get_full_path(const std::string &filename,
50-
const std::string &runtime_library_dir) {
53+
const std::string &runtime_library_dir, bool &ltypes) {
54+
ltypes = false;
5155
std::string input;
5256
bool status = read_file(filename, input);
5357
if (status) {
@@ -58,7 +62,19 @@ LFortran::Result<std::string> get_full_path(const std::string &filename,
5862
if (status) {
5963
return filename_intrinsic;
6064
} else {
61-
return LFortran::Error();
65+
// If this is `ltypes`, do a special lookup
66+
if (filename == "ltypes.py") {
67+
std::string filename_intrinsic = runtime_library_dir + "/ltypes/" + filename;
68+
bool status = read_file(filename_intrinsic, input);
69+
if (status) {
70+
ltypes = true;
71+
return filename_intrinsic;
72+
} else {
73+
return LFortran::Error();
74+
}
75+
} else {
76+
return LFortran::Error();
77+
}
6278
}
6379
}
6480
}
@@ -67,7 +83,9 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
6783
const std::string &module_name,
6884
const Location &loc, bool /*intrinsic*/,
6985
const std::string &rl_path,
86+
bool &ltypes,
7087
const std::function<void (const std::string &, const Location &)> err) {
88+
ltypes = false;
7189
LFORTRAN_ASSERT(symtab);
7290
if (symtab->scope.find(module_name) != symtab->scope.end()) {
7391
ASR::symbol_t *m = symtab->scope[module_name];
@@ -81,10 +99,11 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
8199

82100
// Parse the module `module_name`.py to AST
83101
std::string infile0 = module_name + ".py";
84-
Result<std::string> rinfile = get_full_path(infile0, rl_path);
102+
Result<std::string> rinfile = get_full_path(infile0, rl_path, ltypes);
85103
if (!rinfile.ok) {
86104
err("Could not find the module '" + infile0 + "'", loc);
87105
}
106+
if (ltypes) return nullptr;
88107
std::string infile = rinfile.result;
89108
Result<AST::ast_t*> r = parse_python_file(al, rl_path, infile);
90109
if (!r.ok) {
@@ -95,6 +114,7 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
95114
// Convert the module from AST to ASR
96115
LFortran::LocationManager lm;
97116
lm.in_filename = infile;
117+
// TODO: diagnostic should be an argument to this function
98118
diag::Diagnostics diagnostics;
99119
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast, diagnostics, false);
100120
std::string input;
@@ -441,10 +461,22 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
441461
if (!main_module) {
442462
st = st->parent;
443463
}
464+
bool ltypes;
444465
t = (ASR::symbol_t*)(load_module(al, st,
445-
msym, x.base.base.loc, false, rl_path,
466+
msym, x.base.base.loc, false, rl_path, ltypes,
446467
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); }
447468
));
469+
if (ltypes) {
470+
// TODO: For now we skip ltypes import completely. Later on we should note what symbols
471+
// got imported from it, and give an error message if an annotation is used without
472+
// importing it.
473+
tmp = nullptr;
474+
return;
475+
}
476+
if (!t) {
477+
throw SemanticError("The module '" + msym + "' cannot be loaded",
478+
x.base.base.loc);
479+
}
448480
}
449481

450482
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);

src/runtime/ltypes/ltypes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
i32 = []
2+
i64 = []
3+
f32 = []
4+
f64 = []

0 commit comments

Comments
 (0)