当前位置: 首页 > news >正文

C#使用winform实现简单的梯形图指令编译和执行,带编译器和虚拟机代码

gui效果图,gui代码太长了就不贴了

请添加图片描述

编译结果

LD,test3
NOT,
STORE,TMP1
LD,test1
OR,
LD,TMP1
OUT,test2
LD,TMP1
RST,test
LD,TMP1
OUT,test22
LD,TMP1
OUT,test4
LD,TMP1
CALL,放料

编译器代码

using System.Collections.Generic;
using System.Linq;namespace LDEdit
{public class LadderCompiler{private readonly ConnectionManager _connManager;private readonly Dictionary<CellState, CellState> _cellStateMap;private Dictionary<CellState, string> _commonNodes = new Dictionary<CellState, string>();private int _tempCounter = 0;private Dictionary<CellState, List<List<CellState>>> _coilPathCache = new Dictionary<CellState, List<List<CellState>>>();public LadderCompiler(ConnectionManager connManager, List<CellState> cells){_connManager = connManager;_cellStateMap = cells.ToDictionary(c => c);}public List<Instruction> Compile(){var instructions = new List<Instruction>();var allPaths = new List<List<CellState>>();// 第一阶段:收集所有线圈路径var outputCoils = _cellStateMap.Values.Where(c => IsOutputType(c.cellType)).OrderBy(c => c.row).ToList();foreach (var coil in outputCoils){var branches = FindAllBranches(coil);_coilPathCache[coil] = branches;allPaths.AddRange(branches);}// 第二阶段:分析公共节点var commonNodes = AnalyzeCommonNodes(allPaths);GenerateCommonLogic(commonNodes, instructions);// 第三阶段:生成优化后的指令foreach (var coil in outputCoils){CompileOptimizedCoil(coil, instructions);}return instructions;}private Dictionary<CellState, int> AnalyzeCommonNodes(List<List<CellState>> allPaths){var nodeFrequency = new Dictionary<CellState, int>();var visited = new HashSet<CellState>();foreach (var path in allPaths){var reversed = Enumerable.Reve

http://www.mrgr.cn/news/93573.html

相关文章:

  • Wpf-ReactiveUI-Usercontrol与主界面交互
  • C语言实现贪吃蛇
  • 高考數學。。。
  • 200W数据需要去重,如何优化?
  • 20250306-笔记-精读class CVRPEnv:step(self, selected)
  • Flink深入浅出之03:状态、窗口、checkpoint、两阶段提交
  • FPGA学习篇——Verilog学习3(关键字+注释方法+程序基本框架)
  • 蓝桥杯单片机——第十五届蓝桥杯省赛
  • STM32之I2C硬件外设
  • C语言100天练习题【记录本】
  • STM32之硬件SPI
  • 苦瓜书盘官网,免费pdf/mobi电子书下载网站
  • 100天精通Python(爬虫篇)——第115天:爬虫在线小工具_Curl转python爬虫代码工具(快速构建初始爬虫代码)
  • Kubernetes Pod网络组件解析与选型指南
  • python从入门到精通(二十五):文件操作和目录管理难度分级练习题
  • 【华三】STP端口角色与状态深度解析
  • MySQL------存储引擎和用户和授权
  • 从0开始的操作系统手搓教程25:使用环状缓冲区来让我们的键盘驱动真正的有作用起来
  • 200W数据去重入库的几种方法及优缺点
  • STM32-I2C通信协议