2022-02-09 23:14:29 +00:00
|
|
|
|
module Luau.Heap where
|
|
|
|
|
|
|
|
|
|
open import Agda.Builtin.Equality using (_≡_)
|
|
|
|
|
open import FFI.Data.Maybe using (Maybe; just)
|
|
|
|
|
open import FFI.Data.Vector using (Vector; length; snoc; empty)
|
|
|
|
|
open import Luau.Addr using (Addr)
|
|
|
|
|
open import Luau.Var using (Var)
|
2022-02-12 01:03:26 +00:00
|
|
|
|
open import Luau.Syntax using (Block; Expr; Annotated; FunDec; nil; addr; function_is_end)
|
2022-02-09 23:14:29 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
data HeapValue (a : Annotated) : Set where
|
|
|
|
|
function_is_end : FunDec a → Block a → HeapValue a
|
2022-02-09 23:14:29 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
Heap : Annotated → Set
|
|
|
|
|
Heap a = Vector (HeapValue a)
|
2022-02-09 23:14:29 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
data _≡_⊕_↦_ {a} : Heap a → Heap a → Addr → HeapValue a → Set where
|
2022-02-09 23:14:29 +00:00
|
|
|
|
|
|
|
|
|
defn : ∀ {H val} →
|
|
|
|
|
|
|
|
|
|
-----------------------------------
|
|
|
|
|
(snoc H val) ≡ H ⊕ (length H) ↦ val
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
_[_] : ∀ {a} → Heap a → Addr → Maybe (HeapValue a)
|
|
|
|
|
_[_] = FFI.Data.Vector.lookup
|
2022-02-09 23:14:29 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
∅ : ∀ {a} → Heap a
|
|
|
|
|
∅ = empty
|
2022-02-09 23:14:29 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
data AllocResult a (H : Heap a) (V : HeapValue a) : Set where
|
|
|
|
|
ok : ∀ b H′ → (H′ ≡ H ⊕ b ↦ V) → AllocResult a H V
|
2022-02-09 23:14:29 +00:00
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
alloc : ∀ {a} H V → AllocResult a H V
|
2022-02-09 23:14:29 +00:00
|
|
|
|
alloc H V = ok (length H) (snoc H V) defn
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
next : ∀ {a} → Heap a → Addr
|
2022-02-09 23:14:29 +00:00
|
|
|
|
next = length
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
allocated : ∀ {a} → Heap a → HeapValue a → Heap a
|
2022-02-09 23:14:29 +00:00
|
|
|
|
allocated = snoc
|
|
|
|
|
|
2022-02-12 01:03:26 +00:00
|
|
|
|
-- next-emp : (length ∅ ≡ 0)
|
2022-02-09 23:14:29 +00:00
|
|
|
|
next-emp = FFI.Data.Vector.length-empty
|
|
|
|
|
|
|
|
|
|
-- lookup-next : ∀ V H → (lookup (allocated H V) (next H) ≡ just V)
|
|
|
|
|
lookup-next = FFI.Data.Vector.lookup-snoc
|
|
|
|
|
|
|
|
|
|
-- lookup-next-emp : ∀ V → (lookup (allocated emp V) 0 ≡ just V)
|
|
|
|
|
lookup-next-emp = FFI.Data.Vector.lookup-snoc-empty
|
|
|
|
|
|