mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:30:12 +01:00
Consistent naming for Expression and Value. (#621)
- `Kind` enumerator names always match the corresponding factory function, accessor, and type names (if any). - All abbreviations in those names are expanded. - Those names always have a suffix to disambiguate expressions from values. `VarTV` is excluded from these changes because there's a pending PR to remove it.
This commit is contained in:
@@ -8,145 +8,158 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto Expression::GetVariable() const -> const Variable& {
|
||||
return std::get<Variable>(value);
|
||||
auto Expression::GetIdentifierExpression() const
|
||||
-> const IdentifierExpression& {
|
||||
return std::get<IdentifierExpression>(value);
|
||||
}
|
||||
|
||||
auto Expression::GetFieldAccess() const -> const FieldAccess& {
|
||||
return std::get<FieldAccess>(value);
|
||||
auto Expression::GetFieldAccessExpression() const
|
||||
-> const FieldAccessExpression& {
|
||||
return std::get<FieldAccessExpression>(value);
|
||||
}
|
||||
|
||||
auto Expression::GetIndex() const -> const Index& {
|
||||
return std::get<Index>(value);
|
||||
auto Expression::GetIndexExpression() const -> const IndexExpression& {
|
||||
return std::get<IndexExpression>(value);
|
||||
}
|
||||
|
||||
auto Expression::GetPatternVariable() const -> const PatternVariable& {
|
||||
return std::get<PatternVariable>(value);
|
||||
auto Expression::GetPatternVariableExpression() const
|
||||
-> const PatternVariableExpression& {
|
||||
return std::get<PatternVariableExpression>(value);
|
||||
}
|
||||
|
||||
auto Expression::GetInteger() const -> int {
|
||||
auto Expression::GetIntLiteral() const -> int {
|
||||
return std::get<IntLiteral>(value).value;
|
||||
}
|
||||
|
||||
auto Expression::GetBoolean() const -> bool {
|
||||
auto Expression::GetBoolLiteral() const -> bool {
|
||||
return std::get<BoolLiteral>(value).value;
|
||||
}
|
||||
|
||||
auto Expression::GetTuple() const -> const Tuple& {
|
||||
return std::get<Tuple>(value);
|
||||
auto Expression::GetTupleLiteral() const -> const TupleLiteral& {
|
||||
return std::get<TupleLiteral>(value);
|
||||
}
|
||||
|
||||
auto Expression::GetPrimitiveOperator() const -> const PrimitiveOperator& {
|
||||
return std::get<PrimitiveOperator>(value);
|
||||
auto Expression::GetPrimitiveOperatorExpression() const
|
||||
-> const PrimitiveOperatorExpression& {
|
||||
return std::get<PrimitiveOperatorExpression>(value);
|
||||
}
|
||||
|
||||
auto Expression::GetCall() const -> const Call& {
|
||||
return std::get<Call>(value);
|
||||
auto Expression::GetCallExpression() const -> const CallExpression& {
|
||||
return std::get<CallExpression>(value);
|
||||
}
|
||||
|
||||
auto Expression::GetFunctionType() const -> const FunctionType& {
|
||||
return std::get<FunctionType>(value);
|
||||
auto Expression::GetFunctionTypeLiteral() const -> const FunctionTypeLiteral& {
|
||||
return std::get<FunctionTypeLiteral>(value);
|
||||
}
|
||||
|
||||
auto Expression::MakeTypeType(int line_num) -> const Expression* {
|
||||
auto Expression::MakeTypeTypeLiteral(int line_num) -> const Expression* {
|
||||
auto* t = new Expression();
|
||||
t->line_num = line_num;
|
||||
t->value = TypeT();
|
||||
t->value = TypeTypeLiteral();
|
||||
return t;
|
||||
}
|
||||
|
||||
auto Expression::MakeIntType(int line_num) -> const Expression* {
|
||||
auto Expression::MakeIntTypeLiteral(int line_num) -> const Expression* {
|
||||
auto* t = new Expression();
|
||||
t->line_num = line_num;
|
||||
t->value = IntT();
|
||||
t->value = IntTypeLiteral();
|
||||
return t;
|
||||
}
|
||||
|
||||
auto Expression::MakeBoolType(int line_num) -> const Expression* {
|
||||
auto Expression::MakeBoolTypeLiteral(int line_num) -> const Expression* {
|
||||
auto* t = new Expression();
|
||||
t->line_num = line_num;
|
||||
t->value = BoolT();
|
||||
t->value = BoolTypeLiteral();
|
||||
return t;
|
||||
}
|
||||
|
||||
auto Expression::MakeAutoType(int line_num) -> const Expression* {
|
||||
auto Expression::MakeAutoTypeLiteral(int line_num) -> const Expression* {
|
||||
auto* t = new Expression();
|
||||
t->line_num = line_num;
|
||||
t->value = AutoT();
|
||||
t->value = AutoTypeLiteral();
|
||||
return t;
|
||||
}
|
||||
|
||||
// Returns a Continuation type AST node at the given source location.
|
||||
auto Expression::MakeContinuationType(int line_num) -> const Expression* {
|
||||
auto Expression::MakeContinuationTypeLiteral(int line_num)
|
||||
-> const Expression* {
|
||||
auto* type = new Expression();
|
||||
type->line_num = line_num;
|
||||
type->value = ContinuationT();
|
||||
type->value = ContinuationTypeLiteral();
|
||||
return type;
|
||||
}
|
||||
|
||||
auto Expression::MakeFunType(int line_num, const Expression* param,
|
||||
const Expression* ret) -> const Expression* {
|
||||
auto Expression::MakeFunctionTypeLiteral(int line_num, const Expression* param,
|
||||
const Expression* ret)
|
||||
-> const Expression* {
|
||||
auto* t = new Expression();
|
||||
t->line_num = line_num;
|
||||
t->value = FunctionType({.parameter = param, .return_type = ret});
|
||||
t->value = FunctionTypeLiteral({.parameter = param, .return_type = ret});
|
||||
return t;
|
||||
}
|
||||
|
||||
auto Expression::MakeVar(int line_num, std::string var) -> const Expression* {
|
||||
auto Expression::MakeIdentifierExpression(int line_num, std::string var)
|
||||
-> const Expression* {
|
||||
auto* v = new Expression();
|
||||
v->line_num = line_num;
|
||||
v->value = Variable({.name = std::move(var)});
|
||||
v->value = IdentifierExpression({.name = std::move(var)});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Expression::MakeVarPat(int line_num, std::string var,
|
||||
const Expression* type) -> const Expression* {
|
||||
auto Expression::MakePatternVariableExpression(int line_num, std::string var,
|
||||
const Expression* type)
|
||||
-> const Expression* {
|
||||
auto* v = new Expression();
|
||||
v->line_num = line_num;
|
||||
v->value = PatternVariable({.name = std::move(var), .type = type});
|
||||
v->value = PatternVariableExpression({.name = std::move(var), .type = type});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Expression::MakeInt(int line_num, int i) -> const Expression* {
|
||||
auto Expression::MakeIntLiteral(int line_num, int i) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = IntLiteral({.value = i});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeBool(int line_num, bool b) -> const Expression* {
|
||||
auto Expression::MakeBoolLiteral(int line_num, bool b) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = BoolLiteral({.value = b});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeOp(int line_num, enum Operator op,
|
||||
std::vector<const Expression*> args)
|
||||
auto Expression::MakePrimitiveOperatorExpression(
|
||||
int line_num, enum Operator op, std::vector<const Expression*> args)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = std::move(args)});
|
||||
e->value =
|
||||
PrimitiveOperatorExpression({.op = op, .arguments = std::move(args)});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeCall(int line_num, const Expression* fun,
|
||||
const Expression* arg) -> const Expression* {
|
||||
auto Expression::MakeCallExpression(int line_num, const Expression* fun,
|
||||
const Expression* arg)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = Call({.function = fun, .argument = arg});
|
||||
e->value = CallExpression({.function = fun, .argument = arg});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeGetField(int line_num, const Expression* exp,
|
||||
std::string field) -> const Expression* {
|
||||
auto Expression::MakeFieldAccessExpression(int line_num, const Expression* exp,
|
||||
std::string field)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = FieldAccess({.aggregate = exp, .field = std::move(field)});
|
||||
e->value =
|
||||
FieldAccessExpression({.aggregate = exp, .field = std::move(field)});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeTuple(int line_num, std::vector<FieldInitializer> args)
|
||||
auto Expression::MakeTupleLiteral(int line_num,
|
||||
std::vector<FieldInitializer> args)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
@@ -166,15 +179,15 @@ auto Expression::MakeTuple(int line_num, std::vector<FieldInitializer> args)
|
||||
seen_named_member = true;
|
||||
}
|
||||
}
|
||||
e->value = Tuple({.fields = args});
|
||||
e->value = TupleLiteral({.fields = args});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeIndex(int line_num, const Expression* exp,
|
||||
const Expression* i) -> const Expression* {
|
||||
auto Expression::MakeIndexExpression(int line_num, const Expression* exp,
|
||||
const Expression* i) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = Index({.aggregate = exp, .offset = i});
|
||||
e->value = IndexExpression({.aggregate = exp, .offset = i});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -220,32 +233,32 @@ static void PrintFields(const std::vector<FieldInitializer>& fields) {
|
||||
|
||||
void PrintExp(const Expression* e) {
|
||||
switch (e->tag()) {
|
||||
case ExpressionKind::Index:
|
||||
PrintExp(e->GetIndex().aggregate);
|
||||
case ExpressionKind::IndexExpression:
|
||||
PrintExp(e->GetIndexExpression().aggregate);
|
||||
std::cout << "[";
|
||||
PrintExp(e->GetIndex().offset);
|
||||
PrintExp(e->GetIndexExpression().offset);
|
||||
std::cout << "]";
|
||||
break;
|
||||
case ExpressionKind::GetField:
|
||||
PrintExp(e->GetFieldAccess().aggregate);
|
||||
case ExpressionKind::FieldAccessExpression:
|
||||
PrintExp(e->GetFieldAccessExpression().aggregate);
|
||||
std::cout << ".";
|
||||
std::cout << e->GetFieldAccess().field;
|
||||
std::cout << e->GetFieldAccessExpression().field;
|
||||
break;
|
||||
case ExpressionKind::Tuple:
|
||||
case ExpressionKind::TupleLiteral:
|
||||
std::cout << "(";
|
||||
PrintFields(e->GetTuple().fields);
|
||||
PrintFields(e->GetTupleLiteral().fields);
|
||||
std::cout << ")";
|
||||
break;
|
||||
case ExpressionKind::Integer:
|
||||
std::cout << e->GetInteger();
|
||||
case ExpressionKind::IntLiteral:
|
||||
std::cout << e->GetIntLiteral();
|
||||
break;
|
||||
case ExpressionKind::Boolean:
|
||||
case ExpressionKind::BoolLiteral:
|
||||
std::cout << std::boolalpha;
|
||||
std::cout << e->GetBoolean();
|
||||
std::cout << e->GetBoolLiteral();
|
||||
break;
|
||||
case ExpressionKind::PrimitiveOp: {
|
||||
case ExpressionKind::PrimitiveOperatorExpression: {
|
||||
std::cout << "(";
|
||||
PrimitiveOperator op = e->GetPrimitiveOperator();
|
||||
PrimitiveOperatorExpression op = e->GetPrimitiveOperatorExpression();
|
||||
if (op.arguments.size() == 0) {
|
||||
PrintOp(op.op);
|
||||
} else if (op.arguments.size() == 1) {
|
||||
@@ -265,44 +278,45 @@ void PrintExp(const Expression* e) {
|
||||
std::cout << ")";
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Variable:
|
||||
std::cout << e->GetVariable().name;
|
||||
case ExpressionKind::IdentifierExpression:
|
||||
std::cout << e->GetIdentifierExpression().name;
|
||||
break;
|
||||
case ExpressionKind::PatternVariable:
|
||||
PrintExp(e->GetPatternVariable().type);
|
||||
case ExpressionKind::PatternVariableExpression:
|
||||
PrintExp(e->GetPatternVariableExpression().type);
|
||||
std::cout << ": ";
|
||||
std::cout << e->GetPatternVariable().name;
|
||||
std::cout << e->GetPatternVariableExpression().name;
|
||||
break;
|
||||
case ExpressionKind::Call:
|
||||
PrintExp(e->GetCall().function);
|
||||
if (e->GetCall().argument->tag() == ExpressionKind::Tuple) {
|
||||
PrintExp(e->GetCall().argument);
|
||||
case ExpressionKind::CallExpression:
|
||||
PrintExp(e->GetCallExpression().function);
|
||||
if (e->GetCallExpression().argument->tag() ==
|
||||
ExpressionKind::TupleLiteral) {
|
||||
PrintExp(e->GetCallExpression().argument);
|
||||
} else {
|
||||
std::cout << "(";
|
||||
PrintExp(e->GetCall().argument);
|
||||
PrintExp(e->GetCallExpression().argument);
|
||||
std::cout << ")";
|
||||
}
|
||||
break;
|
||||
case ExpressionKind::BoolT:
|
||||
case ExpressionKind::BoolTypeLiteral:
|
||||
std::cout << "Bool";
|
||||
break;
|
||||
case ExpressionKind::IntT:
|
||||
case ExpressionKind::IntTypeLiteral:
|
||||
std::cout << "Int";
|
||||
break;
|
||||
case ExpressionKind::TypeT:
|
||||
case ExpressionKind::TypeTypeLiteral:
|
||||
std::cout << "Type";
|
||||
break;
|
||||
case ExpressionKind::AutoT:
|
||||
case ExpressionKind::AutoTypeLiteral:
|
||||
std::cout << "auto";
|
||||
break;
|
||||
case ExpressionKind::ContinuationT:
|
||||
case ExpressionKind::ContinuationTypeLiteral:
|
||||
std::cout << "Continuation";
|
||||
break;
|
||||
case ExpressionKind::FunctionT:
|
||||
case ExpressionKind::FunctionTypeLiteral:
|
||||
std::cout << "fn ";
|
||||
PrintExp(e->GetFunctionType().parameter);
|
||||
PrintExp(e->GetFunctionTypeLiteral().parameter);
|
||||
std::cout << " -> ";
|
||||
PrintExp(e->GetFunctionType().return_type);
|
||||
PrintExp(e->GetFunctionTypeLiteral().return_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,21 +23,21 @@ struct FieldInitializer {
|
||||
};
|
||||
|
||||
enum class ExpressionKind {
|
||||
AutoT,
|
||||
BoolT,
|
||||
Boolean,
|
||||
Call,
|
||||
FunctionT,
|
||||
GetField,
|
||||
Index,
|
||||
IntT,
|
||||
ContinuationT, // The type of a continuation value.
|
||||
Integer,
|
||||
PatternVariable,
|
||||
PrimitiveOp,
|
||||
Tuple,
|
||||
TypeT,
|
||||
Variable,
|
||||
AutoTypeLiteral,
|
||||
BoolTypeLiteral,
|
||||
BoolLiteral,
|
||||
CallExpression,
|
||||
FunctionTypeLiteral,
|
||||
FieldAccessExpression,
|
||||
IndexExpression,
|
||||
IntTypeLiteral,
|
||||
ContinuationTypeLiteral, // The type of a continuation value.
|
||||
IntLiteral,
|
||||
PatternVariableExpression,
|
||||
PrimitiveOperatorExpression,
|
||||
TupleLiteral,
|
||||
TypeTypeLiteral,
|
||||
IdentifierExpression,
|
||||
};
|
||||
|
||||
enum class Operator {
|
||||
@@ -55,124 +55,134 @@ enum class Operator {
|
||||
|
||||
struct Expression;
|
||||
|
||||
struct Variable {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Variable;
|
||||
struct IdentifierExpression {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::IdentifierExpression;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
struct FieldAccess {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::GetField;
|
||||
struct FieldAccessExpression {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::FieldAccessExpression;
|
||||
const Expression* aggregate;
|
||||
std::string field;
|
||||
};
|
||||
|
||||
struct Index {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Index;
|
||||
struct IndexExpression {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::IndexExpression;
|
||||
const Expression* aggregate;
|
||||
const Expression* offset;
|
||||
};
|
||||
|
||||
struct PatternVariable {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::PatternVariable;
|
||||
struct PatternVariableExpression {
|
||||
static constexpr ExpressionKind Kind =
|
||||
ExpressionKind::PatternVariableExpression;
|
||||
std::string name;
|
||||
const Expression* type;
|
||||
};
|
||||
|
||||
struct IntLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Integer;
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::IntLiteral;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct BoolLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Boolean;
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::BoolLiteral;
|
||||
bool value;
|
||||
};
|
||||
|
||||
struct Tuple {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Tuple;
|
||||
struct TupleLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::TupleLiteral;
|
||||
std::vector<FieldInitializer> fields;
|
||||
};
|
||||
|
||||
struct PrimitiveOperator {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::PrimitiveOp;
|
||||
struct PrimitiveOperatorExpression {
|
||||
static constexpr ExpressionKind Kind =
|
||||
ExpressionKind::PrimitiveOperatorExpression;
|
||||
Operator op;
|
||||
std::vector<const Expression*> arguments;
|
||||
};
|
||||
|
||||
struct Call {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Call;
|
||||
struct CallExpression {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::CallExpression;
|
||||
const Expression* function;
|
||||
const Expression* argument;
|
||||
};
|
||||
|
||||
struct FunctionType {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::FunctionT;
|
||||
struct FunctionTypeLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::FunctionTypeLiteral;
|
||||
const Expression* parameter;
|
||||
const Expression* return_type;
|
||||
};
|
||||
|
||||
struct AutoT {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::AutoT;
|
||||
struct AutoTypeLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::AutoTypeLiteral;
|
||||
};
|
||||
|
||||
struct BoolT {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::BoolT;
|
||||
struct BoolTypeLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::BoolTypeLiteral;
|
||||
};
|
||||
|
||||
struct IntT {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::IntT;
|
||||
struct IntTypeLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::IntTypeLiteral;
|
||||
};
|
||||
|
||||
struct ContinuationT {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::ContinuationT;
|
||||
struct ContinuationTypeLiteral {
|
||||
static constexpr ExpressionKind Kind =
|
||||
ExpressionKind::ContinuationTypeLiteral;
|
||||
};
|
||||
|
||||
struct TypeT {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::TypeT;
|
||||
struct TypeTypeLiteral {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::TypeTypeLiteral;
|
||||
};
|
||||
|
||||
struct Expression {
|
||||
int line_num;
|
||||
inline auto tag() const -> ExpressionKind;
|
||||
|
||||
static auto MakeVar(int line_num, std::string var) -> const Expression*;
|
||||
static auto MakeVarPat(int line_num, std::string var, const Expression* type)
|
||||
static auto MakeIdentifierExpression(int line_num, std::string var)
|
||||
-> const Expression*;
|
||||
static auto MakeInt(int line_num, int i) -> const Expression*;
|
||||
static auto MakeBool(int line_num, bool b) -> const Expression*;
|
||||
static auto MakeOp(int line_num, Operator op,
|
||||
std::vector<const Expression*> args) -> const Expression*;
|
||||
static auto MakeCall(int line_num, const Expression* fun,
|
||||
const Expression* arg) -> const Expression*;
|
||||
static auto MakeGetField(int line_num, const Expression* exp,
|
||||
std::string field) -> const Expression*;
|
||||
static auto MakeTuple(int line_num, std::vector<FieldInitializer> args)
|
||||
static auto MakePatternVariableExpression(int line_num, std::string var,
|
||||
const Expression* type)
|
||||
-> const Expression*;
|
||||
static auto MakeIndex(int line_num, const Expression* exp,
|
||||
const Expression* i) -> const Expression*;
|
||||
static auto MakeTypeType(int line_num) -> const Expression*;
|
||||
static auto MakeIntType(int line_num) -> const Expression*;
|
||||
static auto MakeBoolType(int line_num) -> const Expression*;
|
||||
static auto MakeFunType(int line_num, const Expression* param,
|
||||
const Expression* ret) -> const Expression*;
|
||||
static auto MakeAutoType(int line_num) -> const Expression*;
|
||||
static auto MakeContinuationType(int line_num) -> const Expression*;
|
||||
static auto MakeIntLiteral(int line_num, int i) -> const Expression*;
|
||||
static auto MakeBoolLiteral(int line_num, bool b) -> const Expression*;
|
||||
static auto MakePrimitiveOperatorExpression(
|
||||
int line_num, Operator op, std::vector<const Expression*> args)
|
||||
-> const Expression*;
|
||||
static auto MakeCallExpression(int line_num, const Expression* fun,
|
||||
const Expression* arg) -> const Expression*;
|
||||
static auto MakeFieldAccessExpression(int line_num, const Expression* exp,
|
||||
std::string field) -> const Expression*;
|
||||
static auto MakeTupleLiteral(int line_num, std::vector<FieldInitializer> args)
|
||||
-> const Expression*;
|
||||
static auto MakeIndexExpression(int line_num, const Expression* exp,
|
||||
const Expression* i) -> const Expression*;
|
||||
static auto MakeTypeTypeLiteral(int line_num) -> const Expression*;
|
||||
static auto MakeIntTypeLiteral(int line_num) -> const Expression*;
|
||||
static auto MakeBoolTypeLiteral(int line_num) -> const Expression*;
|
||||
static auto MakeFunctionTypeLiteral(int line_num, const Expression* param,
|
||||
const Expression* ret)
|
||||
-> const Expression*;
|
||||
static auto MakeAutoTypeLiteral(int line_num) -> const Expression*;
|
||||
static auto MakeContinuationTypeLiteral(int line_num) -> const Expression*;
|
||||
|
||||
auto GetVariable() const -> const Variable&;
|
||||
auto GetFieldAccess() const -> const FieldAccess&;
|
||||
auto GetIndex() const -> const Index&;
|
||||
auto GetPatternVariable() const -> const PatternVariable&;
|
||||
auto GetInteger() const -> int;
|
||||
auto GetBoolean() const -> bool;
|
||||
auto GetTuple() const -> const Tuple&;
|
||||
auto GetPrimitiveOperator() const -> const PrimitiveOperator&;
|
||||
auto GetCall() const -> const Call&;
|
||||
auto GetFunctionType() const -> const FunctionType&;
|
||||
auto GetIdentifierExpression() const -> const IdentifierExpression&;
|
||||
auto GetFieldAccessExpression() const -> const FieldAccessExpression&;
|
||||
auto GetIndexExpression() const -> const IndexExpression&;
|
||||
auto GetPatternVariableExpression() const -> const PatternVariableExpression&;
|
||||
auto GetIntLiteral() const -> int;
|
||||
auto GetBoolLiteral() const -> bool;
|
||||
auto GetTupleLiteral() const -> const TupleLiteral&;
|
||||
auto GetPrimitiveOperatorExpression() const
|
||||
-> const PrimitiveOperatorExpression&;
|
||||
auto GetCallExpression() const -> const CallExpression&;
|
||||
auto GetFunctionTypeLiteral() const -> const FunctionTypeLiteral&;
|
||||
|
||||
private:
|
||||
std::variant<Variable, FieldAccess, Index, PatternVariable, IntLiteral,
|
||||
BoolLiteral, Tuple, PrimitiveOperator, Call, FunctionType, AutoT,
|
||||
BoolT, IntT, ContinuationT, TypeT>
|
||||
std::variant<IdentifierExpression, FieldAccessExpression, IndexExpression,
|
||||
PatternVariableExpression, IntLiteral, BoolLiteral, TupleLiteral,
|
||||
PrimitiveOperatorExpression, CallExpression, FunctionTypeLiteral,
|
||||
AutoTypeLiteral, BoolTypeLiteral, IntTypeLiteral,
|
||||
ContinuationTypeLiteral, TypeTypeLiteral>
|
||||
value;
|
||||
};
|
||||
|
||||
|
||||
@@ -66,65 +66,66 @@ void Heap::CheckAlive(Address address, int line_num) {
|
||||
|
||||
auto CopyVal(const Value* val, int line_num) -> const Value* {
|
||||
switch (val->tag) {
|
||||
case ValKind::TupleV: {
|
||||
case ValKind::TupleValue: {
|
||||
auto* elements = new std::vector<TupleElement>();
|
||||
for (const TupleElement& element : *val->GetTuple().elements) {
|
||||
for (const TupleElement& element : *val->GetTupleValue().elements) {
|
||||
const Value* new_element =
|
||||
CopyVal(state->heap.Read(element.address, line_num), line_num);
|
||||
Address new_address = state->heap.AllocateValue(new_element);
|
||||
elements->push_back({.name = element.name, .address = new_address});
|
||||
}
|
||||
return Value::MakeTupleVal(elements);
|
||||
return Value::MakeTupleValue(elements);
|
||||
}
|
||||
case ValKind::AltV: {
|
||||
case ValKind::AlternativeValue: {
|
||||
const Value* arg = CopyVal(
|
||||
state->heap.Read(val->GetAlternative().argument, line_num), line_num);
|
||||
state->heap.Read(val->GetAlternativeValue().argument, line_num),
|
||||
line_num);
|
||||
Address argument_address = state->heap.AllocateValue(arg);
|
||||
return Value::MakeAltVal(*val->GetAlternative().alt_name,
|
||||
*val->GetAlternative().choice_name,
|
||||
argument_address);
|
||||
return Value::MakeAlternativeValue(
|
||||
*val->GetAlternativeValue().alt_name,
|
||||
*val->GetAlternativeValue().choice_name, argument_address);
|
||||
}
|
||||
case ValKind::StructV: {
|
||||
const Value* inits = CopyVal(val->GetStruct().inits, line_num);
|
||||
return Value::MakeStructVal(val->GetStruct().type, inits);
|
||||
case ValKind::StructValue: {
|
||||
const Value* inits = CopyVal(val->GetStructValue().inits, line_num);
|
||||
return Value::MakeStructValue(val->GetStructValue().type, inits);
|
||||
}
|
||||
case ValKind::IntV:
|
||||
return Value::MakeIntVal(val->GetInteger());
|
||||
case ValKind::BoolV:
|
||||
return Value::MakeBoolVal(val->GetBoolean());
|
||||
case ValKind::FunV:
|
||||
return Value::MakeFunVal(*val->GetFunction().name,
|
||||
val->GetFunction().param,
|
||||
val->GetFunction().body);
|
||||
case ValKind::PtrV:
|
||||
return Value::MakePtrVal(val->GetPointer());
|
||||
case ValKind::ContinuationV:
|
||||
case ValKind::IntValue:
|
||||
return Value::MakeIntValue(val->GetIntValue());
|
||||
case ValKind::BoolValue:
|
||||
return Value::MakeBoolValue(val->GetBoolValue());
|
||||
case ValKind::FunctionValue:
|
||||
return Value::MakeFunctionValue(*val->GetFunctionValue().name,
|
||||
val->GetFunctionValue().param,
|
||||
val->GetFunctionValue().body);
|
||||
case ValKind::PointerValue:
|
||||
return Value::MakePointerValue(val->GetPointerValue());
|
||||
case ValKind::ContinuationValue:
|
||||
// Copying a continuation is "shallow".
|
||||
return val;
|
||||
case ValKind::FunctionTV:
|
||||
return Value::MakeFunTypeVal(
|
||||
case ValKind::FunctionType:
|
||||
return Value::MakeFunctionType(
|
||||
CopyVal(val->GetFunctionType().param, line_num),
|
||||
CopyVal(val->GetFunctionType().ret, line_num));
|
||||
|
||||
case ValKind::PointerTV:
|
||||
return Value::MakePtrTypeVal(
|
||||
case ValKind::PointerType:
|
||||
return Value::MakePointerType(
|
||||
CopyVal(val->GetPointerType().type, line_num));
|
||||
case ValKind::IntTV:
|
||||
return Value::MakeIntTypeVal();
|
||||
case ValKind::BoolTV:
|
||||
return Value::MakeBoolTypeVal();
|
||||
case ValKind::TypeTV:
|
||||
return Value::MakeTypeTypeVal();
|
||||
case ValKind::IntType:
|
||||
return Value::MakeIntType();
|
||||
case ValKind::BoolType:
|
||||
return Value::MakeBoolType();
|
||||
case ValKind::TypeType:
|
||||
return Value::MakeTypeType();
|
||||
case ValKind::VarTV:
|
||||
return Value::MakeVarTypeVal(*val->GetVariableType());
|
||||
case ValKind::AutoTV:
|
||||
return Value::MakeAutoTypeVal();
|
||||
case ValKind::ContinuationTV:
|
||||
return Value::MakeContinuationTypeVal();
|
||||
case ValKind::StructTV:
|
||||
case ValKind::ChoiceTV:
|
||||
case ValKind::VarPatV:
|
||||
case ValKind::AltConsV:
|
||||
case ValKind::AutoType:
|
||||
return Value::MakeAutoType();
|
||||
case ValKind::ContinuationType:
|
||||
return Value::MakeContinuationType();
|
||||
case ValKind::StructType:
|
||||
case ValKind::ChoiceType:
|
||||
case ValKind::PatternVariableValue:
|
||||
case ValKind::AlternativeConstructorValue:
|
||||
return val; // no need to copy these because they are immutable?
|
||||
// No, they need to be copied so they don't get killed. -Jeremy
|
||||
}
|
||||
@@ -132,14 +133,14 @@ auto CopyVal(const Value* val, int line_num) -> const Value* {
|
||||
|
||||
void Heap::DeallocateSubObjects(const Value* val) {
|
||||
switch (val->tag) {
|
||||
case ValKind::AltV:
|
||||
Deallocate(val->GetAlternative().argument);
|
||||
case ValKind::AlternativeValue:
|
||||
Deallocate(val->GetAlternativeValue().argument);
|
||||
break;
|
||||
case ValKind::StructV:
|
||||
DeallocateSubObjects(val->GetStruct().inits);
|
||||
case ValKind::StructValue:
|
||||
DeallocateSubObjects(val->GetStructValue().inits);
|
||||
break;
|
||||
case ValKind::TupleV:
|
||||
for (const TupleElement& element : *val->GetTuple().elements) {
|
||||
case ValKind::TupleValue:
|
||||
for (const TupleElement& element : *val->GetTupleValue().elements) {
|
||||
Deallocate(element.address);
|
||||
}
|
||||
break;
|
||||
@@ -226,8 +227,8 @@ void PrintState(std::ostream& out) {
|
||||
|
||||
auto ValToInt(const Value* v, int line_num) -> int {
|
||||
switch (v->tag) {
|
||||
case ValKind::IntV:
|
||||
return v->GetInteger();
|
||||
case ValKind::IntValue:
|
||||
return v->GetIntValue();
|
||||
default:
|
||||
std::cerr << line_num << ": runtime error: expected an integer"
|
||||
<< std::endl;
|
||||
@@ -237,8 +238,8 @@ auto ValToInt(const Value* v, int line_num) -> int {
|
||||
|
||||
auto ValToBool(const Value* v, int line_num) -> int {
|
||||
switch (v->tag) {
|
||||
case ValKind::BoolV:
|
||||
return v->GetBoolean();
|
||||
case ValKind::BoolValue:
|
||||
return v->GetBoolValue();
|
||||
default:
|
||||
std::cerr << "runtime type error: expected a Boolean" << std::endl;
|
||||
exit(-1);
|
||||
@@ -247,8 +248,8 @@ auto ValToBool(const Value* v, int line_num) -> int {
|
||||
|
||||
auto ValToPtr(const Value* v, int line_num) -> Address {
|
||||
switch (v->tag) {
|
||||
case ValKind::PtrV:
|
||||
return v->GetPointer();
|
||||
case ValKind::PointerValue:
|
||||
return v->GetPointerValue();
|
||||
default:
|
||||
std::cerr << "runtime type error: expected a pointer, not ";
|
||||
PrintValue(v, std::cerr);
|
||||
@@ -262,8 +263,8 @@ auto ValToPtr(const Value* v, int line_num) -> Address {
|
||||
// - Precondition: continuation->tag == ValKind::ContinuationV.
|
||||
auto ContinuationToVector(const Value* continuation, int sourceLocation)
|
||||
-> std::vector<Frame*> {
|
||||
if (continuation->tag == ValKind::ContinuationV) {
|
||||
return *continuation->GetContinuation().stack;
|
||||
if (continuation->tag == ValKind::ContinuationValue) {
|
||||
return *continuation->GetContinuationValue().stack;
|
||||
} else {
|
||||
std::cerr << sourceLocation << ": runtime error: expected an integer"
|
||||
<< std::endl;
|
||||
@@ -275,28 +276,28 @@ auto EvalPrim(Operator op, const std::vector<const Value*>& args, int line_num)
|
||||
-> const Value* {
|
||||
switch (op) {
|
||||
case Operator::Neg:
|
||||
return Value::MakeIntVal(-ValToInt(args[0], line_num));
|
||||
return Value::MakeIntValue(-ValToInt(args[0], line_num));
|
||||
case Operator::Add:
|
||||
return Value::MakeIntVal(ValToInt(args[0], line_num) +
|
||||
ValToInt(args[1], line_num));
|
||||
return Value::MakeIntValue(ValToInt(args[0], line_num) +
|
||||
ValToInt(args[1], line_num));
|
||||
case Operator::Sub:
|
||||
return Value::MakeIntVal(ValToInt(args[0], line_num) -
|
||||
ValToInt(args[1], line_num));
|
||||
return Value::MakeIntValue(ValToInt(args[0], line_num) -
|
||||
ValToInt(args[1], line_num));
|
||||
case Operator::Mul:
|
||||
return Value::MakeIntVal(ValToInt(args[0], line_num) *
|
||||
ValToInt(args[1], line_num));
|
||||
return Value::MakeIntValue(ValToInt(args[0], line_num) *
|
||||
ValToInt(args[1], line_num));
|
||||
case Operator::Not:
|
||||
return Value::MakeBoolVal(!ValToBool(args[0], line_num));
|
||||
return Value::MakeBoolValue(!ValToBool(args[0], line_num));
|
||||
case Operator::And:
|
||||
return Value::MakeBoolVal(ValToBool(args[0], line_num) &&
|
||||
ValToBool(args[1], line_num));
|
||||
return Value::MakeBoolValue(ValToBool(args[0], line_num) &&
|
||||
ValToBool(args[1], line_num));
|
||||
case Operator::Or:
|
||||
return Value::MakeBoolVal(ValToBool(args[0], line_num) ||
|
||||
ValToBool(args[1], line_num));
|
||||
return Value::MakeBoolValue(ValToBool(args[0], line_num) ||
|
||||
ValToBool(args[1], line_num));
|
||||
case Operator::Eq:
|
||||
return Value::MakeBoolVal(ValueEqual(args[0], args[1], line_num));
|
||||
return Value::MakeBoolValue(ValueEqual(args[0], args[1], line_num));
|
||||
case Operator::Ptr:
|
||||
return Value::MakePtrTypeVal(args[0]);
|
||||
return Value::MakePointerType(args[0]);
|
||||
case Operator::Deref:
|
||||
std::cerr << line_num << ": dereference not implemented yet\n";
|
||||
exit(-1);
|
||||
@@ -318,7 +319,7 @@ auto ChoiceDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
auto t = InterpExp(Env(), signature);
|
||||
alts->push_back(make_pair(name, t));
|
||||
}
|
||||
auto ct = Value::MakeChoiceTypeVal(name, alts);
|
||||
auto ct = Value::MakeChoiceType(name, alts);
|
||||
auto a = state->heap.AllocateValue(ct);
|
||||
globals.Set(name, a);
|
||||
}
|
||||
@@ -336,14 +337,14 @@ auto StructDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
}
|
||||
}
|
||||
}
|
||||
auto st = Value::MakeStructTypeVal(*definition.name, fields, methods);
|
||||
auto st = Value::MakeStructType(*definition.name, fields, methods);
|
||||
auto a = state->heap.AllocateValue(st);
|
||||
globals.Set(*definition.name, a);
|
||||
}
|
||||
|
||||
auto FunctionDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
auto pt = InterpExp(globals, definition.param_pattern);
|
||||
auto f = Value::MakeFunVal(definition.name, pt, definition.body);
|
||||
auto f = Value::MakeFunctionValue(definition.name, pt, definition.body);
|
||||
Address a = state->heap.AllocateValue(f);
|
||||
globals.Set(definition.name, a);
|
||||
}
|
||||
@@ -363,11 +364,11 @@ auto VariableDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
void CallFunction(int line_num, std::vector<const Value*> operas,
|
||||
State* state) {
|
||||
switch (operas[0]->tag) {
|
||||
case ValKind::FunV: {
|
||||
case ValKind::FunctionValue: {
|
||||
// Bind arguments to parameters
|
||||
std::list<std::string> params;
|
||||
std::optional<Env> matches =
|
||||
PatternMatch(operas[0]->GetFunction().param, operas[1], globals,
|
||||
PatternMatch(operas[0]->GetFunctionValue().param, operas[1], globals,
|
||||
¶ms, line_num);
|
||||
if (!matches) {
|
||||
std::cerr << "internal error in call_function, pattern match failed"
|
||||
@@ -377,24 +378,24 @@ void CallFunction(int line_num, std::vector<const Value*> operas,
|
||||
// Create the new frame and push it on the stack
|
||||
auto* scope = new Scope(*matches, params);
|
||||
auto* frame =
|
||||
new Frame(*operas[0]->GetFunction().name, Stack(scope),
|
||||
Stack(MakeStmtAct(operas[0]->GetFunction().body)));
|
||||
new Frame(*operas[0]->GetFunctionValue().name, Stack(scope),
|
||||
Stack(MakeStmtAct(operas[0]->GetFunctionValue().body)));
|
||||
state->stack.Push(frame);
|
||||
break;
|
||||
}
|
||||
case ValKind::StructTV: {
|
||||
case ValKind::StructType: {
|
||||
const Value* arg = CopyVal(operas[1], line_num);
|
||||
const Value* sv = Value::MakeStructVal(operas[0], arg);
|
||||
const Value* sv = Value::MakeStructValue(operas[0], arg);
|
||||
Frame* frame = state->stack.Top();
|
||||
frame->todo.Push(MakeValAct(sv));
|
||||
break;
|
||||
}
|
||||
case ValKind::AltConsV: {
|
||||
case ValKind::AlternativeConstructorValue: {
|
||||
const Value* arg = CopyVal(operas[1], line_num);
|
||||
const Value* av =
|
||||
Value::MakeAltVal(*operas[0]->GetAlternativeConstructor().alt_name,
|
||||
*operas[0]->GetAlternativeConstructor().choice_name,
|
||||
state->heap.AllocateValue(arg));
|
||||
const Value* av = Value::MakeAlternativeValue(
|
||||
*operas[0]->GetAlternativeConstructorValue().alt_name,
|
||||
*operas[0]->GetAlternativeConstructorValue().choice_name,
|
||||
state->heap.AllocateValue(arg));
|
||||
Frame* frame = state->stack.Top();
|
||||
frame->todo.Push(MakeValAct(av));
|
||||
break;
|
||||
@@ -428,12 +429,12 @@ void CreateTuple(Frame* frame, Action* act, const Expression* /*exp*/) {
|
||||
// { { (v1,...,vn) :: C, E, F} :: S, H}
|
||||
// -> { { `(v1,...,vn) :: C, E, F} :: S, H}
|
||||
auto elements = new std::vector<TupleElement>();
|
||||
auto f = act->u.exp->GetTuple().fields.begin();
|
||||
auto f = act->u.exp->GetTupleLiteral().fields.begin();
|
||||
for (auto i = act->results.begin(); i != act->results.end(); ++i, ++f) {
|
||||
Address a = state->heap.AllocateValue(*i); // copy?
|
||||
elements->push_back({.name = f->name, .address = a});
|
||||
}
|
||||
const Value* tv = Value::MakeTupleVal(elements);
|
||||
const Value* tv = Value::MakeTupleValue(elements);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(tv));
|
||||
}
|
||||
@@ -447,22 +448,22 @@ auto PatternMatch(const Value* p, const Value* v, Env values,
|
||||
std::list<std::string>* vars, int line_num)
|
||||
-> std::optional<Env> {
|
||||
switch (p->tag) {
|
||||
case ValKind::VarPatV: {
|
||||
case ValKind::PatternVariableValue: {
|
||||
Address a = state->heap.AllocateValue(CopyVal(v, line_num));
|
||||
vars->push_back(*p->GetVariablePattern().name);
|
||||
values.Set(*p->GetVariablePattern().name, a);
|
||||
vars->push_back(*p->GetPatternVariableValue().name);
|
||||
values.Set(*p->GetPatternVariableValue().name, a);
|
||||
return values;
|
||||
}
|
||||
case ValKind::TupleV:
|
||||
case ValKind::TupleValue:
|
||||
switch (v->tag) {
|
||||
case ValKind::TupleV: {
|
||||
if (p->GetTuple().elements->size() !=
|
||||
v->GetTuple().elements->size()) {
|
||||
case ValKind::TupleValue: {
|
||||
if (p->GetTupleValue().elements->size() !=
|
||||
v->GetTupleValue().elements->size()) {
|
||||
std::cerr << "runtime error: arity mismatch in tuple pattern match"
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
for (const TupleElement& element : *p->GetTuple().elements) {
|
||||
for (const TupleElement& element : *p->GetTupleValue().elements) {
|
||||
auto a = FindTupleField(element.name, v);
|
||||
if (a == std::nullopt) {
|
||||
std::cerr << "runtime error: field " << element.name << "not in ";
|
||||
@@ -487,18 +488,19 @@ auto PatternMatch(const Value* p, const Value* v, Env values,
|
||||
std::cerr << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
case ValKind::AltV:
|
||||
case ValKind::AlternativeValue:
|
||||
switch (v->tag) {
|
||||
case ValKind::AltV: {
|
||||
if (*p->GetAlternative().choice_name !=
|
||||
*v->GetAlternative().choice_name ||
|
||||
*p->GetAlternative().alt_name != *v->GetAlternative().alt_name) {
|
||||
case ValKind::AlternativeValue: {
|
||||
if (*p->GetAlternativeValue().choice_name !=
|
||||
*v->GetAlternativeValue().choice_name ||
|
||||
*p->GetAlternativeValue().alt_name !=
|
||||
*v->GetAlternativeValue().alt_name) {
|
||||
return std::nullopt;
|
||||
}
|
||||
std::optional<Env> matches = PatternMatch(
|
||||
state->heap.Read(p->GetAlternative().argument, line_num),
|
||||
state->heap.Read(v->GetAlternative().argument, line_num), values,
|
||||
vars, line_num);
|
||||
state->heap.Read(p->GetAlternativeValue().argument, line_num),
|
||||
state->heap.Read(v->GetAlternativeValue().argument, line_num),
|
||||
values, vars, line_num);
|
||||
if (!matches) {
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -512,9 +514,9 @@ auto PatternMatch(const Value* p, const Value* v, Env values,
|
||||
std::cerr << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
case ValKind::FunctionTV:
|
||||
case ValKind::FunctionType:
|
||||
switch (v->tag) {
|
||||
case ValKind::FunctionTV: {
|
||||
case ValKind::FunctionType: {
|
||||
std::optional<Env> matches =
|
||||
PatternMatch(p->GetFunctionType().param,
|
||||
v->GetFunctionType().param, values, vars, line_num);
|
||||
@@ -539,20 +541,20 @@ auto PatternMatch(const Value* p, const Value* v, Env values,
|
||||
|
||||
void PatternAssignment(const Value* pat, const Value* val, int line_num) {
|
||||
switch (pat->tag) {
|
||||
case ValKind::PtrV:
|
||||
case ValKind::PointerValue:
|
||||
state->heap.Write(ValToPtr(pat, line_num), CopyVal(val, line_num),
|
||||
line_num);
|
||||
break;
|
||||
case ValKind::TupleV: {
|
||||
case ValKind::TupleValue: {
|
||||
switch (val->tag) {
|
||||
case ValKind::TupleV: {
|
||||
if (pat->GetTuple().elements->size() !=
|
||||
val->GetTuple().elements->size()) {
|
||||
case ValKind::TupleValue: {
|
||||
if (pat->GetTupleValue().elements->size() !=
|
||||
val->GetTupleValue().elements->size()) {
|
||||
std::cerr << "runtime error: arity mismatch in tuple pattern match"
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
for (const TupleElement& element : *pat->GetTuple().elements) {
|
||||
for (const TupleElement& element : *pat->GetTupleValue().elements) {
|
||||
auto a = FindTupleField(element.name, val);
|
||||
if (a == std::nullopt) {
|
||||
std::cerr << "runtime error: field " << element.name << "not in ";
|
||||
@@ -575,19 +577,19 @@ void PatternAssignment(const Value* pat, const Value* val, int line_num) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ValKind::AltV: {
|
||||
case ValKind::AlternativeValue: {
|
||||
switch (val->tag) {
|
||||
case ValKind::AltV: {
|
||||
if (*pat->GetAlternative().choice_name !=
|
||||
*val->GetAlternative().choice_name ||
|
||||
*pat->GetAlternative().alt_name !=
|
||||
*val->GetAlternative().alt_name) {
|
||||
case ValKind::AlternativeValue: {
|
||||
if (*pat->GetAlternativeValue().choice_name !=
|
||||
*val->GetAlternativeValue().choice_name ||
|
||||
*pat->GetAlternativeValue().alt_name !=
|
||||
*val->GetAlternativeValue().alt_name) {
|
||||
std::cerr << "internal error in pattern assignment" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
PatternAssignment(
|
||||
state->heap.Read(pat->GetAlternative().argument, line_num),
|
||||
state->heap.Read(val->GetAlternative().argument, line_num),
|
||||
state->heap.Read(pat->GetAlternativeValue().argument, line_num),
|
||||
state->heap.Read(val->GetAlternativeValue().argument, line_num),
|
||||
line_num);
|
||||
break;
|
||||
}
|
||||
@@ -621,46 +623,48 @@ void StepLvalue() {
|
||||
std::cout << " --->" << std::endl;
|
||||
}
|
||||
switch (exp->tag()) {
|
||||
case ExpressionKind::Variable: {
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
// { {x :: C, E, F} :: S, H}
|
||||
// -> { {E(x) :: C, E, F} :: S, H}
|
||||
std::optional<Address> pointer =
|
||||
CurrentEnv(state).Get(exp->GetVariable().name);
|
||||
CurrentEnv(state).Get(exp->GetIdentifierExpression().name);
|
||||
if (!pointer) {
|
||||
std::cerr << exp->line_num << ": could not find `"
|
||||
<< exp->GetVariable().name << "`" << std::endl;
|
||||
<< exp->GetIdentifierExpression().name << "`" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
const Value* v = Value::MakePtrVal(*pointer);
|
||||
const Value* v = Value::MakePointerValue(*pointer);
|
||||
frame->todo.Pop();
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::GetField: {
|
||||
case ExpressionKind::FieldAccessExpression: {
|
||||
if (act->pos == 0) {
|
||||
// { {e.f :: C, E, F} :: S, H}
|
||||
// -> { e :: [].f :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeLvalAct(exp->GetFieldAccess().aggregate));
|
||||
frame->todo.Push(
|
||||
MakeLvalAct(exp->GetFieldAccessExpression().aggregate));
|
||||
act->pos++;
|
||||
} else {
|
||||
// { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { &v.f :: C, E, F} :: S, H }
|
||||
const Value* str = act->results[0];
|
||||
Address a = GetMember(ValToPtr(str, exp->line_num),
|
||||
exp->GetFieldAccess().field, exp->line_num);
|
||||
Address a =
|
||||
GetMember(ValToPtr(str, exp->line_num),
|
||||
exp->GetFieldAccessExpression().field, exp->line_num);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(Value::MakePtrVal(a)));
|
||||
frame->todo.Push(MakeValAct(Value::MakePointerValue(a)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Index: {
|
||||
case ExpressionKind::IndexExpression: {
|
||||
if (act->pos == 0) {
|
||||
// { {e[i] :: C, E, F} :: S, H}
|
||||
// -> { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().aggregate));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndexExpression().aggregate));
|
||||
act->pos++;
|
||||
} else if (act->pos == 1) {
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().offset));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndexExpression().offset));
|
||||
act->pos++;
|
||||
} else if (act->pos == 2) {
|
||||
// { v :: [][i] :: C, E, F} :: S, H}
|
||||
@@ -675,23 +679,25 @@ void StepLvalue() {
|
||||
exit(-1);
|
||||
}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(Value::MakePtrVal(*a)));
|
||||
frame->todo.Push(MakeValAct(Value::MakePointerValue(*a)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Tuple: {
|
||||
case ExpressionKind::TupleLiteral: {
|
||||
if (act->pos == 0) {
|
||||
// { {(f1=e1,...) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H}
|
||||
const Expression* e1 = exp->GetTuple().fields[0].expression;
|
||||
const Expression* e1 = exp->GetTupleLiteral().fields[0].expression;
|
||||
frame->todo.Push(MakeLvalAct(e1));
|
||||
act->pos++;
|
||||
} else if (act->pos != static_cast<int>(exp->GetTuple().fields.size())) {
|
||||
} else if (act->pos !=
|
||||
static_cast<int>(exp->GetTupleLiteral().fields.size())) {
|
||||
// { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S,
|
||||
// H}
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
const Expression* elt = exp->GetTuple().fields[act->pos].expression;
|
||||
const Expression* elt =
|
||||
exp->GetTupleLiteral().fields[act->pos].expression;
|
||||
frame->todo.Push(MakeLvalAct(elt));
|
||||
act->pos++;
|
||||
} else {
|
||||
@@ -699,17 +705,17 @@ void StepLvalue() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Integer:
|
||||
case ExpressionKind::Boolean:
|
||||
case ExpressionKind::Call:
|
||||
case ExpressionKind::PrimitiveOp:
|
||||
case ExpressionKind::IntT:
|
||||
case ExpressionKind::BoolT:
|
||||
case ExpressionKind::TypeT:
|
||||
case ExpressionKind::FunctionT:
|
||||
case ExpressionKind::AutoT:
|
||||
case ExpressionKind::ContinuationT:
|
||||
case ExpressionKind::PatternVariable: {
|
||||
case ExpressionKind::IntLiteral:
|
||||
case ExpressionKind::BoolLiteral:
|
||||
case ExpressionKind::CallExpression:
|
||||
case ExpressionKind::PrimitiveOperatorExpression:
|
||||
case ExpressionKind::IntTypeLiteral:
|
||||
case ExpressionKind::BoolTypeLiteral:
|
||||
case ExpressionKind::TypeTypeLiteral:
|
||||
case ExpressionKind::FunctionTypeLiteral:
|
||||
case ExpressionKind::AutoTypeLiteral:
|
||||
case ExpressionKind::ContinuationTypeLiteral:
|
||||
case ExpressionKind::PatternVariableExpression: {
|
||||
frame->todo.Pop();
|
||||
frame->todo.Push(MakeExpToLvalAct());
|
||||
frame->todo.Push(MakeExpAct(exp));
|
||||
@@ -729,31 +735,31 @@ void StepExp() {
|
||||
std::cout << " --->" << std::endl;
|
||||
}
|
||||
switch (exp->tag()) {
|
||||
case ExpressionKind::PatternVariable: {
|
||||
case ExpressionKind::PatternVariableExpression: {
|
||||
if (act->pos == 0) {
|
||||
frame->todo.Push(MakeExpAct(exp->GetPatternVariable().type));
|
||||
frame->todo.Push(MakeExpAct(exp->GetPatternVariableExpression().type));
|
||||
act->pos++;
|
||||
} else {
|
||||
auto v = Value::MakeVarPatVal(exp->GetPatternVariable().name,
|
||||
act->results[0]);
|
||||
auto v = Value::MakePatternVariableValue(
|
||||
exp->GetPatternVariableExpression().name, act->results[0]);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Index: {
|
||||
case ExpressionKind::IndexExpression: {
|
||||
if (act->pos == 0) {
|
||||
// { { e[i] :: C, E, F} :: S, H}
|
||||
// -> { { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().aggregate));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndexExpression().aggregate));
|
||||
act->pos++;
|
||||
} else if (act->pos == 1) {
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().offset));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndexExpression().offset));
|
||||
act->pos++;
|
||||
} else if (act->pos == 2) {
|
||||
auto tuple = act->results[0];
|
||||
switch (tuple->tag) {
|
||||
case ValKind::TupleV: {
|
||||
case ValKind::TupleValue: {
|
||||
// { { v :: [][i] :: C, E, F} :: S, H}
|
||||
// -> { { v_i :: C, E, F} : S, H}
|
||||
std::string f = std::to_string(ToInteger(act->results[1]));
|
||||
@@ -779,23 +785,25 @@ void StepExp() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Tuple: {
|
||||
case ExpressionKind::TupleLiteral: {
|
||||
if (act->pos == 0) {
|
||||
if (exp->GetTuple().fields.size() > 0) {
|
||||
if (exp->GetTupleLiteral().fields.size() > 0) {
|
||||
// { {(f1=e1,...) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H}
|
||||
const Expression* e1 = exp->GetTuple().fields[0].expression;
|
||||
const Expression* e1 = exp->GetTupleLiteral().fields[0].expression;
|
||||
frame->todo.Push(MakeExpAct(e1));
|
||||
act->pos++;
|
||||
} else {
|
||||
CreateTuple(frame, act, exp);
|
||||
}
|
||||
} else if (act->pos != static_cast<int>(exp->GetTuple().fields.size())) {
|
||||
} else if (act->pos !=
|
||||
static_cast<int>(exp->GetTupleLiteral().fields.size())) {
|
||||
// { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S,
|
||||
// H}
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
const Expression* elt = exp->GetTuple().fields[act->pos].expression;
|
||||
const Expression* elt =
|
||||
exp->GetTupleLiteral().fields[act->pos].expression;
|
||||
frame->todo.Push(MakeExpAct(elt));
|
||||
act->pos++;
|
||||
} else {
|
||||
@@ -803,31 +811,33 @@ void StepExp() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::GetField: {
|
||||
case ExpressionKind::FieldAccessExpression: {
|
||||
if (act->pos == 0) {
|
||||
// { { e.f :: C, E, F} :: S, H}
|
||||
// -> { { e :: [].f :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeLvalAct(exp->GetFieldAccess().aggregate));
|
||||
frame->todo.Push(
|
||||
MakeLvalAct(exp->GetFieldAccessExpression().aggregate));
|
||||
act->pos++;
|
||||
} else {
|
||||
// { { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { v_f :: C, E, F} : S, H}
|
||||
auto a = GetMember(ValToPtr(act->results[0], exp->line_num),
|
||||
exp->GetFieldAccess().field, exp->line_num);
|
||||
auto a =
|
||||
GetMember(ValToPtr(act->results[0], exp->line_num),
|
||||
exp->GetFieldAccessExpression().field, exp->line_num);
|
||||
const Value* element = state->heap.Read(a, exp->line_num);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(element));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Variable: {
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
CHECK(act->pos == 0);
|
||||
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
|
||||
std::optional<Address> pointer =
|
||||
CurrentEnv(state).Get(exp->GetVariable().name);
|
||||
CurrentEnv(state).Get(exp->GetIdentifierExpression().name);
|
||||
if (!pointer) {
|
||||
std::cerr << exp->line_num << ": could not find `"
|
||||
<< exp->GetVariable().name << "`" << std::endl;
|
||||
<< exp->GetIdentifierExpression().name << "`" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
const Value* pointee = state->heap.Read(*pointer, exp->line_num);
|
||||
@@ -835,45 +845,47 @@ void StepExp() {
|
||||
frame->todo.Push(MakeValAct(pointee));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Integer:
|
||||
case ExpressionKind::IntLiteral:
|
||||
CHECK(act->pos == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(Value::MakeIntVal(exp->GetInteger())));
|
||||
frame->todo.Push(MakeValAct(Value::MakeIntValue(exp->GetIntLiteral())));
|
||||
break;
|
||||
case ExpressionKind::Boolean:
|
||||
case ExpressionKind::BoolLiteral:
|
||||
CHECK(act->pos == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(Value::MakeBoolVal(exp->GetBoolean())));
|
||||
frame->todo.Push(MakeValAct(Value::MakeBoolValue(exp->GetBoolLiteral())));
|
||||
break;
|
||||
case ExpressionKind::PrimitiveOp:
|
||||
case ExpressionKind::PrimitiveOperatorExpression:
|
||||
if (act->pos !=
|
||||
static_cast<int>(exp->GetPrimitiveOperator().arguments.size())) {
|
||||
static_cast<int>(
|
||||
exp->GetPrimitiveOperatorExpression().arguments.size())) {
|
||||
// { {v :: op(vs,[],e,es) :: C, E, F} :: S, H}
|
||||
// -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H}
|
||||
const Expression* arg = exp->GetPrimitiveOperator().arguments[act->pos];
|
||||
const Expression* arg =
|
||||
exp->GetPrimitiveOperatorExpression().arguments[act->pos];
|
||||
frame->todo.Push(MakeExpAct(arg));
|
||||
act->pos++;
|
||||
} else {
|
||||
// { {v :: op(vs,[]) :: C, E, F} :: S, H}
|
||||
// -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H}
|
||||
const Value* v = EvalPrim(exp->GetPrimitiveOperator().op, act->results,
|
||||
exp->line_num);
|
||||
const Value* v = EvalPrim(exp->GetPrimitiveOperatorExpression().op,
|
||||
act->results, exp->line_num);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
}
|
||||
break;
|
||||
case ExpressionKind::Call:
|
||||
case ExpressionKind::CallExpression:
|
||||
if (act->pos == 0) {
|
||||
// { {e1(e2) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: [](e2) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetCall().function));
|
||||
frame->todo.Push(MakeExpAct(exp->GetCallExpression().function));
|
||||
act->pos++;
|
||||
} else if (act->pos == 1) {
|
||||
// { { v :: [](e) :: C, E, F} :: S, H}
|
||||
// -> { { e :: v([]) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetCall().argument));
|
||||
frame->todo.Push(MakeExpAct(exp->GetCallExpression().argument));
|
||||
act->pos++;
|
||||
} else if (act->pos == 2) {
|
||||
// { { v2 :: v1([]) :: C, E, F} :: S, H}
|
||||
@@ -885,56 +897,56 @@ void StepExp() {
|
||||
exit(-1);
|
||||
}
|
||||
break;
|
||||
case ExpressionKind::IntT: {
|
||||
case ExpressionKind::IntTypeLiteral: {
|
||||
CHECK(act->pos == 0);
|
||||
const Value* v = Value::MakeIntTypeVal();
|
||||
const Value* v = Value::MakeIntType();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::BoolT: {
|
||||
case ExpressionKind::BoolTypeLiteral: {
|
||||
CHECK(act->pos == 0);
|
||||
const Value* v = Value::MakeBoolTypeVal();
|
||||
const Value* v = Value::MakeBoolType();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::AutoT: {
|
||||
case ExpressionKind::AutoTypeLiteral: {
|
||||
CHECK(act->pos == 0);
|
||||
const Value* v = Value::MakeAutoTypeVal();
|
||||
const Value* v = Value::MakeAutoType();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::TypeT: {
|
||||
case ExpressionKind::TypeTypeLiteral: {
|
||||
CHECK(act->pos == 0);
|
||||
const Value* v = Value::MakeTypeTypeVal();
|
||||
const Value* v = Value::MakeTypeType();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::FunctionT: {
|
||||
case ExpressionKind::FunctionTypeLiteral: {
|
||||
if (act->pos == 0) {
|
||||
frame->todo.Push(MakeExpAct(exp->GetFunctionType().parameter));
|
||||
frame->todo.Push(MakeExpAct(exp->GetFunctionTypeLiteral().parameter));
|
||||
act->pos++;
|
||||
} else if (act->pos == 1) {
|
||||
// { { pt :: fn [] -> e :: C, E, F} :: S, H}
|
||||
// -> { { e :: fn pt -> []) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetFunctionType().return_type));
|
||||
frame->todo.Push(MakeExpAct(exp->GetFunctionTypeLiteral().return_type));
|
||||
act->pos++;
|
||||
} else if (act->pos == 2) {
|
||||
// { { rt :: fn pt -> [] :: C, E, F} :: S, H}
|
||||
// -> { fn pt -> rt :: {C, E, F} :: S, H}
|
||||
const Value* v =
|
||||
Value::MakeFunTypeVal(act->results[0], act->results[1]);
|
||||
Value::MakeFunctionType(act->results[0], act->results[1]);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::ContinuationT: {
|
||||
case ExpressionKind::ContinuationTypeLiteral: {
|
||||
CHECK(act->pos == 0);
|
||||
const Value* v = Value::MakeContinuationTypeVal();
|
||||
const Value* v = Value::MakeContinuationType();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
break;
|
||||
@@ -1223,11 +1235,11 @@ void StepStmt() {
|
||||
scopes.Push(scope);
|
||||
Stack<Action*> todo;
|
||||
todo.Push(MakeStmtAct(Statement::MakeReturn(
|
||||
stmt->line_num, Expression::MakeTuple(stmt->line_num, {}))));
|
||||
stmt->line_num, Expression::MakeTupleLiteral(stmt->line_num, {}))));
|
||||
todo.Push(MakeStmtAct(stmt->GetContinuation().body));
|
||||
Frame* continuation_frame = new Frame("__continuation", scopes, todo);
|
||||
Address continuation_address = state->heap.AllocateValue(
|
||||
Value::MakeContinuation({continuation_frame}));
|
||||
Value::MakeContinuationValue({continuation_frame}));
|
||||
// Store the continuation's address in the frame.
|
||||
continuation_frame->continuation = continuation_address;
|
||||
// Bind the continuation object to the continuation variable
|
||||
@@ -1247,7 +1259,7 @@ void StepStmt() {
|
||||
// Push an expression statement action to ignore the result
|
||||
// value from the continuation.
|
||||
Action* ignore_result = MakeStmtAct(Statement::MakeExpStmt(
|
||||
stmt->line_num, Expression::MakeTuple(stmt->line_num, {})));
|
||||
stmt->line_num, Expression::MakeTupleLiteral(stmt->line_num, {})));
|
||||
ignore_result->pos = 0;
|
||||
frame->todo.Push(ignore_result);
|
||||
// Push the continuation onto the current stack.
|
||||
@@ -1269,7 +1281,7 @@ void StepStmt() {
|
||||
} while (!paused.back()->IsContinuation());
|
||||
// Update the continuation with the paused stack.
|
||||
state->heap.Write(paused.back()->continuation,
|
||||
Value::MakeContinuation(paused), stmt->line_num);
|
||||
Value::MakeContinuationValue(paused), stmt->line_num);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1277,8 +1289,8 @@ void StepStmt() {
|
||||
auto GetMember(Address a, const std::string& f, int line_num) -> Address {
|
||||
const Value* v = state->heap.Read(a, line_num);
|
||||
switch (v->tag) {
|
||||
case ValKind::StructV: {
|
||||
auto a = FindTupleField(f, v->GetStruct().inits);
|
||||
case ValKind::StructValue: {
|
||||
auto a = FindTupleField(f, v->GetStructValue().inits);
|
||||
if (a == std::nullopt) {
|
||||
std::cerr << "runtime error, member " << f << " not in ";
|
||||
PrintValue(v, std::cerr);
|
||||
@@ -1287,7 +1299,7 @@ auto GetMember(Address a, const std::string& f, int line_num) -> Address {
|
||||
}
|
||||
return *a;
|
||||
}
|
||||
case ValKind::TupleV: {
|
||||
case ValKind::TupleValue: {
|
||||
auto a = FindTupleField(f, v);
|
||||
if (a == std::nullopt) {
|
||||
std::cerr << "field " << f << " not in ";
|
||||
@@ -1297,14 +1309,15 @@ auto GetMember(Address a, const std::string& f, int line_num) -> Address {
|
||||
}
|
||||
return *a;
|
||||
}
|
||||
case ValKind::ChoiceTV: {
|
||||
case ValKind::ChoiceType: {
|
||||
if (FindInVarValues(f, v->GetChoiceType().alternatives) == nullptr) {
|
||||
std::cerr << "alternative " << f << " not in ";
|
||||
PrintValue(v, std::cerr);
|
||||
std::cerr << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
auto ac = Value::MakeAltCons(f, *v->GetChoiceType().name);
|
||||
auto ac =
|
||||
Value::MakeAlternativeConstructorValue(f, *v->GetChoiceType().name);
|
||||
return state->heap.AllocateValue(ac);
|
||||
}
|
||||
default:
|
||||
@@ -1361,7 +1374,7 @@ void Step() {
|
||||
auto del = MakeDeleteAct(a);
|
||||
frame->todo.Pop(1);
|
||||
InsertDelete(del, frame->todo);
|
||||
frame->todo.Push(MakeValAct(Value::MakePtrVal(a)));
|
||||
frame->todo.Push(MakeValAct(Value::MakePointerValue(a)));
|
||||
break;
|
||||
}
|
||||
case ActionKind::ValAction: {
|
||||
@@ -1390,9 +1403,9 @@ auto InterpProgram(std::list<Declaration>* fs) -> int {
|
||||
}
|
||||
InitGlobals(fs);
|
||||
|
||||
const Expression* arg = Expression::MakeTuple(0, {});
|
||||
const Expression* call_main =
|
||||
Expression::MakeCall(0, Expression::MakeVar(0, "main"), arg);
|
||||
const Expression* arg = Expression::MakeTupleLiteral(0, {});
|
||||
const Expression* call_main = Expression::MakeCallExpression(
|
||||
0, Expression::MakeIdentifierExpression(0, "main"), arg);
|
||||
auto todo = Stack(MakeExpAct(call_main));
|
||||
auto* scope = new Scope(globals, std::list<std::string>());
|
||||
auto* frame = new Frame("top", Stack(scope), todo);
|
||||
|
||||
@@ -32,7 +32,7 @@ void ExpectType(int line_num, const std::string& context, const Value* expected,
|
||||
|
||||
void ExpectPointerType(int line_num, const std::string& context,
|
||||
const Value* actual) {
|
||||
if (actual->tag != ValKind::PointerTV) {
|
||||
if (actual->tag != ValKind::PointerType) {
|
||||
std::cerr << line_num << ": type error in " << context << std::endl;
|
||||
std::cerr << "expected a pointer type\n";
|
||||
std::cerr << "actual: ";
|
||||
@@ -56,35 +56,35 @@ void PrintTypeEnv(TypeEnv types, std::ostream& out) {
|
||||
auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
switch (t->tag) {
|
||||
case ValKind::VarTV:
|
||||
return Expression::MakeVar(0, *t->GetVariableType());
|
||||
case ValKind::IntTV:
|
||||
return Expression::MakeIntType(0);
|
||||
case ValKind::BoolTV:
|
||||
return Expression::MakeBoolType(0);
|
||||
case ValKind::TypeTV:
|
||||
return Expression::MakeTypeType(0);
|
||||
case ValKind::ContinuationTV:
|
||||
return Expression::MakeContinuationType(0);
|
||||
case ValKind::FunctionTV:
|
||||
return Expression::MakeFunType(
|
||||
return Expression::MakeIdentifierExpression(0, *t->GetVariableType());
|
||||
case ValKind::IntType:
|
||||
return Expression::MakeIntTypeLiteral(0);
|
||||
case ValKind::BoolType:
|
||||
return Expression::MakeBoolTypeLiteral(0);
|
||||
case ValKind::TypeType:
|
||||
return Expression::MakeTypeTypeLiteral(0);
|
||||
case ValKind::ContinuationType:
|
||||
return Expression::MakeContinuationTypeLiteral(0);
|
||||
case ValKind::FunctionType:
|
||||
return Expression::MakeFunctionTypeLiteral(
|
||||
0, ReifyType(t->GetFunctionType().param, line_num),
|
||||
ReifyType(t->GetFunctionType().ret, line_num));
|
||||
case ValKind::TupleV: {
|
||||
case ValKind::TupleValue: {
|
||||
std::vector<FieldInitializer> args;
|
||||
for (const TupleElement& field : *t->GetTuple().elements) {
|
||||
for (const TupleElement& field : *t->GetTupleValue().elements) {
|
||||
args.push_back(
|
||||
{.name = field.name,
|
||||
.expression = ReifyType(state->heap.Read(field.address, line_num),
|
||||
line_num)});
|
||||
}
|
||||
return Expression::MakeTuple(0, args);
|
||||
return Expression::MakeTupleLiteral(0, args);
|
||||
}
|
||||
case ValKind::StructTV:
|
||||
return Expression::MakeVar(0, *t->GetStructType().name);
|
||||
case ValKind::ChoiceTV:
|
||||
return Expression::MakeVar(0, *t->GetChoiceType().name);
|
||||
case ValKind::PointerTV:
|
||||
return Expression::MakeOp(
|
||||
case ValKind::StructType:
|
||||
return Expression::MakeIdentifierExpression(0, *t->GetStructType().name);
|
||||
case ValKind::ChoiceType:
|
||||
return Expression::MakeIdentifierExpression(0, *t->GetChoiceType().name);
|
||||
case ValKind::PointerType:
|
||||
return Expression::MakePrimitiveOperatorExpression(
|
||||
0, Operator::Ptr, {ReifyType(t->GetPointerType().type, line_num)});
|
||||
default:
|
||||
std::cerr << line_num << ": expected a type, not ";
|
||||
@@ -135,7 +135,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
std::cout << std::endl;
|
||||
}
|
||||
switch (e->tag()) {
|
||||
case ExpressionKind::PatternVariable: {
|
||||
case ExpressionKind::PatternVariableExpression: {
|
||||
if (context != TCContext::PatternContext) {
|
||||
std::cerr
|
||||
<< e->line_num
|
||||
@@ -144,8 +144,8 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
auto t = InterpExp(values, e->GetPatternVariable().type);
|
||||
if (t->tag == ValKind::AutoTV) {
|
||||
auto t = InterpExp(values, e->GetPatternVariableExpression().type);
|
||||
if (t->tag == ValKind::AutoType) {
|
||||
if (expected == nullptr) {
|
||||
std::cerr << e->line_num
|
||||
<< ": compilation error, auto not allowed here"
|
||||
@@ -157,18 +157,19 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
} else if (expected) {
|
||||
ExpectType(e->line_num, "pattern variable", t, expected);
|
||||
}
|
||||
auto new_e = Expression::MakeVarPat(
|
||||
e->line_num, e->GetPatternVariable().name, ReifyType(t, e->line_num));
|
||||
types.Set(e->GetPatternVariable().name, t);
|
||||
auto new_e = Expression::MakePatternVariableExpression(
|
||||
e->line_num, e->GetPatternVariableExpression().name,
|
||||
ReifyType(t, e->line_num));
|
||||
types.Set(e->GetPatternVariableExpression().name, t);
|
||||
return TCResult(new_e, t, types);
|
||||
}
|
||||
case ExpressionKind::Index: {
|
||||
auto res = TypeCheckExp(e->GetIndex().aggregate, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
case ExpressionKind::IndexExpression: {
|
||||
auto res = TypeCheckExp(e->GetIndexExpression().aggregate, types, values,
|
||||
nullptr, TCContext::ValueContext);
|
||||
auto t = res.type;
|
||||
switch (t->tag) {
|
||||
case ValKind::TupleV: {
|
||||
auto i = ToInteger(InterpExp(values, e->GetIndex().offset));
|
||||
case ValKind::TupleValue: {
|
||||
auto i = ToInteger(InterpExp(values, e->GetIndexExpression().offset));
|
||||
std::string f = std::to_string(i);
|
||||
std::optional<Address> field_address = FindTupleField(f, t);
|
||||
if (field_address == std::nullopt) {
|
||||
@@ -179,8 +180,8 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
exit(-1);
|
||||
}
|
||||
auto field_t = state->heap.Read(*field_address, e->line_num);
|
||||
auto new_e = Expression::MakeIndex(
|
||||
e->line_num, res.exp, Expression::MakeInt(e->line_num, i));
|
||||
auto new_e = Expression::MakeIndexExpression(
|
||||
e->line_num, res.exp, Expression::MakeIntLiteral(e->line_num, i));
|
||||
return TCResult(new_e, field_t, res.types);
|
||||
}
|
||||
default:
|
||||
@@ -189,36 +190,37 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
case ExpressionKind::Tuple: {
|
||||
case ExpressionKind::TupleLiteral: {
|
||||
std::vector<FieldInitializer> new_args;
|
||||
auto arg_types = new std::vector<TupleElement>();
|
||||
auto new_types = types;
|
||||
if (expected && expected->tag != ValKind::TupleV) {
|
||||
if (expected && expected->tag != ValKind::TupleValue) {
|
||||
std::cerr << e->line_num << ": compilation error, didn't expect a tuple"
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
if (expected && e->GetTuple().fields.size() !=
|
||||
expected->GetTuple().elements->size()) {
|
||||
if (expected && e->GetTupleLiteral().fields.size() !=
|
||||
expected->GetTupleValue().elements->size()) {
|
||||
std::cerr << e->line_num
|
||||
<< ": compilation error, tuples of different length"
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
int i = 0;
|
||||
for (auto arg = e->GetTuple().fields.begin();
|
||||
arg != e->GetTuple().fields.end(); ++arg, ++i) {
|
||||
for (auto arg = e->GetTupleLiteral().fields.begin();
|
||||
arg != e->GetTupleLiteral().fields.end(); ++arg, ++i) {
|
||||
const Value* arg_expected = nullptr;
|
||||
if (expected && expected->tag == ValKind::TupleV) {
|
||||
if ((*expected->GetTuple().elements)[i].name != arg->name) {
|
||||
if (expected && expected->tag == ValKind::TupleValue) {
|
||||
if ((*expected->GetTupleValue().elements)[i].name != arg->name) {
|
||||
std::cerr << e->line_num
|
||||
<< ": compilation error, field names do not match, "
|
||||
<< "expected " << (*expected->GetTuple().elements)[i].name
|
||||
<< "expected "
|
||||
<< (*expected->GetTupleValue().elements)[i].name
|
||||
<< " but got " << arg->name << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
arg_expected = state->heap.Read(
|
||||
(*expected->GetTuple().elements)[i].address, e->line_num);
|
||||
(*expected->GetTupleValue().elements)[i].address, e->line_num);
|
||||
}
|
||||
auto arg_res = TypeCheckExp(arg->expression, new_types, values,
|
||||
arg_expected, context);
|
||||
@@ -228,42 +230,42 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
{.name = arg->name,
|
||||
.address = state->heap.AllocateValue(arg_res.type)});
|
||||
}
|
||||
auto tuple_e = Expression::MakeTuple(e->line_num, new_args);
|
||||
auto tuple_t = Value::MakeTupleVal(arg_types);
|
||||
auto tuple_e = Expression::MakeTupleLiteral(e->line_num, new_args);
|
||||
auto tuple_t = Value::MakeTupleValue(arg_types);
|
||||
return TCResult(tuple_e, tuple_t, new_types);
|
||||
}
|
||||
case ExpressionKind::GetField: {
|
||||
auto res = TypeCheckExp(e->GetFieldAccess().aggregate, types, values,
|
||||
nullptr, TCContext::ValueContext);
|
||||
case ExpressionKind::FieldAccessExpression: {
|
||||
auto res = TypeCheckExp(e->GetFieldAccessExpression().aggregate, types,
|
||||
values, nullptr, TCContext::ValueContext);
|
||||
auto t = res.type;
|
||||
switch (t->tag) {
|
||||
case ValKind::StructTV:
|
||||
case ValKind::StructType:
|
||||
// Search for a field
|
||||
for (auto& field : *t->GetStructType().fields) {
|
||||
if (e->GetFieldAccess().field == field.first) {
|
||||
const Expression* new_e = Expression::MakeGetField(
|
||||
e->line_num, res.exp, e->GetFieldAccess().field);
|
||||
if (e->GetFieldAccessExpression().field == field.first) {
|
||||
const Expression* new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
return TCResult(new_e, field.second, res.types);
|
||||
}
|
||||
}
|
||||
// Search for a method
|
||||
for (auto& method : *t->GetStructType().methods) {
|
||||
if (e->GetFieldAccess().field == method.first) {
|
||||
const Expression* new_e = Expression::MakeGetField(
|
||||
e->line_num, res.exp, e->GetFieldAccess().field);
|
||||
if (e->GetFieldAccessExpression().field == method.first) {
|
||||
const Expression* new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
return TCResult(new_e, method.second, res.types);
|
||||
}
|
||||
}
|
||||
std::cerr << e->line_num << ": compilation error, struct "
|
||||
<< *t->GetStructType().name
|
||||
<< " does not have a field named "
|
||||
<< e->GetFieldAccess().field << std::endl;
|
||||
<< e->GetFieldAccessExpression().field << std::endl;
|
||||
exit(-1);
|
||||
case ValKind::TupleV:
|
||||
for (const TupleElement& field : *t->GetTuple().elements) {
|
||||
if (e->GetFieldAccess().field == field.name) {
|
||||
auto new_e = Expression::MakeGetField(e->line_num, res.exp,
|
||||
e->GetFieldAccess().field);
|
||||
case ValKind::TupleValue:
|
||||
for (const TupleElement& field : *t->GetTupleValue().elements) {
|
||||
if (e->GetFieldAccessExpression().field == field.name) {
|
||||
auto new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
return TCResult(new_e,
|
||||
state->heap.Read(field.address, e->line_num),
|
||||
res.types);
|
||||
@@ -272,22 +274,22 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
std::cerr << e->line_num << ": compilation error, struct "
|
||||
<< *t->GetStructType().name
|
||||
<< " does not have a field named "
|
||||
<< e->GetFieldAccess().field << std::endl;
|
||||
<< e->GetFieldAccessExpression().field << std::endl;
|
||||
exit(-1);
|
||||
case ValKind::ChoiceTV:
|
||||
case ValKind::ChoiceType:
|
||||
for (auto vt = t->GetChoiceType().alternatives->begin();
|
||||
vt != t->GetChoiceType().alternatives->end(); ++vt) {
|
||||
if (e->GetFieldAccess().field == vt->first) {
|
||||
const Expression* new_e = Expression::MakeGetField(
|
||||
e->line_num, res.exp, e->GetFieldAccess().field);
|
||||
auto fun_ty = Value::MakeFunTypeVal(vt->second, t);
|
||||
if (e->GetFieldAccessExpression().field == vt->first) {
|
||||
const Expression* new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
auto fun_ty = Value::MakeFunctionType(vt->second, t);
|
||||
return TCResult(new_e, fun_ty, res.types);
|
||||
}
|
||||
}
|
||||
std::cerr << e->line_num << ": compilation error, struct "
|
||||
<< *t->GetStructType().name
|
||||
<< " does not have a field named "
|
||||
<< e->GetFieldAccess().field << std::endl;
|
||||
<< e->GetFieldAccessExpression().field << std::endl;
|
||||
exit(-1);
|
||||
|
||||
default:
|
||||
@@ -299,91 +301,91 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
case ExpressionKind::Variable: {
|
||||
std::optional<const Value*> type = types.Get(e->GetVariable().name);
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
std::optional<const Value*> type =
|
||||
types.Get(e->GetIdentifierExpression().name);
|
||||
if (type) {
|
||||
return TCResult(e, *type, types);
|
||||
} else {
|
||||
std::cerr << e->line_num << ": could not find `"
|
||||
<< e->GetVariable().name << "`" << std::endl;
|
||||
<< e->GetIdentifierExpression().name << "`" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
case ExpressionKind::Integer:
|
||||
return TCResult(e, Value::MakeIntTypeVal(), types);
|
||||
case ExpressionKind::Boolean:
|
||||
return TCResult(e, Value::MakeBoolTypeVal(), types);
|
||||
case ExpressionKind::PrimitiveOp: {
|
||||
case ExpressionKind::IntLiteral:
|
||||
return TCResult(e, Value::MakeIntType(), types);
|
||||
case ExpressionKind::BoolLiteral:
|
||||
return TCResult(e, Value::MakeBoolType(), types);
|
||||
case ExpressionKind::PrimitiveOperatorExpression: {
|
||||
std::vector<const Expression*> es;
|
||||
std::vector<const Value*> ts;
|
||||
auto new_types = types;
|
||||
for (const Expression* argument : e->GetPrimitiveOperator().arguments) {
|
||||
for (const Expression* argument :
|
||||
e->GetPrimitiveOperatorExpression().arguments) {
|
||||
auto res = TypeCheckExp(argument, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
new_types = res.types;
|
||||
es.push_back(res.exp);
|
||||
ts.push_back(res.type);
|
||||
}
|
||||
auto new_e =
|
||||
Expression::MakeOp(e->line_num, e->GetPrimitiveOperator().op, es);
|
||||
switch (e->GetPrimitiveOperator().op) {
|
||||
auto new_e = Expression::MakePrimitiveOperatorExpression(
|
||||
e->line_num, e->GetPrimitiveOperatorExpression().op, es);
|
||||
switch (e->GetPrimitiveOperatorExpression().op) {
|
||||
case Operator::Neg:
|
||||
ExpectType(e->line_num, "negation", Value::MakeIntTypeVal(), ts[0]);
|
||||
return TCResult(new_e, Value::MakeIntTypeVal(), new_types);
|
||||
ExpectType(e->line_num, "negation", Value::MakeIntType(), ts[0]);
|
||||
return TCResult(new_e, Value::MakeIntType(), new_types);
|
||||
case Operator::Add:
|
||||
ExpectType(e->line_num, "addition(1)", Value::MakeIntTypeVal(),
|
||||
ts[0]);
|
||||
ExpectType(e->line_num, "addition(2)", Value::MakeIntTypeVal(),
|
||||
ts[1]);
|
||||
return TCResult(new_e, Value::MakeIntTypeVal(), new_types);
|
||||
ExpectType(e->line_num, "addition(1)", Value::MakeIntType(), ts[0]);
|
||||
ExpectType(e->line_num, "addition(2)", Value::MakeIntType(), ts[1]);
|
||||
return TCResult(new_e, Value::MakeIntType(), new_types);
|
||||
case Operator::Sub:
|
||||
ExpectType(e->line_num, "subtraction(1)", Value::MakeIntTypeVal(),
|
||||
ExpectType(e->line_num, "subtraction(1)", Value::MakeIntType(),
|
||||
ts[0]);
|
||||
ExpectType(e->line_num, "subtraction(2)", Value::MakeIntTypeVal(),
|
||||
ExpectType(e->line_num, "subtraction(2)", Value::MakeIntType(),
|
||||
ts[1]);
|
||||
return TCResult(new_e, Value::MakeIntTypeVal(), new_types);
|
||||
return TCResult(new_e, Value::MakeIntType(), new_types);
|
||||
case Operator::Mul:
|
||||
ExpectType(e->line_num, "multiplication(1)", Value::MakeIntTypeVal(),
|
||||
ExpectType(e->line_num, "multiplication(1)", Value::MakeIntType(),
|
||||
ts[0]);
|
||||
ExpectType(e->line_num, "multiplication(2)", Value::MakeIntTypeVal(),
|
||||
ExpectType(e->line_num, "multiplication(2)", Value::MakeIntType(),
|
||||
ts[1]);
|
||||
return TCResult(new_e, Value::MakeIntTypeVal(), new_types);
|
||||
return TCResult(new_e, Value::MakeIntType(), new_types);
|
||||
case Operator::And:
|
||||
ExpectType(e->line_num, "&&(1)", Value::MakeBoolTypeVal(), ts[0]);
|
||||
ExpectType(e->line_num, "&&(2)", Value::MakeBoolTypeVal(), ts[1]);
|
||||
return TCResult(new_e, Value::MakeBoolTypeVal(), new_types);
|
||||
ExpectType(e->line_num, "&&(1)", Value::MakeBoolType(), ts[0]);
|
||||
ExpectType(e->line_num, "&&(2)", Value::MakeBoolType(), ts[1]);
|
||||
return TCResult(new_e, Value::MakeBoolType(), new_types);
|
||||
case Operator::Or:
|
||||
ExpectType(e->line_num, "||(1)", Value::MakeBoolTypeVal(), ts[0]);
|
||||
ExpectType(e->line_num, "||(2)", Value::MakeBoolTypeVal(), ts[1]);
|
||||
return TCResult(new_e, Value::MakeBoolTypeVal(), new_types);
|
||||
ExpectType(e->line_num, "||(1)", Value::MakeBoolType(), ts[0]);
|
||||
ExpectType(e->line_num, "||(2)", Value::MakeBoolType(), ts[1]);
|
||||
return TCResult(new_e, Value::MakeBoolType(), new_types);
|
||||
case Operator::Not:
|
||||
ExpectType(e->line_num, "!", Value::MakeBoolTypeVal(), ts[0]);
|
||||
return TCResult(new_e, Value::MakeBoolTypeVal(), new_types);
|
||||
ExpectType(e->line_num, "!", Value::MakeBoolType(), ts[0]);
|
||||
return TCResult(new_e, Value::MakeBoolType(), new_types);
|
||||
case Operator::Eq:
|
||||
ExpectType(e->line_num, "==", ts[0], ts[1]);
|
||||
return TCResult(new_e, Value::MakeBoolTypeVal(), new_types);
|
||||
return TCResult(new_e, Value::MakeBoolType(), new_types);
|
||||
case Operator::Deref:
|
||||
ExpectPointerType(e->line_num, "*", ts[0]);
|
||||
return TCResult(new_e, ts[0]->GetPointerType().type, new_types);
|
||||
case Operator::Ptr:
|
||||
ExpectType(e->line_num, "*", Value::MakeTypeTypeVal(), ts[0]);
|
||||
return TCResult(new_e, Value::MakeTypeTypeVal(), new_types);
|
||||
ExpectType(e->line_num, "*", Value::MakeTypeType(), ts[0]);
|
||||
return TCResult(new_e, Value::MakeTypeType(), new_types);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Call: {
|
||||
auto fun_res = TypeCheckExp(e->GetCall().function, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
case ExpressionKind::CallExpression: {
|
||||
auto fun_res = TypeCheckExp(e->GetCallExpression().function, types,
|
||||
values, nullptr, TCContext::ValueContext);
|
||||
switch (fun_res.type->tag) {
|
||||
case ValKind::FunctionTV: {
|
||||
case ValKind::FunctionType: {
|
||||
auto fun_t = fun_res.type;
|
||||
auto arg_res =
|
||||
TypeCheckExp(e->GetCall().argument, fun_res.types, values,
|
||||
fun_t->GetFunctionType().param, context);
|
||||
TypeCheckExp(e->GetCallExpression().argument, fun_res.types,
|
||||
values, fun_t->GetFunctionType().param, context);
|
||||
ExpectType(e->line_num, "call", fun_t->GetFunctionType().param,
|
||||
arg_res.type);
|
||||
auto new_e =
|
||||
Expression::MakeCall(e->line_num, fun_res.exp, arg_res.exp);
|
||||
auto new_e = Expression::MakeCallExpression(e->line_num, fun_res.exp,
|
||||
arg_res.exp);
|
||||
return TCResult(new_e, fun_t->GetFunctionType().ret, arg_res.types);
|
||||
}
|
||||
default: {
|
||||
@@ -397,40 +399,40 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::FunctionT: {
|
||||
case ExpressionKind::FunctionTypeLiteral: {
|
||||
switch (context) {
|
||||
case TCContext::ValueContext:
|
||||
case TCContext::TypeContext: {
|
||||
auto pt = InterpExp(values, e->GetFunctionType().parameter);
|
||||
auto rt = InterpExp(values, e->GetFunctionType().return_type);
|
||||
auto new_e =
|
||||
Expression::MakeFunType(e->line_num, ReifyType(pt, e->line_num),
|
||||
ReifyType(rt, e->line_num));
|
||||
return TCResult(new_e, Value::MakeTypeTypeVal(), types);
|
||||
auto pt = InterpExp(values, e->GetFunctionTypeLiteral().parameter);
|
||||
auto rt = InterpExp(values, e->GetFunctionTypeLiteral().return_type);
|
||||
auto new_e = Expression::MakeFunctionTypeLiteral(
|
||||
e->line_num, ReifyType(pt, e->line_num),
|
||||
ReifyType(rt, e->line_num));
|
||||
return TCResult(new_e, Value::MakeTypeType(), types);
|
||||
}
|
||||
case TCContext::PatternContext: {
|
||||
auto param_res = TypeCheckExp(e->GetFunctionType().parameter, types,
|
||||
values, nullptr, context);
|
||||
auto param_res = TypeCheckExp(e->GetFunctionTypeLiteral().parameter,
|
||||
types, values, nullptr, context);
|
||||
auto ret_res =
|
||||
TypeCheckExp(e->GetFunctionType().return_type, param_res.types,
|
||||
values, nullptr, context);
|
||||
auto new_e = Expression::MakeFunType(
|
||||
TypeCheckExp(e->GetFunctionTypeLiteral().return_type,
|
||||
param_res.types, values, nullptr, context);
|
||||
auto new_e = Expression::MakeFunctionTypeLiteral(
|
||||
e->line_num, ReifyType(param_res.type, e->line_num),
|
||||
ReifyType(ret_res.type, e->line_num));
|
||||
return TCResult(new_e, Value::MakeTypeTypeVal(), ret_res.types);
|
||||
return TCResult(new_e, Value::MakeTypeType(), ret_res.types);
|
||||
}
|
||||
}
|
||||
}
|
||||
case ExpressionKind::IntT:
|
||||
return TCResult(e, Value::MakeTypeTypeVal(), types);
|
||||
case ExpressionKind::BoolT:
|
||||
return TCResult(e, Value::MakeTypeTypeVal(), types);
|
||||
case ExpressionKind::TypeT:
|
||||
return TCResult(e, Value::MakeTypeTypeVal(), types);
|
||||
case ExpressionKind::AutoT:
|
||||
return TCResult(e, Value::MakeTypeTypeVal(), types);
|
||||
case ExpressionKind::ContinuationT:
|
||||
return TCResult(e, Value::MakeTypeTypeVal(), types);
|
||||
case ExpressionKind::IntTypeLiteral:
|
||||
return TCResult(e, Value::MakeTypeType(), types);
|
||||
case ExpressionKind::BoolTypeLiteral:
|
||||
return TCResult(e, Value::MakeTypeType(), types);
|
||||
case ExpressionKind::TypeTypeLiteral:
|
||||
return TCResult(e, Value::MakeTypeType(), types);
|
||||
case ExpressionKind::AutoTypeLiteral:
|
||||
return TCResult(e, Value::MakeTypeType(), types);
|
||||
case ExpressionKind::ContinuationTypeLiteral:
|
||||
return TCResult(e, Value::MakeTypeType(), types);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -474,7 +476,7 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
case StatementKind::While: {
|
||||
auto cnd_res = TypeCheckExp(s->GetWhile().cond, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
ExpectType(s->line_num, "condition of `while`", Value::MakeBoolTypeVal(),
|
||||
ExpectType(s->line_num, "condition of `while`", Value::MakeBoolType(),
|
||||
cnd_res.type);
|
||||
auto body_res =
|
||||
TypeCheckStmt(s->GetWhile().body, types, values, ret_type);
|
||||
@@ -532,7 +534,7 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
case StatementKind::If: {
|
||||
auto cnd_res = TypeCheckExp(s->GetIf().cond, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
ExpectType(s->line_num, "condition of `if`", Value::MakeBoolTypeVal(),
|
||||
ExpectType(s->line_num, "condition of `if`", Value::MakeBoolType(),
|
||||
cnd_res.type);
|
||||
auto thn_res =
|
||||
TypeCheckStmt(s->GetIf().then_stmt, types, values, ret_type);
|
||||
@@ -545,7 +547,7 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
case StatementKind::Return: {
|
||||
auto res = TypeCheckExp(s->GetReturn(), types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
if (ret_type->tag == ValKind::AutoTV) {
|
||||
if (ret_type->tag == ValKind::AutoType) {
|
||||
// The following infers the return type from the first 'return'
|
||||
// statement. This will get more difficult with subtyping, when we
|
||||
// should infer the least-upper bound of all the 'return' statements.
|
||||
@@ -562,7 +564,7 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
s->line_num, *s->GetContinuation().continuation_variable,
|
||||
body_result.stmt);
|
||||
types.Set(*s->GetContinuation().continuation_variable,
|
||||
Value::MakeContinuationTypeVal());
|
||||
Value::MakeContinuationType());
|
||||
return TCStatement(new_continuation, types);
|
||||
}
|
||||
case StatementKind::Run: {
|
||||
@@ -570,7 +572,7 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
TypeCheckExp(s->GetRun().argument, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
ExpectType(s->line_num, "argument of `run`",
|
||||
Value::MakeContinuationTypeVal(), argument_result.type);
|
||||
Value::MakeContinuationType(), argument_result.type);
|
||||
const Statement* new_run =
|
||||
Statement::MakeRun(s->line_num, argument_result.exp);
|
||||
return TCStatement(new_run, types);
|
||||
@@ -587,7 +589,7 @@ auto CheckOrEnsureReturn(const Statement* stmt, bool void_return, int line_num)
|
||||
if (!stmt) {
|
||||
if (void_return) {
|
||||
return Statement::MakeReturn(line_num,
|
||||
Expression::MakeTuple(line_num, {}));
|
||||
Expression::MakeTupleLiteral(line_num, {}));
|
||||
} else {
|
||||
std::cerr
|
||||
<< "control-flow reaches end of non-void function without a return"
|
||||
@@ -643,8 +645,8 @@ auto CheckOrEnsureReturn(const Statement* stmt, bool void_return, int line_num)
|
||||
if (void_return) {
|
||||
return Statement::MakeSeq(
|
||||
stmt->line_num, stmt,
|
||||
Statement::MakeReturn(stmt->line_num,
|
||||
Expression::MakeTuple(stmt->line_num, {})));
|
||||
Statement::MakeReturn(stmt->line_num, Expression::MakeTupleLiteral(
|
||||
stmt->line_num, {})));
|
||||
} else {
|
||||
std::cerr
|
||||
<< stmt->line_num
|
||||
@@ -662,7 +664,7 @@ auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types, Env values)
|
||||
TCContext::PatternContext);
|
||||
auto return_type = InterpExp(values, f->return_type);
|
||||
if (f->name == "main") {
|
||||
ExpectType(f->line_num, "return type of `main`", Value::MakeIntTypeVal(),
|
||||
ExpectType(f->line_num, "return type of `main`", Value::MakeIntType(),
|
||||
return_type);
|
||||
// TODO: Check that main doesn't have any parameters.
|
||||
}
|
||||
@@ -679,11 +681,11 @@ auto TypeOfFunDef(TypeEnv types, Env values, const FunctionDefinition* fun_def)
|
||||
auto param_res = TypeCheckExp(fun_def->param_pattern, types, values, nullptr,
|
||||
TCContext::PatternContext);
|
||||
auto ret = InterpExp(values, fun_def->return_type);
|
||||
if (ret->tag == ValKind::AutoTV) {
|
||||
if (ret->tag == ValKind::AutoType) {
|
||||
auto f = TypeCheckFunDef(fun_def, types, values);
|
||||
ret = InterpExp(values, f->return_type);
|
||||
}
|
||||
return Value::MakeFunTypeVal(param_res.type, ret);
|
||||
return Value::MakeFunctionType(param_res.type, ret);
|
||||
}
|
||||
|
||||
auto TypeOfStructDef(const StructDefinition* sd, TypeEnv /*types*/, Env ct_top)
|
||||
@@ -696,7 +698,7 @@ auto TypeOfStructDef(const StructDefinition* sd, TypeEnv /*types*/, Env ct_top)
|
||||
fields->push_back(std::make_pair(*(*m)->u.field.name, t));
|
||||
}
|
||||
}
|
||||
return Value::MakeStructTypeVal(*sd->name, fields, methods);
|
||||
return Value::MakeStructType(*sd->name, fields, methods);
|
||||
}
|
||||
|
||||
auto FunctionDeclaration::Name() const -> std::string {
|
||||
@@ -779,7 +781,7 @@ auto StructDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
|
||||
field_types->push_back({.name = field_name,
|
||||
.address = state->heap.AllocateValue(field_value)});
|
||||
}
|
||||
auto fun_ty = Value::MakeFunTypeVal(Value::MakeTupleVal(field_types), st);
|
||||
auto fun_ty = Value::MakeFunctionType(Value::MakeTupleValue(field_types), st);
|
||||
tops.types.Set(Name(), fun_ty);
|
||||
}
|
||||
|
||||
@@ -789,7 +791,7 @@ auto ChoiceDeclaration::TopLevel(TypeCheckContext& tops) const -> void {
|
||||
auto t = InterpExp(tops.values, signature);
|
||||
alts->push_back(std::make_pair(name, t));
|
||||
}
|
||||
auto ct = Value::MakeChoiceTypeVal(name, alts);
|
||||
auto ct = Value::MakeChoiceType(name, alts);
|
||||
Address a = state->heap.AllocateValue(ct);
|
||||
tops.values.Set(Name(), a); // Is this obsolete?
|
||||
tops.types.Set(Name(), ct);
|
||||
|
||||
@@ -12,43 +12,43 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
int Value::GetInteger() const {
|
||||
CHECK(tag == ValKind::IntV);
|
||||
int Value::GetIntValue() const {
|
||||
CHECK(tag == ValKind::IntValue);
|
||||
return u.integer;
|
||||
}
|
||||
|
||||
bool Value::GetBoolean() const {
|
||||
CHECK(tag == ValKind::BoolV);
|
||||
bool Value::GetBoolValue() const {
|
||||
CHECK(tag == ValKind::BoolValue);
|
||||
return u.boolean;
|
||||
}
|
||||
|
||||
Function Value::GetFunction() const {
|
||||
CHECK(tag == ValKind::FunV);
|
||||
FunctionValue Value::GetFunctionValue() const {
|
||||
CHECK(tag == ValKind::FunctionValue);
|
||||
return u.fun;
|
||||
}
|
||||
|
||||
StructConstructor Value::GetStruct() const {
|
||||
CHECK(tag == ValKind::StructV);
|
||||
StructValue Value::GetStructValue() const {
|
||||
CHECK(tag == ValKind::StructValue);
|
||||
return u.struct_val;
|
||||
}
|
||||
|
||||
AlternativeConstructor Value::GetAlternativeConstructor() const {
|
||||
CHECK(tag == ValKind::AltConsV);
|
||||
AlternativeConstructorValue Value::GetAlternativeConstructorValue() const {
|
||||
CHECK(tag == ValKind::AlternativeConstructorValue);
|
||||
return u.alt_cons;
|
||||
}
|
||||
|
||||
Alternative Value::GetAlternative() const {
|
||||
CHECK(tag == ValKind::AltV);
|
||||
AlternativeValue Value::GetAlternativeValue() const {
|
||||
CHECK(tag == ValKind::AlternativeValue);
|
||||
return u.alt;
|
||||
}
|
||||
|
||||
TupleValue Value::GetTuple() const {
|
||||
CHECK(tag == ValKind::TupleV);
|
||||
TupleValue Value::GetTupleValue() const {
|
||||
CHECK(tag == ValKind::TupleValue);
|
||||
return u.tuple;
|
||||
}
|
||||
|
||||
Address Value::GetPointer() const {
|
||||
CHECK(tag == ValKind::PtrV);
|
||||
Address Value::GetPointerValue() const {
|
||||
CHECK(tag == ValKind::PointerValue);
|
||||
return u.ptr;
|
||||
}
|
||||
|
||||
@@ -57,33 +57,33 @@ std::string* Value::GetVariableType() const {
|
||||
return u.var_type;
|
||||
}
|
||||
|
||||
VariablePatternValue Value::GetVariablePattern() const {
|
||||
CHECK(tag == ValKind::VarPatV);
|
||||
PatternVariableValue Value::GetPatternVariableValue() const {
|
||||
CHECK(tag == ValKind::PatternVariableValue);
|
||||
return u.var_pat;
|
||||
}
|
||||
|
||||
FunctionTypeValue Value::GetFunctionType() const {
|
||||
CHECK(tag == ValKind::FunctionTV);
|
||||
FunctionType Value::GetFunctionType() const {
|
||||
CHECK(tag == ValKind::FunctionType);
|
||||
return u.fun_type;
|
||||
}
|
||||
|
||||
PointerType Value::GetPointerType() const {
|
||||
CHECK(tag == ValKind::PointerTV);
|
||||
CHECK(tag == ValKind::PointerType);
|
||||
return u.ptr_type;
|
||||
}
|
||||
|
||||
StructType Value::GetStructType() const {
|
||||
CHECK(tag == ValKind::StructTV);
|
||||
CHECK(tag == ValKind::StructType);
|
||||
return u.struct_type;
|
||||
}
|
||||
|
||||
ChoiceType Value::GetChoiceType() const {
|
||||
CHECK(tag == ValKind::ChoiceTV);
|
||||
CHECK(tag == ValKind::ChoiceType);
|
||||
return u.choice_type;
|
||||
}
|
||||
|
||||
ContinuationValue Value::GetContinuation() const {
|
||||
CHECK(tag == ValKind::ContinuationV);
|
||||
ContinuationValue Value::GetContinuationValue() const {
|
||||
CHECK(tag == ValKind::ContinuationValue);
|
||||
return u.continuation;
|
||||
}
|
||||
|
||||
@@ -116,8 +116,8 @@ auto FieldsEqual(VarValues* ts1, VarValues* ts2) -> bool {
|
||||
|
||||
auto FindTupleField(const std::string& name, const Value* tuple)
|
||||
-> std::optional<Address> {
|
||||
CHECK(tuple->tag == ValKind::TupleV);
|
||||
for (const TupleElement& element : *tuple->GetTuple().elements) {
|
||||
CHECK(tuple->tag == ValKind::TupleValue);
|
||||
for (const TupleElement& element : *tuple->GetTupleValue().elements) {
|
||||
if (element.name == name) {
|
||||
return element.address;
|
||||
}
|
||||
@@ -125,67 +125,69 @@ auto FindTupleField(const std::string& name, const Value* tuple)
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto Value::MakeIntVal(int i) -> const Value* {
|
||||
auto Value::MakeIntValue(int i) -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::IntV;
|
||||
v->tag = ValKind::IntValue;
|
||||
v->u.integer = i;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeBoolVal(bool b) -> const Value* {
|
||||
auto Value::MakeBoolValue(bool b) -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::BoolV;
|
||||
v->tag = ValKind::BoolValue;
|
||||
v->u.boolean = b;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeFunVal(std::string name, const Value* param,
|
||||
const Statement* body) -> const Value* {
|
||||
auto Value::MakeFunctionValue(std::string name, const Value* param,
|
||||
const Statement* body) -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::FunV;
|
||||
v->tag = ValKind::FunctionValue;
|
||||
v->u.fun.name = new std::string(std::move(name));
|
||||
v->u.fun.param = param;
|
||||
v->u.fun.body = body;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakePtrVal(Address addr) -> const Value* {
|
||||
auto Value::MakePointerValue(Address addr) -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::PtrV;
|
||||
v->tag = ValKind::PointerValue;
|
||||
v->u.ptr = addr;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeStructVal(const Value* type, const Value* inits)
|
||||
auto Value::MakeStructValue(const Value* type, const Value* inits)
|
||||
-> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::StructV;
|
||||
v->tag = ValKind::StructValue;
|
||||
v->u.struct_val.type = type;
|
||||
v->u.struct_val.inits = inits;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeTupleVal(std::vector<TupleElement>* elements) -> const Value* {
|
||||
auto Value::MakeTupleValue(std::vector<TupleElement>* elements)
|
||||
-> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::TupleV;
|
||||
v->tag = ValKind::TupleValue;
|
||||
v->u.tuple.elements = elements;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeAltVal(std::string alt_name, std::string choice_name,
|
||||
Address argument) -> const Value* {
|
||||
auto Value::MakeAlternativeValue(std::string alt_name, std::string choice_name,
|
||||
Address argument) -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::AltV;
|
||||
v->tag = ValKind::AlternativeValue;
|
||||
v->u.alt.alt_name = new std::string(std::move(alt_name));
|
||||
v->u.alt.choice_name = new std::string(std::move(choice_name));
|
||||
v->u.alt.argument = argument;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeAltCons(std::string alt_name, std::string choice_name)
|
||||
auto Value::MakeAlternativeConstructorValue(std::string alt_name,
|
||||
std::string choice_name)
|
||||
-> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::AltConsV;
|
||||
v->tag = ValKind::AlternativeConstructorValue;
|
||||
v->u.alt.alt_name = new std::string(std::move(alt_name));
|
||||
v->u.alt.choice_name = new std::string(std::move(choice_name));
|
||||
return v;
|
||||
@@ -193,16 +195,17 @@ auto Value::MakeAltCons(std::string alt_name, std::string choice_name)
|
||||
|
||||
// Return a first-class continuation represented a fragment
|
||||
// of the stack.
|
||||
auto Value::MakeContinuation(std::vector<Frame*> stack) -> Value* {
|
||||
auto Value::MakeContinuationValue(std::vector<Frame*> stack) -> Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::ContinuationV;
|
||||
v->tag = ValKind::ContinuationValue;
|
||||
v->u.continuation.stack = new std::vector<Frame*>(stack);
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeVarPatVal(std::string name, const Value* type) -> const Value* {
|
||||
auto Value::MakePatternVariableValue(std::string name, const Value* type)
|
||||
-> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::VarPatV;
|
||||
v->tag = ValKind::PatternVariableValue;
|
||||
v->u.var_pat.name = new std::string(std::move(name));
|
||||
v->u.var_pat.type = type;
|
||||
return v;
|
||||
@@ -215,57 +218,57 @@ auto Value::MakeVarTypeVal(std::string name) -> const Value* {
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeIntTypeVal() -> const Value* {
|
||||
auto Value::MakeIntType() -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::IntTV;
|
||||
v->tag = ValKind::IntType;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeBoolTypeVal() -> const Value* {
|
||||
auto Value::MakeBoolType() -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::BoolTV;
|
||||
v->tag = ValKind::BoolType;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeTypeTypeVal() -> const Value* {
|
||||
auto Value::MakeTypeType() -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::TypeTV;
|
||||
v->tag = ValKind::TypeType;
|
||||
return v;
|
||||
}
|
||||
|
||||
// Return a Continuation type.
|
||||
auto Value::MakeContinuationTypeVal() -> const Value* {
|
||||
auto Value::MakeContinuationType() -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::ContinuationTV;
|
||||
v->tag = ValKind::ContinuationType;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeAutoTypeVal() -> const Value* {
|
||||
auto Value::MakeAutoType() -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::AutoTV;
|
||||
v->tag = ValKind::AutoType;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeFunTypeVal(const Value* param, const Value* ret)
|
||||
auto Value::MakeFunctionType(const Value* param, const Value* ret)
|
||||
-> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::FunctionTV;
|
||||
v->tag = ValKind::FunctionType;
|
||||
v->u.fun_type.param = param;
|
||||
v->u.fun_type.ret = ret;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakePtrTypeVal(const Value* type) -> const Value* {
|
||||
auto Value::MakePointerType(const Value* type) -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::PointerTV;
|
||||
v->tag = ValKind::PointerType;
|
||||
v->u.ptr_type.type = type;
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeStructTypeVal(std::string name, VarValues* fields,
|
||||
VarValues* methods) -> const Value* {
|
||||
auto Value::MakeStructType(std::string name, VarValues* fields,
|
||||
VarValues* methods) -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::StructTV;
|
||||
v->tag = ValKind::StructType;
|
||||
v->u.struct_type.name = new std::string(std::move(name));
|
||||
v->u.struct_type.fields = fields;
|
||||
v->u.struct_type.methods = methods;
|
||||
@@ -274,16 +277,16 @@ auto Value::MakeStructTypeVal(std::string name, VarValues* fields,
|
||||
|
||||
auto Value::MakeUnitTypeVal() -> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::TupleV;
|
||||
v->tag = ValKind::TupleValue;
|
||||
v->u.tuple.elements = new std::vector<TupleElement>();
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeChoiceTypeVal(
|
||||
auto Value::MakeChoiceType(
|
||||
std::string name, std::list<std::pair<std::string, const Value*>>* alts)
|
||||
-> const Value* {
|
||||
auto* v = new Value();
|
||||
v->tag = ValKind::ChoiceTV;
|
||||
v->tag = ValKind::ChoiceType;
|
||||
// Transitional leak: when we get rid of all pointers, this will disappear.
|
||||
v->u.choice_type.name = new std::string(name);
|
||||
v->u.choice_type.alternatives = alts;
|
||||
@@ -292,31 +295,31 @@ auto Value::MakeChoiceTypeVal(
|
||||
|
||||
auto PrintValue(const Value* val, std::ostream& out) -> void {
|
||||
switch (val->tag) {
|
||||
case ValKind::AltConsV: {
|
||||
out << *val->GetAlternativeConstructor().choice_name << "."
|
||||
<< *val->GetAlternativeConstructor().alt_name;
|
||||
case ValKind::AlternativeConstructorValue: {
|
||||
out << *val->GetAlternativeConstructorValue().choice_name << "."
|
||||
<< *val->GetAlternativeConstructorValue().alt_name;
|
||||
break;
|
||||
}
|
||||
case ValKind::VarPatV: {
|
||||
PrintValue(val->GetVariablePattern().type, out);
|
||||
out << ": " << *val->GetVariablePattern().name;
|
||||
case ValKind::PatternVariableValue: {
|
||||
PrintValue(val->GetPatternVariableValue().type, out);
|
||||
out << ": " << *val->GetPatternVariableValue().name;
|
||||
break;
|
||||
}
|
||||
case ValKind::AltV: {
|
||||
out << "alt " << *val->GetAlternative().choice_name << "."
|
||||
<< *val->GetAlternative().alt_name << " ";
|
||||
state->heap.PrintAddress(val->GetAlternative().argument, out);
|
||||
case ValKind::AlternativeValue: {
|
||||
out << "alt " << *val->GetAlternativeValue().choice_name << "."
|
||||
<< *val->GetAlternativeValue().alt_name << " ";
|
||||
state->heap.PrintAddress(val->GetAlternativeValue().argument, out);
|
||||
break;
|
||||
}
|
||||
case ValKind::StructV: {
|
||||
out << *val->GetStruct().type->GetStructType().name;
|
||||
PrintValue(val->GetStruct().inits, out);
|
||||
case ValKind::StructValue: {
|
||||
out << *val->GetStructValue().type->GetStructType().name;
|
||||
PrintValue(val->GetStructValue().inits, out);
|
||||
break;
|
||||
}
|
||||
case ValKind::TupleV: {
|
||||
case ValKind::TupleValue: {
|
||||
out << "(";
|
||||
bool add_commas = false;
|
||||
for (const TupleElement& element : *val->GetTuple().elements) {
|
||||
for (const TupleElement& element : *val->GetTupleValue().elements) {
|
||||
if (add_commas) {
|
||||
out << ", ";
|
||||
} else {
|
||||
@@ -329,38 +332,38 @@ auto PrintValue(const Value* val, std::ostream& out) -> void {
|
||||
out << ")";
|
||||
break;
|
||||
}
|
||||
case ValKind::IntV:
|
||||
out << val->GetInteger();
|
||||
case ValKind::IntValue:
|
||||
out << val->GetIntValue();
|
||||
break;
|
||||
case ValKind::BoolV:
|
||||
out << std::boolalpha << val->GetBoolean();
|
||||
case ValKind::BoolValue:
|
||||
out << std::boolalpha << val->GetBoolValue();
|
||||
break;
|
||||
case ValKind::FunV:
|
||||
out << "fun<" << *val->GetFunction().name << ">";
|
||||
case ValKind::FunctionValue:
|
||||
out << "fun<" << *val->GetFunctionValue().name << ">";
|
||||
break;
|
||||
case ValKind::PtrV:
|
||||
out << "ptr<" << val->GetPointer() << ">";
|
||||
case ValKind::PointerValue:
|
||||
out << "ptr<" << val->GetPointerValue() << ">";
|
||||
break;
|
||||
case ValKind::BoolTV:
|
||||
case ValKind::BoolType:
|
||||
out << "Bool";
|
||||
break;
|
||||
case ValKind::IntTV:
|
||||
case ValKind::IntType:
|
||||
out << "Int";
|
||||
break;
|
||||
case ValKind::TypeTV:
|
||||
case ValKind::TypeType:
|
||||
out << "Type";
|
||||
break;
|
||||
case ValKind::AutoTV:
|
||||
case ValKind::AutoType:
|
||||
out << "auto";
|
||||
break;
|
||||
case ValKind::ContinuationTV:
|
||||
case ValKind::ContinuationType:
|
||||
out << "Continuation";
|
||||
break;
|
||||
case ValKind::PointerTV:
|
||||
case ValKind::PointerType:
|
||||
PrintValue(val->GetPointerType().type, out);
|
||||
out << "*";
|
||||
break;
|
||||
case ValKind::FunctionTV:
|
||||
case ValKind::FunctionType:
|
||||
out << "fn ";
|
||||
PrintValue(val->GetFunctionType().param, out);
|
||||
out << " -> ";
|
||||
@@ -369,15 +372,15 @@ auto PrintValue(const Value* val, std::ostream& out) -> void {
|
||||
case ValKind::VarTV:
|
||||
out << *val->GetVariableType();
|
||||
break;
|
||||
case ValKind::StructTV:
|
||||
case ValKind::StructType:
|
||||
out << "struct " << *val->GetStructType().name;
|
||||
break;
|
||||
case ValKind::ChoiceTV:
|
||||
case ValKind::ChoiceType:
|
||||
out << "choice " << *val->GetChoiceType().name;
|
||||
break;
|
||||
case ValKind::ContinuationV:
|
||||
case ValKind::ContinuationValue:
|
||||
out << "continuation[[";
|
||||
for (Frame* frame : *val->GetContinuation().stack) {
|
||||
for (Frame* frame : *val->GetContinuationValue().stack) {
|
||||
PrintFrame(frame, out);
|
||||
out << " :: ";
|
||||
}
|
||||
@@ -393,37 +396,39 @@ auto TypeEqual(const Value* t1, const Value* t2) -> bool {
|
||||
switch (t1->tag) {
|
||||
case ValKind::VarTV:
|
||||
return *t1->GetVariableType() == *t2->GetVariableType();
|
||||
case ValKind::PointerTV:
|
||||
case ValKind::PointerType:
|
||||
return TypeEqual(t1->GetPointerType().type, t2->GetPointerType().type);
|
||||
case ValKind::FunctionTV:
|
||||
case ValKind::FunctionType:
|
||||
return TypeEqual(t1->GetFunctionType().param,
|
||||
t2->GetFunctionType().param) &&
|
||||
TypeEqual(t1->GetFunctionType().ret, t2->GetFunctionType().ret);
|
||||
case ValKind::StructTV:
|
||||
case ValKind::StructType:
|
||||
return *t1->GetStructType().name == *t2->GetStructType().name;
|
||||
case ValKind::ChoiceTV:
|
||||
case ValKind::ChoiceType:
|
||||
return *t1->GetChoiceType().name == *t2->GetChoiceType().name;
|
||||
case ValKind::TupleV: {
|
||||
if (t1->GetTuple().elements->size() != t2->GetTuple().elements->size()) {
|
||||
case ValKind::TupleValue: {
|
||||
if (t1->GetTupleValue().elements->size() !=
|
||||
t2->GetTupleValue().elements->size()) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < t1->GetTuple().elements->size(); ++i) {
|
||||
if ((*t1->GetTuple().elements)[i].name !=
|
||||
(*t2->GetTuple().elements)[i].name) {
|
||||
for (size_t i = 0; i < t1->GetTupleValue().elements->size(); ++i) {
|
||||
if ((*t1->GetTupleValue().elements)[i].name !=
|
||||
(*t2->GetTupleValue().elements)[i].name) {
|
||||
return false;
|
||||
}
|
||||
if (!TypeEqual(
|
||||
state->heap.Read((*t1->GetTuple().elements)[i].address, 0),
|
||||
state->heap.Read((*t2->GetTuple().elements)[i].address, 0))) {
|
||||
state->heap.Read((*t1->GetTupleValue().elements)[i].address, 0),
|
||||
state->heap.Read((*t2->GetTupleValue().elements)[i].address,
|
||||
0))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case ValKind::IntTV:
|
||||
case ValKind::BoolTV:
|
||||
case ValKind::ContinuationTV:
|
||||
case ValKind::TypeTV:
|
||||
case ValKind::IntType:
|
||||
case ValKind::BoolType:
|
||||
case ValKind::ContinuationType:
|
||||
case ValKind::TypeType:
|
||||
return true;
|
||||
default:
|
||||
std::cerr << "TypeEqual used to compare non-type values" << std::endl;
|
||||
@@ -465,34 +470,34 @@ auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
|
||||
return false;
|
||||
}
|
||||
switch (v1->tag) {
|
||||
case ValKind::IntV:
|
||||
return v1->GetInteger() == v2->GetInteger();
|
||||
case ValKind::BoolV:
|
||||
return v1->GetBoolean() == v2->GetBoolean();
|
||||
case ValKind::PtrV:
|
||||
return v1->GetPointer() == v2->GetPointer();
|
||||
case ValKind::FunV:
|
||||
return v1->GetFunction().body == v2->GetFunction().body;
|
||||
case ValKind::TupleV:
|
||||
return FieldsValueEqual(v1->GetTuple().elements, v2->GetTuple().elements,
|
||||
line_num);
|
||||
case ValKind::IntValue:
|
||||
return v1->GetIntValue() == v2->GetIntValue();
|
||||
case ValKind::BoolValue:
|
||||
return v1->GetBoolValue() == v2->GetBoolValue();
|
||||
case ValKind::PointerValue:
|
||||
return v1->GetPointerValue() == v2->GetPointerValue();
|
||||
case ValKind::FunctionValue:
|
||||
return v1->GetFunctionValue().body == v2->GetFunctionValue().body;
|
||||
case ValKind::TupleValue:
|
||||
return FieldsValueEqual(v1->GetTupleValue().elements,
|
||||
v2->GetTupleValue().elements, line_num);
|
||||
default:
|
||||
case ValKind::VarTV:
|
||||
case ValKind::IntTV:
|
||||
case ValKind::BoolTV:
|
||||
case ValKind::TypeTV:
|
||||
case ValKind::FunctionTV:
|
||||
case ValKind::PointerTV:
|
||||
case ValKind::AutoTV:
|
||||
case ValKind::StructTV:
|
||||
case ValKind::ChoiceTV:
|
||||
case ValKind::ContinuationTV:
|
||||
case ValKind::IntType:
|
||||
case ValKind::BoolType:
|
||||
case ValKind::TypeType:
|
||||
case ValKind::FunctionType:
|
||||
case ValKind::PointerType:
|
||||
case ValKind::AutoType:
|
||||
case ValKind::StructType:
|
||||
case ValKind::ChoiceType:
|
||||
case ValKind::ContinuationType:
|
||||
return TypeEqual(v1, v2);
|
||||
case ValKind::StructV:
|
||||
case ValKind::AltV:
|
||||
case ValKind::VarPatV:
|
||||
case ValKind::AltConsV:
|
||||
case ValKind::ContinuationV:
|
||||
case ValKind::StructValue:
|
||||
case ValKind::AlternativeValue:
|
||||
case ValKind::PatternVariableValue:
|
||||
case ValKind::AlternativeConstructorValue:
|
||||
case ValKind::ContinuationValue:
|
||||
std::cerr << "ValueEqual does not support this kind of value."
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
@@ -501,8 +506,8 @@ auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
|
||||
|
||||
auto ToInteger(const Value* v) -> int {
|
||||
switch (v->tag) {
|
||||
case ValKind::IntV:
|
||||
return v->GetInteger();
|
||||
case ValKind::IntValue:
|
||||
return v->GetIntValue();
|
||||
default:
|
||||
std::cerr << "expected an integer, not ";
|
||||
PrintValue(v, std::cerr);
|
||||
|
||||
@@ -37,47 +37,47 @@ struct TupleElement {
|
||||
};
|
||||
|
||||
enum class ValKind {
|
||||
IntV,
|
||||
FunV,
|
||||
PtrV,
|
||||
BoolV,
|
||||
StructV,
|
||||
AltV,
|
||||
TupleV,
|
||||
IntValue,
|
||||
FunctionValue,
|
||||
PointerValue,
|
||||
BoolValue,
|
||||
StructValue,
|
||||
AlternativeValue,
|
||||
TupleValue,
|
||||
VarTV,
|
||||
IntTV,
|
||||
BoolTV,
|
||||
TypeTV,
|
||||
FunctionTV,
|
||||
PointerTV,
|
||||
AutoTV,
|
||||
StructTV,
|
||||
ChoiceTV,
|
||||
ContinuationTV, // The type of a continuation.
|
||||
VarPatV,
|
||||
AltConsV,
|
||||
ContinuationV // A first-class continuation value.
|
||||
IntType,
|
||||
BoolType,
|
||||
TypeType,
|
||||
FunctionType,
|
||||
PointerType,
|
||||
AutoType,
|
||||
StructType,
|
||||
ChoiceType,
|
||||
ContinuationType, // The type of a continuation.
|
||||
PatternVariableValue,
|
||||
AlternativeConstructorValue,
|
||||
ContinuationValue // A first-class continuation value.
|
||||
};
|
||||
|
||||
struct Frame; // used by continuation
|
||||
|
||||
struct Function {
|
||||
struct FunctionValue {
|
||||
std::string* name;
|
||||
const Value* param;
|
||||
const Statement* body;
|
||||
};
|
||||
|
||||
struct StructConstructor {
|
||||
struct StructValue {
|
||||
const Value* type;
|
||||
const Value* inits;
|
||||
};
|
||||
|
||||
struct AlternativeConstructor {
|
||||
struct AlternativeConstructorValue {
|
||||
std::string* alt_name;
|
||||
std::string* choice_name;
|
||||
};
|
||||
|
||||
struct Alternative {
|
||||
struct AlternativeValue {
|
||||
std::string* alt_name;
|
||||
std::string* choice_name;
|
||||
Address argument;
|
||||
@@ -87,12 +87,12 @@ struct TupleValue {
|
||||
std::vector<TupleElement>* elements;
|
||||
};
|
||||
|
||||
struct VariablePatternValue {
|
||||
struct PatternVariableValue {
|
||||
std::string* name;
|
||||
const Value* type;
|
||||
};
|
||||
|
||||
struct FunctionTypeValue {
|
||||
struct FunctionType {
|
||||
const Value* param;
|
||||
const Value* ret;
|
||||
};
|
||||
@@ -123,66 +123,67 @@ struct Value {
|
||||
|
||||
// Return a first-class continuation represented by the
|
||||
// given stack, down to the nearest enclosing `__continuation`.
|
||||
static auto MakeContinuation(std::vector<Frame*> stack) -> Value*;
|
||||
static auto MakeIntVal(int i) -> const Value*;
|
||||
static auto MakeBoolVal(bool b) -> const Value*;
|
||||
static auto MakeFunVal(std::string name, const Value* param,
|
||||
const Statement* body) -> const Value*;
|
||||
static auto MakePtrVal(Address addr) -> const Value*;
|
||||
static auto MakeStructVal(const Value* type, const Value* inits)
|
||||
static auto MakeContinuationValue(std::vector<Frame*> stack) -> Value*;
|
||||
static auto MakeIntValue(int i) -> const Value*;
|
||||
static auto MakeBoolValue(bool b) -> const Value*;
|
||||
static auto MakeFunctionValue(std::string name, const Value* param,
|
||||
const Statement* body) -> const Value*;
|
||||
static auto MakePointerValue(Address addr) -> const Value*;
|
||||
static auto MakeStructValue(const Value* type, const Value* inits)
|
||||
-> const Value*;
|
||||
static auto MakeTupleVal(std::vector<TupleElement>* elts) -> const Value*;
|
||||
static auto MakeAltVal(std::string alt_name, std::string choice_name,
|
||||
Address argument) -> const Value*;
|
||||
static auto MakeAltCons(std::string alt_name, std::string choice_name)
|
||||
static auto MakeTupleValue(std::vector<TupleElement>* elts) -> const Value*;
|
||||
static auto MakeAlternativeValue(std::string alt_name,
|
||||
std::string choice_name, Address argument)
|
||||
-> const Value*;
|
||||
static auto MakeVarPatVal(std::string name, const Value* type)
|
||||
static auto MakeAlternativeConstructorValue(std::string alt_name,
|
||||
std::string choice_name)
|
||||
-> const Value*;
|
||||
static auto MakePatternVariableValue(std::string name, const Value* type)
|
||||
-> const Value*;
|
||||
static auto MakeVarTypeVal(std::string name) -> const Value*;
|
||||
static auto MakeIntTypeVal() -> const Value*;
|
||||
static auto MakeContinuationTypeVal() -> const Value*;
|
||||
static auto MakeAutoTypeVal() -> const Value*;
|
||||
static auto MakeBoolTypeVal() -> const Value*;
|
||||
static auto MakeTypeTypeVal() -> const Value*;
|
||||
static auto MakeFunTypeVal(const Value* param, const Value* ret)
|
||||
static auto MakeIntType() -> const Value*;
|
||||
static auto MakeContinuationType() -> const Value*;
|
||||
static auto MakeAutoType() -> const Value*;
|
||||
static auto MakeBoolType() -> const Value*;
|
||||
static auto MakeTypeType() -> const Value*;
|
||||
static auto MakeFunctionType(const Value* param, const Value* ret)
|
||||
-> const Value*;
|
||||
static auto MakePtrTypeVal(const Value* type) -> const Value*;
|
||||
static auto MakeStructTypeVal(std::string name, VarValues* fields,
|
||||
VarValues* methods) -> const Value*;
|
||||
static auto MakePointerType(const Value* type) -> const Value*;
|
||||
static auto MakeStructType(std::string name, VarValues* fields,
|
||||
VarValues* methods) -> const Value*;
|
||||
static auto MakeUnitTypeVal() -> const Value*;
|
||||
static auto MakeChoiceTypeVal(std::string name, VarValues* alts)
|
||||
-> const Value*;
|
||||
static auto MakeChoiceType(std::string name, VarValues* alts) -> const Value*;
|
||||
|
||||
// Access to alternatives
|
||||
int GetInteger() const;
|
||||
bool GetBoolean() const;
|
||||
Function GetFunction() const;
|
||||
StructConstructor GetStruct() const;
|
||||
AlternativeConstructor GetAlternativeConstructor() const;
|
||||
Alternative GetAlternative() const;
|
||||
TupleValue GetTuple() const;
|
||||
Address GetPointer() const;
|
||||
int GetIntValue() const;
|
||||
bool GetBoolValue() const;
|
||||
FunctionValue GetFunctionValue() const;
|
||||
StructValue GetStructValue() const;
|
||||
AlternativeConstructorValue GetAlternativeConstructorValue() const;
|
||||
AlternativeValue GetAlternativeValue() const;
|
||||
TupleValue GetTupleValue() const;
|
||||
Address GetPointerValue() const;
|
||||
std::string* GetVariableType() const;
|
||||
VariablePatternValue GetVariablePattern() const;
|
||||
FunctionTypeValue GetFunctionType() const;
|
||||
PatternVariableValue GetPatternVariableValue() const;
|
||||
FunctionType GetFunctionType() const;
|
||||
PointerType GetPointerType() const;
|
||||
StructType GetStructType() const;
|
||||
ChoiceType GetChoiceType() const;
|
||||
ContinuationValue GetContinuation() const;
|
||||
ContinuationValue GetContinuationValue() const;
|
||||
|
||||
private:
|
||||
union {
|
||||
int integer;
|
||||
bool boolean;
|
||||
Function fun;
|
||||
StructConstructor struct_val;
|
||||
AlternativeConstructor alt_cons;
|
||||
Alternative alt;
|
||||
FunctionValue fun;
|
||||
StructValue struct_val;
|
||||
AlternativeConstructorValue alt_cons;
|
||||
AlternativeValue alt;
|
||||
TupleValue tuple;
|
||||
Address ptr;
|
||||
std::string* var_type;
|
||||
VariablePatternValue var_pat;
|
||||
FunctionTypeValue fun_type;
|
||||
PatternVariableValue var_pat;
|
||||
FunctionType fun_type;
|
||||
PointerType ptr_type;
|
||||
StructType struct_type;
|
||||
ChoiceType choice_type;
|
||||
|
||||
@@ -16,7 +16,7 @@ const Expression* ParenContents::AsExpression(int line_number) const {
|
||||
}
|
||||
|
||||
const Expression* ParenContents::AsTuple(int line_number) const {
|
||||
return Expression::MakeTuple(line_number, fields_);
|
||||
return Expression::MakeTupleLiteral(line_number, fields_);
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -13,16 +13,16 @@ TEST(ParenContentsTest, EmptyAsExpression) {
|
||||
ParenContents contents;
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 1);
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::Tuple);
|
||||
EXPECT_EQ(expression->GetTuple().fields.size(), 0);
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::TupleLiteral);
|
||||
EXPECT_EQ(expression->GetTupleLiteral().fields.size(), 0);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, EmptyAsTuple) {
|
||||
ParenContents contents;
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::Tuple);
|
||||
EXPECT_EQ(tuple->GetTuple().fields.size(), 0);
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
|
||||
EXPECT_EQ(tuple->GetTupleLiteral().fields.size(), 0);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, UnaryNoCommaAsExpression) {
|
||||
@@ -33,81 +33,81 @@ TEST(ParenContentsTest, UnaryNoCommaAsExpression) {
|
||||
// )
|
||||
// ```
|
||||
ParenContents contents(
|
||||
{{.expression = Expression::MakeInt(/*line_num=*/2, 42)}},
|
||||
{{.expression = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::No);
|
||||
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 2);
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::Integer);
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::IntLiteral);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, UnaryNoCommaAsTuple) {
|
||||
ParenContents contents(
|
||||
{{.expression = Expression::MakeInt(/*line_num=*/2, 42)}},
|
||||
{{.expression = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::No);
|
||||
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::Tuple);
|
||||
std::vector<FieldInitializer> fields = tuple->GetTuple().fields;
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
|
||||
std::vector<FieldInitializer> fields = tuple->GetTupleLiteral().fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::IntLiteral);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, UnaryWithCommaAsExpression) {
|
||||
ParenContents contents(
|
||||
{{.expression = Expression::MakeInt(/*line_num=*/2, 42)}},
|
||||
{{.expression = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 1);
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::Tuple);
|
||||
std::vector<FieldInitializer> fields = expression->GetTuple().fields;
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::TupleLiteral);
|
||||
std::vector<FieldInitializer> fields = expression->GetTupleLiteral().fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::IntLiteral);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, UnaryWithCommaAsTuple) {
|
||||
ParenContents contents(
|
||||
{{.expression = Expression::MakeInt(/*line_num=*/2, 42)}},
|
||||
{{.expression = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::Tuple);
|
||||
std::vector<FieldInitializer> fields = tuple->GetTuple().fields;
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
|
||||
std::vector<FieldInitializer> fields = tuple->GetTupleLiteral().fields;
|
||||
ASSERT_EQ(fields.size(), 1);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::IntLiteral);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, BinaryAsExpression) {
|
||||
ParenContents contents(
|
||||
{{.expression = Expression::MakeInt(/*line_num=*/2, 42)},
|
||||
{.expression = Expression::MakeInt(/*line_num=*/3, 42)}},
|
||||
{{.expression = Expression::MakeIntLiteral(/*line_num=*/2, 42)},
|
||||
{.expression = Expression::MakeIntLiteral(/*line_num=*/3, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
const Expression* expression = contents.AsExpression(/*line_num=*/1);
|
||||
EXPECT_EQ(expression->line_num, 1);
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::Tuple);
|
||||
std::vector<FieldInitializer> fields = expression->GetTuple().fields;
|
||||
ASSERT_EQ(expression->tag(), ExpressionKind::TupleLiteral);
|
||||
std::vector<FieldInitializer> fields = expression->GetTupleLiteral().fields;
|
||||
ASSERT_EQ(fields.size(), 2);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[1].expression->tag(), ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::IntLiteral);
|
||||
EXPECT_EQ(fields[1].expression->tag(), ExpressionKind::IntLiteral);
|
||||
}
|
||||
|
||||
TEST(ParenContentsTest, BinaryAsTuple) {
|
||||
ParenContents contents(
|
||||
{{.expression = Expression::MakeInt(/*line_num=*/2, 42)},
|
||||
{.expression = Expression::MakeInt(/*line_num=*/3, 42)}},
|
||||
{{.expression = Expression::MakeIntLiteral(/*line_num=*/2, 42)},
|
||||
{.expression = Expression::MakeIntLiteral(/*line_num=*/3, 42)}},
|
||||
ParenContents::HasTrailingComma::Yes);
|
||||
|
||||
const Expression* tuple = contents.AsTuple(/*line_num=*/1);
|
||||
EXPECT_EQ(tuple->line_num, 1);
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::Tuple);
|
||||
std::vector<FieldInitializer> fields = tuple->GetTuple().fields;
|
||||
ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
|
||||
std::vector<FieldInitializer> fields = tuple->GetTupleLiteral().fields;
|
||||
ASSERT_EQ(fields.size(), 2);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[1].expression->tag(), ExpressionKind::Integer);
|
||||
EXPECT_EQ(fields[0].expression->tag(), ExpressionKind::IntLiteral);
|
||||
EXPECT_EQ(fields[1].expression->tag(), ExpressionKind::IntLiteral);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -201,58 +201,72 @@ pattern:
|
||||
;
|
||||
expression:
|
||||
identifier
|
||||
{ $$ = Carbon::Expression::MakeVar(yylineno, $1); }
|
||||
{ $$ = Carbon::Expression::MakeIdentifierExpression(yylineno, $1); }
|
||||
| expression designator
|
||||
{ $$ = Carbon::Expression::MakeGetField(yylineno, $1, $2); }
|
||||
{ $$ = Carbon::Expression::MakeFieldAccessExpression(yylineno, $1, $2); }
|
||||
| expression "[" expression "]"
|
||||
{ $$ = Carbon::Expression::MakeIndex(yylineno, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeIndexExpression(yylineno, $1, $3); }
|
||||
| identifier ":" expression
|
||||
{ $$ = Carbon::Expression::MakeVarPat(yylineno, $1, $3); }
|
||||
{
|
||||
$$ = Carbon::Expression::MakePatternVariableExpression(yylineno, $1, $3);
|
||||
}
|
||||
| integer_literal
|
||||
{ $$ = Carbon::Expression::MakeInt(yylineno, $1); }
|
||||
{ $$ = Carbon::Expression::MakeIntLiteral(yylineno, $1); }
|
||||
| TRUE
|
||||
{ $$ = Carbon::Expression::MakeBool(yylineno, true); }
|
||||
{ $$ = Carbon::Expression::MakeBoolLiteral(yylineno, true); }
|
||||
| FALSE
|
||||
{ $$ = Carbon::Expression::MakeBool(yylineno, false); }
|
||||
{ $$ = Carbon::Expression::MakeBoolLiteral(yylineno, false); }
|
||||
| INT
|
||||
{ $$ = Carbon::Expression::MakeIntType(yylineno); }
|
||||
{ $$ = Carbon::Expression::MakeIntTypeLiteral(yylineno); }
|
||||
| BOOL
|
||||
{ $$ = Carbon::Expression::MakeBoolType(yylineno); }
|
||||
{ $$ = Carbon::Expression::MakeBoolTypeLiteral(yylineno); }
|
||||
| TYPE
|
||||
{ $$ = Carbon::Expression::MakeTypeType(yylineno); }
|
||||
{ $$ = Carbon::Expression::MakeTypeTypeLiteral(yylineno); }
|
||||
| AUTO
|
||||
{ $$ = Carbon::Expression::MakeAutoType(yylineno); }
|
||||
{ $$ = Carbon::Expression::MakeAutoTypeLiteral(yylineno); }
|
||||
| CONTINUATION_TYPE
|
||||
{ $$ = Carbon::Expression::MakeContinuationType(yylineno); }
|
||||
{ $$ = Carbon::Expression::MakeContinuationTypeLiteral(yylineno); }
|
||||
| paren_expression { $$ = $1; }
|
||||
| expression EQUAL_EQUAL expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Eq, {$1, $3}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Eq, {$1, $3}); }
|
||||
| expression "+" expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Add, {$1, $3}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Add, {$1, $3}); }
|
||||
| expression "-" expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Sub, {$1, $3}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Sub, {$1, $3}); }
|
||||
| expression BINARY_STAR expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Mul, {$1, $3}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Mul, {$1, $3}); }
|
||||
| expression AND expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::And, {$1, $3}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::And, {$1, $3}); }
|
||||
| expression OR expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Or, {$1, $3}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Or, {$1, $3}); }
|
||||
| NOT expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Not, {$2}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Not, {$2}); }
|
||||
| "-" expression %prec UNARY_MINUS
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Neg, {$2}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Neg, {$2}); }
|
||||
| PREFIX_STAR expression
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Deref, {$2}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Deref, {$2}); }
|
||||
| UNARY_STAR expression %prec PREFIX_STAR
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Deref, {$2}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Deref, {$2}); }
|
||||
| expression tuple
|
||||
{ $$ = Carbon::Expression::MakeCall(yylineno, $1, $2); }
|
||||
{ $$ = Carbon::Expression::MakeCallExpression(yylineno, $1, $2); }
|
||||
| expression POSTFIX_STAR
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Ptr, {$1}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Ptr, {$1}); }
|
||||
| expression UNARY_STAR
|
||||
{ $$ = Carbon::Expression::MakeOp(yylineno, Carbon::Operator::Ptr, {$1}); }
|
||||
{ $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
|
||||
yylineno, Carbon::Operator::Ptr, {$1}); }
|
||||
| FNTY tuple return_type
|
||||
{ $$ = Carbon::Expression::MakeFunType(yylineno, $2, $3); }
|
||||
{ $$ = Carbon::Expression::MakeFunctionTypeLiteral(yylineno, $2, $3); }
|
||||
;
|
||||
designator: "." identifier { $$ = $2; }
|
||||
;
|
||||
@@ -296,8 +310,8 @@ clause:
|
||||
{ $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>($2, $4); }
|
||||
| DEFAULT DBLARROW statement
|
||||
{
|
||||
auto vp = Carbon::Expression::MakeVarPat(yylineno, "_",
|
||||
Carbon::Expression::MakeAutoType(yylineno));
|
||||
auto vp = Carbon::Expression::MakePatternVariableExpression(
|
||||
yylineno, "_", Carbon::Expression::MakeAutoTypeLiteral(yylineno));
|
||||
$$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>(vp, $3);
|
||||
}
|
||||
;
|
||||
@@ -350,7 +364,7 @@ statement_list:
|
||||
;
|
||||
return_type:
|
||||
// Empty
|
||||
{ $$ = Carbon::Expression::MakeTuple(yylineno, {}); }
|
||||
{ $$ = Carbon::Expression::MakeTupleLiteral(yylineno, {}); }
|
||||
| ARROW expression %prec FNARROW
|
||||
{ $$ = $2; }
|
||||
;
|
||||
@@ -359,8 +373,9 @@ function_definition:
|
||||
{ $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
|
||||
| FN identifier tuple DBLARROW expression ";"
|
||||
{
|
||||
$$ = Carbon::MakeFunDef(yylineno, $2, Carbon::Expression::MakeAutoType(yylineno), $3,
|
||||
Carbon::Statement::MakeReturn(yylineno, $5));
|
||||
$$ = Carbon::MakeFunDef(yylineno, $2,
|
||||
Carbon::Expression::MakeAutoTypeLiteral(yylineno),
|
||||
$3, Carbon::Statement::MakeReturn(yylineno, $5));
|
||||
}
|
||||
;
|
||||
function_declaration:
|
||||
@@ -385,7 +400,7 @@ alternative:
|
||||
| identifier
|
||||
{
|
||||
$$ = std::pair<std::string, const Carbon::Expression*>(
|
||||
$1, Carbon::Expression::MakeTuple(yylineno, {}));
|
||||
$1, Carbon::Expression::MakeTupleLiteral(yylineno, {}));
|
||||
}
|
||||
;
|
||||
alternative_list:
|
||||
|
||||
Reference in New Issue
Block a user