当前位置: 首页 > 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

相关文章:

  • 浏览器安全问题
  • 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
  • 【探商宝】大数据企业销售线索平台:销售型公司的战略转型引擎
  • 企业网站 微博模块/搜索引擎排名营销
  • 安阳网站制作哪家好/电商运营入门基础知识
  • 手机网站无法访问的解决方法/steam交易链接在哪复制
  • 刚察县wap网站建设公司/营销公司
  • 网上购物网站设计/2021年热门关键词
  • 做彩票网站推广犯法吗/网站访问量统计工具