public ICLS_Expression Compiler_Expression_SubValue(Token value) { if (value.type == TokenType.VALUE) { if (value.text.Contains(".")) { CLS_Value_Value<double> number = new CLS_Value_Value<double>(); number.value_value = -double.Parse(value.text); return number; } else { CLS_Value_Value<int> number = new CLS_Value_Value<int>(); number.value_value = -int.Parse(value.text); return number; } } else { logger.Log_Error("无法识别的简单表达式" + value); return null; } }
int GetToken(string line, int npos, out Token t) { //找到开始字符 int nstart = FindStart(line, npos); t.col = nstart; t.text = " "; t.type = TokenType.UNKNOWN; if (nstart < 0) return -1; if (line[nstart] == '\"') { //字符串查找 int nend = line.IndexOf('\"', nstart + 1); int nsub = line.IndexOf('\\', nstart + 1); while (nsub > 0 && nsub < nend) { nend = line.IndexOf('\"', nsub + 2); nsub = line.IndexOf('\\', nsub + 2); } t.type = TokenType.STRING; int pos = nend + 1; t.text = line.Substring(nstart, nend - nstart + 1); t.text = t.text.Replace("\\\"", "\""); t.text = t.text.Replace("\\\'", "\'"); t.text = t.text.Replace("\\\\", "\\"); t.text = t.text.Replace("\\0", "\0"); t.text = t.text.Replace("\\a", "\a"); t.text = t.text.Replace("\\b", "\b"); t.text = t.text.Replace("\\f", "\f"); t.text = t.text.Replace("\\n", "\n"); t.text = t.text.Replace("\\r", "\r"); t.text = t.text.Replace("\\t", "\t"); t.text = t.text.Replace("\\v", "\v"); int sp = t.text.IndexOf('\\'); if (sp > 0) { throw new Exception("不可识别的转义序列:" + t.text.Substring(sp)); } return pos; } else if (line[nstart] == '/')// / /= 注释 { if (nstart < line.Length - 1 && line[nstart + 1] == '=') { t.type = TokenType.PUNCTUATION; t.text = line.Substring(nstart, 2); } else if (nstart < line.Length - 1 && line[nstart + 1] == '/') { t.type = TokenType.COMMENT; int enterpos = line.IndexOf('\n', nstart + 2); if (enterpos < 0) t.text = line.Substring(nstart); else t.text = line.Substring(nstart, line.IndexOf('\n', nstart + 2) - nstart); } else { t.type = TokenType.PUNCTUATION; t.text = line.Substring(nstart, 1); } return nstart + t.text.Length; } else if (line[nstart] == '=')//= == { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '!')//= == { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '+')//+ += ++ { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && (line[nstart + 1] == '=' || line[nstart + 1] == '+')) t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } //通用的一元二元运算符检查 else if (line[nstart] == '-')//- -= -- 负数也先作为符号处理 { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=' || line[nstart + 1] == '-') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '*')//* *= { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '/')/// /= { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '%')/// /= { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '>')//> >= { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '<')//< <= { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '=') t.text = line.Substring(nstart, 2); else t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } else if (line[nstart] == '&')//&& { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '&') t.text = line.Substring(nstart, 2); else return -1; } else if (line[nstart] == '|')//|| { t.type = TokenType.PUNCTUATION; if (nstart < line.Length - 1 && line[nstart + 1] == '|') t.text = line.Substring(nstart, 2); else return -1; } else if (char.IsLetter(line, nstart) || line[nstart] == '_') { //字母逻辑 //判断完整性 int i = nstart + 1; while (i < line.Length - 1 && (char.IsLetterOrDigit(line, i) || line[i] == '_')) { i++; } t.text = line.Substring(nstart, i - nstart); //判断字母类型: 关键字 类型 标识符 foreach (string s in keywords) { if (t.text == s) { t.type = TokenType.KEYWORD; return nstart + t.text.Length; } } foreach (string s in types) { if (t.text == s) { t.type = TokenType.TYPE; return nstart + t.text.Length; } } if (line[i] == '<' || line[i] == '[')//检查特别类型 { foreach (string s in types) { if (line.IndexOf(s, nstart) == nstart) { t.type = TokenType.TYPE; t.text = s; return nstart + s.Length; } } } t.type = TokenType.IDENTIFIER; return nstart + t.text.Length; } else if (char.IsPunctuation(line, nstart)) { //else { t.type = TokenType.PUNCTUATION; t.text = line.Substring(nstart, 1); return nstart + t.text.Length; } //符号逻辑 //-号逻辑 //"号逻辑 ///逻辑 //其他符号 } else if (char.IsNumber(line, nstart)) { //数字逻辑 //判断数字合法性 if (line[nstart] == '0' && line[nstart + 1] == 'x') //0x.... { int iend = nstart + 2; for (int i = nstart + 2; i < line.Length; i++) { if (char.IsNumber(line, i)) { iend = i; } else { break; } } t.type = TokenType.VALUE; t.text = line.Substring(nstart, iend - nstart + 1); } else { //纯数字 int iend = nstart; for (int i = nstart + 1; i < line.Length; i++) { if (char.IsNumber(line, i)) { iend = i; } else { break; } } t.type = TokenType.VALUE; int dend = iend + 1; if (dend < line.Length && line[dend] == '.') { int fend = dend; for (int i = dend + 1; i < line.Length; i++) { if (char.IsNumber(line, i)) { fend = i; } else { break; } } if (fend + 1 < line.Length && line[fend + 1] == 'f') { t.text = line.Substring(nstart, fend + 2 - nstart); } else { t.text = line.Substring(nstart, fend + 1 - nstart); } //.111 //.123f } else { if (dend < line.Length && line[dend] == 'f') { t.text = line.Substring(nstart, dend - nstart + 1); } else { t.text = line.Substring(nstart, dend - nstart); } } } return nstart + t.text.Length; } else { //不可识别逻辑 int i = nstart + 1; while (i < line.Length - 1 && char.IsSeparator(line, i) == false && line[i] != '\n' && line[i] != '\r' && line[i] != '\t') { i++; } t.text = line.Substring(nstart, i - nstart); return nstart + t.text.Length; } // // -逻辑 // // "逻辑 // // /逻辑 // // 其他符号逻辑 return nstart + t.text.Length; }
public IList<Token> ReadTokenList(System.IO.Stream stream) { byte[] bs = new byte[0xffff]; stream.Read(bs, 0, 2); UInt16 len = BitConverter.ToUInt16(bs, 0); stream.Read(bs, 0, 2); UInt16 lenstr = BitConverter.ToUInt16(bs, 0); List<string> strstore = new List<string>(); List<Token> tokens = new List<Token>(); for (int i = 0; i < lenstr; i++) { stream.Read(bs, 0, 2); UInt16 slen = BitConverter.ToUInt16(bs, 0); stream.Read(bs, 0, slen); strstore.Add(System.Text.Encoding.UTF8.GetString(bs, 0, slen)); } for (int i = 0; i < len; i++) { Token t = new Token(); stream.Read(bs, 0, 4); UInt32 type = BitConverter.ToUInt32(bs, 0); t.type = (TokenType)(type % 0x0100); t.text = strstore[(int)(type / 0x100)]; tokens.Add(t); } return tokens; }
public ICLS_Expression Compiler_Expression_Value(Token value,int pos) { if (value.type == TokenType.VALUE) { if(value.text[value.text.Length-1]=='f') { CLS_Value_Value<float> number = new CLS_Value_Value<float>(); number.value_value = float.Parse(value.text.Substring(0,value.text.Length-1)); return number; } else if (value.text.Contains(".")) { CLS_Value_Value<double> number = new CLS_Value_Value<double>(); number.value_value = double.Parse(value.text); return number; } else { CLS_Value_Value<int> number = new CLS_Value_Value<int>(); number.value_value = int.Parse(value.text); return number; } } else if (value.type == TokenType.STRING) { CLS_Value_Value<string> str = new CLS_Value_Value<string>(); str.value_value = value.text.Substring(1,value.text.Length-2); return str; } else if (value.type == TokenType.IDENTIFIER) { CLS_Expression_GetValue getvalue = new CLS_Expression_GetValue(pos,pos); getvalue.value_name = value.text; return getvalue; } else { logger.Log_Error("无法识别的简单表达式" + value); return null; } }