diff --git a/executable_semantics/interpreter/BUILD b/executable_semantics/interpreter/BUILD index a2955fce1619..10b79209a29b 100644 --- a/executable_semantics/interpreter/BUILD +++ b/executable_semantics/interpreter/BUILD @@ -11,7 +11,7 @@ cc_library( srcs = ["action.cpp"], hdrs = ["action.h"], deps = [ - ":containers", + ":stack", ":value", "//common:ostream", "//executable_semantics/ast:expression", @@ -33,15 +33,9 @@ cc_library( ) cc_library( - name = "containers", - srcs = [ - "list_node.h", - ], - hdrs = [ - "dictionary.h", - "stack.h", - ], - deps = ["//common:check"], + name = "dictionary", + hdrs = ["dictionary.h"], + deps = ["//executable_semantics/common:arena"], ) cc_library( @@ -60,7 +54,8 @@ cc_library( deps = [ ":action", ":address", - ":containers", + ":dictionary", + ":stack", "//common:ostream", "@llvm-project//llvm:Support", ], @@ -89,7 +84,6 @@ cc_library( deps = [ ":action", ":address", - ":containers", ":frame", ":heap", ":value", @@ -104,12 +98,18 @@ cc_library( ], ) +cc_library( + name = "stack", + hdrs = ["stack.h"], + deps = ["//common:check"], +) + cc_library( name = "typecheck", srcs = ["typecheck.cpp"], hdrs = ["typecheck.h"], deps = [ - ":containers", + ":dictionary", ":interpreter", "//common:ostream", "//executable_semantics/ast:expression", @@ -127,8 +127,8 @@ cc_library( hdrs = ["value.h"], deps = [ ":address", - ":containers", ":field_path", + ":stack", "//common:ostream", "//executable_semantics/ast:function_definition", "//executable_semantics/ast:statement", diff --git a/executable_semantics/interpreter/dictionary.h b/executable_semantics/interpreter/dictionary.h index 95058b780148..a38a0c06fb23 100644 --- a/executable_semantics/interpreter/dictionary.h +++ b/executable_semantics/interpreter/dictionary.h @@ -5,12 +5,10 @@ #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_DICTIONARY_H_ #define EXECUTABLE_SEMANTICS_INTERPRETER_DICTIONARY_H_ -#include +#include #include -#include #include "executable_semantics/common/arena.h" -#include "executable_semantics/interpreter/list_node.h" namespace Carbon { @@ -19,6 +17,48 @@ namespace Carbon { template class Dictionary { public: + struct Node { + using ValueType = std::pair; + + Node(ValueType e, Node* n) : curr(e), next(n) {} + + const ValueType curr; + Node* const next; + + // Node cells are part of a "persistent data structure" and are thus + // immutable. + Node& operator=(const Node&) = delete; + Node& operator=(Node&&) = delete; + }; + + // A forward iterator over elements of a `Node` list. + struct Iterator { + using value_type = typename Node::ValueType; + using difference_type = std::ptrdiff_t; + using pointer = const value_type*; + using reference = const value_type&; + using iterator_category = std::forward_iterator_tag; + + Iterator(Node* x) : p(x) {} + Iterator(const Iterator& iter) : p(iter.p) {} + Iterator& operator++() { + p = p->next; + return *this; + } + Iterator operator++(int) { + Iterator tmp(*this); + operator++(); + return tmp; + } + bool operator==(const Iterator& rhs) const { return p == rhs.p; } + bool operator!=(const Iterator& rhs) const { return p != rhs.p; } + const value_type& operator*() { return p->curr; } + const value_type* operator->() { return &p->curr; } + + private: + Node* p; + }; + // Create an empty dictionary. Dictionary() { head = nullptr; } @@ -37,12 +77,9 @@ 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->RawNew>>(std::make_pair(k, v), - head); + head = global_arena->RawNew(std::make_pair(k, v), head); } - typedef ListNodeIterator> Iterator; - // The position of the first element of the dictionary // or `end()` if the dictionary is empty. auto begin() const -> Iterator { return Iterator(head); } @@ -51,9 +88,7 @@ class Dictionary { auto end() const -> Iterator { return Iterator(nullptr); } private: - Dictionary(ListNode>* h) : head(h) {} - - ListNode>* head; + Node* head; }; } // namespace Carbon diff --git a/executable_semantics/interpreter/list_node.h b/executable_semantics/interpreter/list_node.h deleted file mode 100644 index bf5ad15d9233..000000000000 --- a/executable_semantics/interpreter/list_node.h +++ /dev/null @@ -1,63 +0,0 @@ -// 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_INTERPRETER_LIST_NODE_H_ -#define EXECUTABLE_SEMANTICS_INTERPRETER_LIST_NODE_H_ - -namespace Carbon { - -template -struct ListNode { - ListNode(T e, ListNode* n) : curr(e), next(n) {} - - const T curr; - ListNode* const next; - - // ListNode cells are part of a "persistent data structure" and are thus - // immutable. - ListNode& operator=(const ListNode&) = delete; - ListNode& operator=(ListNode&&) = delete; -}; - -// A forward iterator over elements of a `ListNode` list. -template -struct ListNodeIterator { - using value_type = T; - using difference_type = std::ptrdiff_t; - using pointer = const T*; - using reference = const T&; - using iterator_category = std::forward_iterator_tag; - - ListNodeIterator(ListNode* x) : p(x) {} - ListNodeIterator(const ListNodeIterator& iter) : p(iter.p) {} - ListNodeIterator& operator++() { - p = p->next; - return *this; - } - ListNodeIterator operator++(int) { - ListNodeIterator tmp(*this); - operator++(); - return tmp; - } - bool operator==(const ListNodeIterator& rhs) const { return p == rhs.p; } - bool operator!=(const ListNodeIterator& rhs) const { return p != rhs.p; } - const T& operator*() { return p->curr; } - const T* operator->() { return &p->curr; } - - private: - ListNode* p; -}; - -template -auto Length(ListNode* ls) -> unsigned int { - if (ls) { - return 1 + Length(ls->next); - } else { - return 0; - } -} - -} // namespace Carbon - -#endif // EXECUTABLE_SEMANTICS_INTERPRETER_LIST_NODE_H_ diff --git a/executable_semantics/interpreter/stack.h b/executable_semantics/interpreter/stack.h index e2e14117a2e3..023aba4008b9 100644 --- a/executable_semantics/interpreter/stack.h +++ b/executable_semantics/interpreter/stack.h @@ -10,7 +10,6 @@ #include #include "common/check.h" -#include "executable_semantics/interpreter/list_node.h" namespace Carbon {