using System.Collections.Generic;
public class BasicInterpreter
private readonly Dictionary<string, Command> _commands = new();
public void RegisterCommand(string name, Action<List<string>> action) => _commands[name] = new Command(action);
public void Run(string code)
var lines = code.Split('\n');
foreach (var line in lines)
private void ExecuteLine(string line)
if (string.IsNullOrEmpty(line)) return;
var parts = line.Split(' ');
var commandName = parts[0].ToUpper();
var args = new List<string>(parts);
if (_commands.ContainsKey(commandName))
_commands[commandName].Execute(args);
Console.WriteLine($"Unknown command: {commandName}");
private readonly Action<List<string>> _action;
public Command(Action<List<string>> action) => _action = action;
public void Execute(List<string> args) => _action.Invoke(args);
var interpreter = new BasicInterpreter();
interpreter.RegisterCommand("PRINT", PrintCommand);
interpreter.RegisterCommand("LET", LetCommand);
interpreter.Run(basicCode);
static void PrintCommand(List<string> args)
Console.WriteLine(string.Join(" ", args));
static void LetCommand(List<string> args)
if (args.Count != 2 || !int.TryParse(args[1], out int value))
Console.WriteLine("LET command syntax error");
Console.WriteLine($"Variable {args[0]} set to {value}");