mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 13:00:11 +01:00
* [executable semantics] class-ify Declaration NFC (no functional change). Proof of concept that we can simplify code by replacing unions with safer, more regular types. Hand-rolled existentials (type-erasing CoW wrappers) are a follow-on step that will further simplify usage. Began adding `const` where possible, and replacing `std::string*` with `std::string`. Most `const`s can disappear as we replace reference semantics with value semantics, but in the meantime it's an important step in the right direction.
40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
// 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 "executable_semantics/ast/function_definition.h"
|
|
|
|
#include <iostream>
|
|
|
|
namespace Carbon {
|
|
|
|
auto MakeFunDef(int line_num, std::string name, Expression* ret_type,
|
|
Expression* param_pattern, Statement* body)
|
|
-> struct FunctionDefinition* {
|
|
auto* f = new struct FunctionDefinition();
|
|
f->line_num = line_num;
|
|
f->name = std::move(name);
|
|
f->return_type = ret_type;
|
|
f->param_pattern = param_pattern;
|
|
f->body = body;
|
|
return f;
|
|
}
|
|
|
|
void PrintFunDefDepth(const FunctionDefinition* f, int depth) {
|
|
std::cout << "fn " << f->name << " ";
|
|
PrintExp(f->param_pattern);
|
|
std::cout << " -> ";
|
|
PrintExp(f->return_type);
|
|
if (f->body) {
|
|
std::cout << " {" << std::endl;
|
|
PrintStatement(f->body, depth);
|
|
std::cout << std::endl << "}" << std::endl;
|
|
} else {
|
|
std::cout << ";" << std::endl;
|
|
}
|
|
}
|
|
|
|
void PrintFunDef(const FunctionDefinition* f) { PrintFunDefDepth(f, -1); }
|
|
|
|
} // namespace Carbon
|