mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
Notes versus what jsiek wrote:
- This adopts Bazel for building.
- System-local versions of bison/flex are used. I found https://github.com/jmillikin/rules_bison, but those print a lot of warnings (things like -Wsign-compare IIRC) which makes builds hard to read. Plus I think the underlying bison_cc_library rule didn't work, so this would really only get a hermetic bison/flex build (helpful, but didn't seem worth more time).
- I'm adding in a .bazeliskrc to push a somewhat more standard choice of bazel versions. I noticed I was getting unstable versions by default, possible Google-specific, but seemed good to include.
- The `-lpthread` kludge.
- Turn all of the examples into golden tests.
- Including adding a golden test rule.
- Fixed various style guide issues. For example:
- Fixing function names to be CamelCase instead of snake_case (https://google.github.io/styleguide/cppguide.html#Function_Names)
- Removed exception use (https://google.github.io/styleguide/cppguide.html#Exceptions)
- File name fixes (https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#file-names)
- Dropped `using` of `std` names -- I believe this is preferred (maybe we should be explicit about this in the Carbon style guide)
- Switched `enum` uses to `enum class` for ease-of-identification.
- Spent some time breaking out files to hopefully be easier to read/edit pieces, and understand relations between structs.
- Added `code requires` to `syntax.ypp` to address include issues
Possibly other things -- but the fundamental structure is, I believe, unchanged. I put in the golden tests pretty early to ensure I wasn't mutating output/results.
285 lines
7.3 KiB
C++
285 lines
7.3 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/expression.h"
|
|
|
|
#include <iostream>
|
|
|
|
namespace Carbon {
|
|
|
|
auto MakeTypeType(int line_num) -> Expression* {
|
|
auto* t = new Expression();
|
|
t->tag = ExpressionKind::TypeT;
|
|
t->line_num = line_num;
|
|
return t;
|
|
}
|
|
|
|
auto MakeIntType(int line_num) -> Expression* {
|
|
auto* t = new Expression();
|
|
t->tag = ExpressionKind::IntT;
|
|
t->line_num = line_num;
|
|
return t;
|
|
}
|
|
|
|
auto MakeBoolType(int line_num) -> Expression* {
|
|
auto* t = new Expression();
|
|
t->tag = ExpressionKind::BoolT;
|
|
t->line_num = line_num;
|
|
return t;
|
|
}
|
|
|
|
auto MakeAutoType(int line_num) -> Expression* {
|
|
auto* t = new Expression();
|
|
t->tag = ExpressionKind::AutoT;
|
|
t->line_num = line_num;
|
|
return t;
|
|
}
|
|
|
|
auto MakeFunType(int line_num, Expression* param, Expression* ret)
|
|
-> Expression* {
|
|
auto* t = new Expression();
|
|
t->tag = ExpressionKind::FunctionT;
|
|
t->line_num = line_num;
|
|
t->u.function_type.parameter = param;
|
|
t->u.function_type.return_type = ret;
|
|
return t;
|
|
}
|
|
|
|
auto MakeVar(int line_num, std::string var) -> Expression* {
|
|
auto* v = new Expression();
|
|
v->line_num = line_num;
|
|
v->tag = ExpressionKind::Variable;
|
|
v->u.variable.name = new std::string(std::move(var));
|
|
return v;
|
|
}
|
|
|
|
auto MakeVarPat(int line_num, std::string var, Expression* type)
|
|
-> Expression* {
|
|
auto* v = new Expression();
|
|
v->line_num = line_num;
|
|
v->tag = ExpressionKind::PatternVariable;
|
|
v->u.pattern_variable.name = new std::string(std::move(var));
|
|
v->u.pattern_variable.type = type;
|
|
return v;
|
|
}
|
|
|
|
auto MakeInt(int line_num, int i) -> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::Integer;
|
|
e->u.integer = i;
|
|
return e;
|
|
}
|
|
|
|
auto MakeBool(int line_num, bool b) -> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::Boolean;
|
|
e->u.boolean = b;
|
|
return e;
|
|
}
|
|
|
|
auto MakeOp(int line_num, enum Operator op, std::vector<Expression*>* args)
|
|
-> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::PrimitiveOp;
|
|
e->u.primitive_op.op = op;
|
|
e->u.primitive_op.arguments = args;
|
|
return e;
|
|
}
|
|
|
|
auto MakeUnOp(int line_num, enum Operator op, Expression* arg) -> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::PrimitiveOp;
|
|
e->u.primitive_op.op = op;
|
|
auto* args = new std::vector<Expression*>();
|
|
args->push_back(arg);
|
|
e->u.primitive_op.arguments = args;
|
|
return e;
|
|
}
|
|
|
|
auto MakeBinOp(int line_num, enum Operator op, Expression* arg1,
|
|
Expression* arg2) -> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::PrimitiveOp;
|
|
e->u.primitive_op.op = op;
|
|
auto* args = new std::vector<Expression*>();
|
|
args->push_back(arg1);
|
|
args->push_back(arg2);
|
|
e->u.primitive_op.arguments = args;
|
|
return e;
|
|
}
|
|
|
|
auto MakeCall(int line_num, Expression* fun, Expression* arg) -> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::Call;
|
|
e->u.call.function = fun;
|
|
e->u.call.argument = arg;
|
|
return e;
|
|
}
|
|
|
|
auto MakeGetField(int line_num, Expression* exp, std::string field)
|
|
-> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::GetField;
|
|
e->u.get_field.aggregate = exp;
|
|
e->u.get_field.field = new std::string(std::move(field));
|
|
return e;
|
|
}
|
|
|
|
auto MakeTuple(int line_num,
|
|
std::vector<std::pair<std::string, Expression*>>* args)
|
|
-> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::Tuple;
|
|
int i = 0;
|
|
for (auto& arg : *args) {
|
|
if (arg.first == "") {
|
|
arg.first = std::to_string(i);
|
|
++i;
|
|
}
|
|
}
|
|
e->u.tuple.fields = args;
|
|
return e;
|
|
}
|
|
|
|
auto MakeIndex(int line_num, Expression* exp, Expression* i) -> Expression* {
|
|
auto* e = new Expression();
|
|
e->line_num = line_num;
|
|
e->tag = ExpressionKind::Index;
|
|
e->u.index.aggregate = exp;
|
|
e->u.index.offset = i;
|
|
return e;
|
|
}
|
|
|
|
static void PrintOp(Operator op) {
|
|
switch (op) {
|
|
case Operator::Neg:
|
|
std::cout << "-";
|
|
break;
|
|
case Operator::Add:
|
|
std::cout << "+";
|
|
break;
|
|
case Operator::Sub:
|
|
std::cout << "-";
|
|
break;
|
|
case Operator::Not:
|
|
std::cout << "!";
|
|
break;
|
|
case Operator::And:
|
|
std::cout << "&&";
|
|
break;
|
|
case Operator::Or:
|
|
std::cout << "||";
|
|
break;
|
|
case Operator::Eq:
|
|
std::cout << "==";
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void PrintFields(
|
|
std::vector<std::pair<std::string, Expression*>>* fields) {
|
|
int i = 0;
|
|
for (auto iter = fields->begin(); iter != fields->end(); ++iter, ++i) {
|
|
if (i != 0) {
|
|
std::cout << ", ";
|
|
}
|
|
std::cout << iter->first << " = ";
|
|
PrintExp(iter->second);
|
|
}
|
|
}
|
|
|
|
void PrintExp(Expression* e) {
|
|
switch (e->tag) {
|
|
case ExpressionKind::Index:
|
|
PrintExp(e->u.index.aggregate);
|
|
std::cout << "[";
|
|
PrintExp(e->u.index.offset);
|
|
std::cout << "]";
|
|
break;
|
|
case ExpressionKind::GetField:
|
|
PrintExp(e->u.get_field.aggregate);
|
|
std::cout << ".";
|
|
std::cout << *e->u.get_field.field;
|
|
break;
|
|
case ExpressionKind::Tuple:
|
|
std::cout << "(";
|
|
PrintFields(e->u.tuple.fields);
|
|
std::cout << ")";
|
|
break;
|
|
case ExpressionKind::Integer:
|
|
std::cout << e->u.integer;
|
|
break;
|
|
case ExpressionKind::Boolean:
|
|
std::cout << std::boolalpha;
|
|
std::cout << e->u.boolean;
|
|
break;
|
|
case ExpressionKind::PrimitiveOp:
|
|
std::cout << "(";
|
|
if (e->u.primitive_op.arguments->size() == 0) {
|
|
PrintOp(e->u.primitive_op.op);
|
|
} else if (e->u.primitive_op.arguments->size() == 1) {
|
|
PrintOp(e->u.primitive_op.op);
|
|
std::cout << " ";
|
|
auto iter = e->u.primitive_op.arguments->begin();
|
|
PrintExp(*iter);
|
|
} else if (e->u.primitive_op.arguments->size() == 2) {
|
|
auto iter = e->u.primitive_op.arguments->begin();
|
|
PrintExp(*iter);
|
|
std::cout << " ";
|
|
PrintOp(e->u.primitive_op.op);
|
|
std::cout << " ";
|
|
++iter;
|
|
PrintExp(*iter);
|
|
}
|
|
std::cout << ")";
|
|
break;
|
|
case ExpressionKind::Variable:
|
|
std::cout << *e->u.variable.name;
|
|
break;
|
|
case ExpressionKind::PatternVariable:
|
|
PrintExp(e->u.pattern_variable.type);
|
|
std::cout << ": ";
|
|
std::cout << *e->u.pattern_variable.name;
|
|
break;
|
|
case ExpressionKind::Call:
|
|
PrintExp(e->u.call.function);
|
|
if (e->u.call.argument->tag == ExpressionKind::Tuple) {
|
|
PrintExp(e->u.call.argument);
|
|
} else {
|
|
std::cout << "(";
|
|
PrintExp(e->u.call.argument);
|
|
std::cout << ")";
|
|
}
|
|
break;
|
|
case ExpressionKind::BoolT:
|
|
std::cout << "Bool";
|
|
break;
|
|
case ExpressionKind::IntT:
|
|
std::cout << "Int";
|
|
break;
|
|
case ExpressionKind::TypeT:
|
|
std::cout << "Type";
|
|
break;
|
|
case ExpressionKind::AutoT:
|
|
std::cout << "auto";
|
|
break;
|
|
case ExpressionKind::FunctionT:
|
|
std::cout << "fn ";
|
|
PrintExp(e->u.function_type.parameter);
|
|
std::cout << " -> ";
|
|
PrintExp(e->u.function_type.return_type);
|
|
break;
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|