Exemplo n.º 1
0
 public ClassDefinationOperation(SignTable signs)
 {
     if (signs[0].type == "保留字")
     {
         foreach (Sign sign in signs)
         {
             if (sign.type == "保留字")
             {
                 continue;                 //讲道理不需要验证,但是听说验证一下显得专业
             }
             if (sign.type == "classname") //讲道理不需要验证,但是听说验证一下显得专业
             {
                 classname = sign.id;
                 c         = new Class(sign.id);
                 cb        = new ClassBuilder(classname);
                 continue;
             }
             if (sign.type == "args")
             {
                 cb.addArgs(sign);
             }
         }
         Lex.TotalClassList.Add(cb);
     }
 }
Exemplo n.º 2
0
 public FunctionDefinationOpration(SignTable signs)
 {
     if (signs[0].type == "保留字")
     {
         foreach (Sign sign in signs)
         {
             if (sign.type == "保留字")
             {
                 continue;                //讲道理不需要验证,但是验证一下显得专业
             }
             if (sign.type == "funcname") //讲道理不需要验证,但是验证一下显得专业
             {
                 funcname = sign.id;
                 f        = new Function(sign.id);
                 fb       = new FunctionBuilder(funcname);
                 continue;
             }
             if (sign.type == "args")
             {
                 fb.addArgs(sign);
             }
         }
         Lex.TotalFunctionList.Add(fb);
     }
 }
Exemplo n.º 3
0
 public void setFormalArgsList(SignTable localVariables)
 {
     foreach (Sign sign in localVariables)
     {
         this.localVariables.Add(new Sign(sign.id));
     }
 }
Exemplo n.º 4
0
 public override void doSomethings(SignTable signTable)
 {
     this.initSignTable(signTable);
     while (this.isBreak())
     {
         foreach (Operation op in this.circulateBody)
         {
             op.doSomethings(signTable);
             ConditionLeft.content = signTable.getSignById(ConditionLeft.id).content;
         }
     }
 }
Exemplo n.º 5
0
 public void initSignTable(SignTable sTable)
 {
     //从上下文符号符号标准中查找值赋值
     foreach (Sign sign in sTable)
     {
         if (this.signTable.has(sign.id))
         {
             this.signTable.getSignById(sign.id).content = sign.content;
             //sign.content = this.localSignTable.getSignById(sign.id).content;
         }
     }
 }
Exemplo n.º 6
0
        public static ClassTable TotalClassList = new ClassTable();          //存有哪些声明的类

        public Lex(String program)
        {
            List <String>    rawSentences  = program.Split('\n').ToList();
            List <SignTable> rawOperations = new List <SignTable>();

            for (int i = 0; i < rawSentences.Count; i++)
            {
                SignTable     tempT         = new SignTable();
                int           operationMode = 0;
                List <String> words         = rawSentences[i].Replace("\t", "").Replace(";", "").Split(' ').ToList();

                foreach (String word in words)
                {
                    if (word == "")
                    {
                        continue;
                    }
                    if (word[0] == '/' && word[1] == '/')
                    {
                        break;
                    }
                    if (word == "func")
                    {
                        operationMode = 2;
                        tempT.Add(new Sign(word, "保留字"));
                    }
                    else if (word == "while")
                    {
                        operationMode = 3;
                        tempT.Add(new Sign(word, "保留字"));
                    }
                    else
                    {
                        if (word == "return")
                        {
                            tempT.Add(new Sign(word, "保留字"));
                        }
                        else if (IsNumeric(word))
                        {
                            tempT.Add(new Sign(new DadaInt("", word)));
                        }
                        else if (IsMark(word))
                        {
                            tempT.Add(new Sign(word, "标点"));
                        }
                        else if (IsOperator(word))
                        {
                            tempT.Add(new Sign(word, "op"));
                        }
                        else
                        {
                            tempT.Add(new Sign(word, "id"));
                        }
                    }
                }
                if (operationMode == 0)
                {
                    operationMode = 1;
                }
                tempT.Add(new Sign(operationMode + "", "操作"));
                rawOperations.Add(tempT);
            }

            foreach (SignTable operation in rawOperations)
            {
                Console.WriteLine(operation);
            }
        }
Exemplo n.º 7
0
        //测试用
        public void test()
        {
            //测试print函数
            //FunctionBuilder foobuilder = Lex.TotalFunctionList.getFunctionBuilderByName("print");
            //SignTable s = new SignTable();
            //Sign a1 = new Sign(new DadaInt("", "12456"), "args");
            //Sign a2 = new Sign(new DadaInt("", "12456"), "args");
            //Sign a3 = new Sign(new DadaInt("", "12456"), "args");
            //s.Add(a1);
            //s.Add(a1);
            //s.Add(a1);
            //Function f = foobuilder.build(s);
            //f.run();



            /*
             * a=1;
             * while(a<100){
             *  a=a+1:
             *  print(a);
             * }
             *
             */


            //a=1;=====================================
            SignTable st1 = new SignTable();

            st1.Add(new Sign("a", "结果"));
            st1.Add(new Sign(new DadaInt("", "1")));
            AssignOperation ass = new AssignOperation(st1);

            //while(a<100)
            CirculateOperation cop       = new CirculateOperation();
            SignTable          condition = new SignTable();

            condition.Add(new Sign("a", "ConditionLeft"));
            condition.Add(new Sign("", "<"));
            condition.Add(new Sign(new DadaInt("", "1000"), "ConditionRight"));
            cop.setCondition(condition);

            //{
            List <Operation> cb = new List <Operation>();


            //a=a+1:
            SignTable st2 = new SignTable();

            st2.Add(new Sign("a", "结果"));
            st2.Add(new Sign("a"));
            st2.Add(new Sign("", "+"));
            st2.Add(new Sign(new DadaInt("", "1")));
            AssignOperation ass2 = new AssignOperation(st2);



            //print(a);
            FunctionBuilder print = Lex.TotalFunctionList.getFunctionBuilderByName("print");
            SignTable       st3   = new SignTable();

            st3.Add(new Sign(print));
            st3.Add(new Sign("a", "args"));
            AssignOperation ass3 = new AssignOperation(st3);

            //}
            cb.Add(ass2);
            cb.Add(ass3);
            cop.setCirculateBody(cb);



            //测试开始执行
            ass.doSomethings(Lex.TotalSignList);
            cop.doSomethings(Lex.TotalSignList);
        }
Exemplo n.º 8
0
        public override void doSomethings(SignTable contextTable)
        {
            this.initSignTable(contextTable);
            SignTable runtimeTable = Utils.DeepCopy <SignTable>(this.signTable);//深拷贝运行时符号表

            //执行函数
            for (int i = 0; i < runtimeTable.size(); i++)
            {
                if (runtimeTable[i].content is FunctionBuilder)
                {
                    List <Sign> trueArgs = new List <Sign>();
                    for (int j = i + 1; j < signTable.size(); j++)
                    {//向后检索实参列表
                        if (runtimeTable[j].type == "args")
                        {
                            trueArgs.Add(runtimeTable[j]);
                        }
                        else
                        {
                            break;
                        }
                    }
                    Function f       = ((FunctionBuilder)runtimeTable[i].content).build(trueArgs);
                    MateData fResult = f.run();

                    runtimeTable[i].content = fResult;
                }
            }
            String s = "";


            //从上下中给变量赋值
            foreach (Sign sign in runtimeTable)
            {
                if (contextTable.has(sign.id))
                {
                    sign.content = contextTable.getSignById(sign.id).content;
                }
                else if (sign.content is Hashable)
                {
                    if (sign.id != "" && sign.id[0] != '-')
                    {
                        contextTable.Add(sign);
                    }
                }

                if (sign.type == "结果")
                {
                    continue;
                }
                if (sign.content is Hashable)
                {
                    s = s + sign.content.toString();
                }
                else if (sign.content == null)
                {
                    s = s + sign.type;
                }
            }
            DataTable dataTable = new DataTable();
            double    x         = double.Parse(dataTable.Compute(s, null).ToString());
            DadaInt   data      = new DadaInt("");

            data.setData(x.ToString());
            Sign ss = runtimeTable.Find((e) => e.type == "结果");

            //以下写的很乱,但功能是对的
            if (ss != null)
            {
                if (contextTable.has(ss.id))
                {
                    contextTable.getSignById(ss.id).content = ss.content;
                }
                else
                {
                    contextTable.Add(ss);
                }
            }
            if (ss != null)
            {
                contextTable.getSignById(ss.id).content = data;
            }
            else
            {
                result    = data;
                this.mean = "返回";
            }
        }
Exemplo n.º 9
0
 public AssignOperation(SignTable signTable)
 {
     this.signTable = signTable;
     this.mean      = "赋值";
 }
Exemplo n.º 10
0
 public override void doSomethings(SignTable signTable)
 {
     //nothing to do
 }
Exemplo n.º 11
0
 public SignTable signTable;                                    //这一句话里有什么符号
 abstract public void doSomethings(SignTable contextSignTable); //拿到局部变量的符号表,上下文符号