mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Add Ptr and RawNew for migration (#751)
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
parent
1eeaa208f2
commit
c6ebe0db67
@@ -30,7 +30,7 @@ auto ExpressionFromParenContents(
|
||||
auto TupleExpressionFromParenContents(
|
||||
int line_num, const ParenContents<Expression>& paren_contents)
|
||||
-> const Expression* {
|
||||
return global_arena->New<TupleLiteral>(
|
||||
return global_arena->RawNew<TupleLiteral>(
|
||||
line_num, paren_contents.TupleElements<FieldInitializer>(line_num));
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,8 @@ TEST(ExpressionTest, UnaryNoCommaAsExpression) {
|
||||
// ```
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Expression* expression =
|
||||
@@ -67,7 +68,8 @@ TEST(ExpressionTest, UnaryNoCommaAsExpression) {
|
||||
TEST(ExpressionTest, UnaryNoCommaAsTuple) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Expression* tuple =
|
||||
@@ -81,7 +83,8 @@ TEST(ExpressionTest, UnaryNoCommaAsTuple) {
|
||||
TEST(ExpressionTest, UnaryWithCommaAsExpression) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* expression =
|
||||
@@ -95,7 +98,8 @@ TEST(ExpressionTest, UnaryWithCommaAsExpression) {
|
||||
TEST(ExpressionTest, UnaryWithCommaAsTuple) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* tuple =
|
||||
@@ -109,9 +113,11 @@ TEST(ExpressionTest, UnaryWithCommaAsTuple) {
|
||||
TEST(ExpressionTest, BinaryAsExpression) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/3, 42)}},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/3, 42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* expression =
|
||||
@@ -125,9 +131,11 @@ TEST(ExpressionTest, BinaryAsExpression) {
|
||||
TEST(ExpressionTest, BinaryAsTuple) {
|
||||
ParenContents<Expression> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/2, 42)},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->New<IntLiteral>(/*line_num=*/3, 42)}},
|
||||
.term =
|
||||
global_arena->RawNew<IntLiteral>(/*line_num=*/3, 42)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Expression* tuple =
|
||||
|
||||
@@ -59,7 +59,7 @@ TuplePattern::TuplePattern(const Expression* tuple_literal)
|
||||
const auto& tuple = cast<TupleLiteral>(*tuple_literal);
|
||||
for (const FieldInitializer& init : tuple.Fields()) {
|
||||
fields.push_back(Field(
|
||||
init.name, global_arena->New<ExpressionPattern>(init.expression)));
|
||||
init.name, global_arena->RawNew<ExpressionPattern>(init.expression)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ auto PatternFromParenContents(int line_num,
|
||||
auto TuplePatternFromParenContents(int line_num,
|
||||
const ParenContents<Pattern>& paren_contents)
|
||||
-> const TuplePattern* {
|
||||
return global_arena->New<TuplePattern>(
|
||||
return global_arena->RawNew<TuplePattern>(
|
||||
line_num, paren_contents.TupleElements<TuplePattern::Field>(line_num));
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ auto ParenExpressionToParenPattern(const ParenContents<Expression>& contents)
|
||||
for (const auto& element : contents.elements) {
|
||||
result.elements.push_back(
|
||||
{.name = element.name,
|
||||
.term = global_arena->New<ExpressionPattern>(element.term)});
|
||||
.term = global_arena->RawNew<ExpressionPattern>(element.term)});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ TEST(PatternTest, UnaryNoCommaAsPattern) {
|
||||
// ```
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/2)}},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
@@ -63,7 +63,7 @@ TEST(PatternTest, UnaryNoCommaAsPattern) {
|
||||
TEST(PatternTest, UnaryNoCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/2)}},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.has_trailing_comma = false};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
@@ -75,7 +75,7 @@ TEST(PatternTest, UnaryNoCommaAsTuplePattern) {
|
||||
TEST(PatternTest, UnaryWithCommaAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/2)}},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
@@ -88,7 +88,7 @@ TEST(PatternTest, UnaryWithCommaAsPattern) {
|
||||
TEST(PatternTest, UnaryWithCommaAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/2)}},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
@@ -100,9 +100,9 @@ TEST(PatternTest, UnaryWithCommaAsTuplePattern) {
|
||||
TEST(PatternTest, BinaryAsPattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/2)},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/3)}},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/3)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const Pattern* pattern = PatternFromParenContents(/*line_num=*/1, contents);
|
||||
@@ -115,9 +115,9 @@ TEST(PatternTest, BinaryAsPattern) {
|
||||
TEST(PatternTest, BinaryAsTuplePattern) {
|
||||
ParenContents<Pattern> contents = {
|
||||
.elements = {{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/2)},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/2)},
|
||||
{.name = std::nullopt,
|
||||
.term = global_arena->New<AutoPattern>(/*line_num=*/3)}},
|
||||
.term = global_arena->RawNew<AutoPattern>(/*line_num=*/3)}},
|
||||
.has_trailing_comma = true};
|
||||
|
||||
const TuplePattern* tuple =
|
||||
|
||||
@@ -132,7 +132,7 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
|
||||
Return::Return(int line_num, const Expression* exp, bool is_omitted_exp)
|
||||
: Statement(Kind::Return, line_num),
|
||||
exp(exp != nullptr ? exp : global_arena->New<TupleLiteral>(line_num)),
|
||||
exp(exp != nullptr ? exp : global_arena->RawNew<TupleLiteral>(line_num)),
|
||||
is_omitted_exp(is_omitted_exp) {
|
||||
CHECK(exp != nullptr || is_omitted_exp);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ cc_library(
|
||||
srcs = ["arena.cpp"],
|
||||
hdrs = ["arena.h"],
|
||||
deps = [
|
||||
":ptr",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
@@ -31,6 +32,15 @@ cc_test(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "ptr",
|
||||
hdrs = ["ptr.h"],
|
||||
deps = [
|
||||
"//common:check",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "tracing_flag",
|
||||
srcs = ["tracing_flag.cpp"],
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/common/ptr.h"
|
||||
#include "llvm/Support/ManagedStatic.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -16,10 +17,20 @@ class Arena {
|
||||
public:
|
||||
// Allocates an object in the arena, returning a pointer to it.
|
||||
template <typename T, typename... Args>
|
||||
auto New(Args&&... args) -> T* {
|
||||
auto New(Args&&... args) -> Ptr<T> {
|
||||
auto smart_ptr =
|
||||
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
|
||||
T* raw_ptr = smart_ptr->Instance();
|
||||
Ptr<T> ptr = smart_ptr->Instance();
|
||||
arena.push_back(std::move(smart_ptr));
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// TODO: Remove. This is only to help findability during migration.
|
||||
template <typename T, typename... Args>
|
||||
auto RawNew(Args&&... args) -> T* {
|
||||
auto smart_ptr =
|
||||
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
|
||||
T* raw_ptr = smart_ptr->Instance().Get();
|
||||
arena.push_back(std::move(smart_ptr));
|
||||
return raw_ptr;
|
||||
}
|
||||
@@ -40,7 +51,7 @@ class Arena {
|
||||
explicit ArenaEntryTyped(Args&&... args)
|
||||
: instance(std::forward<Args>(args)...) {}
|
||||
|
||||
auto Instance() -> T* { return &instance; }
|
||||
auto Instance() -> Ptr<T> { return Ptr<T>(&instance); }
|
||||
|
||||
private:
|
||||
T instance;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
#define EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "common/check.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
// A non-nullable pointer. Written as `Ptr<T>` instead of `T*`.
|
||||
template <typename T>
|
||||
class Ptr {
|
||||
public:
|
||||
explicit Ptr(T* ptr) : ptr(ptr) { CHECK(ptr != nullptr); }
|
||||
|
||||
template <typename OtherT,
|
||||
std::enable_if_t<std::is_convertible_v<OtherT*, T*>>* = nullptr>
|
||||
Ptr(Ptr<OtherT> other) {
|
||||
return Ptr<OtherT>(other.ptr);
|
||||
}
|
||||
|
||||
Ptr(std::nullptr_t) = delete;
|
||||
|
||||
Ptr(const Ptr& other) = default;
|
||||
Ptr& operator=(const Ptr& rhs) = default;
|
||||
|
||||
auto operator*() const -> T& { return *ptr; }
|
||||
auto operator->() const -> T* { return ptr; }
|
||||
|
||||
T* Get() const { return ptr; }
|
||||
|
||||
private:
|
||||
T* ptr;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_COMMON_PTR_H_
|
||||
@@ -37,8 +37,8 @@ class Dictionary {
|
||||
// Associate the value v with key k in the dictionary.
|
||||
// Time complexity: O(1).
|
||||
auto Set(const K& k, const V& v) -> void {
|
||||
head = global_arena->New<ListNode<std::pair<K, V>>>(std::make_pair(k, v),
|
||||
head);
|
||||
head = global_arena->RawNew<ListNode<std::pair<K, V>>>(std::make_pair(k, v),
|
||||
head);
|
||||
}
|
||||
|
||||
typedef ListNodeIterator<std::pair<K, V>> Iterator;
|
||||
|
||||
@@ -84,29 +84,29 @@ auto EvalPrim(Operator op, const std::vector<const Value*>& args, int line_num)
|
||||
-> const Value* {
|
||||
switch (op) {
|
||||
case Operator::Neg:
|
||||
return global_arena->New<IntValue>(-cast<IntValue>(*args[0]).Val());
|
||||
return global_arena->RawNew<IntValue>(-cast<IntValue>(*args[0]).Val());
|
||||
case Operator::Add:
|
||||
return global_arena->New<IntValue>(cast<IntValue>(*args[0]).Val() +
|
||||
cast<IntValue>(*args[1]).Val());
|
||||
return global_arena->RawNew<IntValue>(cast<IntValue>(*args[0]).Val() +
|
||||
cast<IntValue>(*args[1]).Val());
|
||||
case Operator::Sub:
|
||||
return global_arena->New<IntValue>(cast<IntValue>(*args[0]).Val() -
|
||||
cast<IntValue>(*args[1]).Val());
|
||||
return global_arena->RawNew<IntValue>(cast<IntValue>(*args[0]).Val() -
|
||||
cast<IntValue>(*args[1]).Val());
|
||||
case Operator::Mul:
|
||||
return global_arena->New<IntValue>(cast<IntValue>(*args[0]).Val() *
|
||||
cast<IntValue>(*args[1]).Val());
|
||||
return global_arena->RawNew<IntValue>(cast<IntValue>(*args[0]).Val() *
|
||||
cast<IntValue>(*args[1]).Val());
|
||||
case Operator::Not:
|
||||
return global_arena->New<BoolValue>(!cast<BoolValue>(*args[0]).Val());
|
||||
return global_arena->RawNew<BoolValue>(!cast<BoolValue>(*args[0]).Val());
|
||||
case Operator::And:
|
||||
return global_arena->New<BoolValue>(cast<BoolValue>(*args[0]).Val() &&
|
||||
cast<BoolValue>(*args[1]).Val());
|
||||
return global_arena->RawNew<BoolValue>(cast<BoolValue>(*args[0]).Val() &&
|
||||
cast<BoolValue>(*args[1]).Val());
|
||||
case Operator::Or:
|
||||
return global_arena->New<BoolValue>(cast<BoolValue>(*args[0]).Val() ||
|
||||
cast<BoolValue>(*args[1]).Val());
|
||||
return global_arena->RawNew<BoolValue>(cast<BoolValue>(*args[0]).Val() ||
|
||||
cast<BoolValue>(*args[1]).Val());
|
||||
case Operator::Eq:
|
||||
return global_arena->New<BoolValue>(
|
||||
return global_arena->RawNew<BoolValue>(
|
||||
ValueEqual(args[0], args[1], line_num));
|
||||
case Operator::Ptr:
|
||||
return global_arena->New<PointerType>(args[0]);
|
||||
return global_arena->RawNew<PointerType>(args[0]);
|
||||
case Operator::Deref:
|
||||
FATAL() << "dereference not implemented yet";
|
||||
}
|
||||
@@ -124,12 +124,12 @@ void InitEnv(const Declaration& d, Env* env) {
|
||||
// Bring the deduced parameters into scope.
|
||||
for (const auto& deduced : func_def.deduced_parameters) {
|
||||
Address a = state->heap.AllocateValue(
|
||||
global_arena->New<VariableType>(deduced.name));
|
||||
global_arena->RawNew<VariableType>(deduced.name));
|
||||
new_env.Set(deduced.name, a);
|
||||
}
|
||||
auto pt = InterpPattern(new_env, func_def.param_pattern);
|
||||
auto f =
|
||||
global_arena->New<FunctionValue>(func_def.name, pt, func_def.body);
|
||||
global_arena->RawNew<FunctionValue>(func_def.name, pt, func_def.body);
|
||||
Address a = state->heap.AllocateValue(f);
|
||||
env->Set(func_def.name, a);
|
||||
break;
|
||||
@@ -152,7 +152,7 @@ void InitEnv(const Declaration& d, Env* env) {
|
||||
}
|
||||
}
|
||||
}
|
||||
auto st = global_arena->New<StructType>(
|
||||
auto st = global_arena->RawNew<StructType>(
|
||||
struct_def.name, std::move(fields), std::move(methods));
|
||||
auto a = state->heap.AllocateValue(st);
|
||||
env->Set(struct_def.name, a);
|
||||
@@ -166,7 +166,8 @@ void InitEnv(const Declaration& d, Env* env) {
|
||||
auto t = InterpExp(Env(), signature);
|
||||
alts.push_back(make_pair(name, t));
|
||||
}
|
||||
auto ct = global_arena->New<ChoiceType>(choice.Name(), std::move(alts));
|
||||
auto ct =
|
||||
global_arena->RawNew<ChoiceType>(choice.Name(), std::move(alts));
|
||||
auto a = state->heap.AllocateValue(ct);
|
||||
env->Set(choice.Name(), a);
|
||||
break;
|
||||
@@ -205,27 +206,27 @@ void CallFunction(int line_num, std::vector<const Value*> operas,
|
||||
PatternMatch(fn.Param(), operas[1], globals, ¶ms, line_num);
|
||||
CHECK(matches) << "internal error in call_function, pattern match failed";
|
||||
// Create the new frame and push it on the stack
|
||||
auto* scope = global_arena->New<Scope>(*matches, params);
|
||||
auto* frame = global_arena->New<Frame>(
|
||||
auto* scope = global_arena->RawNew<Scope>(*matches, params);
|
||||
auto* frame = global_arena->RawNew<Frame>(
|
||||
fn.Name(), Stack(scope),
|
||||
Stack<Action*>(global_arena->New<StatementAction>(fn.Body())));
|
||||
Stack<Action*>(global_arena->RawNew<StatementAction>(fn.Body())));
|
||||
state->stack.Push(frame);
|
||||
break;
|
||||
}
|
||||
case Value::Kind::StructType: {
|
||||
const Value* arg = CopyVal(operas[1], line_num);
|
||||
const Value* sv = global_arena->New<StructValue>(operas[0], arg);
|
||||
const Value* sv = global_arena->RawNew<StructValue>(operas[0], arg);
|
||||
Frame* frame = state->stack.Top();
|
||||
frame->todo.Push(global_arena->New<ValAction>(sv));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(sv));
|
||||
break;
|
||||
}
|
||||
case Value::Kind::AlternativeConstructorValue: {
|
||||
const auto& alt = cast<AlternativeConstructorValue>(*operas[0]);
|
||||
const Value* arg = CopyVal(operas[1], line_num);
|
||||
const Value* av = global_arena->New<AlternativeValue>(
|
||||
const Value* av = global_arena->RawNew<AlternativeValue>(
|
||||
alt.AltName(), alt.ChoiceName(), arg);
|
||||
Frame* frame = state->stack.Top();
|
||||
frame->todo.Push(global_arena->New<ValAction>(av));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(av));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -260,9 +261,9 @@ void CreateTuple(Frame* frame, Action* act, const Expression* exp) {
|
||||
{.name = tup_lit.Fields()[i].name, .value = act->Results()[i]});
|
||||
}
|
||||
|
||||
const Value* tv = global_arena->New<TupleValue>(std::move(elements));
|
||||
const Value* tv = global_arena->RawNew<TupleValue>(std::move(elements));
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(tv));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(tv));
|
||||
}
|
||||
|
||||
// Returns an updated environment that includes the bindings of
|
||||
@@ -422,16 +423,16 @@ void StepLvalue() {
|
||||
// -> { {E(x) :: C, E, F} :: S, H}
|
||||
Address pointer = GetFromEnv(exp->LineNumber(),
|
||||
cast<IdentifierExpression>(*exp).Name());
|
||||
const Value* v = global_arena->New<PointerValue>(pointer);
|
||||
const Value* v = global_arena->RawNew<PointerValue>(pointer);
|
||||
frame->todo.Pop();
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
if (act->Pos() == 0) {
|
||||
// { {e.f :: C, E, F} :: S, H}
|
||||
// -> { e :: [].f :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<LValAction>(
|
||||
frame->todo.Push(global_arena->RawNew<LValAction>(
|
||||
cast<FieldAccessExpression>(*exp).Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
@@ -441,8 +442,8 @@ void StepLvalue() {
|
||||
Address field = aggregate.SubobjectAddress(
|
||||
cast<FieldAccessExpression>(*exp).Field());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<PointerValue>(field)));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(
|
||||
global_arena->RawNew<PointerValue>(field)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -450,11 +451,11 @@ void StepLvalue() {
|
||||
if (act->Pos() == 0) {
|
||||
// { {e[i] :: C, E, F} :: S, H}
|
||||
// -> { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<LValAction>(
|
||||
frame->todo.Push(global_arena->RawNew<LValAction>(
|
||||
cast<IndexExpression>(*exp).Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<IndexExpression>(*exp).Offset()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
@@ -465,8 +466,8 @@ void StepLvalue() {
|
||||
std::to_string(cast<IntValue>(*act->Results()[1]).Val());
|
||||
Address field = aggregate.SubobjectAddress(f);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<PointerValue>(field)));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(
|
||||
global_arena->RawNew<PointerValue>(field)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -475,7 +476,7 @@ void StepLvalue() {
|
||||
// { {(f1=e1,...) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H}
|
||||
const Expression* e1 = cast<TupleLiteral>(*exp).Fields()[0].expression;
|
||||
frame->todo.Push(global_arena->New<LValAction>(e1));
|
||||
frame->todo.Push(global_arena->RawNew<LValAction>(e1));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() !=
|
||||
static_cast<int>(cast<TupleLiteral>(*exp).Fields().size())) {
|
||||
@@ -485,7 +486,7 @@ void StepLvalue() {
|
||||
// H}
|
||||
const Expression* elt =
|
||||
cast<TupleLiteral>(*exp).Fields()[act->Pos()].expression;
|
||||
frame->todo.Push(global_arena->New<LValAction>(elt));
|
||||
frame->todo.Push(global_arena->RawNew<LValAction>(elt));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
CreateTuple(frame, act, exp);
|
||||
@@ -523,11 +524,11 @@ void StepExp() {
|
||||
if (act->Pos() == 0) {
|
||||
// { { e[i] :: C, E, F} :: S, H}
|
||||
// -> { { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<IndexExpression>(*exp).Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<IndexExpression>(*exp).Offset()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
@@ -544,7 +545,7 @@ void StepExp() {
|
||||
<< "field " << f << " not in " << *tuple;
|
||||
}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(field));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(field));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -561,7 +562,7 @@ void StepExp() {
|
||||
// -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H}
|
||||
const Expression* e1 =
|
||||
cast<TupleLiteral>(*exp).Fields()[0].expression;
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(e1));
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(e1));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
CreateTuple(frame, act, exp);
|
||||
@@ -574,7 +575,7 @@ void StepExp() {
|
||||
// H}
|
||||
const Expression* elt =
|
||||
cast<TupleLiteral>(*exp).Fields()[act->Pos()].expression;
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(elt));
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(elt));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
CreateTuple(frame, act, exp);
|
||||
@@ -587,7 +588,7 @@ void StepExp() {
|
||||
// { { e.f :: C, E, F} :: S, H}
|
||||
// -> { { e :: [].f :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(access.Aggregate()));
|
||||
global_arena->RawNew<ExpressionAction>(access.Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
// { { v :: [].f :: C, E, F} :: S, H}
|
||||
@@ -595,7 +596,7 @@ void StepExp() {
|
||||
const Value* element = act->Results()[0]->GetField(
|
||||
FieldPath(access.Field()), exp->LineNumber());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(element));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(element));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -606,22 +607,22 @@ void StepExp() {
|
||||
Address pointer = GetFromEnv(exp->LineNumber(), ident.Name());
|
||||
const Value* pointee = state->heap.Read(pointer, exp->LineNumber());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(pointee));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(pointee));
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::IntLiteral:
|
||||
CHECK(act->Pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<IntValue>(cast<IntLiteral>(*exp).Val())));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(
|
||||
global_arena->RawNew<IntValue>(cast<IntLiteral>(*exp).Val())));
|
||||
break;
|
||||
case Expression::Kind::BoolLiteral:
|
||||
CHECK(act->Pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<BoolValue>(cast<BoolLiteral>(*exp).Val())));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(
|
||||
global_arena->RawNew<BoolValue>(cast<BoolLiteral>(*exp).Val())));
|
||||
break;
|
||||
case Expression::Kind::PrimitiveOperatorExpression: {
|
||||
const auto& op = cast<PrimitiveOperatorExpression>(*exp);
|
||||
@@ -629,14 +630,14 @@ void StepExp() {
|
||||
// { {v :: op(vs,[],e,es) :: C, E, F} :: S, H}
|
||||
// -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H}
|
||||
const Expression* arg = op.Arguments()[act->Pos()];
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(arg));
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(arg));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
// { {v :: op(vs,[]) :: C, E, F} :: S, H}
|
||||
// -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H}
|
||||
const Value* v = EvalPrim(op.Op(), act->Results(), exp->LineNumber());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -644,13 +645,13 @@ void StepExp() {
|
||||
if (act->Pos() == 0) {
|
||||
// { {e1(e2) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: [](e2) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<CallExpression>(*exp).Function()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
// { { v :: [](e) :: C, E, F} :: S, H}
|
||||
// -> { { e :: v([]) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<CallExpression>(*exp).Argument()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
@@ -673,73 +674,74 @@ void StepExp() {
|
||||
CHECK(pointee->Tag() == Value::Kind::StringValue);
|
||||
// TODO: This could eventually use something like llvm::formatv.
|
||||
llvm::outs() << cast<StringValue>(*pointee).Val();
|
||||
frame->todo.Push(global_arena->New<ValAction>(&TupleValue::Empty()));
|
||||
frame->todo.Push(
|
||||
global_arena->RawNew<ValAction>(&TupleValue::Empty()));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case Expression::Kind::IntTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<IntType>();
|
||||
const Value* v = global_arena->RawNew<IntType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::BoolTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<BoolType>();
|
||||
const Value* v = global_arena->RawNew<BoolType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::TypeTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<TypeType>();
|
||||
const Value* v = global_arena->RawNew<TypeType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::FunctionTypeLiteral: {
|
||||
if (act->Pos() == 0) {
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<FunctionTypeLiteral>(*exp).Parameter()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
// { { pt :: fn [] -> e :: C, E, F} :: S, H}
|
||||
// -> { { e :: fn pt -> []) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<FunctionTypeLiteral>(*exp).ReturnType()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
// { { rt :: fn pt -> [] :: C, E, F} :: S, H}
|
||||
// -> { fn pt -> rt :: {C, E, F} :: S, H}
|
||||
const Value* v = global_arena->New<FunctionType>(
|
||||
const Value* v = global_arena->RawNew<FunctionType>(
|
||||
std::vector<GenericBinding>(), act->Results()[0],
|
||||
act->Results()[1]);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::ContinuationTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<ContinuationType>();
|
||||
const Value* v = global_arena->RawNew<ContinuationType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case Expression::Kind::StringLiteral:
|
||||
CHECK(act->Pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<StringValue>(cast<StringLiteral>(*exp).Val())));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(
|
||||
global_arena->RawNew<StringValue>(cast<StringLiteral>(*exp).Val())));
|
||||
break;
|
||||
case Expression::Kind::StringTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<StringType>();
|
||||
const Value* v = global_arena->RawNew<StringType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
} // switch (exp->Tag)
|
||||
@@ -755,21 +757,21 @@ void StepPattern() {
|
||||
switch (pattern->Tag()) {
|
||||
case Pattern::Kind::AutoPattern: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<AutoType>();
|
||||
const Value* v = global_arena->RawNew<AutoType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case Pattern::Kind::BindingPattern: {
|
||||
const auto& binding = cast<BindingPattern>(*pattern);
|
||||
if (act->Pos() == 0) {
|
||||
frame->todo.Push(global_arena->New<PatternAction>(binding.Type()));
|
||||
frame->todo.Push(global_arena->RawNew<PatternAction>(binding.Type()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
auto v = global_arena->New<BindingPlaceholderValue>(binding.Name(),
|
||||
act->Results()[0]);
|
||||
auto v = global_arena->RawNew<BindingPlaceholderValue>(
|
||||
binding.Name(), act->Results()[0]);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(v));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -778,10 +780,11 @@ void StepPattern() {
|
||||
if (act->Pos() == 0) {
|
||||
if (tuple.Fields().empty()) {
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(&TupleValue::Empty()));
|
||||
frame->todo.Push(
|
||||
global_arena->RawNew<ValAction>(&TupleValue::Empty()));
|
||||
} else {
|
||||
const Pattern* p1 = tuple.Fields()[0].pattern;
|
||||
frame->todo.Push(global_arena->New<PatternAction>(p1));
|
||||
frame->todo.Push(global_arena->RawNew<PatternAction>(p1));
|
||||
act->IncrementPos();
|
||||
}
|
||||
} else if (act->Pos() != static_cast<int>(tuple.Fields().size())) {
|
||||
@@ -790,7 +793,7 @@ void StepPattern() {
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
const Pattern* elt = tuple.Fields()[act->Pos()].pattern;
|
||||
frame->todo.Push(global_arena->New<PatternAction>(elt));
|
||||
frame->todo.Push(global_arena->RawNew<PatternAction>(elt));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
std::vector<TupleElement> elements;
|
||||
@@ -799,9 +802,9 @@ void StepPattern() {
|
||||
{.name = tuple.Fields()[i].name, .value = act->Results()[i]});
|
||||
}
|
||||
const Value* tuple_value =
|
||||
global_arena->New<TupleValue>(std::move(elements));
|
||||
global_arena->RawNew<TupleValue>(std::move(elements));
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(tuple_value));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(tuple_value));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -809,18 +812,18 @@ void StepPattern() {
|
||||
const auto& alternative = cast<AlternativePattern>(*pattern);
|
||||
if (act->Pos() == 0) {
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(alternative.ChoiceType()));
|
||||
global_arena->RawNew<ExpressionAction>(alternative.ChoiceType()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
frame->todo.Push(
|
||||
global_arena->New<PatternAction>(alternative.Arguments()));
|
||||
global_arena->RawNew<PatternAction>(alternative.Arguments()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
CHECK(act->Pos() == 2);
|
||||
const auto& choice_type = cast<ChoiceType>(*act->Results()[0]);
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(
|
||||
global_arena->New<ValAction>(global_arena->New<AlternativeValue>(
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(
|
||||
global_arena->RawNew<AlternativeValue>(
|
||||
alternative.AlternativeName(), choice_type.Name(),
|
||||
act->Results()[1])));
|
||||
}
|
||||
@@ -828,7 +831,7 @@ void StepPattern() {
|
||||
}
|
||||
case Pattern::Kind::ExpressionPattern:
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<ExpressionPattern>(pattern)->Expression()));
|
||||
break;
|
||||
}
|
||||
@@ -880,7 +883,7 @@ void StepStmt() {
|
||||
// { { (match (e) ...) :: C, E, F} :: S, H}
|
||||
// -> { { e :: (match ([]) ...) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(cast<Match>(*stmt).Exp()));
|
||||
global_arena->RawNew<ExpressionAction>(cast<Match>(*stmt).Exp()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
// Regarding act->Pos():
|
||||
@@ -905,7 +908,7 @@ void StepStmt() {
|
||||
// start interpreting the pattern of the clause
|
||||
// { {v :: (match ([]) ...) :: C, E, F} :: S, H}
|
||||
// -> { {pi :: (match ([]) ...) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<PatternAction>(c->first));
|
||||
frame->todo.Push(global_arena->RawNew<PatternAction>(c->first));
|
||||
act->IncrementPos();
|
||||
} else { // try to match
|
||||
auto v = act->Results()[0];
|
||||
@@ -915,15 +918,16 @@ void StepStmt() {
|
||||
std::optional<Env> matches =
|
||||
PatternMatch(pat, v, values, &vars, stmt->LineNumber());
|
||||
if (matches) { // we have a match, start the body
|
||||
auto* new_scope = global_arena->New<Scope>(*matches, vars);
|
||||
auto* new_scope = global_arena->RawNew<Scope>(*matches, vars);
|
||||
frame->scopes.Push(new_scope);
|
||||
const Statement* body_block =
|
||||
global_arena->New<Block>(stmt->LineNumber(), c->second);
|
||||
Action* body_act = global_arena->New<StatementAction>(body_block);
|
||||
global_arena->RawNew<Block>(stmt->LineNumber(), c->second);
|
||||
Action* body_act =
|
||||
global_arena->RawNew<StatementAction>(body_block);
|
||||
body_act->IncrementPos();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(body_act);
|
||||
frame->todo.Push(global_arena->New<StatementAction>(c->second));
|
||||
frame->todo.Push(global_arena->RawNew<StatementAction>(c->second));
|
||||
} else {
|
||||
// this case did not match, moving on
|
||||
act->IncrementPos();
|
||||
@@ -941,14 +945,14 @@ void StepStmt() {
|
||||
// { { (while (e) s) :: C, E, F} :: S, H}
|
||||
// -> { { e :: (while ([]) s) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(cast<While>(*stmt).Cond()));
|
||||
global_arena->RawNew<ExpressionAction>(cast<While>(*stmt).Cond()));
|
||||
act->IncrementPos();
|
||||
} else if (cast<BoolValue>(*act->Results()[0]).Val()) {
|
||||
// { {true :: (while ([]) s) :: C, E, F} :: S, H}
|
||||
// -> { { s :: (while (e) s) :: C, E, F } :: S, H}
|
||||
frame->todo.Top()->Clear();
|
||||
frame->todo.Push(
|
||||
global_arena->New<StatementAction>(cast<While>(*stmt).Body()));
|
||||
global_arena->RawNew<StatementAction>(cast<While>(*stmt).Body()));
|
||||
} else {
|
||||
// { {false :: (while ([]) s) :: C, E, F} :: S, H}
|
||||
// -> { { C, E, F } :: S, H}
|
||||
@@ -986,11 +990,11 @@ void StepStmt() {
|
||||
case Statement::Kind::Block: {
|
||||
if (act->Pos() == 0) {
|
||||
if (cast<Block>(*stmt).Stmt()) {
|
||||
auto* scope = global_arena->New<Scope>(CurrentEnv(state),
|
||||
std::list<std::string>());
|
||||
auto* scope = global_arena->RawNew<Scope>(CurrentEnv(state),
|
||||
std::list<std::string>());
|
||||
frame->scopes.Push(scope);
|
||||
frame->todo.Push(
|
||||
global_arena->New<StatementAction>(cast<Block>(*stmt).Stmt()));
|
||||
global_arena->RawNew<StatementAction>(cast<Block>(*stmt).Stmt()));
|
||||
act->IncrementPos();
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
@@ -1008,11 +1012,11 @@ void StepStmt() {
|
||||
if (act->Pos() == 0) {
|
||||
// { {(var x = e) :: C, E, F} :: S, H}
|
||||
// -> { {e :: (var x = []) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<VariableDefinition>(*stmt).Init()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
frame->todo.Push(global_arena->New<PatternAction>(
|
||||
frame->todo.Push(global_arena->RawNew<PatternAction>(
|
||||
cast<VariableDefinition>(*stmt).Pat()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
@@ -1035,7 +1039,7 @@ void StepStmt() {
|
||||
if (act->Pos() == 0) {
|
||||
// { {e :: C, E, F} :: S, H}
|
||||
// -> { {e :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<ExpressionStatement>(*stmt).Exp()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
@@ -1047,13 +1051,13 @@ void StepStmt() {
|
||||
// { {(lv = e) :: C, E, F} :: S, H}
|
||||
// -> { {lv :: ([] = e) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<LValAction>(cast<Assign>(*stmt).Lhs()));
|
||||
global_arena->RawNew<LValAction>(cast<Assign>(*stmt).Lhs()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
// { { a :: ([] = e) :: C, E, F} :: S, H}
|
||||
// -> { { e :: (a = []) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(cast<Assign>(*stmt).Rhs()));
|
||||
global_arena->RawNew<ExpressionAction>(cast<Assign>(*stmt).Rhs()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
// { { v :: (a = []) :: C, E, F} :: S, H}
|
||||
@@ -1069,7 +1073,7 @@ void StepStmt() {
|
||||
// { {(if (e) then_stmt else else_stmt) :: C, E, F} :: S, H}
|
||||
// -> { { e :: (if ([]) then_stmt else else_stmt) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(cast<If>(*stmt).Cond()));
|
||||
global_arena->RawNew<ExpressionAction>(cast<If>(*stmt).Cond()));
|
||||
act->IncrementPos();
|
||||
} else if (cast<BoolValue>(*act->Results()[0]).Val()) {
|
||||
// { {true :: if ([]) then_stmt else else_stmt :: C, E, F} ::
|
||||
@@ -1077,14 +1081,14 @@ void StepStmt() {
|
||||
// -> { { then_stmt :: C, E, F } :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(
|
||||
global_arena->New<StatementAction>(cast<If>(*stmt).ThenStmt()));
|
||||
global_arena->RawNew<StatementAction>(cast<If>(*stmt).ThenStmt()));
|
||||
} else if (cast<If>(*stmt).ElseStmt()) {
|
||||
// { {false :: if ([]) then_stmt else else_stmt :: C, E, F} ::
|
||||
// S, H}
|
||||
// -> { { else_stmt :: C, E, F } :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(
|
||||
global_arena->New<StatementAction>(cast<If>(*stmt).ElseStmt()));
|
||||
global_arena->RawNew<StatementAction>(cast<If>(*stmt).ElseStmt()));
|
||||
} else {
|
||||
frame->todo.Pop(1);
|
||||
}
|
||||
@@ -1094,7 +1098,7 @@ void StepStmt() {
|
||||
// { {return e :: C, E, F} :: S, H}
|
||||
// -> { {e :: return [] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(cast<Return>(*stmt).Exp()));
|
||||
global_arena->RawNew<ExpressionAction>(cast<Return>(*stmt).Exp()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
// { {v :: return [] :: C, E, F} :: {C', E', F'} :: S, H}
|
||||
@@ -1103,7 +1107,7 @@ void StepStmt() {
|
||||
DeallocateLocals(stmt->LineNumber(), frame);
|
||||
state->stack.Pop(1);
|
||||
frame = state->stack.Top();
|
||||
frame->todo.Push(global_arena->New<ValAction>(ret_val));
|
||||
frame->todo.Push(global_arena->RawNew<ValAction>(ret_val));
|
||||
}
|
||||
break;
|
||||
case Statement::Kind::Sequence:
|
||||
@@ -1112,30 +1116,30 @@ void StepStmt() {
|
||||
// -> { { s1 :: s2 :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
if (cast<Sequence>(*stmt).Next()) {
|
||||
frame->todo.Push(
|
||||
global_arena->New<StatementAction>(cast<Sequence>(*stmt).Next()));
|
||||
frame->todo.Push(global_arena->RawNew<StatementAction>(
|
||||
cast<Sequence>(*stmt).Next()));
|
||||
}
|
||||
frame->todo.Push(
|
||||
global_arena->New<StatementAction>(cast<Sequence>(*stmt).Stmt()));
|
||||
global_arena->RawNew<StatementAction>(cast<Sequence>(*stmt).Stmt()));
|
||||
break;
|
||||
case Statement::Kind::Continuation: {
|
||||
CHECK(act->Pos() == 0);
|
||||
// Create a continuation object by creating a frame similar the
|
||||
// way one is created in a function call.
|
||||
Scope* scope =
|
||||
global_arena->New<Scope>(CurrentEnv(state), std::list<std::string>());
|
||||
Scope* scope = global_arena->RawNew<Scope>(CurrentEnv(state),
|
||||
std::list<std::string>());
|
||||
Stack<Scope*> scopes;
|
||||
scopes.Push(scope);
|
||||
Stack<Action*> todo;
|
||||
todo.Push(global_arena->New<StatementAction>(
|
||||
global_arena->New<Return>(stmt->LineNumber(), nullptr,
|
||||
/*is_omitted_exp=*/true)));
|
||||
todo.Push(
|
||||
global_arena->New<StatementAction>(cast<Continuation>(*stmt).Body()));
|
||||
todo.Push(global_arena->RawNew<StatementAction>(
|
||||
global_arena->RawNew<Return>(stmt->LineNumber(), nullptr,
|
||||
/*is_omitted_exp=*/true)));
|
||||
todo.Push(global_arena->RawNew<StatementAction>(
|
||||
cast<Continuation>(*stmt).Body()));
|
||||
Frame* continuation_frame =
|
||||
global_arena->New<Frame>("__continuation", scopes, todo);
|
||||
global_arena->RawNew<Frame>("__continuation", scopes, todo);
|
||||
Address continuation_address =
|
||||
state->heap.AllocateValue(global_arena->New<ContinuationValue>(
|
||||
state->heap.AllocateValue(global_arena->RawNew<ContinuationValue>(
|
||||
std::vector<Frame*>({continuation_frame})));
|
||||
// Store the continuation's address in the frame.
|
||||
continuation_frame->continuation = continuation_address;
|
||||
@@ -1150,17 +1154,17 @@ void StepStmt() {
|
||||
case Statement::Kind::Run:
|
||||
if (act->Pos() == 0) {
|
||||
// Evaluate the argument of the run statement.
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(cast<Run>(*stmt).Argument()));
|
||||
frame->todo.Push(global_arena->RawNew<ExpressionAction>(
|
||||
cast<Run>(*stmt).Argument()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
frame->todo.Pop(1);
|
||||
// Push an expression statement action to ignore the result
|
||||
// value from the continuation.
|
||||
Action* ignore_result = global_arena->New<StatementAction>(
|
||||
global_arena->New<ExpressionStatement>(
|
||||
Action* ignore_result = global_arena->RawNew<StatementAction>(
|
||||
global_arena->RawNew<ExpressionStatement>(
|
||||
stmt->LineNumber(),
|
||||
global_arena->New<TupleLiteral>(stmt->LineNumber())));
|
||||
global_arena->RawNew<TupleLiteral>(stmt->LineNumber())));
|
||||
frame->todo.Push(ignore_result);
|
||||
// Push the continuation onto the current stack.
|
||||
const std::vector<Frame*>& continuation_vector =
|
||||
@@ -1181,7 +1185,7 @@ void StepStmt() {
|
||||
} while (paused.back()->continuation == std::nullopt);
|
||||
// Update the continuation with the paused stack.
|
||||
state->heap.Write(*paused.back()->continuation,
|
||||
global_arena->New<ContinuationValue>(paused),
|
||||
global_arena->RawNew<ContinuationValue>(paused),
|
||||
stmt->LineNumber());
|
||||
break;
|
||||
}
|
||||
@@ -1220,18 +1224,18 @@ void Step() {
|
||||
|
||||
// Interpret the whole porogram.
|
||||
auto InterpProgram(const std::list<const Declaration*>& fs) -> int {
|
||||
state = global_arena->New<State>(); // Runtime state.
|
||||
state = global_arena->RawNew<State>(); // Runtime state.
|
||||
if (tracing_output) {
|
||||
llvm::outs() << "********** initializing globals **********\n";
|
||||
}
|
||||
InitGlobals(fs);
|
||||
|
||||
const Expression* arg = global_arena->New<TupleLiteral>(0);
|
||||
const Expression* call_main = global_arena->New<CallExpression>(
|
||||
0, global_arena->New<IdentifierExpression>(0, "main"), arg);
|
||||
auto todo = Stack<Action*>(global_arena->New<ExpressionAction>(call_main));
|
||||
auto* scope = global_arena->New<Scope>(globals, std::list<std::string>());
|
||||
auto* frame = global_arena->New<Frame>("top", Stack(scope), todo);
|
||||
const Expression* arg = global_arena->RawNew<TupleLiteral>(0);
|
||||
const Expression* call_main = global_arena->RawNew<CallExpression>(
|
||||
0, global_arena->RawNew<IdentifierExpression>(0, "main"), arg);
|
||||
auto todo = Stack<Action*>(global_arena->RawNew<ExpressionAction>(call_main));
|
||||
auto* scope = global_arena->RawNew<Scope>(globals, std::list<std::string>());
|
||||
auto* frame = global_arena->RawNew<Frame>("top", Stack(scope), todo);
|
||||
state->stack = Stack(frame);
|
||||
|
||||
if (tracing_output) {
|
||||
@@ -1252,9 +1256,9 @@ auto InterpProgram(const std::list<const Declaration*>& fs) -> int {
|
||||
|
||||
// Interpret an expression at compile-time.
|
||||
auto InterpExp(Env values, const Expression* e) -> const Value* {
|
||||
auto todo = Stack<Action*>(global_arena->New<ExpressionAction>(e));
|
||||
auto* scope = global_arena->New<Scope>(values, std::list<std::string>());
|
||||
auto* frame = global_arena->New<Frame>("InterpExp", Stack(scope), todo);
|
||||
auto todo = Stack<Action*>(global_arena->RawNew<ExpressionAction>(e));
|
||||
auto* scope = global_arena->RawNew<Scope>(values, std::list<std::string>());
|
||||
auto* frame = global_arena->RawNew<Frame>("InterpExp", Stack(scope), todo);
|
||||
state->stack = Stack(frame);
|
||||
|
||||
while (state->stack.Count() > 1 || state->stack.Top()->todo.Count() > 1 ||
|
||||
@@ -1266,9 +1270,10 @@ auto InterpExp(Env values, const Expression* e) -> const Value* {
|
||||
|
||||
// Interpret a pattern at compile-time.
|
||||
auto InterpPattern(Env values, const Pattern* p) -> const Value* {
|
||||
auto todo = Stack<Action*>(global_arena->New<PatternAction>(p));
|
||||
auto* scope = global_arena->New<Scope>(values, std::list<std::string>());
|
||||
auto* frame = global_arena->New<Frame>("InterpPattern", Stack(scope), todo);
|
||||
auto todo = Stack<Action*>(global_arena->RawNew<PatternAction>(p));
|
||||
auto* scope = global_arena->RawNew<Scope>(values, std::list<std::string>());
|
||||
auto* frame =
|
||||
global_arena->RawNew<Frame>("InterpPattern", Stack(scope), todo);
|
||||
state->stack = Stack(frame);
|
||||
|
||||
while (state->stack.Count() > 1 || state->stack.Top()->todo.Count() > 1 ||
|
||||
|
||||
@@ -46,16 +46,16 @@ static void ExpectPointerType(int line_num, const std::string& context,
|
||||
static auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
switch (t->Tag()) {
|
||||
case Value::Kind::IntType:
|
||||
return global_arena->New<IntTypeLiteral>(0);
|
||||
return global_arena->RawNew<IntTypeLiteral>(0);
|
||||
case Value::Kind::BoolType:
|
||||
return global_arena->New<BoolTypeLiteral>(0);
|
||||
return global_arena->RawNew<BoolTypeLiteral>(0);
|
||||
case Value::Kind::TypeType:
|
||||
return global_arena->New<TypeTypeLiteral>(0);
|
||||
return global_arena->RawNew<TypeTypeLiteral>(0);
|
||||
case Value::Kind::ContinuationType:
|
||||
return global_arena->New<ContinuationTypeLiteral>(0);
|
||||
return global_arena->RawNew<ContinuationTypeLiteral>(0);
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn_type = cast<FunctionType>(*t);
|
||||
return global_arena->New<FunctionTypeLiteral>(
|
||||
return global_arena->RawNew<FunctionTypeLiteral>(
|
||||
0, ReifyType(fn_type.Param(), line_num),
|
||||
ReifyType(fn_type.Ret(), line_num),
|
||||
/*is_omitted_return_type=*/false);
|
||||
@@ -66,24 +66,24 @@ static auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
args.push_back(
|
||||
FieldInitializer(field.name, ReifyType(field.value, line_num)));
|
||||
}
|
||||
return global_arena->New<TupleLiteral>(0, args);
|
||||
return global_arena->RawNew<TupleLiteral>(0, args);
|
||||
}
|
||||
case Value::Kind::StructType:
|
||||
return global_arena->New<IdentifierExpression>(
|
||||
return global_arena->RawNew<IdentifierExpression>(
|
||||
0, cast<StructType>(*t).Name());
|
||||
case Value::Kind::ChoiceType:
|
||||
return global_arena->New<IdentifierExpression>(
|
||||
return global_arena->RawNew<IdentifierExpression>(
|
||||
0, cast<ChoiceType>(*t).Name());
|
||||
case Value::Kind::PointerType:
|
||||
return global_arena->New<PrimitiveOperatorExpression>(
|
||||
return global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
0, Operator::Ptr,
|
||||
std::vector<const Expression*>(
|
||||
{ReifyType(cast<PointerType>(*t).Type(), line_num)}));
|
||||
case Value::Kind::VariableType:
|
||||
return global_arena->New<IdentifierExpression>(
|
||||
return global_arena->RawNew<IdentifierExpression>(
|
||||
0, cast<VariableType>(*t).Name());
|
||||
case Value::Kind::StringType:
|
||||
return global_arena->New<StringTypeLiteral>(0);
|
||||
return global_arena->RawNew<StringTypeLiteral>(0);
|
||||
case Value::Kind::AlternativeConstructorValue:
|
||||
case Value::Kind::AlternativeValue:
|
||||
case Value::Kind::AutoType:
|
||||
@@ -206,17 +206,17 @@ static auto Substitute(TypeEnv dict, const Value* type) -> const Value* {
|
||||
auto t = Substitute(dict, elt.value);
|
||||
elts.push_back({.name = elt.name, .value = t});
|
||||
}
|
||||
return global_arena->New<TupleValue>(elts);
|
||||
return global_arena->RawNew<TupleValue>(elts);
|
||||
}
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn_type = cast<FunctionType>(*type);
|
||||
auto param = Substitute(dict, fn_type.Param());
|
||||
auto ret = Substitute(dict, fn_type.Ret());
|
||||
return global_arena->New<FunctionType>(std::vector<GenericBinding>(),
|
||||
param, ret);
|
||||
return global_arena->RawNew<FunctionType>(std::vector<GenericBinding>(),
|
||||
param, ret);
|
||||
}
|
||||
case Value::Kind::PointerType: {
|
||||
return global_arena->New<PointerType>(
|
||||
return global_arena->RawNew<PointerType>(
|
||||
Substitute(dict, cast<PointerType>(*type).Type()));
|
||||
}
|
||||
case Value::Kind::AutoType:
|
||||
@@ -275,9 +275,9 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "field " << f << " is not in the tuple " << *t;
|
||||
}
|
||||
auto new_e = global_arena->New<IndexExpression>(
|
||||
auto new_e = global_arena->RawNew<IndexExpression>(
|
||||
e->LineNumber(), res.exp,
|
||||
global_arena->New<IntLiteral>(e->LineNumber(), i));
|
||||
global_arena->RawNew<IntLiteral>(e->LineNumber(), i));
|
||||
return TCExpression(new_e, field_t, res.types);
|
||||
}
|
||||
default:
|
||||
@@ -294,8 +294,9 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
new_args.push_back(FieldInitializer(arg.name, arg_res.exp));
|
||||
arg_types.push_back({.name = arg.name, .value = arg_res.type});
|
||||
}
|
||||
auto tuple_e = global_arena->New<TupleLiteral>(e->LineNumber(), new_args);
|
||||
auto tuple_t = global_arena->New<TupleValue>(std::move(arg_types));
|
||||
auto tuple_e =
|
||||
global_arena->RawNew<TupleLiteral>(e->LineNumber(), new_args);
|
||||
auto tuple_t = global_arena->RawNew<TupleValue>(std::move(arg_types));
|
||||
return TCExpression(tuple_e, tuple_t, new_types);
|
||||
}
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
@@ -309,7 +310,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
for (auto& field : t_struct.Fields()) {
|
||||
if (access.Field() == field.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->New<FieldAccessExpression>(
|
||||
global_arena->RawNew<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
return TCExpression(new_e, field.second, res.types);
|
||||
}
|
||||
@@ -318,7 +319,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
for (auto& method : t_struct.Methods()) {
|
||||
if (access.Field() == method.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->New<FieldAccessExpression>(
|
||||
global_arena->RawNew<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
return TCExpression(new_e, method.second, res.types);
|
||||
}
|
||||
@@ -331,7 +332,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
const auto& tup = cast<TupleValue>(*t);
|
||||
for (const TupleElement& field : tup.Elements()) {
|
||||
if (access.Field() == field.name) {
|
||||
auto new_e = global_arena->New<FieldAccessExpression>(
|
||||
auto new_e = global_arena->RawNew<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
return TCExpression(new_e, field.value, res.types);
|
||||
}
|
||||
@@ -345,9 +346,9 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
for (const auto& vt : choice.Alternatives()) {
|
||||
if (access.Field() == vt.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->New<FieldAccessExpression>(
|
||||
global_arena->RawNew<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
auto fun_ty = global_arena->New<FunctionType>(
|
||||
auto fun_ty = global_arena->RawNew<FunctionType>(
|
||||
std::vector<GenericBinding>(), vt.second, t);
|
||||
return TCExpression(new_e, fun_ty, res.types);
|
||||
}
|
||||
@@ -373,9 +374,9 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
}
|
||||
}
|
||||
case Expression::Kind::IntLiteral:
|
||||
return TCExpression(e, global_arena->New<IntType>(), types);
|
||||
return TCExpression(e, global_arena->RawNew<IntType>(), types);
|
||||
case Expression::Kind::BoolLiteral:
|
||||
return TCExpression(e, global_arena->New<BoolType>(), types);
|
||||
return TCExpression(e, global_arena->RawNew<BoolType>(), types);
|
||||
case Expression::Kind::PrimitiveOperatorExpression: {
|
||||
const auto& op = cast<PrimitiveOperatorExpression>(*e);
|
||||
std::vector<const Expression*> es;
|
||||
@@ -387,58 +388,67 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
es.push_back(res.exp);
|
||||
ts.push_back(res.type);
|
||||
}
|
||||
auto new_e = global_arena->New<PrimitiveOperatorExpression>(
|
||||
auto new_e = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
e->LineNumber(), op.Op(), es);
|
||||
switch (op.Op()) {
|
||||
case Operator::Neg:
|
||||
ExpectType(e->LineNumber(), "negation", global_arena->New<IntType>(),
|
||||
ts[0]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
ExpectType(e->LineNumber(), "negation",
|
||||
global_arena->RawNew<IntType>(), ts[0]);
|
||||
return TCExpression(new_e, global_arena->RawNew<IntType>(),
|
||||
new_types);
|
||||
case Operator::Add:
|
||||
ExpectType(e->LineNumber(), "addition(1)",
|
||||
global_arena->New<IntType>(), ts[0]);
|
||||
global_arena->RawNew<IntType>(), ts[0]);
|
||||
ExpectType(e->LineNumber(), "addition(2)",
|
||||
global_arena->New<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
global_arena->RawNew<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->RawNew<IntType>(),
|
||||
new_types);
|
||||
case Operator::Sub:
|
||||
ExpectType(e->LineNumber(), "subtraction(1)",
|
||||
global_arena->New<IntType>(), ts[0]);
|
||||
global_arena->RawNew<IntType>(), ts[0]);
|
||||
ExpectType(e->LineNumber(), "subtraction(2)",
|
||||
global_arena->New<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
global_arena->RawNew<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->RawNew<IntType>(),
|
||||
new_types);
|
||||
case Operator::Mul:
|
||||
ExpectType(e->LineNumber(), "multiplication(1)",
|
||||
global_arena->New<IntType>(), ts[0]);
|
||||
global_arena->RawNew<IntType>(), ts[0]);
|
||||
ExpectType(e->LineNumber(), "multiplication(2)",
|
||||
global_arena->New<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
global_arena->RawNew<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->RawNew<IntType>(),
|
||||
new_types);
|
||||
case Operator::And:
|
||||
ExpectType(e->LineNumber(), "&&(1)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "&&(1)", global_arena->RawNew<BoolType>(),
|
||||
ts[0]);
|
||||
ExpectType(e->LineNumber(), "&&(2)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "&&(2)", global_arena->RawNew<BoolType>(),
|
||||
ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
return TCExpression(new_e, global_arena->RawNew<BoolType>(),
|
||||
new_types);
|
||||
case Operator::Or:
|
||||
ExpectType(e->LineNumber(), "||(1)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "||(1)", global_arena->RawNew<BoolType>(),
|
||||
ts[0]);
|
||||
ExpectType(e->LineNumber(), "||(2)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "||(2)", global_arena->RawNew<BoolType>(),
|
||||
ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
return TCExpression(new_e, global_arena->RawNew<BoolType>(),
|
||||
new_types);
|
||||
case Operator::Not:
|
||||
ExpectType(e->LineNumber(), "!", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "!", global_arena->RawNew<BoolType>(),
|
||||
ts[0]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
return TCExpression(new_e, global_arena->RawNew<BoolType>(),
|
||||
new_types);
|
||||
case Operator::Eq:
|
||||
ExpectType(e->LineNumber(), "==", ts[0], ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
return TCExpression(new_e, global_arena->RawNew<BoolType>(),
|
||||
new_types);
|
||||
case Operator::Deref:
|
||||
ExpectPointerType(e->LineNumber(), "*", ts[0]);
|
||||
return TCExpression(new_e, cast<PointerType>(*ts[0]).Type(),
|
||||
new_types);
|
||||
case Operator::Ptr:
|
||||
ExpectType(e->LineNumber(), "*", global_arena->New<TypeType>(),
|
||||
ExpectType(e->LineNumber(), "*", global_arena->RawNew<TypeType>(),
|
||||
ts[0]);
|
||||
return TCExpression(new_e, global_arena->New<TypeType>(), new_types);
|
||||
return TCExpression(new_e, global_arena->RawNew<TypeType>(),
|
||||
new_types);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -468,7 +478,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
} else {
|
||||
ExpectType(e->LineNumber(), "call", parameter_type, arg_res.type);
|
||||
}
|
||||
auto new_e = global_arena->New<CallExpression>(
|
||||
auto new_e = global_arena->RawNew<CallExpression>(
|
||||
e->LineNumber(), fun_res.exp, arg_res.exp);
|
||||
return TCExpression(new_e, return_type, arg_res.types);
|
||||
}
|
||||
@@ -484,14 +494,14 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
const auto& fn = cast<FunctionTypeLiteral>(*e);
|
||||
auto pt = InterpExp(values, fn.Parameter());
|
||||
auto rt = InterpExp(values, fn.ReturnType());
|
||||
auto new_e = global_arena->New<FunctionTypeLiteral>(
|
||||
auto new_e = global_arena->RawNew<FunctionTypeLiteral>(
|
||||
e->LineNumber(), ReifyType(pt, e->LineNumber()),
|
||||
ReifyType(rt, e->LineNumber()),
|
||||
/*is_omitted_return_type=*/false);
|
||||
return TCExpression(new_e, global_arena->New<TypeType>(), types);
|
||||
return TCExpression(new_e, global_arena->RawNew<TypeType>(), types);
|
||||
}
|
||||
case Expression::Kind::StringLiteral:
|
||||
return TCExpression(e, global_arena->New<StringType>(), types);
|
||||
return TCExpression(e, global_arena->RawNew<StringType>(), types);
|
||||
case Expression::Kind::IntrinsicExpression:
|
||||
switch (cast<IntrinsicExpression>(*e).Intrinsic()) {
|
||||
case IntrinsicExpression::IntrinsicKind::Print:
|
||||
@@ -502,7 +512,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
case Expression::Kind::StringTypeLiteral:
|
||||
case Expression::Kind::TypeTypeLiteral:
|
||||
case Expression::Kind::ContinuationTypeLiteral:
|
||||
return TCExpression(e, global_arena->New<TypeType>(), types);
|
||||
return TCExpression(e, global_arena->RawNew<TypeType>(), types);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,8 +530,9 @@ auto TypeCheckPattern(const Pattern* p, TypeEnv types, Env values,
|
||||
}
|
||||
switch (p->Tag()) {
|
||||
case Pattern::Kind::AutoPattern: {
|
||||
return {
|
||||
.pattern = p, .type = global_arena->New<TypeType>(), .types = types};
|
||||
return {.pattern = p,
|
||||
.type = global_arena->RawNew<TypeType>(),
|
||||
.types = types};
|
||||
}
|
||||
case Pattern::Kind::BindingPattern: {
|
||||
const auto& binding = cast<BindingPattern>(*p);
|
||||
@@ -552,9 +563,9 @@ auto TypeCheckPattern(const Pattern* p, TypeEnv types, Env values,
|
||||
FATAL_COMPILATION_ERROR(binding.LineNumber())
|
||||
<< "Unsupported type pattern";
|
||||
}
|
||||
auto new_p = global_arena->New<BindingPattern>(
|
||||
auto new_p = global_arena->RawNew<BindingPattern>(
|
||||
binding.LineNumber(), binding.Name(),
|
||||
global_arena->New<ExpressionPattern>(
|
||||
global_arena->RawNew<ExpressionPattern>(
|
||||
ReifyType(type, binding.LineNumber())));
|
||||
if (binding.Name().has_value()) {
|
||||
types.Set(*binding.Name(), type);
|
||||
@@ -595,8 +606,8 @@ auto TypeCheckPattern(const Pattern* p, TypeEnv types, Env values,
|
||||
field_types.push_back({.name = field.name, .value = field_result.type});
|
||||
}
|
||||
auto new_tuple =
|
||||
global_arena->New<TuplePattern>(tuple.LineNumber(), new_fields);
|
||||
auto tuple_t = global_arena->New<TupleValue>(std::move(field_types));
|
||||
global_arena->RawNew<TuplePattern>(tuple.LineNumber(), new_fields);
|
||||
auto tuple_t = global_arena->RawNew<TupleValue>(std::move(field_types));
|
||||
return {.pattern = new_tuple, .type = tuple_t, .types = new_types};
|
||||
}
|
||||
case Pattern::Kind::AlternativePattern: {
|
||||
@@ -620,7 +631,7 @@ auto TypeCheckPattern(const Pattern* p, TypeEnv types, Env values,
|
||||
}
|
||||
TCPattern arg_results = TypeCheckPattern(alternative.Arguments(), types,
|
||||
values, parameter_types);
|
||||
return {.pattern = global_arena->New<AlternativePattern>(
|
||||
return {.pattern = global_arena->RawNew<AlternativePattern>(
|
||||
alternative.LineNumber(),
|
||||
ReifyType(choice_type, alternative.LineNumber()),
|
||||
alternative.AlternativeName(),
|
||||
@@ -631,7 +642,7 @@ auto TypeCheckPattern(const Pattern* p, TypeEnv types, Env values,
|
||||
case Pattern::Kind::ExpressionPattern: {
|
||||
TCExpression result =
|
||||
TypeCheckExp(cast<ExpressionPattern>(p)->Expression(), types, values);
|
||||
return {.pattern = global_arena->New<ExpressionPattern>(result.exp),
|
||||
return {.pattern = global_arena->RawNew<ExpressionPattern>(result.exp),
|
||||
.type = result.type,
|
||||
.types = result.types};
|
||||
}
|
||||
@@ -666,27 +677,26 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
const auto& match = cast<Match>(*s);
|
||||
auto res = TypeCheckExp(match.Exp(), types, values);
|
||||
auto res_type = res.type;
|
||||
auto new_clauses =
|
||||
global_arena
|
||||
->New<std::list<std::pair<const Pattern*, const Statement*>>>();
|
||||
auto new_clauses = global_arena->RawNew<
|
||||
std::list<std::pair<const Pattern*, const Statement*>>>();
|
||||
for (auto& clause : *match.Clauses()) {
|
||||
new_clauses->push_back(TypecheckCase(res_type, clause.first,
|
||||
clause.second, types, values,
|
||||
ret_type, is_omitted_ret_type));
|
||||
}
|
||||
const Statement* new_s =
|
||||
global_arena->New<Match>(s->LineNumber(), res.exp, new_clauses);
|
||||
global_arena->RawNew<Match>(s->LineNumber(), res.exp, new_clauses);
|
||||
return TCStatement(new_s, types);
|
||||
}
|
||||
case Statement::Kind::While: {
|
||||
const auto& while_stmt = cast<While>(*s);
|
||||
auto cnd_res = TypeCheckExp(while_stmt.Cond(), types, values);
|
||||
ExpectType(s->LineNumber(), "condition of `while`",
|
||||
global_arena->New<BoolType>(), cnd_res.type);
|
||||
global_arena->RawNew<BoolType>(), cnd_res.type);
|
||||
auto body_res = TypeCheckStmt(while_stmt.Body(), types, values, ret_type,
|
||||
is_omitted_ret_type);
|
||||
auto new_s =
|
||||
global_arena->New<While>(s->LineNumber(), cnd_res.exp, body_res.stmt);
|
||||
auto new_s = global_arena->RawNew<While>(s->LineNumber(), cnd_res.exp,
|
||||
body_res.stmt);
|
||||
return TCStatement(new_s, types);
|
||||
}
|
||||
case Statement::Kind::Break:
|
||||
@@ -696,14 +706,14 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
auto stmt_res = TypeCheckStmt(cast<Block>(*s).Stmt(), types, values,
|
||||
ret_type, is_omitted_ret_type);
|
||||
return TCStatement(
|
||||
global_arena->New<Block>(s->LineNumber(), stmt_res.stmt), types);
|
||||
global_arena->RawNew<Block>(s->LineNumber(), stmt_res.stmt), types);
|
||||
}
|
||||
case Statement::Kind::VariableDefinition: {
|
||||
const auto& var = cast<VariableDefinition>(*s);
|
||||
auto res = TypeCheckExp(var.Init(), types, values);
|
||||
const Value* rhs_ty = res.type;
|
||||
auto lhs_res = TypeCheckPattern(var.Pat(), types, values, rhs_ty);
|
||||
const Statement* new_s = global_arena->New<VariableDefinition>(
|
||||
const Statement* new_s = global_arena->RawNew<VariableDefinition>(
|
||||
s->LineNumber(), var.Pat(), res.exp);
|
||||
return TCStatement(new_s, lhs_res.types);
|
||||
}
|
||||
@@ -715,7 +725,7 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
auto next_res = TypeCheckStmt(seq.Next(), types2, values, ret_type,
|
||||
is_omitted_ret_type);
|
||||
auto types3 = next_res.types;
|
||||
return TCStatement(global_arena->New<Sequence>(
|
||||
return TCStatement(global_arena->RawNew<Sequence>(
|
||||
s->LineNumber(), stmt_res.stmt, next_res.stmt),
|
||||
types3);
|
||||
}
|
||||
@@ -726,28 +736,28 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
auto lhs_res = TypeCheckExp(assign.Lhs(), types, values);
|
||||
auto lhs_t = lhs_res.type;
|
||||
ExpectType(s->LineNumber(), "assign", lhs_t, rhs_t);
|
||||
auto new_s =
|
||||
global_arena->New<Assign>(s->LineNumber(), lhs_res.exp, rhs_res.exp);
|
||||
auto new_s = global_arena->RawNew<Assign>(s->LineNumber(), lhs_res.exp,
|
||||
rhs_res.exp);
|
||||
return TCStatement(new_s, lhs_res.types);
|
||||
}
|
||||
case Statement::Kind::ExpressionStatement: {
|
||||
auto res =
|
||||
TypeCheckExp(cast<ExpressionStatement>(*s).Exp(), types, values);
|
||||
auto new_s =
|
||||
global_arena->New<ExpressionStatement>(s->LineNumber(), res.exp);
|
||||
global_arena->RawNew<ExpressionStatement>(s->LineNumber(), res.exp);
|
||||
return TCStatement(new_s, types);
|
||||
}
|
||||
case Statement::Kind::If: {
|
||||
const auto& if_stmt = cast<If>(*s);
|
||||
auto cnd_res = TypeCheckExp(if_stmt.Cond(), types, values);
|
||||
ExpectType(s->LineNumber(), "condition of `if`",
|
||||
global_arena->New<BoolType>(), cnd_res.type);
|
||||
global_arena->RawNew<BoolType>(), cnd_res.type);
|
||||
auto then_res = TypeCheckStmt(if_stmt.ThenStmt(), types, values, ret_type,
|
||||
is_omitted_ret_type);
|
||||
auto else_res = TypeCheckStmt(if_stmt.ElseStmt(), types, values, ret_type,
|
||||
is_omitted_ret_type);
|
||||
auto new_s = global_arena->New<If>(s->LineNumber(), cnd_res.exp,
|
||||
then_res.stmt, else_res.stmt);
|
||||
auto new_s = global_arena->RawNew<If>(s->LineNumber(), cnd_res.exp,
|
||||
then_res.stmt, else_res.stmt);
|
||||
return TCStatement(new_s, types);
|
||||
}
|
||||
case Statement::Kind::Return: {
|
||||
@@ -766,27 +776,28 @@ auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
|
||||
<< *s << " should" << (is_omitted_ret_type ? " not" : "")
|
||||
<< " provide a return value, to match the function's signature.";
|
||||
}
|
||||
return TCStatement(global_arena->New<Return>(s->LineNumber(), res.exp,
|
||||
ret.IsOmittedExp()),
|
||||
return TCStatement(global_arena->RawNew<Return>(s->LineNumber(), res.exp,
|
||||
ret.IsOmittedExp()),
|
||||
types);
|
||||
}
|
||||
case Statement::Kind::Continuation: {
|
||||
const auto& cont = cast<Continuation>(*s);
|
||||
TCStatement body_result = TypeCheckStmt(cont.Body(), types, values,
|
||||
ret_type, is_omitted_ret_type);
|
||||
const Statement* new_continuation = global_arena->New<Continuation>(
|
||||
const Statement* new_continuation = global_arena->RawNew<Continuation>(
|
||||
s->LineNumber(), cont.ContinuationVariable(), body_result.stmt);
|
||||
types.Set(cont.ContinuationVariable(),
|
||||
global_arena->New<ContinuationType>());
|
||||
global_arena->RawNew<ContinuationType>());
|
||||
return TCStatement(new_continuation, types);
|
||||
}
|
||||
case Statement::Kind::Run: {
|
||||
TCExpression argument_result =
|
||||
TypeCheckExp(cast<Run>(*s).Argument(), types, values);
|
||||
ExpectType(s->LineNumber(), "argument of `run`",
|
||||
global_arena->New<ContinuationType>(), argument_result.type);
|
||||
global_arena->RawNew<ContinuationType>(),
|
||||
argument_result.type);
|
||||
const Statement* new_run =
|
||||
global_arena->New<Run>(s->LineNumber(), argument_result.exp);
|
||||
global_arena->RawNew<Run>(s->LineNumber(), argument_result.exp);
|
||||
return TCStatement(new_run, types);
|
||||
}
|
||||
case Statement::Kind::Await: {
|
||||
@@ -800,8 +811,8 @@ static auto CheckOrEnsureReturn(const Statement* stmt, bool omitted_ret_type,
|
||||
int line_num) -> const Statement* {
|
||||
if (!stmt) {
|
||||
if (omitted_ret_type) {
|
||||
return global_arena->New<Return>(line_num, nullptr,
|
||||
/*is_omitted_exp=*/true);
|
||||
return global_arena->RawNew<Return>(line_num, nullptr,
|
||||
/*is_omitted_exp=*/true);
|
||||
} else {
|
||||
FATAL_COMPILATION_ERROR(line_num)
|
||||
<< "control-flow reaches end of function that provides a `->` return "
|
||||
@@ -811,25 +822,24 @@ static auto CheckOrEnsureReturn(const Statement* stmt, bool omitted_ret_type,
|
||||
switch (stmt->Tag()) {
|
||||
case Statement::Kind::Match: {
|
||||
const auto& match = cast<Match>(*stmt);
|
||||
auto new_clauses =
|
||||
global_arena
|
||||
->New<std::list<std::pair<const Pattern*, const Statement*>>>();
|
||||
auto new_clauses = global_arena->RawNew<
|
||||
std::list<std::pair<const Pattern*, const Statement*>>>();
|
||||
for (const auto& clause : *match.Clauses()) {
|
||||
auto s = CheckOrEnsureReturn(clause.second, omitted_ret_type,
|
||||
stmt->LineNumber());
|
||||
new_clauses->push_back(std::make_pair(clause.first, s));
|
||||
}
|
||||
return global_arena->New<Match>(stmt->LineNumber(), match.Exp(),
|
||||
new_clauses);
|
||||
return global_arena->RawNew<Match>(stmt->LineNumber(), match.Exp(),
|
||||
new_clauses);
|
||||
}
|
||||
case Statement::Kind::Block:
|
||||
return global_arena->New<Block>(
|
||||
return global_arena->RawNew<Block>(
|
||||
stmt->LineNumber(),
|
||||
CheckOrEnsureReturn(cast<Block>(*stmt).Stmt(), omitted_ret_type,
|
||||
stmt->LineNumber()));
|
||||
case Statement::Kind::If: {
|
||||
const auto& if_stmt = cast<If>(*stmt);
|
||||
return global_arena->New<If>(
|
||||
return global_arena->RawNew<If>(
|
||||
stmt->LineNumber(), if_stmt.Cond(),
|
||||
CheckOrEnsureReturn(if_stmt.ThenStmt(), omitted_ret_type,
|
||||
stmt->LineNumber()),
|
||||
@@ -841,7 +851,7 @@ static auto CheckOrEnsureReturn(const Statement* stmt, bool omitted_ret_type,
|
||||
case Statement::Kind::Sequence: {
|
||||
const auto& seq = cast<Sequence>(*stmt);
|
||||
if (seq.Next()) {
|
||||
return global_arena->New<Sequence>(
|
||||
return global_arena->RawNew<Sequence>(
|
||||
stmt->LineNumber(), seq.Stmt(),
|
||||
CheckOrEnsureReturn(seq.Next(), omitted_ret_type,
|
||||
stmt->LineNumber()));
|
||||
@@ -861,10 +871,10 @@ static auto CheckOrEnsureReturn(const Statement* stmt, bool omitted_ret_type,
|
||||
case Statement::Kind::Continue:
|
||||
case Statement::Kind::VariableDefinition:
|
||||
if (omitted_ret_type) {
|
||||
return global_arena->New<Sequence>(
|
||||
return global_arena->RawNew<Sequence>(
|
||||
stmt->LineNumber(), stmt,
|
||||
global_arena->New<Return>(line_num, nullptr,
|
||||
/*is_omitted_exp=*/true));
|
||||
global_arena->RawNew<Return>(line_num, nullptr,
|
||||
/*is_omitted_exp=*/true));
|
||||
} else {
|
||||
FATAL_COMPILATION_ERROR(stmt->LineNumber())
|
||||
<< "control-flow reaches end of function that provides a `->` "
|
||||
@@ -883,7 +893,7 @@ static auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types,
|
||||
for (const auto& deduced : f->deduced_parameters) {
|
||||
// auto t = InterpExp(values, deduced.type);
|
||||
Address a = state->heap.AllocateValue(
|
||||
global_arena->New<VariableType>(deduced.name));
|
||||
global_arena->RawNew<VariableType>(deduced.name));
|
||||
values.Set(deduced.name, a);
|
||||
}
|
||||
// Type check the parameter pattern
|
||||
@@ -892,16 +902,17 @@ static auto TypeCheckFunDef(const FunctionDefinition* f, TypeEnv types,
|
||||
auto return_type = InterpPattern(values, f->return_type);
|
||||
if (f->name == "main") {
|
||||
ExpectType(f->line_num, "return type of `main`",
|
||||
global_arena->New<IntType>(), return_type);
|
||||
global_arena->RawNew<IntType>(), return_type);
|
||||
// TODO: Check that main doesn't have any parameters.
|
||||
}
|
||||
auto res = TypeCheckStmt(f->body, param_res.types, values, return_type,
|
||||
f->is_omitted_return_type);
|
||||
auto body =
|
||||
CheckOrEnsureReturn(res.stmt, f->is_omitted_return_type, f->line_num);
|
||||
return global_arena->New<FunctionDefinition>(
|
||||
return global_arena->RawNew<FunctionDefinition>(
|
||||
f->line_num, f->name, f->deduced_parameters, f->param_pattern,
|
||||
global_arena->New<ExpressionPattern>(ReifyType(return_type, f->line_num)),
|
||||
global_arena->RawNew<ExpressionPattern>(
|
||||
ReifyType(return_type, f->line_num)),
|
||||
/*is_omitted_return_type=*/false, body);
|
||||
}
|
||||
|
||||
@@ -911,7 +922,7 @@ static auto TypeOfFunDef(TypeEnv types, Env values,
|
||||
for (const auto& deduced : fun_def->deduced_parameters) {
|
||||
// auto t = InterpExp(values, deduced.type);
|
||||
Address a = state->heap.AllocateValue(
|
||||
global_arena->New<VariableType>(deduced.name));
|
||||
global_arena->RawNew<VariableType>(deduced.name));
|
||||
values.Set(deduced.name, a);
|
||||
}
|
||||
// Type check the parameter pattern
|
||||
@@ -923,8 +934,8 @@ static auto TypeOfFunDef(TypeEnv types, Env values,
|
||||
auto f = TypeCheckFunDef(fun_def, types, values);
|
||||
ret = InterpPattern(values, f->return_type);
|
||||
}
|
||||
return global_arena->New<FunctionType>(fun_def->deduced_parameters,
|
||||
param_res.type, ret);
|
||||
return global_arena->RawNew<FunctionType>(fun_def->deduced_parameters,
|
||||
param_res.type, ret);
|
||||
}
|
||||
|
||||
static auto TypeOfStructDef(const StructDefinition* sd, TypeEnv /*types*/,
|
||||
@@ -951,8 +962,8 @@ static auto TypeOfStructDef(const StructDefinition* sd, TypeEnv /*types*/,
|
||||
}
|
||||
}
|
||||
}
|
||||
return global_arena->New<StructType>(sd->name, std::move(fields),
|
||||
std::move(methods));
|
||||
return global_arena->RawNew<StructType>(sd->name, std::move(fields),
|
||||
std::move(methods));
|
||||
}
|
||||
|
||||
static auto GetName(const Declaration& d) -> const std::string& {
|
||||
@@ -978,7 +989,7 @@ auto MakeTypeChecked(const Declaration& d, const TypeEnv& types,
|
||||
const Env& values) -> const Declaration* {
|
||||
switch (d.Tag()) {
|
||||
case Declaration::Kind::FunctionDeclaration:
|
||||
return global_arena->New<FunctionDeclaration>(TypeCheckFunDef(
|
||||
return global_arena->RawNew<FunctionDeclaration>(TypeCheckFunDef(
|
||||
&cast<FunctionDeclaration>(d).Definition(), types, values));
|
||||
|
||||
case Declaration::Kind::StructDeclaration: {
|
||||
@@ -993,7 +1004,7 @@ auto MakeTypeChecked(const Declaration& d, const TypeEnv& types,
|
||||
break;
|
||||
}
|
||||
}
|
||||
return global_arena->New<StructDeclaration>(
|
||||
return global_arena->RawNew<StructDeclaration>(
|
||||
struct_def.line_num, struct_def.name, std::move(fields));
|
||||
}
|
||||
|
||||
@@ -1045,9 +1056,9 @@ static void TopLevel(const Declaration& d, TypeCheckContext* tops) {
|
||||
cast<StructType>(*st).Fields()) {
|
||||
field_types.push_back({.name = field_name, .value = field_value});
|
||||
}
|
||||
auto fun_ty = global_arena->New<FunctionType>(
|
||||
auto fun_ty = global_arena->RawNew<FunctionType>(
|
||||
std::vector<GenericBinding>(),
|
||||
global_arena->New<TupleValue>(std::move(field_types)), st);
|
||||
global_arena->RawNew<TupleValue>(std::move(field_types)), st);
|
||||
tops->types.Set(struct_def.name, fun_ty);
|
||||
break;
|
||||
}
|
||||
@@ -1059,7 +1070,8 @@ static void TopLevel(const Declaration& d, TypeCheckContext* tops) {
|
||||
auto t = InterpExp(tops->values, signature);
|
||||
alts.push_back(std::make_pair(name, t));
|
||||
}
|
||||
auto ct = global_arena->New<ChoiceType>(choice.Name(), std::move(alts));
|
||||
auto ct =
|
||||
global_arena->RawNew<ChoiceType>(choice.Name(), std::move(alts));
|
||||
Address a = state->heap.AllocateValue(ct);
|
||||
tops->values.Set(choice.Name(), a); // Is this obsolete?
|
||||
tops->types.Set(choice.Name(), ct);
|
||||
|
||||
@@ -78,7 +78,8 @@ auto GetMember(const Value* v, const std::string& f, int line_num)
|
||||
FATAL_RUNTIME_ERROR(line_num)
|
||||
<< "alternative " << f << " not in " << *v;
|
||||
}
|
||||
return global_arena->New<AlternativeConstructorValue>(f, choice.Name());
|
||||
return global_arena->RawNew<AlternativeConstructorValue>(f,
|
||||
choice.Name());
|
||||
}
|
||||
default:
|
||||
FATAL() << "field access not allowed for value " << *v;
|
||||
@@ -122,7 +123,7 @@ auto SetFieldImpl(const Value* value,
|
||||
}
|
||||
it->value = SetFieldImpl(it->value, path_begin + 1, path_end, field_value,
|
||||
line_num);
|
||||
return global_arena->New<TupleValue>(elements);
|
||||
return global_arena->RawNew<TupleValue>(elements);
|
||||
}
|
||||
default:
|
||||
FATAL() << "field access not allowed for value " << *value;
|
||||
@@ -255,56 +256,56 @@ auto CopyVal(const Value* val, int line_num) -> const Value* {
|
||||
elements.push_back(
|
||||
{.name = element.name, .value = CopyVal(element.value, line_num)});
|
||||
}
|
||||
return global_arena->New<TupleValue>(std::move(elements));
|
||||
return global_arena->RawNew<TupleValue>(std::move(elements));
|
||||
}
|
||||
case Value::Kind::AlternativeValue: {
|
||||
const auto& alt = cast<AlternativeValue>(*val);
|
||||
const Value* arg = CopyVal(alt.Argument(), line_num);
|
||||
return global_arena->New<AlternativeValue>(alt.AltName(),
|
||||
alt.ChoiceName(), arg);
|
||||
return global_arena->RawNew<AlternativeValue>(alt.AltName(),
|
||||
alt.ChoiceName(), arg);
|
||||
}
|
||||
case Value::Kind::StructValue: {
|
||||
const auto& s = cast<StructValue>(*val);
|
||||
const Value* inits = CopyVal(s.Inits(), line_num);
|
||||
return global_arena->New<StructValue>(s.Type(), inits);
|
||||
return global_arena->RawNew<StructValue>(s.Type(), inits);
|
||||
}
|
||||
case Value::Kind::IntValue:
|
||||
return global_arena->New<IntValue>(cast<IntValue>(*val).Val());
|
||||
return global_arena->RawNew<IntValue>(cast<IntValue>(*val).Val());
|
||||
case Value::Kind::BoolValue:
|
||||
return global_arena->New<BoolValue>(cast<BoolValue>(*val).Val());
|
||||
return global_arena->RawNew<BoolValue>(cast<BoolValue>(*val).Val());
|
||||
case Value::Kind::FunctionValue: {
|
||||
const auto& fn_value = cast<FunctionValue>(*val);
|
||||
return global_arena->New<FunctionValue>(fn_value.Name(), fn_value.Param(),
|
||||
fn_value.Body());
|
||||
return global_arena->RawNew<FunctionValue>(
|
||||
fn_value.Name(), fn_value.Param(), fn_value.Body());
|
||||
}
|
||||
case Value::Kind::PointerValue:
|
||||
return global_arena->New<PointerValue>(cast<PointerValue>(*val).Val());
|
||||
return global_arena->RawNew<PointerValue>(cast<PointerValue>(*val).Val());
|
||||
case Value::Kind::ContinuationValue:
|
||||
// Copying a continuation is "shallow".
|
||||
return val;
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn_type = cast<FunctionType>(*val);
|
||||
return global_arena->New<FunctionType>(fn_type.Deduced(),
|
||||
CopyVal(fn_type.Param(), line_num),
|
||||
CopyVal(fn_type.Ret(), line_num));
|
||||
return global_arena->RawNew<FunctionType>(
|
||||
fn_type.Deduced(), CopyVal(fn_type.Param(), line_num),
|
||||
CopyVal(fn_type.Ret(), line_num));
|
||||
}
|
||||
case Value::Kind::PointerType:
|
||||
return global_arena->New<PointerType>(
|
||||
return global_arena->RawNew<PointerType>(
|
||||
CopyVal(cast<PointerType>(*val).Type(), line_num));
|
||||
case Value::Kind::IntType:
|
||||
return global_arena->New<IntType>();
|
||||
return global_arena->RawNew<IntType>();
|
||||
case Value::Kind::BoolType:
|
||||
return global_arena->New<BoolType>();
|
||||
return global_arena->RawNew<BoolType>();
|
||||
case Value::Kind::TypeType:
|
||||
return global_arena->New<TypeType>();
|
||||
return global_arena->RawNew<TypeType>();
|
||||
case Value::Kind::AutoType:
|
||||
return global_arena->New<AutoType>();
|
||||
return global_arena->RawNew<AutoType>();
|
||||
case Value::Kind::ContinuationType:
|
||||
return global_arena->New<ContinuationType>();
|
||||
return global_arena->RawNew<ContinuationType>();
|
||||
case Value::Kind::StringType:
|
||||
return global_arena->New<StringType>();
|
||||
return global_arena->RawNew<StringType>();
|
||||
case Value::Kind::StringValue:
|
||||
return global_arena->New<StringValue>(cast<StringValue>(*val).Val());
|
||||
return global_arena->RawNew<StringValue>(cast<StringValue>(*val).Val());
|
||||
case Value::Kind::VariableType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ChoiceType:
|
||||
|
||||
@@ -213,75 +213,75 @@ input: declaration_list
|
||||
;
|
||||
expression:
|
||||
identifier
|
||||
{ $$ = global_arena->New<IdentifierExpression>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<IdentifierExpression>(yylineno, $1); }
|
||||
| expression designator
|
||||
{ $$ = global_arena->New<FieldAccessExpression>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<FieldAccessExpression>(yylineno, $1, $2); }
|
||||
| expression "[" expression "]"
|
||||
{ $$ = global_arena->New<IndexExpression>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<IndexExpression>(yylineno, $1, $3); }
|
||||
| integer_literal
|
||||
{ $$ = global_arena->New<IntLiteral>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<IntLiteral>(yylineno, $1); }
|
||||
| string_literal
|
||||
{ $$ = global_arena->New<StringLiteral>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<StringLiteral>(yylineno, $1); }
|
||||
| TRUE
|
||||
{ $$ = global_arena->New<BoolLiteral>(yylineno, true); }
|
||||
{ $$ = global_arena->RawNew<BoolLiteral>(yylineno, true); }
|
||||
| FALSE
|
||||
{ $$ = global_arena->New<BoolLiteral>(yylineno, false); }
|
||||
{ $$ = global_arena->RawNew<BoolLiteral>(yylineno, false); }
|
||||
| sized_type_literal
|
||||
{
|
||||
int val;
|
||||
CHECK(llvm::to_integer(llvm::StringRef($1).substr(1), val));
|
||||
CHECK($1[0] == 'i' && val == 32) << "Only i32 is supported for now: " << $1;
|
||||
$$ = global_arena->New<IntTypeLiteral>(yylineno);
|
||||
$$ = global_arena->RawNew<IntTypeLiteral>(yylineno);
|
||||
}
|
||||
| STRING
|
||||
{ $$ = global_arena->New<StringTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<StringTypeLiteral>(yylineno); }
|
||||
| BOOL
|
||||
{ $$ = global_arena->New<BoolTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<BoolTypeLiteral>(yylineno); }
|
||||
| TYPE
|
||||
{ $$ = global_arena->New<TypeTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<TypeTypeLiteral>(yylineno); }
|
||||
| CONTINUATION_TYPE
|
||||
{ $$ = global_arena->New<ContinuationTypeLiteral>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<ContinuationTypeLiteral>(yylineno); }
|
||||
| paren_expression { $$ = $1; }
|
||||
| expression EQUAL_EQUAL expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Eq, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression "+" expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Add, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression "-" expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Sub, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression BINARY_STAR expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Mul, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression AND expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::And, std::vector<const Expression*>({$1, $3})); }
|
||||
| expression OR expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Or, std::vector<const Expression*>({$1, $3})); }
|
||||
| NOT expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Not, std::vector<const Expression*>({$2})); }
|
||||
| "-" expression %prec UNARY_MINUS
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Neg, std::vector<const Expression*>({$2})); }
|
||||
| PREFIX_STAR expression
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Deref, std::vector<const Expression*>({$2})); }
|
||||
| UNARY_STAR expression %prec PREFIX_STAR
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Deref, std::vector<const Expression*>({$2})); }
|
||||
| expression tuple
|
||||
{ $$ = global_arena->New<CallExpression>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<CallExpression>(yylineno, $1, $2); }
|
||||
| expression POSTFIX_STAR
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Ptr, std::vector<const Expression*>({$1})); }
|
||||
| expression UNARY_STAR
|
||||
{ $$ = global_arena->New<PrimitiveOperatorExpression>(
|
||||
{ $$ = global_arena->RawNew<PrimitiveOperatorExpression>(
|
||||
yylineno, Operator::Ptr, std::vector<const Expression*>({$1})); }
|
||||
| FNTY tuple return_type
|
||||
{ $$ = global_arena->New<FunctionTypeLiteral>(
|
||||
{ $$ = global_arena->RawNew<FunctionTypeLiteral>(
|
||||
yylineno, $2, $3.first, $3.second); }
|
||||
;
|
||||
designator: "." identifier { $$ = $2; }
|
||||
@@ -329,17 +329,17 @@ pattern:
|
||||
non_expression_pattern
|
||||
{ $$ = $1; }
|
||||
| expression
|
||||
{ $$ = global_arena->New<ExpressionPattern>($1); }
|
||||
{ $$ = global_arena->RawNew<ExpressionPattern>($1); }
|
||||
;
|
||||
non_expression_pattern:
|
||||
AUTO
|
||||
{ $$ = global_arena->New<AutoPattern>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<AutoPattern>(yylineno); }
|
||||
| binding_lhs ":" pattern
|
||||
{ $$ = global_arena->New<BindingPattern>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<BindingPattern>(yylineno, $1, $3); }
|
||||
| paren_pattern
|
||||
{ $$ = $1; }
|
||||
| expression tuple_pattern
|
||||
{ $$ = global_arena->New<AlternativePattern>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<AlternativePattern>(yylineno, $1, $2); }
|
||||
;
|
||||
binding_lhs:
|
||||
identifier { $$ = $1; }
|
||||
@@ -373,7 +373,7 @@ paren_pattern_contents:
|
||||
| paren_pattern_contents "," paren_expression_element
|
||||
{
|
||||
$$ = $1;
|
||||
$$.elements.push_back({.name = $3.name, .term = global_arena->New<ExpressionPattern>($3.term)});
|
||||
$$.elements.push_back({.name = $3.name, .term = global_arena->RawNew<ExpressionPattern>($3.term)});
|
||||
}
|
||||
| paren_pattern_contents "," paren_pattern_element
|
||||
{
|
||||
@@ -395,24 +395,24 @@ tuple_pattern: paren_pattern_base
|
||||
// rules out the possibility of an `expression` at this point.
|
||||
maybe_empty_tuple_pattern:
|
||||
"(" ")"
|
||||
{ $$ = global_arena->New<TuplePattern>(yylineno, std::vector<TuplePattern::Field>()); }
|
||||
{ $$ = global_arena->RawNew<TuplePattern>(yylineno, std::vector<TuplePattern::Field>()); }
|
||||
| tuple_pattern
|
||||
{ $$ = $1; }
|
||||
;
|
||||
clause:
|
||||
CASE pattern DBLARROW statement
|
||||
{ $$ = global_arena->New<std::pair<const Pattern*, const Statement*>>($2, $4); }
|
||||
{ $$ = global_arena->RawNew<std::pair<const Pattern*, const Statement*>>($2, $4); }
|
||||
| DEFAULT DBLARROW statement
|
||||
{
|
||||
auto vp = global_arena->New<BindingPattern>(
|
||||
yylineno, std::nullopt, global_arena->New<AutoPattern>(yylineno));
|
||||
$$ = global_arena->New<std::pair<const Pattern*, const Statement*>>(vp, $3);
|
||||
auto vp = global_arena->RawNew<BindingPattern>(
|
||||
yylineno, std::nullopt, global_arena->RawNew<AutoPattern>(yylineno));
|
||||
$$ = global_arena->RawNew<std::pair<const Pattern*, const Statement*>>(vp, $3);
|
||||
}
|
||||
;
|
||||
clause_list:
|
||||
// Empty
|
||||
{
|
||||
$$ = global_arena->New<std::list<
|
||||
$$ = global_arena->RawNew<std::list<
|
||||
std::pair<const Pattern*, const Statement*>>>();
|
||||
}
|
||||
| clause clause_list
|
||||
@@ -420,35 +420,35 @@ clause_list:
|
||||
;
|
||||
statement:
|
||||
expression "=" expression ";"
|
||||
{ $$ = global_arena->New<Assign>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<Assign>(yylineno, $1, $3); }
|
||||
| VAR pattern "=" expression ";"
|
||||
{ $$ = global_arena->New<VariableDefinition>(yylineno, $2, $4); }
|
||||
{ $$ = global_arena->RawNew<VariableDefinition>(yylineno, $2, $4); }
|
||||
| expression ";"
|
||||
{ $$ = global_arena->New<ExpressionStatement>(yylineno, $1); }
|
||||
{ $$ = global_arena->RawNew<ExpressionStatement>(yylineno, $1); }
|
||||
| if_statement
|
||||
{ $$ = $1; }
|
||||
| WHILE "(" expression ")" block
|
||||
{ $$ = global_arena->New<While>(yylineno, $3, $5); }
|
||||
{ $$ = global_arena->RawNew<While>(yylineno, $3, $5); }
|
||||
| BREAK ";"
|
||||
{ $$ = global_arena->New<Break>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<Break>(yylineno); }
|
||||
| CONTINUE ";"
|
||||
{ $$ = global_arena->New<Continue>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<Continue>(yylineno); }
|
||||
| RETURN return_expression ";"
|
||||
{ $$ = global_arena->New<Return>(yylineno, $2.first, $2.second); }
|
||||
{ $$ = global_arena->RawNew<Return>(yylineno, $2.first, $2.second); }
|
||||
| block
|
||||
{ $$ = $1; }
|
||||
| MATCH "(" expression ")" "{" clause_list "}"
|
||||
{ $$ = global_arena->New<Match>(yylineno, $3, $6); }
|
||||
{ $$ = global_arena->RawNew<Match>(yylineno, $3, $6); }
|
||||
| CONTINUATION identifier statement
|
||||
{ $$ = global_arena->New<Continuation>(yylineno, $2, $3); }
|
||||
{ $$ = global_arena->RawNew<Continuation>(yylineno, $2, $3); }
|
||||
| RUN expression ";"
|
||||
{ $$ = global_arena->New<Run>(yylineno, $2); }
|
||||
{ $$ = global_arena->RawNew<Run>(yylineno, $2); }
|
||||
| AWAIT ";"
|
||||
{ $$ = global_arena->New<Await>(yylineno); }
|
||||
{ $$ = global_arena->RawNew<Await>(yylineno); }
|
||||
;
|
||||
if_statement:
|
||||
IF "(" expression ")" block optional_else
|
||||
{ $$ = global_arena->New<If>(yylineno, $3, $5, $6); }
|
||||
{ $$ = global_arena->RawNew<If>(yylineno, $3, $5, $6); }
|
||||
;
|
||||
optional_else:
|
||||
// Empty
|
||||
@@ -460,7 +460,7 @@ optional_else:
|
||||
;
|
||||
return_expression:
|
||||
// Empty
|
||||
{ $$ = {global_arena->New<TupleLiteral>(yylineno), true}; }
|
||||
{ $$ = {global_arena->RawNew<TupleLiteral>(yylineno), true}; }
|
||||
| expression
|
||||
{ $$ = {$1, false}; }
|
||||
;
|
||||
@@ -468,15 +468,15 @@ statement_list:
|
||||
// Empty
|
||||
{ $$ = 0; }
|
||||
| statement statement_list
|
||||
{ $$ = global_arena->New<Sequence>(yylineno, $1, $2); }
|
||||
{ $$ = global_arena->RawNew<Sequence>(yylineno, $1, $2); }
|
||||
;
|
||||
block:
|
||||
"{" statement_list "}"
|
||||
{ $$ = global_arena->New<Block>(yylineno, $2); }
|
||||
{ $$ = global_arena->RawNew<Block>(yylineno, $2); }
|
||||
;
|
||||
return_type:
|
||||
// Empty
|
||||
{ $$ = {global_arena->New<TupleLiteral>(yylineno), true}; }
|
||||
{ $$ = {global_arena->RawNew<TupleLiteral>(yylineno), true}; }
|
||||
| ARROW expression %prec FNARROW
|
||||
{ $$ = {$2, false}; }
|
||||
;
|
||||
@@ -509,34 +509,34 @@ deduced_params:
|
||||
function_definition:
|
||||
FN identifier deduced_params maybe_empty_tuple_pattern return_type block
|
||||
{
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
$$ = global_arena->RawNew<FunctionDefinition>(
|
||||
yylineno, $2, $3, $4,
|
||||
global_arena->New<ExpressionPattern>($5.first),
|
||||
global_arena->RawNew<ExpressionPattern>($5.first),
|
||||
$5.second, $6);
|
||||
}
|
||||
| FN identifier deduced_params maybe_empty_tuple_pattern DBLARROW expression ";"
|
||||
{
|
||||
// The return type is not considered "omitted" because it's automatic from
|
||||
// the expression.
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
$$ = global_arena->RawNew<FunctionDefinition>(
|
||||
yylineno, $2, $3, $4,
|
||||
global_arena->New<AutoPattern>(yylineno), true,
|
||||
global_arena->New<Return>(yylineno, $6, true));
|
||||
global_arena->RawNew<AutoPattern>(yylineno), true,
|
||||
global_arena->RawNew<Return>(yylineno, $6, true));
|
||||
}
|
||||
;
|
||||
function_declaration:
|
||||
FN identifier deduced_params maybe_empty_tuple_pattern return_type ";"
|
||||
{
|
||||
$$ = global_arena->New<FunctionDefinition>(
|
||||
$$ = global_arena->RawNew<FunctionDefinition>(
|
||||
yylineno, $2, $3, $4,
|
||||
global_arena->New<ExpressionPattern>($5.first),
|
||||
global_arena->RawNew<ExpressionPattern>($5.first),
|
||||
$5.second, nullptr); }
|
||||
;
|
||||
variable_declaration: identifier ":" pattern
|
||||
{ $$ = global_arena->New<BindingPattern>(yylineno, $1, $3); }
|
||||
{ $$ = global_arena->RawNew<BindingPattern>(yylineno, $1, $3); }
|
||||
;
|
||||
member: VAR variable_declaration ";"
|
||||
{ $$ = global_arena->New<FieldMember>(yylineno, $2); }
|
||||
{ $$ = global_arena->RawNew<FieldMember>(yylineno, $2); }
|
||||
;
|
||||
member_list:
|
||||
// Empty
|
||||
@@ -550,7 +550,7 @@ alternative:
|
||||
| identifier
|
||||
{
|
||||
$$ = std::pair<std::string, const Expression*>(
|
||||
$1, global_arena->New<TupleLiteral>(yylineno));
|
||||
$1, global_arena->RawNew<TupleLiteral>(yylineno));
|
||||
}
|
||||
;
|
||||
alternative_list:
|
||||
@@ -566,20 +566,20 @@ alternative_list:
|
||||
;
|
||||
declaration:
|
||||
function_definition
|
||||
{ $$ = global_arena->New<FunctionDeclaration>($1); }
|
||||
{ $$ = global_arena->RawNew<FunctionDeclaration>($1); }
|
||||
| function_declaration
|
||||
{ $$ = global_arena->New<FunctionDeclaration>($1); }
|
||||
{ $$ = global_arena->RawNew<FunctionDeclaration>($1); }
|
||||
| STRUCT identifier "{" member_list "}"
|
||||
{
|
||||
$$ = global_arena->New<StructDeclaration>(yylineno, $2, $4);
|
||||
$$ = global_arena->RawNew<StructDeclaration>(yylineno, $2, $4);
|
||||
}
|
||||
| CHOICE identifier "{" alternative_list "}"
|
||||
{
|
||||
$$ = global_arena->New<ChoiceDeclaration>(yylineno, $2, $4);
|
||||
$$ = global_arena->RawNew<ChoiceDeclaration>(yylineno, $2, $4);
|
||||
}
|
||||
| VAR variable_declaration "=" expression ";"
|
||||
{
|
||||
$$ = global_arena->New<VariableDeclaration>(yylineno, $2, $4);
|
||||
$$ = global_arena->RawNew<VariableDeclaration>(yylineno, $2, $4);
|
||||
}
|
||||
;
|
||||
declaration_list:
|
||||
|
||||
@@ -17,21 +17,21 @@ namespace Carbon {
|
||||
// standardized, but is made available for printing state in tests.
|
||||
static void AddIntrinsics(std::list<const Declaration*>* fs) {
|
||||
std::vector<TuplePattern::Field> print_fields = {TuplePattern::Field(
|
||||
"0", global_arena->New<BindingPattern>(
|
||||
"0", global_arena->RawNew<BindingPattern>(
|
||||
-1, "format_str",
|
||||
global_arena->New<ExpressionPattern>(
|
||||
global_arena->New<StringTypeLiteral>(-1))))};
|
||||
auto* print_return =
|
||||
global_arena->New<Return>(-1,
|
||||
global_arena->New<IntrinsicExpression>(
|
||||
IntrinsicExpression::IntrinsicKind::Print),
|
||||
false);
|
||||
auto* print = global_arena->New<FunctionDeclaration>(
|
||||
global_arena->New<FunctionDefinition>(
|
||||
global_arena->RawNew<ExpressionPattern>(
|
||||
global_arena->RawNew<StringTypeLiteral>(-1))))};
|
||||
auto* print_return = global_arena->RawNew<Return>(
|
||||
-1,
|
||||
global_arena->RawNew<IntrinsicExpression>(
|
||||
IntrinsicExpression::IntrinsicKind::Print),
|
||||
false);
|
||||
auto* print = global_arena->RawNew<FunctionDeclaration>(
|
||||
global_arena->RawNew<FunctionDefinition>(
|
||||
-1, "Print", std::vector<GenericBinding>(),
|
||||
global_arena->New<TuplePattern>(-1, print_fields),
|
||||
global_arena->New<ExpressionPattern>(
|
||||
global_arena->New<TupleLiteral>(-1)),
|
||||
global_arena->RawNew<TuplePattern>(-1, print_fields),
|
||||
global_arena->RawNew<ExpressionPattern>(
|
||||
global_arena->RawNew<TupleLiteral>(-1)),
|
||||
/*is_omitted_return_type=*/false, print_return));
|
||||
fs->insert(fs->begin(), print);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ void ExecProgram(std::list<const Declaration*> fs) {
|
||||
}
|
||||
llvm::outs() << "********** type checking **********\n";
|
||||
}
|
||||
state = global_arena->New<State>(); // Compile-time state.
|
||||
state = global_arena->RawNew<State>(); // Compile-time state.
|
||||
TypeCheckContext p = TopLevel(fs);
|
||||
TypeEnv top = p.types;
|
||||
Env ct_top = p.values;
|
||||
|
||||
Reference in New Issue
Block a user