datatypes.fsm: add missing doc comments for public methods (#23865)

This commit is contained in:
Noa Santo 2025-03-05 13:38:55 +01:00 committed by GitHub
parent 3aed78e76a
commit 95f53a3308
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 0 deletions

View File

@ -31,10 +31,12 @@ mut:
current_state string current_state string
} }
// StateMachine static method returns a new StateMachine instance.
pub fn new() StateMachine { pub fn new() StateMachine {
return StateMachine{} return StateMachine{}
} }
// set_state sets the current state of the state machine to the given state by `name`.
pub fn (mut s StateMachine) set_state(name string) ! { pub fn (mut s StateMachine) set_state(name string) ! {
if name in s.states { if name in s.states {
s.current_state = name s.current_state = name
@ -43,10 +45,13 @@ pub fn (mut s StateMachine) set_state(name string) ! {
} }
} }
// get_state returns the current state of the state machine.
pub fn (mut s StateMachine) get_state() string { pub fn (mut s StateMachine) get_state() string {
return s.current_state return s.current_state
} }
// add_state adds a new state to the state machine.
// It takes the `name` of the state, and three event handlers: `entry`, `run`, and `exit`.
pub fn (mut s StateMachine) add_state(name string, entry EventHandlerFn, run EventHandlerFn, exit EventHandlerFn) { pub fn (mut s StateMachine) add_state(name string, entry EventHandlerFn, run EventHandlerFn, exit EventHandlerFn) {
s.states[name] = State{ s.states[name] = State{
entry_handler: entry entry_handler: entry
@ -58,6 +63,8 @@ pub fn (mut s StateMachine) add_state(name string, entry EventHandlerFn, run Eve
} }
} }
// add_transition adds a new transition to the state machine.
// It takes the `from` and `to` states, and a condition handler.
pub fn (mut s StateMachine) add_transition(from string, to string, condition_handler ConditionFn) { pub fn (mut s StateMachine) add_transition(from string, to string, condition_handler ConditionFn) {
t := Transition{ t := Transition{
to: to to: to
@ -70,6 +77,7 @@ pub fn (mut s StateMachine) add_transition(from string, to string, condition_han
s.transitions[from] = [t] s.transitions[from] = [t]
} }
// run runs the state machine. It takes a `receiver` argument that is passed to the event handlers.
pub fn (mut s StateMachine) run(receiver voidptr) ! { pub fn (mut s StateMachine) run(receiver voidptr) ! {
from_state := s.current_state from_state := s.current_state
mut to_state := s.current_state mut to_state := s.current_state

View File

@ -1,6 +1,7 @@
import os import os
import flag import flag
// read_file reads a file and returns the lines as a list of strings. It takes the file path as an argument.
pub fn read_file(file string) ![]string { pub fn read_file(file string) ![]string {
if os.is_file(file) { if os.is_file(file) {
text := os.read_lines(file) or { text := os.read_lines(file) or {
@ -12,6 +13,7 @@ pub fn read_file(file string) ![]string {
return [''] return ['']
} }
// extract_transitions extracts the transitions from a line and returns it as a formatted string.
pub fn extract_transitions(line string) ?string { pub fn extract_transitions(line string) ?string {
mut result := ' ' mut result := ' '
first_comma := line.index(',')? first_comma := line.index(',')?
@ -24,12 +26,14 @@ pub fn extract_transitions(line string) ?string {
return result + from + ' -> ' + to + ' [label=' + condition + '];' return result + from + ' -> ' + to + ' [label=' + condition + '];'
} }
// get_transitions gets the transitions from a line and returns it as a formatted string using `extract_transitions`.
pub fn get_transitions(line string) ?string { pub fn get_transitions(line string) ?string {
mut raw_text := line[line.index_any('(') + 1..line.index_any(')')] mut raw_text := line[line.index_any('(') + 1..line.index_any(')')]
raw_text = raw_text.replace("'", '').replace(' ', '') raw_text = raw_text.replace("'", '').replace(' ', '')
return extract_transitions(raw_text) return extract_transitions(raw_text)
} }
// main reads a file and generates a graph from the transitions in the file.
pub fn main() { pub fn main() {
mut fp := flag.new_flag_parser(os.args) mut fp := flag.new_flag_parser(os.args)
file := fp.string('file', `f`, '', 'input V file with transitions to generate graph from.') file := fp.string('file', `f`, '', 'input V file with transitions to generate graph from.')