mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Adapting jsiek's executable semantics tooling for commit. (#237)
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.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
# 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
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_library")
|
||||
|
||||
package(default_visibility = ["//executable_semantics:__subpackages__"])
|
||||
|
||||
cc_library(
|
||||
name = "declaration",
|
||||
srcs = ["declaration.cpp"],
|
||||
hdrs = ["declaration.h"],
|
||||
deps = [
|
||||
":function_definition",
|
||||
":member",
|
||||
":struct_definition",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "expression",
|
||||
srcs = ["expression.cpp"],
|
||||
hdrs = ["expression.h"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "expression_or_field_list",
|
||||
srcs = ["expression_or_field_list.cpp"],
|
||||
hdrs = ["expression_or_field_list.h"],
|
||||
deps = [":expression"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "function_definition",
|
||||
srcs = ["function_definition.cpp"],
|
||||
hdrs = ["function_definition.h"],
|
||||
deps = [
|
||||
":expression",
|
||||
":statement",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "member",
|
||||
srcs = ["member.cpp"],
|
||||
hdrs = ["member.h"],
|
||||
deps = [":expression"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "statement",
|
||||
srcs = ["statement.cpp"],
|
||||
hdrs = ["statement.h"],
|
||||
deps = [":expression"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "struct_definition",
|
||||
hdrs = ["struct_definition.h"],
|
||||
deps = [":member"],
|
||||
)
|
||||
@@ -0,0 +1,64 @@
|
||||
// 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/declaration.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto MakeFunDecl(FunctionDefinition* f) -> Declaration* {
|
||||
auto* d = new Declaration();
|
||||
d->tag = DeclarationKind::FunctionDeclaration;
|
||||
d->u.fun_def = f;
|
||||
return d;
|
||||
}
|
||||
|
||||
auto MakeStructDecl(int line_num, std::string name, std::list<Member*>* members)
|
||||
-> Declaration* {
|
||||
auto* d = new Declaration();
|
||||
d->tag = DeclarationKind::StructDeclaration;
|
||||
d->u.struct_def = new StructDefinition();
|
||||
d->u.struct_def->line_num = line_num;
|
||||
d->u.struct_def->name = new std::string(std::move(name));
|
||||
d->u.struct_def->members = members;
|
||||
return d;
|
||||
}
|
||||
|
||||
auto MakeChoiceDecl(int line_num, std::string name,
|
||||
std::list<std::pair<std::string, Expression*>>* alts)
|
||||
-> Declaration* {
|
||||
auto* d = new Declaration();
|
||||
d->tag = DeclarationKind::ChoiceDeclaration;
|
||||
d->u.choice_def.line_num = line_num;
|
||||
d->u.choice_def.name = new std::string(std::move(name));
|
||||
d->u.choice_def.alternatives = alts;
|
||||
return d;
|
||||
}
|
||||
|
||||
void PrintDecl(Declaration* d) {
|
||||
switch (d->tag) {
|
||||
case DeclarationKind::FunctionDeclaration:
|
||||
PrintFunDef(d->u.fun_def);
|
||||
break;
|
||||
case DeclarationKind::StructDeclaration:
|
||||
std::cout << "struct " << *d->u.struct_def->name << " {" << std::endl;
|
||||
for (auto& member : *d->u.struct_def->members) {
|
||||
PrintMember(member);
|
||||
}
|
||||
std::cout << "}" << std::endl;
|
||||
break;
|
||||
case DeclarationKind::ChoiceDeclaration:
|
||||
std::cout << "choice " << *d->u.choice_def.name << " {" << std::endl;
|
||||
for (auto& alternative : *d->u.choice_def.alternatives) {
|
||||
std::cout << "alt " << alternative.first << " ";
|
||||
PrintExp(alternative.second);
|
||||
std::cout << ";" << std::endl;
|
||||
}
|
||||
std::cout << "}" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/ast/member.h"
|
||||
#include "executable_semantics/ast/struct_definition.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
enum class DeclarationKind {
|
||||
FunctionDeclaration,
|
||||
StructDeclaration,
|
||||
ChoiceDeclaration
|
||||
};
|
||||
|
||||
struct Declaration {
|
||||
DeclarationKind tag;
|
||||
union {
|
||||
struct FunctionDefinition* fun_def;
|
||||
struct StructDefinition* struct_def;
|
||||
struct {
|
||||
int line_num;
|
||||
std::string* name;
|
||||
std::list<std::pair<std::string, Expression*>>* alternatives;
|
||||
} choice_def;
|
||||
} u;
|
||||
};
|
||||
|
||||
auto MakeFunDecl(struct FunctionDefinition* f) -> Declaration*;
|
||||
auto MakeStructDecl(int line_num, std::string name, std::list<Member*>* members)
|
||||
-> Declaration*;
|
||||
auto MakeChoiceDecl(int line_num, std::string name,
|
||||
std::list<std::pair<std::string, Expression*>>* alts)
|
||||
-> Declaration*;
|
||||
|
||||
void PrintDecl(Declaration* d);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
|
||||
@@ -0,0 +1,284 @@
|
||||
// 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
|
||||
@@ -0,0 +1,106 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
enum class ExpressionKind {
|
||||
AutoT,
|
||||
BoolT,
|
||||
Boolean,
|
||||
Call,
|
||||
FunctionT,
|
||||
GetField,
|
||||
Index,
|
||||
IntT,
|
||||
Integer,
|
||||
PatternVariable,
|
||||
PrimitiveOp,
|
||||
Tuple,
|
||||
TypeT,
|
||||
Variable,
|
||||
};
|
||||
enum class Operator {
|
||||
Add,
|
||||
And,
|
||||
Eq,
|
||||
Neg,
|
||||
Not,
|
||||
Or,
|
||||
Sub,
|
||||
};
|
||||
|
||||
struct Expression {
|
||||
int line_num;
|
||||
ExpressionKind tag;
|
||||
union {
|
||||
struct {
|
||||
std::string* name;
|
||||
} variable;
|
||||
struct {
|
||||
Expression* aggregate;
|
||||
std::string* field;
|
||||
} get_field;
|
||||
struct {
|
||||
Expression* aggregate;
|
||||
Expression* offset;
|
||||
} index;
|
||||
struct {
|
||||
std::string* name;
|
||||
Expression* type;
|
||||
} pattern_variable;
|
||||
int integer;
|
||||
bool boolean;
|
||||
struct {
|
||||
std::vector<std::pair<std::string, Expression*>>* fields;
|
||||
} tuple;
|
||||
struct {
|
||||
Operator op;
|
||||
std::vector<Expression*>* arguments;
|
||||
} primitive_op;
|
||||
struct {
|
||||
Expression* function;
|
||||
Expression* argument;
|
||||
} call;
|
||||
struct {
|
||||
Expression* parameter;
|
||||
Expression* return_type;
|
||||
} function_type;
|
||||
} u;
|
||||
};
|
||||
|
||||
auto MakeVar(int line_num, std::string var) -> Expression*;
|
||||
auto MakeVarPat(int line_num, std::string var, Expression* type) -> Expression*;
|
||||
auto MakeInt(int line_num, int i) -> Expression*;
|
||||
auto MakeBool(int line_num, bool b) -> Expression*;
|
||||
auto MakeOp(int line_num, Operator op, std::vector<Expression*>* args)
|
||||
-> Expression*;
|
||||
auto MakeUnOp(int line_num, enum Operator op, Expression* arg) -> Expression*;
|
||||
auto MakeBinOp(int line_num, enum Operator op, Expression* arg1,
|
||||
Expression* arg2) -> Expression*;
|
||||
auto MakeCall(int line_num, Expression* fun, Expression* arg) -> Expression*;
|
||||
auto MakeGetField(int line_num, Expression* exp, std::string field)
|
||||
-> Expression*;
|
||||
auto MakeTuple(int line_num,
|
||||
std::vector<std::pair<std::string, Expression*>>* args)
|
||||
-> Expression*;
|
||||
auto MakeIndex(int line_num, Expression* exp, Expression* i) -> Expression*;
|
||||
|
||||
auto MakeTypeType(int line_num) -> Expression*;
|
||||
auto MakeIntType(int line_num) -> Expression*;
|
||||
auto MakeBoolType(int line_num) -> Expression*;
|
||||
auto MakeFunType(int line_num, Expression* param, Expression* ret)
|
||||
-> Expression*;
|
||||
auto MakeAutoType(int line_num) -> Expression*;
|
||||
|
||||
void PrintExp(Expression* exp);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
|
||||
@@ -0,0 +1,49 @@
|
||||
// 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_or_field_list.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto MakeExp(Expression* exp) -> ExpOrFieldList* {
|
||||
auto e = new ExpOrFieldList();
|
||||
e->tag = ExpOrFieldListKind::Exp;
|
||||
e->u.exp = exp;
|
||||
return e;
|
||||
}
|
||||
|
||||
auto MakeFieldList(std::list<std::pair<std::string, Expression*>>* fields)
|
||||
-> ExpOrFieldList* {
|
||||
auto e = new ExpOrFieldList();
|
||||
e->tag = ExpOrFieldListKind::FieldList;
|
||||
e->u.fields = fields;
|
||||
return e;
|
||||
}
|
||||
|
||||
auto MakeConsField(ExpOrFieldList* e1, ExpOrFieldList* e2) -> ExpOrFieldList* {
|
||||
auto fields = new std::list<std::pair<std::string, Expression*>>();
|
||||
switch (e1->tag) {
|
||||
case ExpOrFieldListKind::Exp:
|
||||
fields->push_back(std::make_pair("", e1->u.exp));
|
||||
break;
|
||||
case ExpOrFieldListKind::FieldList:
|
||||
for (auto& field : *e1->u.fields) {
|
||||
fields->push_back(field);
|
||||
}
|
||||
break;
|
||||
}
|
||||
switch (e2->tag) {
|
||||
case ExpOrFieldListKind::Exp:
|
||||
fields->push_back(std::make_pair("", e2->u.exp));
|
||||
break;
|
||||
case ExpOrFieldListKind::FieldList:
|
||||
for (auto& field : *e2->u.fields) {
|
||||
fields->push_back(field);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return MakeFieldList(fields);
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
@@ -0,0 +1,32 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_EXPRESSION_OR_FIELD_LIST_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_EXPRESSION_OR_FIELD_LIST_H_
|
||||
|
||||
#include <list>
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
enum class ExpOrFieldListKind { Exp, FieldList };
|
||||
|
||||
// This is used in the parsing of tuples and parenthesized expressions.
|
||||
struct ExpOrFieldList {
|
||||
ExpOrFieldListKind tag;
|
||||
union {
|
||||
Expression* exp;
|
||||
std::list<std::pair<std::string, Expression*>>* fields;
|
||||
} u;
|
||||
};
|
||||
|
||||
auto MakeExp(Expression* exp) -> ExpOrFieldList*;
|
||||
auto MakeFieldList(std::list<std::pair<std::string, Expression*>>* fields)
|
||||
-> ExpOrFieldList*;
|
||||
auto MakeConsField(ExpOrFieldList* e1, ExpOrFieldList* e2) -> ExpOrFieldList*;
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_OR_FIELD_LIST_H_
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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(struct 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(struct FunctionDefinition* f) { PrintFunDefDepth(f, -1); }
|
||||
|
||||
} // namespace Carbon
|
||||
@@ -0,0 +1,29 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
struct FunctionDefinition {
|
||||
int line_num;
|
||||
std::string name;
|
||||
Expression* param_pattern;
|
||||
Expression* return_type;
|
||||
Statement* body;
|
||||
};
|
||||
|
||||
auto MakeFunDef(int line_num, std::string name, Expression* ret_type,
|
||||
Expression* param, Statement* body)
|
||||
-> struct FunctionDefinition*;
|
||||
void PrintFunDef(struct FunctionDefinition*);
|
||||
void PrintFunDefDepth(struct FunctionDefinition*, int);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_FUNCTION_DEFINITION_H_
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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/member.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto MakeField(int line_num, std::string name, Expression* type) -> Member* {
|
||||
auto m = new Member();
|
||||
m->line_num = line_num;
|
||||
m->tag = MemberKind::FieldMember;
|
||||
m->u.field.name = new std::string(std::move(name));
|
||||
m->u.field.type = type;
|
||||
return m;
|
||||
}
|
||||
|
||||
void PrintMember(Member* m) {
|
||||
switch (m->tag) {
|
||||
case MemberKind::FieldMember:
|
||||
std::cout << "var " << *m->u.field.name << " : ";
|
||||
PrintExp(m->u.field.type);
|
||||
std::cout << ";" << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
@@ -0,0 +1,33 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_MEMBER_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_MEMBER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
enum class MemberKind { FieldMember };
|
||||
|
||||
struct Member {
|
||||
int line_num;
|
||||
MemberKind tag;
|
||||
union {
|
||||
struct {
|
||||
std::string* name;
|
||||
Expression* type;
|
||||
} field;
|
||||
} u;
|
||||
};
|
||||
|
||||
auto MakeField(int line_num, std::string name, Expression* type) -> Member*;
|
||||
|
||||
void PrintMember(Member* m);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_MEMBER_H_
|
||||
@@ -0,0 +1,190 @@
|
||||
// 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/statement.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto MakeExpStmt(int line_num, Expression* exp) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::ExpressionStatement;
|
||||
s->u.exp = exp;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeAssign(int line_num, Expression* lhs, Expression* rhs) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Assign;
|
||||
s->u.assign.lhs = lhs;
|
||||
s->u.assign.rhs = rhs;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeVarDef(int line_num, Expression* pat, Expression* init) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::VariableDefinition;
|
||||
s->u.variable_definition.pat = pat;
|
||||
s->u.variable_definition.init = init;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeIf(int line_num, Expression* cond, Statement* then_stmt,
|
||||
Statement* else_stmt) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::If;
|
||||
s->u.if_stmt.cond = cond;
|
||||
s->u.if_stmt.then_stmt = then_stmt;
|
||||
s->u.if_stmt.else_stmt = else_stmt;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeWhile(int line_num, Expression* cond, Statement* body) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::While;
|
||||
s->u.while_stmt.cond = cond;
|
||||
s->u.while_stmt.body = body;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeBreak(int line_num) -> Statement* {
|
||||
std::cout << "MakeBlock" << std::endl;
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Break;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeContinue(int line_num) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Continue;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeReturn(int line_num, Expression* e) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Return;
|
||||
s->u.return_stmt = e;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeSeq(int line_num, Statement* s1, Statement* s2) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Sequence;
|
||||
s->u.sequence.stmt = s1;
|
||||
s->u.sequence.next = s2;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeBlock(int line_num, Statement* stmt) -> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Block;
|
||||
s->u.block.stmt = stmt;
|
||||
return s;
|
||||
}
|
||||
|
||||
auto MakeMatch(int line_num, Expression* exp,
|
||||
std::list<std::pair<Expression*, Statement*>>* clauses)
|
||||
-> Statement* {
|
||||
auto* s = new Statement();
|
||||
s->line_num = line_num;
|
||||
s->tag = StatementKind::Match;
|
||||
s->u.match_stmt.exp = exp;
|
||||
s->u.match_stmt.clauses = clauses;
|
||||
return s;
|
||||
}
|
||||
|
||||
void PrintStatement(Statement* s, int depth) {
|
||||
if (!s) {
|
||||
return;
|
||||
}
|
||||
if (depth == 0) {
|
||||
std::cout << " ... ";
|
||||
return;
|
||||
}
|
||||
switch (s->tag) {
|
||||
case StatementKind::Match:
|
||||
std::cout << "match (";
|
||||
PrintExp(s->u.match_stmt.exp);
|
||||
std::cout << ") {";
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
for (auto& clause : *s->u.match_stmt.clauses) {
|
||||
std::cout << "case ";
|
||||
PrintExp(clause.first);
|
||||
std::cout << " =>" << std::endl;
|
||||
PrintStatement(clause.second, depth - 1);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "...";
|
||||
}
|
||||
std::cout << "}";
|
||||
break;
|
||||
case StatementKind::While:
|
||||
std::cout << "while (";
|
||||
PrintExp(s->u.while_stmt.cond);
|
||||
std::cout << ")" << std::endl;
|
||||
PrintStatement(s->u.while_stmt.body, depth - 1);
|
||||
break;
|
||||
case StatementKind::Break:
|
||||
std::cout << "break;";
|
||||
break;
|
||||
case StatementKind::Continue:
|
||||
std::cout << "continue;";
|
||||
break;
|
||||
case StatementKind::VariableDefinition:
|
||||
std::cout << "var ";
|
||||
PrintExp(s->u.variable_definition.pat);
|
||||
std::cout << " = ";
|
||||
PrintExp(s->u.variable_definition.init);
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::ExpressionStatement:
|
||||
PrintExp(s->u.exp);
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::Assign:
|
||||
PrintExp(s->u.assign.lhs);
|
||||
std::cout << " = ";
|
||||
PrintExp(s->u.assign.rhs);
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::If:
|
||||
std::cout << "if (";
|
||||
PrintExp(s->u.if_stmt.cond);
|
||||
std::cout << ")" << std::endl;
|
||||
PrintStatement(s->u.if_stmt.then_stmt, depth - 1);
|
||||
std::cout << std::endl << "else" << std::endl;
|
||||
PrintStatement(s->u.if_stmt.else_stmt, depth - 1);
|
||||
break;
|
||||
case StatementKind::Return:
|
||||
std::cout << "return ";
|
||||
PrintExp(s->u.return_stmt);
|
||||
std::cout << ";";
|
||||
break;
|
||||
case StatementKind::Sequence:
|
||||
PrintStatement(s->u.sequence.stmt, depth);
|
||||
if (depth < 0 || depth > 1) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
PrintStatement(s->u.sequence.next, depth - 1);
|
||||
break;
|
||||
case StatementKind::Block:
|
||||
std::cout << "{" << std::endl;
|
||||
PrintStatement(s->u.block.stmt, depth - 1);
|
||||
std::cout << std::endl << "}" << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace Carbon
|
||||
@@ -0,0 +1,84 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
|
||||
|
||||
#include <list>
|
||||
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
enum class StatementKind {
|
||||
ExpressionStatement,
|
||||
Assign,
|
||||
VariableDefinition,
|
||||
If,
|
||||
Return,
|
||||
Sequence,
|
||||
Block,
|
||||
While,
|
||||
Break,
|
||||
Continue,
|
||||
Match
|
||||
};
|
||||
|
||||
struct Statement {
|
||||
int line_num;
|
||||
StatementKind tag;
|
||||
union {
|
||||
Expression* exp;
|
||||
struct {
|
||||
Expression* lhs;
|
||||
Expression* rhs;
|
||||
} assign;
|
||||
struct {
|
||||
Expression* pat;
|
||||
Expression* init;
|
||||
} variable_definition;
|
||||
struct {
|
||||
Expression* cond;
|
||||
Statement* then_stmt;
|
||||
Statement* else_stmt;
|
||||
} if_stmt;
|
||||
Expression* return_stmt;
|
||||
struct {
|
||||
Statement* stmt;
|
||||
Statement* next;
|
||||
} sequence;
|
||||
struct {
|
||||
Statement* stmt;
|
||||
} block;
|
||||
struct {
|
||||
Expression* cond;
|
||||
Statement* body;
|
||||
} while_stmt;
|
||||
struct {
|
||||
Expression* exp;
|
||||
std::list<std::pair<Expression*, Statement*>>* clauses;
|
||||
} match_stmt;
|
||||
} u;
|
||||
};
|
||||
|
||||
auto MakeExpStmt(int line_num, Expression* exp) -> Statement*;
|
||||
auto MakeAssign(int line_num, Expression* lhs, Expression* rhs) -> Statement*;
|
||||
auto MakeVarDef(int line_num, Expression* pat, Expression* init) -> Statement*;
|
||||
auto MakeIf(int line_num, Expression* cond, Statement* then_stmt,
|
||||
Statement* else_stmt) -> Statement*;
|
||||
auto MakeReturn(int line_num, Expression* e) -> Statement*;
|
||||
auto MakeSeq(int line_num, Statement* s1, Statement* s2) -> Statement*;
|
||||
auto MakeBlock(int line_num, Statement* s) -> Statement*;
|
||||
auto MakeWhile(int line_num, Expression* cond, Statement* body) -> Statement*;
|
||||
auto MakeBreak(int line_num) -> Statement*;
|
||||
auto MakeContinue(int line_num) -> Statement*;
|
||||
auto MakeMatch(int line_num, Expression* exp,
|
||||
std::list<std::pair<Expression*, Statement*>>* clauses)
|
||||
-> Statement*;
|
||||
|
||||
void PrintStatement(Statement*, int);
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_STRUCT_DEFINITION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_STRUCT_DEFINITION_H_
|
||||
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
#include "executable_semantics/ast/member.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
struct StructDefinition {
|
||||
int line_num;
|
||||
std::string* name;
|
||||
std::list<Member*>* members;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_STRUCT_DEFINITION_H_
|
||||
Reference in New Issue
Block a user