package org.yinwang.yin.parser;


import java.math.BigInteger;

public class IntNum extends Token {

    public String content;
    public BigInteger value;
    public int base;


    public IntNum(String content, String file, int start, int end, int line, int col) {
        super(TokenType.NUMBER, content, file, start, end, line, col);
        this.content = content;

        int sign;
        String valueContent;

        if (content.startsWith("+")) {
            sign = 1;
            valueContent = content.substring(1);
        } else if (content.startsWith("-")) {
            sign = -1;
            valueContent = content.substring(1);
        } else {
            sign = 1;
            valueContent = content;
        }

        if (valueContent.startsWith("0b")) {
            base = 2;
            valueContent = valueContent.substring(2);
        } else if (valueContent.startsWith("0x")) {
            base = 16;
            valueContent = valueContent.substring(2);
        } else if (content.startsWith("0") && content.length() >= 2) {
            base = 8;
            valueContent = valueContent.substring(1);
        } else {
            base = 10;
        }

        this.value = parseValue(valueContent, base, sign);
    }


    public static String prefix(int base) {
        if (base == 2) {
            return "0b";
        } else if (base == 8) {
            return "0";
        } else if (base == 16) {
            return "0x";
        } else {
            return "";
        }
    }


    public static BigInteger parseValue(String s, int base, int sign) {
        BigInteger value = new BigInteger(s, base);
        if (sign == -1) {
            value = value.negate();
        }

        return value;
    }


    public static IntNum parse(String content, String file, int start, int end, int line, int col) {
        try {
            return new IntNum(content, file, start, end, line, col);
        } catch (NumberFormatException e) {
            return null;
        }
    }


    public String toString() {
        return content + "=" + value;
    }

}

@@ -387,38 +387,8 @@
 gn;%0A
-        String valueContent;%0A%0A
     
@@ -448,38 +448,33 @@
  1;%0A            
-valueC
+c
 ontent = content
@@ -561,38 +561,33 @@
 -1;%0A            
-valueC
+c
 ontent = content
@@ -644,44 +644,8 @@
  1;%0A
-            valueContent = content;%0A
     
@@ -667,35 +667,52 @@
 if (
-valueC
+c
 ontent.
-start
+endsWith(%22b%22) %7C%7C content.end
 sWith(%22
-0b
+B
 %22)) 
@@ -739,38 +739,33 @@
  2;%0A            
-valueC
+c
 ontent = valueCo
@@ -749,38 +749,33 @@
       content = 
-valueC
+c
 ontent.substring
@@ -767,33 +767,55 @@
 ntent.substring(
-2
+0, content.length() - 1
 );%0A        %7D els
@@ -824,35 +824,52 @@
 if (
-valueC
+c
 ontent.
-start
+endsWith(%22h%22) %7C%7C content.end
 sWith(%22
-0x
+H
 %22)) 
@@ -897,38 +897,33 @@
 16;%0A            
-valueC
+c
 ontent = valueCo
@@ -907,38 +907,33 @@
       content = 
-valueC
+c
 ontent.substring
@@ -933,17 +933,39 @@
 bstring(
-2
+0, content.length() - 1
 );%0A     
@@ -990,26 +990,24 @@
 ent.
-start
+end
 sWith(%22
-0%22) &&
+o%22) %7C%7C
  con
@@ -1015,21 +1015,21 @@
 ent.
-l
 en
-gth() %3E= 2
+dsWith(%22O%22)
 ) %7B%0A
@@ -1062,22 +1062,17 @@
         
-valueC
+c
 ontent =
@@ -1072,22 +1072,17 @@
 ntent = 
-valueC
+c
 ontent.s
@@ -1082,32 +1082,54 @@
 ntent.substring(
+0, content.length() - 
 1);%0A        %7D el
@@ -1203,14 +1203,9 @@
 lue(
-valueC
+c
 onte
@@ -1233,274 +1233,8 @@
 %7D%0A%0A%0A
-    public static String prefix(int base) %7B%0A        if (base == 2) %7B%0A            return %220b%22;%0A        %7D else if (base == 8) %7B%0A            return %220%22;%0A        %7D else if (base == 16) %7B%0A            return %220x%22;%0A        %7D else %7B%0A            return %22%22;%0A        %7D%0A    %7D%0A%0A%0A
     

change number format