Projekt

Obecné

Profil

Stáhnout (2.55 KB) Statistiky
| Větev: | Revize:
1
import Long from "@xtuc/long";
2
import parseHexFloat from "@webassemblyjs/floating-point-hex-parser";
3
import { CompileError } from "@webassemblyjs/helper-api-error";
4
export function parse32F(sourceString) {
5
  if (isHexLiteral(sourceString)) {
6
    return parseHexFloat(sourceString);
7
  }
8

    
9
  if (isInfLiteral(sourceString)) {
10
    return sourceString[0] === "-" ? -1 : 1;
11
  }
12

    
13
  if (isNanLiteral(sourceString)) {
14
    return (sourceString[0] === "-" ? -1 : 1) * (sourceString.includes(":") ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16) : 0x400000);
15
  }
16

    
17
  return parseFloat(sourceString);
18
}
19
export function parse64F(sourceString) {
20
  if (isHexLiteral(sourceString)) {
21
    return parseHexFloat(sourceString);
22
  }
23

    
24
  if (isInfLiteral(sourceString)) {
25
    return sourceString[0] === "-" ? -1 : 1;
26
  }
27

    
28
  if (isNanLiteral(sourceString)) {
29
    return (sourceString[0] === "-" ? -1 : 1) * (sourceString.includes(":") ? parseInt(sourceString.substring(sourceString.indexOf(":") + 1), 16) : 0x8000000000000);
30
  }
31

    
32
  if (isHexLiteral(sourceString)) {
33
    return parseHexFloat(sourceString);
34
  }
35

    
36
  return parseFloat(sourceString);
37
}
38
export function parse32I(sourceString) {
39
  var value = 0;
40

    
41
  if (isHexLiteral(sourceString)) {
42
    value = ~~parseInt(sourceString, 16);
43
  } else if (isDecimalExponentLiteral(sourceString)) {
44
    throw new Error("This number literal format is yet to be implemented.");
45
  } else {
46
    value = parseInt(sourceString, 10);
47
  }
48

    
49
  return value;
50
}
51
export function parseU32(sourceString) {
52
  var value = parse32I(sourceString);
53

    
54
  if (value < 0) {
55
    throw new CompileError("Illegal value for u32: " + sourceString);
56
  }
57

    
58
  return value;
59
}
60
export function parse64I(sourceString) {
61
  var long;
62

    
63
  if (isHexLiteral(sourceString)) {
64
    long = Long.fromString(sourceString, false, 16);
65
  } else if (isDecimalExponentLiteral(sourceString)) {
66
    throw new Error("This number literal format is yet to be implemented.");
67
  } else {
68
    long = Long.fromString(sourceString);
69
  }
70

    
71
  return {
72
    high: long.high,
73
    low: long.low
74
  };
75
}
76
var NAN_WORD = /^\+?-?nan/;
77
var INF_WORD = /^\+?-?inf/;
78
export function isInfLiteral(sourceString) {
79
  return INF_WORD.test(sourceString.toLowerCase());
80
}
81
export function isNanLiteral(sourceString) {
82
  return NAN_WORD.test(sourceString.toLowerCase());
83
}
84

    
85
function isDecimalExponentLiteral(sourceString) {
86
  return !isHexLiteral(sourceString) && sourceString.toUpperCase().includes("E");
87
}
88

    
89
function isHexLiteral(sourceString) {
90
  return sourceString.substring(0, 2).toUpperCase() === "0X" || sourceString.substring(0, 3).toUpperCase() === "-0X";
91
}
(3-3/5)