当前位置: 首页 > 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.dtcms.com/a/56468.html

相关文章:

  • 浏览器安全问题
  • Go语言集成DeepSeek API和GoFly框架文本编辑器实现流式输出和对话(GoFly快速开发框架)
  • 【maven】maven依赖报错解决方式
  • 数据分析/数据科学常见SQL题目:连续登录用户、留存率、最大观看人数
  • typora高亮方案+鼠标侧键一键改色
  • 根据歌词自动构建音乐 python
  • 本地部署DeepSeek R1大数据模型知识库
  • 专题·大模型安全 | 大模型的安全挑战及应对建议
  • springboot3 RestClient、HTTP 客户端区别
  • C++蓝桥杯基础篇(八)
  • iOS 聊天 IM 消息收发管理工具
  • 淘宝关键字搜索接口爬虫测试实战指南
  • 【DuodooTEKr 】多度科技 以开源之力,驱动企业数字化转型
  • GreatSQL5.7 与 8.0 对 DATE 非法值处理方式不同
  • e2studio开发RA2E1(15)----配置RTC时钟及显示时间
  • STM32使用有源蜂鸣器
  • Lab16_ Blind SQL injection with out-of-band interaction
  • RabbitTemplate类介绍、应用场景和示例代码
  • 【腾讯云】存储桶API
  • 【探商宝】大数据企业销售线索平台:销售型公司的战略转型引擎
  • Linux流量分析工具
  • RV1126+FFMPEG多路码流监控项目
  • 蓝桥杯java-B组真题—动态规划
  • 9.2go接口
  • Docker创建自定义网桥并指定网段
  • yolov5代码详解--1.python代码脚本
  • 【Multipath】使用(FC)访问远程存储设备
  • linux的文件系统及文件类型
  • 数据库管理-第300期 一个不痛不痒的BUG(20250307)
  • 强化学习(赵世钰版)-学习笔记(2.状态值与贝尔曼方程)