Skip to content

Instantly share code, notes, and snippets.

@nikhil-RGB
Created March 31, 2024 20:43
Show Gist options
  • Save nikhil-RGB/d34a25d3a57b88faf2820f85800e0d2f to your computer and use it in GitHub Desktop.
Save nikhil-RGB/d34a25d3a57b88faf2820f85800e0d2f to your computer and use it in GitHub Desktop.
Parse and store actions for a turing machine with a particular configuration, does not include final state as a part of stored data.
// ignore: file_names
import 'package:flutter/material.dart';
import 'package:turing_machines/exceptions/ActionParserException.dart';
//Represents an Action which can be performed on the tape of a turing machine.
class Actions {
String symbol;
ActionType type;
Actions({required this.type, this.symbol = ""});
//Parses a String containing actions, seperated by a ,(comma)
static List<Actions> parseActions(String input) {
input = input.replaceAll(" ", "");
List<String> tokens = input.split(',');
List<Actions> actions = [];
for (String token in tokens) {
ActionType type;
String symbol = "";
if (token == "L") {
type = ActionType.L;
} else if (token == "R") {
type = ActionType.R;
} else if (token == "E") {
type = ActionType.E;
} else if (token[0] == "P" && token.length == 2) {
type = ActionType.P;
symbol = token[1];
} else {
type = ActionType.NONE;
throw ActionParserException("$token is not a valid action!");
}
Actions object = Actions(type: type, symbol: symbol);
actions.add(object);
}
return actions;
}
}
enum ActionType {
P,
R,
L,
E,
// ignore: constant_identifier_names
NONE;
}
@nikhil-RGB
Copy link
Author

Tape, Configuration and Behaviour classes are seperate.
An object of type TuringMachine will contain a HashMap with <key, value> pairs of <Configuration, Behaviour>
Actions are a property of Behaviour, along with final m-config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment