#!/usr/bin/env python3

from ichor import isa
from ichor.assembler import FuncBuilder
import pytest


@pytest.fixture
def builder() -> FuncBuilder:
    return FuncBuilder()


def test_forwards_label(builder: FuncBuilder):
    l = builder.make_label()
    builder.write(isa.GOTO(l))
    builder.write(isa.DROP(0)) # no-op
    builder.write(l)
    builder.write(isa.DROP(0)) # no-op
    instrs = builder.build()
    assert instrs == [
        isa.GOTO(2),
        isa.DROP(0),
        isa.DROP(0),
    ]


def test_backwards_label(builder: FuncBuilder):
    l = builder.make_label()
    builder.write(l)
    builder.write(isa.DROP(0)) # no-op
    builder.write(isa.GOTO(l))
    instrs = builder.build()
    assert instrs == [
        isa.DROP(0),
        isa.GOTO(0),
    ]


def test_self_label(builder: FuncBuilder):
    l = builder.make_label()
    builder.write(isa.DROP(0)) # no-op
    builder.write(l)
    builder.write(isa.GOTO(l))
    instrs = builder.build()
    assert instrs == [
        isa.DROP(0),
        isa.GOTO(1),
    ]


def test_local_label(builder: FuncBuilder):
    s0 = builder.mark_slot(target=0)
    builder.write(isa.SLOT(s0))

    s999 = builder.mark_slot(target=999)
    builder.write(isa.SLOT(s999))

    instrs = builder.build()
    assert instrs == [
        isa.SLOT(0),
        isa.SLOT(999),
    ]
