Index: llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.rst =================================================================== --- llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.rst +++ llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.rst @@ -138,9 +138,16 @@ if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ std::string NumStr; + int count=0;/*Counts number of '.'. If it is greater than 1 should return error. For example if we have the input as “1.23.45.67”, it should return an error since this is invalid*/ do { NumStr += LastChar; LastChar = getchar(); + if(LastChar == '.'){ + count++; + } + if(count>1){ + return LogError("Invalid number"); + } } while (isdigit(LastChar) || LastChar == '.'); NumVal = strtod(NumStr.c_str(), 0); @@ -149,10 +156,7 @@ This is all pretty straightforward code for processing input. When reading a numeric value from input, we use the C ``strtod`` function to -convert it to a numeric value that we store in ``NumVal``. Note that -this isn't doing sufficient error checking: it will incorrectly read -"1.23.45.67" and handle it as if you typed in "1.23". Feel free to -extend it! Next we handle comments: +convert it to a numeric value that we store in ``NumVal``.Next we handle comments: .. code-block:: c++ Index: llvm/examples/Kaleidoscope/Chapter2/toy.cpp =================================================================== --- llvm/examples/Kaleidoscope/Chapter2/toy.cpp +++ llvm/examples/Kaleidoscope/Chapter2/toy.cpp @@ -48,14 +48,21 @@ return tok_identifier; } - if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ + if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+ std::string NumStr; + int count=0; // Counts number of '.'. If it is greater than 1 should return false. For example if we have the input as “1.23.45.67”, it should return an error since this is invalid do { NumStr += LastChar; LastChar = getchar(); + if(LastChar == '.') { + count++; + } + if(count > 1) { + return LogError("Invalid number");; + } } while (isdigit(LastChar) || LastChar == '.'); - NumVal = strtod(NumStr.c_str(), nullptr); + NumVal = strtod(NumStr.c_str(), 0); return tok_number; }