* Naive pointer implementation

* Bug fixes and better pointer tests

* Remove debug print statement

* Implement changes suggested by @geoffromer

Also add a failing test case that tests applying the address-of operator
to an rvalue.
This commit is contained in:
Darshal Shetty
2022-02-05 10:27:54 -05:00
committed by GitHub
parent f6cbd2231e
commit 4479c55305
11 changed files with 131 additions and 5 deletions
@@ -154,7 +154,9 @@ auto Interpreter::EvalPrim(Operator op,
case Operator::Ptr:
return arena_->New<PointerType>(args[0]);
case Operator::Deref:
FATAL() << "dereference not implemented yet";
return heap_.Read(cast<PointerValue>(*args[0]).address(), source_loc);
case Operator::AddressOf:
return arena_->New<PointerValue>(cast<LValue>(*args[0]).address());
}
}
@@ -313,13 +315,26 @@ void Interpreter::StepLvalue() {
return todo_.FinishAction(arena_->New<LValue>(field));
}
}
case ExpressionKind::PrimitiveOperatorExpression: {
const PrimitiveOperatorExpression& op = cast<PrimitiveOperatorExpression>(exp);
if (op.op() != Operator::Deref) {
FATAL() << "Can't treat primitive operator expression as lvalue: " << exp;
}
if (act.pos() == 0) {
return todo_.Spawn(
std::make_unique<ExpressionAction>(op.arguments()[0]));
} else {
const PointerValue& res = cast<PointerValue>(*act.results()[0]);
return todo_.FinishAction(arena_->New<LValue>(res.address()));
}
break;
}
case ExpressionKind::TupleLiteral:
case ExpressionKind::StructLiteral:
case ExpressionKind::StructTypeLiteral:
case ExpressionKind::IntLiteral:
case ExpressionKind::BoolLiteral:
case ExpressionKind::CallExpression:
case ExpressionKind::PrimitiveOperatorExpression:
case ExpressionKind::IntTypeLiteral:
case ExpressionKind::BoolTypeLiteral:
case ExpressionKind::TypeTypeLiteral:
@@ -340,6 +355,7 @@ auto Interpreter::Convert(Nonnull<const Value*> value,
switch (value->kind()) {
case Value::Kind::IntValue:
case Value::Kind::FunctionValue:
case Value::Kind::PointerValue:
case Value::Kind::LValue:
case Value::Kind::BoolValue:
case Value::Kind::NominalClassValue:
@@ -509,7 +525,11 @@ void Interpreter::StepExp() {
// { {v :: op(vs,[],e,es) :: C, E, F} :: S, H}
// -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H}
Nonnull<const Expression*> arg = op.arguments()[act.pos()];
return todo_.Spawn(std::make_unique<ExpressionAction>(arg));
if (op.op() == Operator::AddressOf) {
return todo_.Spawn(std::make_unique<LValAction>(arg));
} else {
return todo_.Spawn(std::make_unique<ExpressionAction>(arg));
}
} else {
// { {v :: op(vs,[]) :: C, E, F} :: S, H}
// -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H}