Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- else if (cur.isDecimalDigit()) {
- // Number: [0-9]{1,3}(.[0-9]{3})*
- String val = "";
- //get number
- do {
- val += (cur.cp() == '.') ? "." : cur.cp() - '0';
- cur.incPosition();
- }
- while (cur.isDecimalDigit() || cur.cp() == '.');
- //check dot position
- for (int i = val.length() - 1, c = 0; i >= 0; i--, c++) {
- /*
- . && c!=3 err
- . && c==3 c=0
- d && c==3 err
- d && c!=3 ok
- */
- if (val.charAt(i) == '.' && c == 3) {
- c = -1; // c++ = 0
- } else if (c == 3 || val.charAt(i) == '.') {
- compiler.addMessage(
- true,
- start,
- "wrong position of delimiters in number");
- break;
- }
- }
- //remove not num chars
- val = val.replaceAll("\\D", "");
- //calculate value of number
- long num = 0;
- try {
- for (char c: val.toCharArray()) {
- num = Math.addExact(Math.multiplyExact(num, 10), (c - '0'));
- }
- } catch (ArithmeticException e) {
- compiler.addMessage(
- true,
- start,
- "integral constant is too large");
- }
- val = "" + num;
- //add dots
- String res = "";
- for (int i = val.length() - 1, c = 0; i >= 0; i--, c++) {
- if (c == 3) {
- c = 0;
- res = "." + res;
- }
- res = val.charAt(i) + res;
- }
- val = res;
- if (cur.isLetter()) {
- compiler.addMessage(
- true,
- new Position(cur),
- "delimiter required");
- }
- return new NumberToken(
- val,
- start,
- new Position(cur));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement