+/* makeFloatToken: create token for floating literals */
+static Token makeFloatToken(double value, const char* lexeme) {
+ Token t;
+ t.kind = TK_FLOATLIT;
+ t.floatValue = value;
+ t.lexeme = strdup(lexeme);
+ t.intValue = 0;
+ return t;
+}
+
+/* lexNum: lexing numeric literals */
+static Token lexNum(void) {
+ char buffer[256];
+ int idx = 0;
+ bool hasDot = false;
+ bool hasExponent = false;
+
+ /* capture digits */
+ while (isdigit(g_currentChar) && idx < 255) {
+ buffer[idx++] = (char)g_currentChar;
+ advance();
+ }
+
+ /* check for dot */
+ if (g_currentChar == '.') {
+ hasDot = true;
+ buffer[idx++] = (char)g_currentChar;
+ advance();
+ while (isdigit(g_currentChar) && idx < 255) {
+ buffer[idx++] = (char)g_currentChar;
+ advance();
+ }
+ }
+
+ /* check for exponent */
+ if (g_currentChar == 'e' || g_currentChar == 'E') {
+ hasExponent = true;
+ buffer[idx++] = (char)g_currentChar;
+ advance();
+ if (g_currentChar == '+' || g_currentChar == '-') {
+ buffer[idx++] = (char)g_currentChar;
+ advance();
+ }
+ while (isdigit(g_currentChar) && idx <255) {
+ buffer[idx++] = (char)g_currentChar;
+ advance();
+ }
+ }
+
+ buffer[idx] = '\0';
+
+ if (hasDot || hasExponent) {
+ double value = strtod(buffer, NULL);
+ return makeFloatToken(value, buffer);
+ } else {
+ int value = atoi(buffer);
+ return makeNumberToken(value, buffer);
+ }
+}
+