commit b4ab34f84703c578f69cd87ec58e352a1f7dea37
parent b5c269e97ba07b784c94b460da9e07a7d7374186
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 6 May 2021 14:04:40 -0400
hare::lex: floating point constants
Diffstat:
2 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/hare/lex/+test.ha b/hare/lex/+test.ha
@@ -56,8 +56,13 @@ fn lextest(in: str, expected: []token) void = {
};
assert(tl.0 == etok.0);
vassert(tl.1, etok.1);
- assert(tl.2.line == etok.2.line && tl.2.col == etok.2.col
- && tl.2.path == etok.2.path);
+ if (tl.2.line != etok.2.line || tl.2.col != etok.2.col
+ || tl.2.path != etok.2.path) {
+ fmt::errorfln("{}:{},{} != {}:{},{}",
+ tl.2.path, tl.2.line, tl.2.col,
+ etok.2.path, etok.2.line, etok.2.col)!;
+ abort();
+ };
};
let t = lex(&lexer) as token;
assert(t.0 == ltok::EOF);
@@ -240,10 +245,10 @@ fn loc(line: uint, col: uint) location = location {
};
@test fn literals() void = {
- // TODO: Float literals
const in = "1e5 -1i32 9223372036854775809 1e2z 255u8 0o42u16\n"
"0b1000101u32 0xDEADBEEFu64 -0b10i8 -5e0i16 -0o16i32\n"
- "0b00000010000001100000011100001111000000100000011000000111i64";
+ "0b00000010000001100000011100001111000000100000011000000111i64\n"
+ "13.37 13.37f32 13.37f64";
const expected: [_]token = [
(ltok::LIT_ICONST, 1e5i64, loc(1, 1)),
(ltok::LIT_I32, -1i64, loc(1, 5)),
@@ -256,8 +261,10 @@ fn loc(line: uint, col: uint) location = location {
(ltok::LIT_I8, -0b10i64, loc(2, 28)),
(ltok::LIT_I16, -5e0i64, loc(2, 36)),
(ltok::LIT_I32, -0o16i64, loc(2, 44)),
- // Binary solo
(ltok::LIT_I64, 0b00000010000001100000011100001111000000100000011000000111i64, loc(3, 1)),
+ (ltok::LIT_FCONST, 13.37, loc(4, 1)),
+ (ltok::LIT_F32, 13.37, loc(4, 7)),
+ (ltok::LIT_F64, 13.37, loc(4, 16)),
];
lextest(in, expected);
};
diff --git a/hare/lex/lex.ha b/hare/lex/lex.ha
@@ -429,7 +429,8 @@ fn lex_literal(lex: *lexer, loc: location) (token | error) = {
},
ltok::LIT_I8, ltok::LIT_I16, ltok::LIT_I32, ltok::LIT_I64,
ltok::LIT_INT => strconv::stoi64b(val, base),
- ltok::LIT_F32, ltok::LIT_F64, ltok::LIT_FCONST => abort(), // TODO
+ ltok::LIT_F32, ltok::LIT_F64, ltok::LIT_FCONST =>
+ strconv::stof64(val),
};
let val = match (val) {
val: u64 => {
@@ -444,6 +445,12 @@ fn lex_literal(lex: *lexer, loc: location) (token | error) = {
};
val;
},
+ val: f64 => {
+ for (let i = 0z; i < exp; i += 1) {
+ val *= 10.0;
+ };
+ val;
+ },
strconv::invalid => abort(), // Shouldn't be lexed in
strconv::overflow =>
return syntaxerr(loc, "overflow in exponent"),