|
15 | 15 | #include "heavy/Context.h" |
16 | 16 | #include "heavy/Dialect.h" |
17 | 17 | #include "heavy/OpGen.h" |
| 18 | +#include "heavy/SourceManager.h" |
18 | 19 | #include "heavy/Value.h" |
19 | 20 | #include "llvm/ADT/StringExtras.h" |
20 | 21 | #include "llvm/Support/Casting.h" |
@@ -88,6 +89,7 @@ heavy::ExternFunction error; |
88 | 89 | heavy::ExternFunction dynamic_wind; |
89 | 90 | heavy::ExternFunction load_module; |
90 | 91 | heavy::ExternFunction source_loc; |
| 92 | +heavy::ExternFunction dump_source_loc; |
91 | 93 | heavy::ExternFunction make_syntactic_closure; |
92 | 94 |
|
93 | 95 | heavy::ExternFunction eval; |
@@ -402,6 +404,37 @@ void source_loc(Context& C, ValueRefs Args) { |
402 | 404 | C.Cont(C.CreateSourceValue(Loc)); |
403 | 405 | } |
404 | 406 |
|
| 407 | +void dump_source_loc(Context& C, ValueRefs Args) { |
| 408 | + if (Args.size() != 1) |
| 409 | + return C.RaiseError("invalid arity"); |
| 410 | + if (!C.SourceManager) |
| 411 | + return C.RaiseError("source manager not available"); |
| 412 | + |
| 413 | + // TODO |
| 414 | + // This could be generalize to take an optional message to work with |
| 415 | + // errors, warnings, notes, etc. |
| 416 | + |
| 417 | + heavy::SourceLocation Loc = Args[0].getSourceLocation(); |
| 418 | + heavy::FullSourceLocation SL = C.SourceManager->getFullSourceLocation(Loc); |
| 419 | + if (SL.isValid()) { |
| 420 | + heavy::SourceLineContext LineContext = SL.getLineContext(); |
| 421 | + llvm::errs() << LineContext.FileName |
| 422 | + << ':' << LineContext.LineNumber |
| 423 | + << ':' << LineContext.Column |
| 424 | + // TODO optional message could go here |
| 425 | + << '\n' |
| 426 | + << LineContext.LineRange << '\n'; |
| 427 | + // Display the caret pointing to the point of interest. |
| 428 | + for (unsigned i = 1; i < LineContext.Column; i++) { |
| 429 | + llvm::errs() << ' '; |
| 430 | + } |
| 431 | + llvm::errs() << "^\n"; |
| 432 | + } else { |
| 433 | + llvm::errs() << "<<INVALID SOURCE LOCATION>>\n"; |
| 434 | + } |
| 435 | + C.Cont(); |
| 436 | +} |
| 437 | + |
405 | 438 | } // end of namespace heavy::builtins |
406 | 439 |
|
407 | 440 | namespace heavy { |
@@ -1217,6 +1250,7 @@ void HEAVY_BASE_INIT(heavy::Context& Context) { |
1217 | 1250 |
|
1218 | 1251 |
|
1219 | 1252 | HEAVY_BASE_VAR(source_loc) = heavy::builtins::source_loc; |
| 1253 | + HEAVY_BASE_VAR(dump_source_loc) = heavy::builtins::dump_source_loc; |
1220 | 1254 | HEAVY_BASE_VAR(parse_source_file).init(Context); |
1221 | 1255 |
|
1222 | 1256 | // functions |
@@ -1311,6 +1345,7 @@ void HEAVY_BASE_LOAD_MODULE(heavy::Context& Context) { |
1311 | 1345 | HEAVY_BASE_VAR(include_library_declarations)}, |
1312 | 1346 |
|
1313 | 1347 | {"source-loc", HEAVY_BASE_VAR(source_loc)}, |
| 1348 | + {"dump-source-loc", HEAVY_BASE_VAR(dump_source_loc)}, |
1314 | 1349 | {"parse-source-file", |
1315 | 1350 | HEAVY_BASE_VAR(parse_source_file).get(Context)}, |
1316 | 1351 |
|
|
0 commit comments