示例#1
0
 /// <summary>
 /// Visita un objecte 'UnitDeclaration'.
 /// </summary>
 /// <param name="decl">L'objecte a visitar.</param>
 ///
 public override void Visit(UnitDeclaration decl)
 {
     if (decl.Namespace != null)
     {
         decl.Namespace.AcceptVisitor(this);
     }
 }
示例#2
0
        public void Write(Stream stream, UnitDeclaration decl)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (decl == null)
            {
                throw new ArgumentNullException(nameof(decl));
            }

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.CloseOutput = false;
            settings.Encoding    = Encoding.UTF8;
            settings.Indent      = true;
            settings.IndentChars = "    ";

            XmlWriter wr = XmlWriter.Create(stream, settings);

            try {
                IVisitor visitor = new XmlVisitor(wr);
                decl.AcceptVisitor(visitor);
            }
            finally {
                wr.Close();
            }
        }
示例#3
0
        private void GenerateContextCode(UnitDeclaration unitDeclaration)
        {
            if (!String.IsNullOrEmpty(options.ContextCodeFileName))
            {
                using (StreamWriter writer = File.CreateText(options.ContextCodeFileName)) {
                    string code = CodeGenerator.Generate(unitDeclaration);

                    StringBuilder sb = new StringBuilder();
                    sb
                    .AppendLine("// -----------------------------------------------------------------------")
                    .AppendLine("// Generated by FsmCompiler v1.1")
                    .AppendLine("// Finite state machine compiler tool")
                    .AppendLine("// Copyright 2015-2020 Rafael Serrano ([email protected])")
                    .AppendLine("//")
                    .AppendLine("// Warning. Don't touch. Changes will be overwritten!")
                    .AppendLine("//")
                    .AppendLine("// -----------------------------------------------------------------------")
                    .AppendLine()
                    .AppendLine()
                    .AppendFormat("#include \"{0}\"", options.StateHeaderFileName).AppendLine()
                    .AppendFormat("#include \"{0}\"", options.ContextHeaderFileName).AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .Append(code);

                    writer.Write(sb.ToString());
                }
            }
        }
示例#4
0
        private void GenerateContextHeader(UnitDeclaration unitDeclaration)
        {
            if (!String.IsNullOrEmpty(options.ContextHeaderFileName))
            {
                using (StreamWriter writer = File.CreateText(options.ContextHeaderFileName)) {
                    string header = HeaderGenerator.Generate(unitDeclaration);

                    string guardString = Path.GetFileName(options.ContextHeaderFileName).ToUpper().Replace(".", "_");

                    StringBuilder sb = new StringBuilder();
                    sb
                    .AppendLine("// -----------------------------------------------------------------------")
                    .AppendLine("// Generated by FsmCompiler v1.1")
                    .AppendLine("// Finite state machine compiler tool")
                    .AppendLine("// Copyright 2015-2020 Rafael Serrano ([email protected])")
                    .AppendLine("//")
                    .AppendLine("// Warning. Don't touch. Changes will be overwritten!")
                    .AppendLine("//")
                    .AppendLine("// -----------------------------------------------------------------------")
                    .AppendLine()
                    .AppendFormat("#ifndef __{0}", guardString).AppendLine()
                    .AppendFormat("#define __{0}", guardString).AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendFormat("#include \"{0}\"", options.ConfigHeaderFileName).AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(header)
                    .AppendLine()
                    .AppendFormat("#endif // __{0}", guardString).AppendLine();

                    writer.Write(sb.ToString());
                }
            }
        }
示例#5
0
        public override void Generate(Machine machine)
        {
            if (String.IsNullOrEmpty(options.OutputType) || (options.OutputType == "ContextHeader"))
            {
                ContextUnitGenerator generator   = new ContextUnitGenerator(options);
                UnitDeclaration      contextUnit = generator.Generate(machine);
                GenerateContextHeader(contextUnit);
            }

            if (String.IsNullOrEmpty(options.OutputType) || (options.OutputType == "ContextCode"))
            {
                ContextUnitGenerator generator   = new ContextUnitGenerator(options);
                UnitDeclaration      contextUnit = generator.Generate(machine);
                GenerateContextCode(contextUnit);
            }

            if (String.IsNullOrEmpty(options.OutputType) || (options.OutputType == "StateHeader"))
            {
                StateUnitGenerator generator = new StateUnitGenerator(options);
                UnitDeclaration    stateUnit = generator.Generate(machine, StateUnitGenerator.Variant.Header);
                GenerateStateHeader(stateUnit);
            }

            if (String.IsNullOrEmpty(options.OutputType) || (options.OutputType == "StateCode"))
            {
                StateUnitGenerator generator = new StateUnitGenerator(options);
                UnitDeclaration    stateUnit = generator.Generate(machine, StateUnitGenerator.Variant.Code);
                GenerateStateCode(stateUnit);
            }
        }
示例#6
0
        /// <summary>
        /// Genera el codi font.
        /// </summary>
        /// <param name="unitDecl">Objecte 'UnitDeclaration'.</param>
        /// <returns>El codi generat.</returns>
        ///
        public static string Generate(UnitDeclaration unitDecl)
        {
            if (unitDecl == null)
            {
                throw new ArgumentNullException(nameof(unitDecl));
            }

            var cb      = new CodeBuilder();
            var visitor = new GeneratorVisitor(cb);

            visitor.Visit(unitDecl);

            return(cb.ToString());
        }
示例#7
0
文件: Unit.cs 项目: ratijas/SLang.NET
        public SLangUnitDefinition(Context ctx, UnitDeclaration ir) : this(ctx, ir.Name)
        {
            foreach (var declaration in ir.Declarations)
            {
                switch (declaration)
                {
                case RoutineDeclaration routine:
                    RegisterRoutine(new SLangRoutineDefinition(this, routine));
                    break;

                default:
                    throw new NotImplementedException("only routines declarations are supported within units");
                }
            }
        }
示例#8
0
        public override void Generate(Machine machine)
        {
            UnitDeclaration unit = MachineUnitGenerator.Generate(machine, options);

            if (String.IsNullOrEmpty(options.OutputType) || (options.OutputType == "MachineHeader"))
            {
                string header = HeaderGenerator.Generate(unit);
                GenerateMachineHeader(header);
            }

            if (String.IsNullOrEmpty(options.OutputType) || (options.OutputType == "MachineCode"))
            {
                string code = CodeGenerator.Generate(unit);
                GenerateMachineCode(code);
            }
        }
示例#9
0
 public override void Visit(UnitDeclaration decl)
 {
     StartElement("unitDeclaration");
     base.Visit(decl);
     EndElement();
 }