[master] 5ef563a74 Convert CNUM and FNUM to double as part of lexing

Poul-Henning Kamp phk at FreeBSD.org
Fri May 28 13:26:05 UTC 2021


commit 5ef563a742fb73586514bac6008625c7a521f68f
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri May 28 13:14:41 2021 +0000

    Convert CNUM and FNUM to double as part of lexing

diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index f97e5b46d..517dab5a6 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -101,6 +101,7 @@ struct token {
 	VTAILQ_ENTRY(token)	src_list;
 	unsigned		cnt;
 	char			*dec;
+	double			num;
 };
 
 /*---------------------------------------------------------------------*/
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index 020ada098..7d8d82926 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -691,13 +691,13 @@ vcc_number(struct vcc *tl, struct expr **e, vcc_type_t fmt, const char *sign)
 		t = tl->t;
 		vcc_NextToken(tl);
 		if (tl->t->tok == ID) {
-			e1 = vcc_mk_expr(DURATION, "(%s%.*s) * %g",
-			    sign, PF(t), vcc_DurationUnit(tl));
+			e1 = vcc_mk_expr(DURATION, "%s%.3f * %g",
+			    sign, t->num, vcc_DurationUnit(tl));
 			ERRCHK(tl);
 		} else if (fmt == REAL || t->tok == FNUM) {
-			e1 = vcc_mk_expr(REAL, "%s%.*s", sign, PF(t));
+			e1 = vcc_mk_expr(REAL, "%s%.3f", sign, t->num);
 		} else {
-			e1 = vcc_mk_expr(INT, "%s%.*s", sign, PF(t));
+			e1 = vcc_mk_expr(INT, "%s%.0f", sign, t->num);
 		}
 	}
 	e1->constant = EXPR_CONST;
diff --git a/lib/libvcc/vcc_token.c b/lib/libvcc/vcc_token.c
index 05f8d3627..35cc6421b 100644
--- a/lib/libvcc/vcc_token.c
+++ b/lib/libvcc/vcc_token.c
@@ -446,6 +446,7 @@ static const char *
 vcc_lex_number(struct vcc *tl, struct source *sp, const char *p)
 {
 	const char *q, *r;
+	char *s;
 
 	for (q = p; q < sp->e; q++)
 		if (!vct_isdigit(*q))
@@ -457,6 +458,8 @@ vcc_lex_number(struct vcc *tl, struct source *sp, const char *p)
 			vcc_ErrWhere(tl, tl->t);
 			return (NULL);
 		}
+		tl->t->num = strtod(p, &s);
+		assert(s == tl->t->e);
 		return (q);
 	}
 	r = ++q;
@@ -469,6 +472,8 @@ vcc_lex_number(struct vcc *tl, struct source *sp, const char *p)
 		vcc_ErrWhere(tl, tl->t);
 		return(NULL);
 	}
+	tl->t->num = strtod(p, &s);
+	assert(s == tl->t->e);
 	return (r);
 }
 
diff --git a/lib/libvcc/vcc_utils.c b/lib/libvcc/vcc_utils.c
index 47f6ab00c..7ced4edc1 100644
--- a/lib/libvcc/vcc_utils.c
+++ b/lib/libvcc/vcc_utils.c
@@ -284,20 +284,13 @@ vcc_DurationUnit(struct vcc *tl)
 uint64_t
 vcc_UintVal(struct vcc *tl)
 {
-	const char *p, *errtxt;
 	int64_t retval;
 
 	if (tl->t->tok != CNUM) {
 		Expect(tl, CNUM);
 		return (0);
 	}
-	p = tl->t->b;
-	retval = SF_Parse_Integer(&p, &errtxt);
-	if (errno) {
-		VSB_printf(tl->sb, "Bad UINT: %s\n", errtxt);
-		vcc_ErrWhere(tl, tl->t);
-		return (0);
-	}
+	retval = (int64_t)round(tl->t->num);
 	if (retval < 0) {
 		VSB_printf(tl->sb, "UINT cannot be negative\n");
 		vcc_ErrWhere(tl, tl->t);
@@ -310,20 +303,13 @@ vcc_UintVal(struct vcc *tl)
 static double
 vcc_DoubleVal(struct vcc *tl)
 {
-	const char *p, *errtxt;
 	double retval;
 
 	if (tl->t->tok != CNUM && tl->t->tok != FNUM) {
 		Expect(tl, CNUM);
 		return (0);
 	}
-	p = tl->t->b;
-	retval = SF_Parse_Decimal(&p, &errtxt);
-	if (errno) {
-		VSB_printf(tl->sb, "Bad REAL: %s\n", errtxt);
-		vcc_ErrWhere(tl, tl->t);
-		return (0);
-	}
+	retval = tl->t->num;
 	vcc_NextToken(tl);
 	return (retval);
 }
@@ -349,19 +335,13 @@ vcc_ByteVal(struct vcc *tl, VCL_INT *d)
 {
 	double v;
 	VCL_INT retval;
-	const char *p, *errtxt;
+	const char *errtxt;
 
 	if (tl->t->tok != CNUM && tl->t->tok != FNUM) {
 		Expect(tl, CNUM);
 		return;
 	}
-	p = tl->t->b;
-	v = SF_Parse_Number(&p, &errtxt);
-	if (errno) {
-		VSB_printf(tl->sb, "Bad BYTES: %s\n", errtxt);
-		vcc_ErrWhere(tl, tl->t);
-		return;
-	}
+	v = tl->t->num;
 	vcc_NextToken(tl);
 	if (tl->t->tok != ID) {
 		VSB_cat(tl->sb, "Expected BYTES unit (B, KB, MB...) got ");


More information about the varnish-commit mailing list