Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); export fn demo1() void { const StartingState = Example.AOrB(Example.AOrB(Example.AOrB(Example.State1, Example.State2), Example.State2), Example.AOrB(Example.State1, Example.State2)); var gst: GlobalState = .init; staticStateMachine(StartingState, Example.Exit)(&gst); std.mem.doNotOptimizeAway(gst); } pub const GlobalState = struct { counter: u64, a_counter: u64, b_counter: u64, prng: std.Random.DefaultPrng, pub const init: GlobalState = .{ .counter = 0, .a_counter = 0, .b_counter = 0, .prng = .init(123), }; }; pub const Example = struct { pub const Exit = struct { pub const name = getName(Example, Exit); pub fn transition(_: *GlobalState) TransitionResult { return .{ .Exit = {} }; } }; pub const State1 = struct { pub const transitions = .{ Exit, AOrB(State1, State2), }; pub const name = getName(Example, State1); pub fn transition(gst: *GlobalState) TransitionResult { return genericTransition(State1, gst); } pub fn transitionInt(gst: *GlobalState) usize { gst.a_counter += 1; gst.counter += 1; if (gst.counter >= 10) { return intFromTransition(State1, Exit); } return intFromTransition(State1, AOrB(State1, State2)); } }; pub const State2 = struct { pub const transitions = .{ Exit, AOrB(State1, State2), }; pub const name = getName(Example, State2); pub fn transition(gst: *GlobalState) TransitionResult { return genericTransition(State2, gst); } pub fn transitionInt(gst: *GlobalState) usize { gst.b_counter += 1; gst.counter += 1; if (gst.counter >= 10) { return intFromTransition(State2, Exit); } return intFromTransition(State2, AOrB(State1, State2)); } }; pub fn AOrB(comptime A: type, comptime B: type) type { return struct { pub const transitions = .{ A, B, AOrB(A, B), }; pub const name = getComposedName(Example, AOrB, .{ A, B }); const Self = @This(); pub fn transition(gst: *GlobalState) TransitionResult { return genericTransition(Self, gst); } pub fn transitionInt(gst: *GlobalState) usize { const random = gst.prng.random(); if (random.intRangeLessThan(usize, 0, 3) != 0) { return intFromTransition(Self, AOrB(A, B)); } if (random.boolean()) { return intFromTransition(Self, A); } return intFromTransition(Self, B); } }; } }; // Utilities: pub fn staticStateMachine(comptime StartingState: type, comptime EndingState: type) fn (gst: *GlobalState) void { comptime { const reachable_states = getReachableStates(StartingState); const ending_state_int = std.mem.indexOfScalar(type, reachable_states, EndingState) orelse unreachable; const S = struct { pub fn traverse(gst: *GlobalState) void { const starting_state_int = comptime std.mem.indexOfScalar(type, reachable_states, StartingState) orelse unreachable; sw: switch (starting_state_int) { inline 0...reachable_states.len - 1 => |current_state_int| { if (current_state_int == ending_state_int) { return; } const CurrentState = reachable_states[current_state_int]; switch (CurrentState.transitionInt(gst)) { inline 0...CurrentState.transitions.len - 1 => |int| { const NextState = TransitionFromInt(CurrentState, int); // std.debug.print("{s} -> {s}\n", .{ CurrentState.name, NextState.name }); continue :sw comptime std.mem.indexOfScalar(type, reachable_states, NextState) orelse unreachable; }, else => unreachable, } }, else => unreachable, } } }; return S.traverse; } } pub fn getReachableStates(comptime StartingState: type) []const type { comptime { var reachable_states: []const type = &.{}; return getReachableStatesRecursive(StartingState, &reachable_states); } } fn getReachableStatesRecursive(comptime CurrentState: type, comptime reachable_states: *[]const type) []const type { comptime { for (reachable_states.*) |VisitedState| { if (VisitedState == CurrentState) { return reachable_states.*; } } reachable_states.* = reachable_states.* ++ [_]type{CurrentState}; if (@hasDecl(CurrentState, "transitions")) { const transitions = CurrentState.transitions; for (transitions) |TransitionState| { reachable_states.* = getReachableStatesRecursive(TransitionState, reachable_states); } } return reachable_states.*; } } pub fn getName(comptime Container: type, comptime decl: anytype) []const u8 { comptime { for (std.meta.declarations(Container)) |possible_decl| { const name = possible_decl.name; const decl_value = @field(Container, name); if (@TypeOf(decl_value) == @TypeOf(decl) and decl_value == decl) { return name; } } else unreachable; } } pub fn getComposedName(comptime Container: type, comptime decl: anytype, comptime args: anytype) []const u8 { comptime { var composed_name: []const u8 = ""; composed_name = composed_name ++ getName(Container, decl) ++ "("; for (&args, 0..) |arg, i| { if (i > 0) { composed_name = composed_name ++ ", "; } composed_name = composed_name ++ arg.name; } composed_name = composed_name ++ ")"; return composed_name; } } pub fn genericTransition(comptime State: type, gst: *GlobalState) TransitionResult { switch (State.transitionInt(gst)) { inline 0...State.transitions.len - 1 => |int| { //std.debug.print("{s} -> {s}\n", .{ State.name, TransitionFromInt(State, int).name }); return .{ .Current = TransitionFromInt(State, int).transition }; }, else => unreachable, } } pub fn intFromTransition(comptime State: type, comptime TransitionState: type) usize { return comptime std.mem.indexOfScalar(type, &State.transitions, TransitionState) orelse unreachable; } pub fn TransitionFromInt(comptime State: type, comptime int: usize) type { return State.transitions[int]; } pub const TransitionResult = union(enum) { Exit: void, Current: *const fn (gst: *GlobalState) TransitionResult, };
zig source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); export fn demo2() void { const StartingState = Example.AOrB(Example.AOrB(Example.AOrB(Example.State1, Example.State2), Example.State2), Example.AOrB(Example.State1, Example.State2)); var gst: GlobalState = .init; sw: switch (StartingState.transition(&gst)) { .Current => |function| { continue :sw function(&gst); }, .Exit => {}, } std.mem.doNotOptimizeAway(gst); } pub const GlobalState = struct { counter: u64, a_counter: u64, b_counter: u64, prng: std.Random.DefaultPrng, pub const init: GlobalState = .{ .counter = 0, .a_counter = 0, .b_counter = 0, .prng = .init(123), }; }; pub const Example = struct { pub const Exit = struct { pub const name = getName(Example, Exit); pub fn transition(_: *GlobalState) TransitionResult { return .{ .Exit = {} }; } }; pub const State1 = struct { pub const transitions = .{ Exit, AOrB(State1, State2), }; pub const name = getName(Example, State1); pub fn transition(gst: *GlobalState) TransitionResult { return genericTransition(State1, gst); } pub fn transitionInt(gst: *GlobalState) usize { gst.a_counter += 1; gst.counter += 1; if (gst.counter >= 10) { return intFromTransition(State1, Exit); } return intFromTransition(State1, AOrB(State1, State2)); } }; pub const State2 = struct { pub const transitions = .{ Exit, AOrB(State1, State2), }; pub const name = getName(Example, State2); pub fn transition(gst: *GlobalState) TransitionResult { return genericTransition(State2, gst); } pub fn transitionInt(gst: *GlobalState) usize { gst.b_counter += 1; gst.counter += 1; if (gst.counter >= 10) { return intFromTransition(State2, Exit); } return intFromTransition(State2, AOrB(State1, State2)); } }; pub fn AOrB(comptime A: type, comptime B: type) type { return struct { pub const transitions = .{ A, B, AOrB(A, B), }; pub const name = getComposedName(Example, AOrB, .{ A, B }); const Self = @This(); pub fn transition(gst: *GlobalState) TransitionResult { return genericTransition(Self, gst); } pub fn transitionInt(gst: *GlobalState) usize { const random = gst.prng.random(); if (random.intRangeLessThan(usize, 0, 3) != 0) { return intFromTransition(Self, AOrB(A, B)); } if (random.boolean()) { return intFromTransition(Self, A); } return intFromTransition(Self, B); } }; } }; // Utilities: pub fn staticStateMachine(comptime StartingState: type, comptime EndingState: type) fn (gst: *GlobalState) void { comptime { const reachable_states = getReachableStates(StartingState); const ending_state_int = std.mem.indexOfScalar(type, reachable_states, EndingState) orelse unreachable; const S = struct { pub fn traverse(gst: *GlobalState) void { const starting_state_int = comptime std.mem.indexOfScalar(type, reachable_states, StartingState) orelse unreachable; sw: switch (starting_state_int) { inline 0...reachable_states.len - 1 => |current_state_int| { if (current_state_int == ending_state_int) { return; } const CurrentState = reachable_states[current_state_int]; switch (CurrentState.transitionInt(gst)) { inline 0...CurrentState.transitions.len - 1 => |int| { const NextState = TransitionFromInt(CurrentState, int); // std.debug.print("{s} -> {s}\n", .{ CurrentState.name, NextState.name }); continue :sw comptime std.mem.indexOfScalar(type, reachable_states, NextState) orelse unreachable; }, else => unreachable, } }, else => unreachable, } } }; return S.traverse; } } pub fn getReachableStates(comptime StartingState: type) []const type { comptime { var reachable_states: []const type = &.{}; return getReachableStatesRecursive(StartingState, &reachable_states); } } fn getReachableStatesRecursive(comptime CurrentState: type, comptime reachable_states: *[]const type) []const type { comptime { for (reachable_states.*) |VisitedState| { if (VisitedState == CurrentState) { return reachable_states.*; } } reachable_states.* = reachable_states.* ++ [_]type{CurrentState}; if (@hasDecl(CurrentState, "transitions")) { const transitions = CurrentState.transitions; for (transitions) |TransitionState| { reachable_states.* = getReachableStatesRecursive(TransitionState, reachable_states); } } return reachable_states.*; } } pub fn getName(comptime Container: type, comptime decl: anytype) []const u8 { comptime { for (std.meta.declarations(Container)) |possible_decl| { const name = possible_decl.name; const decl_value = @field(Container, name); if (@TypeOf(decl_value) == @TypeOf(decl) and decl_value == decl) { return name; } } else unreachable; } } pub fn getComposedName(comptime Container: type, comptime decl: anytype, comptime args: anytype) []const u8 { comptime { var composed_name: []const u8 = ""; composed_name = composed_name ++ getName(Container, decl) ++ "("; for (&args, 0..) |arg, i| { if (i > 0) { composed_name = composed_name ++ ", "; } composed_name = composed_name ++ arg.name; } composed_name = composed_name ++ ")"; return composed_name; } } pub fn genericTransition(comptime State: type, gst: *GlobalState) TransitionResult { switch (State.transitionInt(gst)) { inline 0...State.transitions.len - 1 => |int| { //std.debug.print("{s} -> {s}\n", .{ State.name, TransitionFromInt(State, int).name }); return .{ .Current = TransitionFromInt(State, int).transition }; }, else => unreachable, } } pub fn intFromTransition(comptime State: type, comptime TransitionState: type) usize { return comptime std.mem.indexOfScalar(type, &State.transitions, TransitionState) orelse unreachable; } pub fn TransitionFromInt(comptime State: type, comptime int: usize) type { return State.transitions[int]; } pub const TransitionResult = union(enum) { Exit: void, Current: *const fn (gst: *GlobalState) TransitionResult, };
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree