number.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package dirty
  2. import (
  3. "math"
  4. "strconv"
  5. "strings"
  6. )
  7. type Int int
  8. func (Int) isElement() {}
  9. func (Int) getType() ElementType {
  10. return ElemInt
  11. }
  12. func (i Int) String() string {
  13. return strconv.FormatInt(int64(i), 10)
  14. }
  15. type Float float64
  16. func (Float) isElement() {}
  17. func (Float) getType() ElementType {
  18. return ElemFloat
  19. }
  20. func (i Float) String() string {
  21. return strconv.FormatFloat(float64(i), 'f', -1, 64)
  22. }
  23. func convertDozenal(d string) string {
  24. result := ""
  25. for _, c := range d {
  26. if c == '↊' {
  27. result += "a"
  28. } else if c == '↋' {
  29. result += "b"
  30. } else if c == '·' {
  31. result += "."
  32. } else {
  33. result += string(c)
  34. }
  35. }
  36. return result
  37. }
  38. func parseNumber(t token) (token, error) {
  39. l := len(t.t)
  40. if (in('.', []rune(t.t)) && t.t[l-1] != '.') || (in('·', []rune(t.t)) && t.t[l-1] != '·') {
  41. return parseFloat(t)
  42. } else if in('e', []rune(t.t)) && t.t[0] != '0' {
  43. return parseEngFloat(t)
  44. } else {
  45. return parseInt(t)
  46. }
  47. }
  48. func parseFloat(t token) (token, error) {
  49. t.ttype = FLOAT
  50. if in('·', []rune(t.t)) {
  51. number := strings.SplitN(convertDozenal(t.t), ".", 2)
  52. whole, err := strconv.ParseInt(number[0], 12, 64)
  53. if err != nil {
  54. return token{}, err
  55. }
  56. fraction, err := strconv.ParseUint(number[1], 12, 64)
  57. if err != nil {
  58. return token{}, err
  59. }
  60. t.t = ""
  61. w := float64(whole)
  62. if fraction != 0 {
  63. var sign float64
  64. if w < 0 {
  65. sign = -1.0
  66. } else {
  67. sign = 1.0
  68. }
  69. l := math.Floor(math.Log10((float64(fraction))/math.Log10(12)) + 1)
  70. w += (float64(fraction) / math.Pow(12, l)) * sign
  71. }
  72. t.f = &w
  73. return t, nil
  74. } else {
  75. f, err := strconv.ParseFloat(t.t, 64)
  76. t.t = ""
  77. t.f = &f
  78. return t, err
  79. }
  80. }
  81. func parseEngFloat(t token) (token, error) {
  82. t.ttype = FLOAT
  83. number := strings.SplitN(t.t, "e", 2)
  84. a, err := strconv.ParseFloat(number[0], 64)
  85. if err != nil {
  86. return token{}, err
  87. }
  88. b, err := strconv.ParseInt(number[1], 10, 64)
  89. if err != nil {
  90. return token{}, err
  91. }
  92. num := a * math.Pow10(int(b))
  93. t.t = ""
  94. t.f = &num
  95. return t, nil
  96. }
  97. func parseComma(num string, z int) (string, error) {
  98. num2 := ""
  99. prevIsComma := false
  100. lastComma := -1
  101. zeroes := ""
  102. for i := 0; i < z; i++ {
  103. zeroes += "0"
  104. }
  105. for i, d := range num {
  106. if d == ',' {
  107. if prevIsComma {
  108. num2 += zeroes
  109. } else if !((i-lastComma) == z+1 || lastComma == -1) {
  110. return "", NewCommaError(num)
  111. }
  112. lastComma = i
  113. } else {
  114. num2 += string(d)
  115. }
  116. prevIsComma = d == ','
  117. }
  118. return num2, nil
  119. }
  120. func parseInt(t token) (token, error) {
  121. num2 := t.t
  122. base := 10
  123. if t.t[0] == '0' && t.t[1] == 'b' {
  124. num := t.t[2:]
  125. if num[0] == ',' {
  126. num = "1" + num
  127. }
  128. var err error = nil
  129. num2, err = parseComma(num, 8)
  130. if err != nil {
  131. return token{}, err
  132. }
  133. num2 = "0b" + num2
  134. base = 0
  135. }
  136. if t.t[0] == '0' && t.t[1] == 'o' {
  137. num2 = t.t
  138. base = 0
  139. }
  140. if t.t[0] == '0' && t.t[1] == 'x' {
  141. num2 = t.t
  142. base = 0
  143. }
  144. if in('·', []rune(t.t)) {
  145. num, err := parseComma(t.t, 3) // 4?
  146. if err != nil {
  147. return token{}, err
  148. }
  149. num2 = convertDozenal(num)
  150. num2 = num2[:len(num2)-1]
  151. base = 12
  152. }
  153. if in('.', []rune(t.t)) {
  154. num2 = t.t[:len(t.t)-1]
  155. var err error = nil
  156. parseComma(num2, 3)
  157. if err != nil {
  158. return token{}, err
  159. }
  160. base = 10
  161. }
  162. result, err := strconv.ParseInt(num2, base, 64)
  163. t.i = &result
  164. return t, err
  165. }