crosslang/tsm-lean/TsmLean/Core/Syntax.lean
Maximus Gorog bd2e14214d Add 'tsm-lean/' from commit '2e9061abead6f2daa464b39a79c17a949db30785'
git-subtree-dir: tsm-lean
git-subtree-mainline: 6592cd058d
git-subtree-split: 2e9061abea
2026-05-12 02:59:14 -06:00

39 lines
1.4 KiB
Text

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