示例#1
0
文件: Scope.cs 项目: colinvh/DCPUC
        public static void CompileRoot(FunctionDeclarationNode root, Assembly assembly, Action<string> onError)
        {
            DCPUC.Scope.Reset();
            var scope = new DCPUC.Scope();
            var end_of_program = new Variable();
            end_of_program.location = Register.STATIC;
            end_of_program.name = "__endofprogram";
            end_of_program.staticLabel = "ENDOFPROGRAM";
            end_of_program.emitBrackets = false;
            scope.variables.Add(end_of_program);

            var library = new List<String>(System.IO.File.ReadAllLines("libdcpuc.txt"));
            //root.InsertLibrary(library);

            try
            {
                root.CompileFunction(assembly, scope);
                foreach (var dataItem in DCPUC.Scope.dataElements)
                {
                    var datString = "";
                    foreach (var item in dataItem.Item2)
                    {
                        datString += item;
                        datString += ", ";
                    }
                    assembly.Add(":" + dataItem.Item1, "DAT", datString.Substring(0, datString.Length - 2));
                }
                assembly.Add(":ENDOFPROGRAM", "", "");
            }
            catch (DCPUC.CompileError c)
            {
                onError(c.Message);
                return;
            }
        }
示例#2
0
文件: Form1.cs 项目: BigEd/DCPUC
        private void compileButton_Click(object sender, EventArgs e)
        {
            outputBox.Clear();
            var program = Parser.Parse(inputBox.Text);
            if (program.HasErrors())
            {
                foreach (var msg in program.ParserMessages)
                {
                    outputBox.AppendText(msg.Level + ": " + msg.Message + " [line:" + msg.Location.Line + " column:" + msg.Location.Column + "]\r\n");
                    outputBox.AppendText(extractLine(inputBox.Text, msg.Location.Line) + "\r\n");
                    outputBox.AppendText(new String(' ', msg.Location.Column) + "^\r\n");
                }
                return;
            }

            var root = program.Root.AstNode as DCPUC.CompilableNode; //Irony.Interpreter.Ast.AstNode;
            var assembly = new List<String>();
            var scope = new DCPUC.Scope();

            try
            {
                root.Compile(assembly, scope);
                assembly.Add("BRK ;Non-standard");
                foreach (var pendingFunction in scope.pendingFunctions)
                    pendingFunction.CompileFunction(assembly);
            }
            catch (DCPUC.CompileError c)
            {
                outputBox.AppendText(c.Message);
                return;
            }

            foreach (var str in assembly)
                outputBox.AppendText(str + "\r\n");
        }