using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; // TODO: // how do you specify output gates? // comments enum TokenType { // default gates AND, // & OR, // | NOT, // ! // special characters COLON, // : PORT, } struct Token { public TokenType type; public string lexeme; } // example definition /* * XOR a b: * OR AND NOT a b AND a NOT b */ class Scanner { private Dictionary keywords = new Dictionary<> { [ "AND" ] = { TokenType.AND }, [ "NOT" ] = { TokenType.NOT }, [ "OR" ] = { TokenType.OR }, }; List scanFile(string file) { return scanTokens(File.ReadAllText(file)); } List scanTokens(string text) { int start = 0; int current = 0; int line = 1; List tokens = new List(); while (current < text.Length) { text[current++] switch { ':' => tokens.Add(new Token { type = TokenType.COLON, lexeme = ":" }), (>= 'A' and <= 'Z') or (>= 'a' and <= 'z') => { while (current < text.Length && isAlphaNum(text[current])) ++current; var sub = text.Substring(start, current); if (keywords.ContainsKey(sub)) tokens.Add(new Token { type = keywords[sub], lexeme = sub }); else tokens.Add(new Token { type = TokenType.PORT, lexeme = sub }); }, ' ' or '\r' or '\t' => { }, '\n' => ++line, }; } } private static bool isAlphaNum(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); } } public class CircuitScanner : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } }