namespace TsmLean.Core

/-! # Tiny Stack Machine (TSM) — abstract syntax.

Third concrete kernel, parallel to golang-lean's TGC and octive-lean's
TOC. Where TGC and TOC have *named variables*, TSM has values living
*by position* on a stack — the deepest substrate-level asymmetry.

Instructions are atomic; programs are arrays of instructions. The PC
indexes into the array. Conditional/unconditional jumps use absolute
target addresses (not relative offsets — simpler to reason about).

Maps to real-world stack-based bytecodes: WebAssembly, JVM, CPython,
.NET CIL, SECD machines. Anything proved here transfers to those. -/

inductive Value where
  | vInt  : Int  → Value
  | vBool : Bool → Value
  deriving Repr, BEq, Inhabited

inductive Instr where
  | push     : Int → Instr           -- push integer literal
  | pushB    : Bool → Instr          -- push bool literal
  | pop      : Instr
  | dup      : Instr                  -- duplicate top
  | swap     : Instr                  -- swap top two
  | add      : Instr
  | sub      : Instr
  | mul      : Instr
  | eq       : Instr                  -- pop two ints, push bool
  | lt       : Instr                  -- pop two ints, push bool
  | jmp      : Nat → Instr           -- absolute jump
  | jmpFalse : Nat → Instr           -- pop bool; if false, jump
  | halt     : Instr
  deriving Repr, BEq, Inhabited

abbrev Code := Array Instr

end TsmLean.Core
