예제 #1
0
파일: Main.cs 프로젝트: modi0012/DSL
        public static void Main(string[] args)
        {
            DslLogDelegation logCallback = (string msg) => {
                Console.WriteLine("{0}", msg);
            };
            //DslFile.DontLoadComments = true;
            DslFile file = new DslFile();

            file.Load("test.txt", logCallback);
#if FULL_VERSION
            file.Save("copy.txt");
            file.SaveBinaryFile("binary.txt");
#endif
            file.DslInfos.Clear();
            var code = File.ReadAllBytes("binary.txt");
            file.LoadBinaryCode(code);
#if FULL_VERSION
            file.Save("unbinary.txt");
#endif
            long t1 = GetLocalMilliseconds();
            for (int i = 0; i < 1000; ++i)
            {
                file.LoadBinaryCode(code);
            }
            long t2 = GetLocalMilliseconds();
            Console.WriteLine("time:{0}", t2 - t1);
        }
예제 #2
0
        public static void Main(string[] args)
        {
            DslLogDelegation logCallback = (string msg) => {
                Console.WriteLine("{0}", msg);
            };
            Dictionary <string, string> encodeTable = new Dictionary <string, string>();
            Dictionary <string, string> decodeTable = new Dictionary <string, string>();

            AddCrypto("skill", encodeTable, decodeTable);
            AddCrypto("section", encodeTable, decodeTable);
            AddCrypto("foreach", encodeTable, decodeTable);
            AddCrypto("looplist", encodeTable, decodeTable);
            AddCrypto("loop", encodeTable, decodeTable);
            AddCrypto("wait", encodeTable, decodeTable);
            AddCrypto("sleep", encodeTable, decodeTable);
            AddCrypto("while", encodeTable, decodeTable);
            AddCrypto("if", encodeTable, decodeTable);
            AddCrypto("log", encodeTable, decodeTable);

            AddCrypto("=", encodeTable, decodeTable);
            AddCrypto("+", encodeTable, decodeTable);
            AddCrypto("-", encodeTable, decodeTable);
            AddCrypto("*", encodeTable, decodeTable);
            AddCrypto("/", encodeTable, decodeTable);
            AddCrypto("%", encodeTable, decodeTable);
            AddCrypto(">", encodeTable, decodeTable);
            AddCrypto(">=", encodeTable, decodeTable);
            AddCrypto("==", encodeTable, decodeTable);
            AddCrypto("!=", encodeTable, decodeTable);
            AddCrypto("<", encodeTable, decodeTable);
            AddCrypto("<=", encodeTable, decodeTable);
            AddCrypto("&&", encodeTable, decodeTable);
            AddCrypto("||", encodeTable, decodeTable);
            AddCrypto("!", encodeTable, decodeTable);

            DslFile file = new DslFile();

            file.Load("test.txt", logCallback);
#if FULL_VERSION
            file.Save("copy.txt");
            string code1 = file.GenerateBinaryCode(File.ReadAllText("test.txt"), encodeTable, logCallback);
            File.WriteAllText("binary.txt", code1);
#endif
            file.DslInfos.Clear();
            string code = File.ReadAllText("binary.txt");
            file.LoadBinaryCode(code, decodeTable);
#if FULL_VERSION
            file.Save("unbinary.txt");
#endif
            long t1 = GetLocalMilliseconds();
            for (int i = 0; i < 1000; ++i)
            {
                file.LoadBinaryCode(code, decodeTable);
            }
            long t2 = GetLocalMilliseconds();
            Console.WriteLine("time:{0}", t2 - t1);
        }
예제 #3
0
파일: Dsl.cs 프로젝트: dreamanlan/DSL
        public bool LoadFromString(string content, string resourceName, DslLogDelegation logCallback)
        {
            #if FULL_VERSION
            mDslInfos.Clear();
            Parser.DslLog log = new Parser.DslLog();
            log.OnLog += logCallback;
            Parser.DslToken tokens = new Parser.DslToken(log, content);
            Parser.DslError error = new Parser.DslError(log, tokens);
            Parser.RuntimeAction action = new Parser.RuntimeAction(mDslInfos);
            action.onGetLastToken = () => { return tokens.getLastToken(); };
            action.onGetLastLineNumber = () => { return tokens.getLastLineNumber(); };
            action.onGetComment = (out bool commentOnNewLine) => { commentOnNewLine = tokens.IsCommentOnNewLine(); List<string> ret = new List<string>();ret.AddRange(tokens.GetComments()); tokens.ResetComments(); return ret; };
            action.onSetStringDelimiter = (string begin, string end) => { tokens.setStringDelimiter(begin, end); };
            action.onSetScriptDelimiter = (string begin, string end) => { tokens.setScriptDelimiter(begin, end); };

            Parser.DslParser.parse(action, tokens, error, 0);
            if (error.HasError) {
                for (int i = 0; i < mDslInfos.Count; i++) {
                    mDslInfos[i].Clear();
                }
            } else {
                for (int i = 0; i < mDslInfos.Count; i++) {
                    mDslInfos[i].SetResourceName(resourceName);
                }
            }
            return !error.HasError;
            #else
              return false;
            #endif
        }
예제 #4
0
파일: Dsl.cs 프로젝트: dreamanlan/DSL
 public bool Load(string file, DslLogDelegation logCallback)
 {
     #if FULL_VERSION
     string content = File.ReadAllText(file);
     //logCallback(string.Format("DslFile.Load {0}:\n{1}", file, content));
     return LoadFromString(content, file, logCallback);
     #else
       return false;
     #endif
 }
예제 #5
0
파일: Dsl.cs 프로젝트: dreamanlan/DSL
        public string GenerateBinaryCode(string content, Dictionary<string, string> encodeTable, DslLogDelegation logCallback)
        {
            #if FULL_VERSION
            List<DslInfo> infos = new List<DslInfo>();
            Parser.DslLog log = new Parser.DslLog();
            log.OnLog += logCallback;
            Parser.DslToken tokens = new Parser.DslToken(log, content);
            Parser.DslError error = new Parser.DslError(log, tokens);
            Parser.RuntimeAction action = new Parser.RuntimeAction(infos);
            action.onGetLastToken = () => { return tokens.getLastToken(); };
            action.onGetLastLineNumber = () => { return tokens.getLastLineNumber(); };
            action.onSetStringDelimiter = (string begin, string end) => { tokens.setStringDelimiter(begin, end); };
            action.onSetScriptDelimiter = (string begin, string end) => { tokens.setScriptDelimiter(begin, end); };
            Parser.DslParser.parse(action, tokens, error, 0);
            if (error.HasError) {
                return string.Empty;
            } else {
                MemoryStream stream = new MemoryStream();
                List<string> identifiers = new List<string>();
                foreach (DslInfo info in infos) {
                    Utility.writeBinary(stream, identifiers, (StatementData)info);
                }

                byte[] bytes = stream.ToArray();
                SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
                int ct = identifiers.Count;
                if (ct > 0x00004000) {
                    System.Diagnostics.Debug.Assert(false);
                    //Console.WriteLine("Identifiers count {0} too large than 0x04000", ct);
                    return string.Empty;
                }
                for (int i = 0; i < ct; ++i) {
                    string key = identifiers[i];
                    key = Encode(key, encodeTable);
                    identifiers[i] = key;
                    if (!dict.ContainsKey(key)) {
                        dict.Add(key, 0);
                    }
                }
                List<string> keys = new List<string>(dict.Keys);
                byte[] bytes2;
                using (MemoryStream ms = new MemoryStream()) {
                    for (int i = 0; i < ct; ++i) {
                        string key = identifiers[i];
                        int ix = keys.BinarySearch(key);
                        if (ix < 0x80) {
                            ms.WriteByte((byte)ix);
                        } else {
                            ms.WriteByte((byte)((ix & 0x0000007f) | 0x00000080));
                            ms.WriteByte((byte)(ix >> 7));
                        }
                    }
                    bytes2 = ms.ToArray();
                }
                return string.Format("{0}|{1}|{2}", Convert.ToBase64String(bytes), Convert.ToBase64String(bytes2), string.Join("`", keys.ToArray()));
            }
            #else
              return string.Empty;
            #endif
        }
예제 #6
0
파일: Dsl.cs 프로젝트: CQiao/DSL
    public bool LoadFromString(string content, string resourceName, DslLogDelegation logCallback)
    {
#if FULL_VERSION
      mDslInfos.Clear();
      Parser.DslLog log = new Parser.DslLog();
      log.OnLog += logCallback;
		  Parser.DslToken tokens = new Parser.DslToken(log, content);
		  Parser.DslError error = new Parser.DslError(log, tokens);
      Parser.RuntimeAction action = new Parser.RuntimeAction(mDslInfos);
      action.onGetLastToken = () => { return tokens.getLastToken(); };
      action.onGetLastLineNumber = () => { return tokens.getLastLineNumber(); };
		  Parser.DslParser.parse(action,tokens,error,0);
		  if(error.HasError)
		  {
        for (int i = 0; i < mDslInfos.Count; i++ )
        {
          mDslInfos[i].Clear();
        }
		  }
      else
      {
        for (int i = 0; i < mDslInfos.Count; i++)
        {
          mDslInfos[i].SetResourceName(resourceName);
        }
		  }
		  return !error.HasError;
#else
      return false;
#endif
    }