Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img align="right" width="160" height="160" src="https://user-images.githubusercontent.com/34946442/152222895-9c8adb22-a22d-4bce-a513-3486ca28bdd5.png"> zig**fsm** is a [finite state machine](https://en.wikipedia.org/wiki/Finite-state_machine) library for Zig.

This library tracks [Zig master](https://github.com/ziglang/zig) and is tested with the `stage2` compiler. Last test was on Zig version `0.11.0-dev.4003+c6aa29b6f`
This library tracks [Zig master](https://github.com/ziglang/zig). Last test was on Zig version `0.12.0-dev.1746+19af8aac8`.

## Table of contents
* [Features](#features)
Expand Down
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn build(b: *std.build.Builder) void {
const mode = b.standardOptimizeOption(.{});

const fsm_mod = b.addModule("fsm", .{
.source_file = .{.path = "src/main.zig"},
.source_file = .{ .path = "src/main.zig" },
});

const lib = b.addStaticLibrary(.{
Expand Down
22 changes: 11 additions & 11 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn StateMachineFromTable(comptime StateType: type, comptime EventType: ?type
if (comptime EventType != null) instance.internal.events = EventPackedIntArray.initAllTo(0);

for (transitions) |t| {
var offset = (@intFromEnum(t.from) * state_type_count) + @intFromEnum(t.to);
const offset = (@intFromEnum(t.from) * state_type_count) + @intFromEnum(t.to);
instance.internal.state_map.setValue(offset, true);

if (comptime EventType != null) {
Expand Down Expand Up @@ -208,7 +208,7 @@ pub fn StateMachineFromTable(comptime StateType: type, comptime EventType: ?type
pub fn addEvent(self: *Self, event: EventTypeArg, from: StateType, to: StateType) !void {
if (comptime EventType != null) {
if (self.canTransitionFromTo(from, to)) {
var slot = computeEventSlot(event, from);
const slot = computeEventSlot(event, from);
if (self.internal.events.get(slot) != 0) return StateError.AlreadyDefined;
self.internal.events.set(slot, @as(CellType, @intCast(@intFromEnum(to))) + 1);
} else return StateError.Invalid;
Expand All @@ -219,9 +219,9 @@ pub fn StateMachineFromTable(comptime StateType: type, comptime EventType: ?type
/// Returns `StateError.Invalid` if the event is not defined for the current state
pub fn do(self: *Self, event: EventTypeArg) !Transition(StateType, EventType) {
if (comptime EventType != null) {
var from_state = self.internal.current_state;
var slot = computeEventSlot(event, self.internal.current_state);
var to_state = self.internal.events.get(slot);
const from_state = self.internal.current_state;
const slot = computeEventSlot(event, self.internal.current_state);
const to_state = self.internal.events.get(slot);
if (to_state != 0) {
try self.transitionToInternal(event, @as(StateType, @enumFromInt(to_state - 1)));
return .{ .event = event, .from = from_state, .to = @as(StateType, @enumFromInt(to_state - 1)) };
Expand Down Expand Up @@ -377,7 +377,7 @@ pub fn StateMachineFromTable(comptime StateType: type, comptime EventType: ?type

if (EventType) |T| {
if (options.show_events) {
var events_start_offset = @as(usize, @intCast(@intFromEnum(from))) * event_type_count;
const events_start_offset = @as(usize, @intCast(@intFromEnum(from))) * event_type_count;
var transition_name_buf: [4096]u8 = undefined;
var transition_name = std.io.fixedBufferStream(&transition_name_buf);
for (0..event_type_count) |event_index| {
Expand Down Expand Up @@ -554,7 +554,7 @@ pub fn FsmFromText(comptime input: []const u8) type {
} else if (std.mem.eql(u8, part, "end:")) {
_ = fsm.do(.endcolon) catch unreachable;
} else {
var current_identifier = part;
const current_identifier = part;
_ = fsm.do(.identifier) catch unreachable;
const to = fsm.currentState();

Expand Down Expand Up @@ -633,8 +633,8 @@ pub const Interface = struct {
pub fn make(comptime InterfaceType: type, comptime Implementer: type) InterfaceType {
var instance: InterfaceType = undefined;
inline for (std.meta.fields(InterfaceType)) |f| {
if (comptime std.meta.trait.hasFn(f.name[0..f.name.len])(Implementer)) {
@field(instance, f.name) = @field(Implementer, f.name[0..f.name.len]);
if (comptime std.meta.hasFn(Implementer, f.name)) {
@field(instance, f.name) = @field(Implementer, f.name);
}
}
return instance;
Expand Down Expand Up @@ -804,7 +804,7 @@ test "csv parser" {
var stream = std.io.fixedBufferStream(self.csv);
const reader = stream.reader();
while (true) : (self.cur_index += 1) {
var input = reader.readByte() catch {
const input = reader.readByte() catch {
// An example of how to handle parsing errors
_ = self.fsm.do(.eof) catch {
try std.io.getStdErr().writer().print("Unexpected end of stream\n", .{});
Expand Down Expand Up @@ -932,7 +932,7 @@ test "csv parser, without handler callback" {
var stream = std.io.fixedBufferStream(self.csv);
const reader = stream.reader();
while (true) : (self.cur_index += 1) {
var input = reader.readByte() catch {
const input = reader.readByte() catch {
// An example of how to handle parsing errors
_ = self.fsm.do(.eof) catch {
try std.io.getStdErr().writer().print("Unexpected end of stream\n", .{});
Expand Down