Пример #1
0
    public void genRepeatExpresion(ILCodeGen cg, ArrayList exp, uint scope, string proc_actual,
                                   Emit.Label ini, Emit.Label body, Emit.Label fin)
    {
        int    tipo = 0;
        Symbol s    = null;

        bool hasAnd = hasToGenAndLabel(exp);

        ReorderWHILEExpression(exp, proc_actual, scope);

        cg.MarkLabel(ini);

        foreach (Expr e in exp)
        {
            if (e is IntLiteral)
            {
                IntLiteral lit = (IntLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.INT, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is RealLiteral)
            {
                RealLiteral lit = (RealLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.REAL, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is BoolLiteral)
            {
                BoolLiteral lit = (BoolLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.BOOLEAN, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
                if (lit.Value == "true")
                {
                    cg.genFalseStatement(body);
                }
                else
                {
                    cg.genTrueStatement(body);
                }
            }
            else if (e is StringLiteral)
            {
                StringLiteral lit = (StringLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.STRING, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is CharLiteral)
            {
                CharLiteral lit = (CharLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.CHAR, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is Variable)
            {
                Variable var = (Variable)e;
                if (scope == 0)
                {
                    s = symtab.getSymbol(var.Value);
                }
                else
                {
                    s = symtab.getSymbol(proc_actual, var.Value, ref tipo);
                }
                if (s != null)
                {
                    if (tipo == 1)
                    {
                        Param p = (Param)s;
                        cg.loadVariable(s, p.Reference, scope, proc_actual);
                    }
                    else
                    {
                        cg.loadVariable(s, false, scope, proc_actual);
                    }
                }
                tipo = 0;
                if (s.Type.toString() == "BOOLEAN")
                {
                    if (var.Tipo == "NOT")
                    {
                        cg.genTrueStatement(body);
                    }
                    else
                    {
                        cg.genFalseStatement(body);
                    }
                }
            }
            else if (e is Function)
            {
                ExpFunction ex = (ExpFunction)e;
            }
            else if (e is EQExpresion)
            {
                if (hasAnd)
                {
                    cg.genDFStatement(fin);
                }
                else
                {
                    cg.genDFStatement(body);
                }
                hasAnd = false;
            }
            else if (e is DFExpresion)
            {
                if (hasAnd)
                {
                    cg.genEQStatement(fin);
                }
                else
                {
                    cg.genEQStatement(body);
                }
                hasAnd = false;
            }
            else if (e is GEExpresion)
            {
                if (hasAnd)
                {
                    cg.genLTStatement(fin);
                }
                else
                {
                    cg.genLTStatement(body);
                }
                hasAnd = false;
            }
            else if (e is GTExpresion)
            {
                if (hasAnd)
                {
                    cg.genLEStatement(fin);
                }
                else
                {
                    cg.genLEStatement(body);
                }
                hasAnd = false;
            }
            else if (e is LEExpresion)
            {
                if (hasAnd)
                {
                    cg.genGTStatement(fin);
                }
                else
                {
                    cg.genGTStatement(body);
                }
                hasAnd = false;
            }
            else if (e is LTExpresion)
            {
                if (hasAnd)
                {
                    cg.genGEStatement(fin);
                }
                else
                {
                    cg.genGEStatement(body);
                }
                hasAnd = false;
            }
        }
    }
Пример #2
0
        private int Export()
        {
            textBox3.Text = "";
            System.Text.StringBuilder tnewmsg = new StringBuilder();
            int exitCode = 0;

            mSPath = textBox1.Text;
            mDPaht = textBox2.Text;

            if (string.IsNullOrEmpty(mSPath) || string.IsNullOrEmpty(mDPaht))
            {
                return(1);
            }
            string        tpathfile = Directory.GetCurrentDirectory() + mPathsFile;
            StringBuilder tstrb     = new StringBuilder();

            tstrb.AppendLine(mSPath);
            tstrb.AppendLine(mDPaht);
            File.WriteAllText(tpathfile, tstrb.ToString());


            tnewmsg.AppendLine(System.DateTime.Now.ToString() + ":开始导出.");
            textBox3.Text = tnewmsg.ToString();
            ILCodeGen codegen = ILCodeGen.Default;
            var       set     = new FileDescriptorSet();

            set.AddImportPath(mSPath);

            string[] tpaths = Directory.GetDirectories(mSPath);

            foreach (string tp in tpaths)
            {
                set.AddImportPath(tp);
            }

            DirectoryInfo tdirfolder = new DirectoryInfo(mSPath);

            FileInfo[] tfileinfos = tdirfolder.GetFiles("*.proto", System.IO.SearchOption.AllDirectories);
            foreach (var input in tfileinfos)
            {
                if (!set.Add(input.Name, true))
                {
                    Console.Error.WriteLine($"File not found: {input}");
                    exitCode = 1;
                }
            }


            set.Process();
            var errors = set.GetErrors();

            foreach (var err in errors)
            {
                if (err.IsError)
                {
                    exitCode++;
                }
                tnewmsg.AppendLine(err.ToString());
                Console.Error.WriteLine(err.ToString());
            }
            textBox3.Text = tnewmsg.ToString();
            if (exitCode != 0)
            {
                return(exitCode);
            }

            var files = codegen.Generate(set);

            foreach (var file in files)
            {
                var path = Path.Combine(mDPaht, file.Name);
                File.WriteAllText(path, file.Text);
            }

            tnewmsg.AppendLine(System.DateTime.Now.ToString() + ":导出结束.");
            textBox3.Text = tnewmsg.ToString();
            return(0);
        }
Пример #3
0
    public void genAritExpresion(ILCodeGen cg, ArrayList exp, uint scope,
                                 string proc_actual, string store)
    {
        int    tipo = 0;
        Symbol s    = null;

        foreach (Expr e in exp)
        {
            if (e is IntLiteral)
            {
                IntLiteral lit = (IntLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.INT, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is RealLiteral)
            {
                RealLiteral lit = (RealLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.REAL, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is BoolLiteral)
            {
                BoolLiteral lit = (BoolLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.BOOLEAN, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is StringLiteral)
            {
                StringLiteral lit = (StringLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.STRING, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is CharLiteral)
            {
                CharLiteral lit = (CharLiteral)e;
                s      = new Symbol(lit.Value, new Type());
                s.Type = new Type(Type.T.CHAR, 0, null);
                cg.loadVariable(s, false, scope, proc_actual);
            }
            else if (e is Variable)
            {
                Variable var = (Variable)e;
                if (scope == 0)
                {
                    s = symtab.getSymbol(var.Value);
                }
                else
                {
                    s = symtab.getSymbol(proc_actual, var.Value, ref tipo);
                }
                if (s != null)
                {
                    if (tipo == 1)
                    {
                        Param p = (Param)s;
                        cg.loadVariable(s, p.Reference, scope, proc_actual);
                    }
                    else
                    {
                        cg.loadVariable(s, false, scope, proc_actual);
                    }
                }
                tipo = 0;
            }
            else if (e is Function)
            {
                ExpFunction ex = (ExpFunction)e;
                if (scope == 0)
                {
                    s = symtab.find(store);
                }
                else
                {
                    s = symtab.getSymbol(proc_actual, store, ref tipo);
                }

                if (s != null)
                {
                    if (s.Type.toString() != ex.Tipo)
                    {
                        throw new fpc2ilException("Cannot assing type " + ex.Tipo + " to variable '" + s.Name + "' (" +
                                                  s.Type.toString() + ")");
                    }
                }
                tipo = 0;
            }
            else if (e is PlusExpresion)
            {
                cg.genSuma();
            }
            else if (e is MinusExpresion)
            {
                cg.genResta();
            }
            else if (e is ProductoExpresion)
            {
                cg.genMult();
            }
            else if (e is DivisionExpresion)
            {
                cg.genDivision();
            }
            else if (e is DIVExpresion)
            {
                cg.genDivision();
            }
            else if (e is MODExpresion)
            {
                cg.genMod();
            }
        }

        /* Una vez estan generados todos los loads generamos el store */
        if (scope == 0)
        {
            s = symtab.getSymbol(store);
        }
        else
        {
            s = symtab.getSymbol(proc_actual, store, ref tipo);
        }

        if (s != null)
        {
            if (tipo == 1)
            {
                Param p = (Param)s;
                cg.storeVariable(s, p.Reference, scope, proc_actual);
            }
            else
            {
                /* TODO: Si es una variable de retorno de funcion de momento no hacemos nada */
                if (s.Name != proc_actual)
                {
                    cg.storeVariable(s, false, scope, proc_actual);
                }
            }
        }
    }