using System; using System.Collections.Generic; using System.Text; namespace Ob51.Parsing { class NumericToken : antlr.CommonToken { object _value; NumericToken(int type, string text, object value) { _value = value; base.setText(text); base.setType(type); } public object Value { get { return _value; } } public static double StrToReal(string str) { double res = 0.0; int i; for (i = 0; str[i] != '.'; ++i) { res = res * 10 + (str[i] - '0'); } double pow = 1.0; for (++i; i < str.Length && str[i] != 'E'; ++i) { pow /= 10; res += pow * (str[i] - '0'); } if (str[i] == 'E') { ++i; int exp = System.Int32.Parse(str.Substring(i, str.Length - i)); res *= Math.Pow(10.0, exp); } return res; } public static int StrHexToInt(string str) { long res = StrHexToLong(str); unchecked { return (int)res; } } public static long StrHexToLong(string str) { return System.Int64.Parse(str.Substring(0, str.Length - 1), System.Globalization.NumberStyles.AllowHexSpecifier); } public static long StrToLong(string str) { return System.Int64.Parse(str); } public static char StrToChar(string str) { str = str.Substring(0, str.Length - 1); int result = System.Int32.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier); unchecked { return (char)result; } } public static NumericToken MakeReal(string text) { return new NumericToken(Ob51Lexer.REAL, text, StrToReal(text)); } public static NumericToken MakeChar(string text) { return new NumericToken(Ob51Lexer.CHAR, text, StrToChar(text)); } public static NumericToken MakeHexInt(string text) { return new NumericToken(Ob51Lexer.INTEGER, text, StrHexToInt(text)); } public static NumericToken MakeHexLong(string text) { return new NumericToken(Ob51Lexer.LONGINT, text, StrHexToLong(text)); } public static NumericToken MakeInt(string text) { long result = StrToLong(text); if (result <= System.Int32.MaxValue) return new NumericToken(Ob51Lexer.INTEGER, text, (int)result); else return new NumericToken(Ob51Lexer.LONGINT, text, result); } } }