From aa098b6a088438ec796c18a94bd974eedd856fc0 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Mon, 24 Aug 2026 12:40:00 +0200 Subject: [PATCH] feat(eval): add continuation and machine state types --- Sources/KiteCore/Kont.swift | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Sources/KiteCore/Kont.swift diff --git a/Sources/KiteCore/Kont.swift b/Sources/KiteCore/Kont.swift new file mode 100644 index 0000000..fe181eb --- /dev/null +++ b/Sources/KiteCore/Kont.swift @@ -0,0 +1,35 @@ +public struct Winder { + public let id: Int + public let before: Value + public let after: Value +} + +public indirect enum Kont { + case done + case ifK(then: Value, els: Value?, env: Environment, Kont) + case defineK(name: String, env: Environment, Kont) + case setK(name: String, env: Environment, Kont) + case argsK(pending: Value, accumulated: [Value], env: Environment, Kont) + case seqK(remaining: [Value], env: Environment, Kont) + case popHandlerK(Kont) + case afterHandlerK(handler: Value, continuable: Bool, Kont) + case dwBeforeK(before: Value, thunk: Value, after: Value, Kont) + case dwThunkK(after: Value, winderId: Int, Kont) + case dwAfterK(result: Value, Kont) + case rewindK(afters: [Value], befores: [Value], target: [Winder], arg: Value, saved: Kont) + case callValuesK(receiver: Value, Kont) +} + +enum MachineState { + case evaluating(Value, Environment, Kont) + case returning(Value, Kont) + case applying(Value, [Value], Kont) +} + +func kontOf(_ state: MachineState) -> Kont { + switch state { + case .evaluating(_, _, let k): return k + case .returning(_, let k): return k + case .applying(_, _, let k): return k + } +}