Make the AST mutable (#849)

The code is pretty intertwined: having the AST be truly mutable means (to me) changing parser.ypp to return non-const values, but then the way things are passed around between objects should be non-const (particularly an issue with lists), which then creates issues with construction of lists in the TypeChecker, which then TypeChecker needs to mostly be non-const.

Due to the difficulties in breaking this apart, whereas I'd previously considering refactoring accessor naming in the same PR, I've largely avoided doing so. The intent is then that this PR focuses mainly on const -> non-const AST behavior.

call_main moves out of interpreter.cpp so that interpreter.cpp can receive a fully const AST.
This commit is contained in:
Jon Meow
2021-09-27 10:57:31 -07:00
committed by GitHub
parent 721743bc58
commit 04ab30f231
15 changed files with 282 additions and 270 deletions
@@ -15,9 +15,8 @@ namespace Carbon {
// Adds builtins, currently only Print(). Note Print() is experimental, not
// standardized, but is made available for printing state in tests.
static void AddIntrinsics(
Nonnull<Arena*> arena,
std::vector<Nonnull<const Declaration*>>* declarations) {
static void AddIntrinsics(Nonnull<Arena*> arena,
std::vector<Nonnull<Declaration*>>* declarations) {
SourceLocation loc("<intrinsic>", 0);
std::vector<TuplePattern::Field> print_fields = {TuplePattern::Field(
"0",
@@ -47,7 +46,7 @@ void ExecProgram(Nonnull<Arena*> arena, AST ast) {
llvm::outs() << "********** type checking **********\n";
}
TypeChecker type_checker(arena);
TypeChecker::TypeCheckContext p = type_checker.TopLevel(ast.declarations);
TypeChecker::TypeCheckContext p = type_checker.TopLevel(&ast.declarations);
TypeEnv top = p.types;
Env ct_top = p.values;
std::vector<Nonnull<const Declaration*>> new_decls;
@@ -62,7 +61,12 @@ void ExecProgram(Nonnull<Arena*> arena, AST ast) {
}
llvm::outs() << "********** starting execution **********\n";
}
int result = Interpreter(arena).InterpProgram(new_decls);
SourceLocation loc("<main()>", 0);
Nonnull<Expression*> call_main = arena->New<CallExpression>(
loc, arena->New<IdentifierExpression>(loc, "main"),
arena->New<TupleLiteral>(loc));
int result = Interpreter(arena).InterpProgram(new_decls, call_main);
llvm::outs() << "result: " << result << "\n";
}