-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathexec_program.cpp
109 lines (95 loc) · 3.79 KB
/
exec_program.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/interpreter/exec_program.h"
#include <variant>
#include "common/check.h"
#include "common/error.h"
#include "common/ostream.h"
#include "explorer/base/arena.h"
#include "explorer/base/trace_stream.h"
#include "explorer/interpreter/interpreter.h"
#include "explorer/interpreter/resolve_control_flow.h"
#include "explorer/interpreter/resolve_names.h"
#include "explorer/interpreter/resolve_unformed.h"
#include "explorer/interpreter/type_checker.h"
#include "llvm/Support/Error.h"
namespace Carbon {
auto AnalyzeProgram(Nonnull<Arena*> arena, AST ast,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<AST> {
SetProgramPhase set_prog_phase(*trace_stream, ProgramPhase::SourceProgram);
SetFileContext set_file_ctx(*trace_stream, std::nullopt);
if (trace_stream->is_enabled()) {
trace_stream->Heading("source program");
llvm::ListSeparator sep("\n\n");
for (auto& declaration : ast.declarations) {
set_file_ctx.update_source_loc(declaration->source_loc());
if (trace_stream->is_enabled()) {
*trace_stream << sep << *declaration;
}
}
if (trace_stream->is_enabled()) {
*trace_stream << "\n";
}
}
SourceLocation source_loc("<Main()>", 0, FileKind::Main);
ast.main_call = arena->New<CallExpression>(
source_loc, arena->New<IdentifierExpression>(source_loc, "Main"),
arena->New<TupleLiteral>(source_loc));
// Although name resolution is currently done once, generic programming
// (particularly templates) may require more passes.
set_prog_phase.update_phase(ProgramPhase::NameResolution);
if (trace_stream->is_enabled()) {
trace_stream->Heading("resolving names");
}
CARBON_RETURN_IF_ERROR(ResolveNames(ast, trace_stream));
set_prog_phase.update_phase(ProgramPhase::ControlFlowResolution);
if (trace_stream->is_enabled()) {
trace_stream->Heading("resolving control flow");
}
CARBON_RETURN_IF_ERROR(ResolveControlFlow(trace_stream, ast));
set_prog_phase.update_phase(ProgramPhase::TypeChecking);
if (trace_stream->is_enabled()) {
trace_stream->Heading("type checking");
}
CARBON_RETURN_IF_ERROR(
TypeChecker(arena, trace_stream, print_stream).TypeCheck(ast));
set_prog_phase.update_phase(ProgramPhase::UnformedVariableResolution);
if (trace_stream->is_enabled()) {
trace_stream->Heading("resolving unformed variables");
}
CARBON_RETURN_IF_ERROR(ResolveUnformed(trace_stream, ast));
set_prog_phase.update_phase(ProgramPhase::Declarations);
if (trace_stream->is_enabled()) {
trace_stream->Heading("printing declarations");
llvm::ListSeparator sep("\n\n");
for (auto& declaration : ast.declarations) {
set_file_ctx.update_source_loc(declaration->source_loc());
if (trace_stream->is_enabled()) {
*trace_stream << sep << *declaration;
}
}
if (trace_stream->is_enabled()) {
*trace_stream << "\n";
}
}
return ast;
}
auto ExecProgram(Nonnull<Arena*> arena, AST ast,
Nonnull<TraceStream*> trace_stream,
Nonnull<llvm::raw_ostream*> print_stream) -> ErrorOr<int> {
SetProgramPhase set_program_phase(*trace_stream, ProgramPhase::Execution);
if (trace_stream->is_enabled()) {
trace_stream->Heading("starting execution");
}
CARBON_ASSIGN_OR_RETURN(
auto interpreter_result,
InterpProgram(ast, arena, trace_stream, print_stream));
if (trace_stream->is_enabled()) {
trace_stream->Result() << "interpreter result: " << interpreter_result
<< "\n";
}
return interpreter_result;
}
} // namespace Carbon