From ad80fc51301f7349b04d50db71b13b5791e00dc3 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Sat, 29 Nov 2014 22:37:57 +0100 Subject: [PATCH] Support for floating point literals. --- Monicelli.lpp | 14 ++++++++++---- examples/float.mc | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 examples/float.mc diff --git a/Monicelli.lpp b/Monicelli.lpp index 1e189c8..f60c81f 100644 --- a/Monicelli.lpp +++ b/Monicelli.lpp @@ -22,7 +22,6 @@ #include "Parser.hpp" #include -#include static void meta(const char *); @@ -191,9 +190,16 @@ CHAR [a-zA-Z_] return token::ID; } -{DIGIT}+ { - lval->intval = strtol(yytext, NULL, 10); - return token::NUMBER; +[-+]?(({DIGIT}*".")?{DIGIT}+|{DIGIT}+".")([eE][-+]?{DIGIT}+)? { + std::string value(yytext); + + if (value.find(".") != std::string::npos) { + lval->floatval = std::stod(value); + return token::FLOAT; + } else { + lval->intval = std::stol(value); + return token::NUMBER; + } } . { diff --git a/examples/float.mc b/examples/float.mc new file mode 100644 index 0000000..12389c9 --- /dev/null +++ b/examples/float.mc @@ -0,0 +1,12 @@ +# Test all possible numerical forms (integer and float) + +Lei ha clacsonato + +2.0 a posterdati +-2.0 a posterdati +2. a posterdati +.232 a posterdati +-2 a posterdati +-.2233 a posterdati ++2 a posterdati ++2.233e-23 a posterdati