Skip to content

Commit 9670580

Browse files
committed
Half-assed implementation of str2float
1 parent 086af3a commit 9670580

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { configuration } from '../../configuration/configuration';
44
import { ErrorCode, VimError } from '../../error';
55
import { globalState } from '../../state/globalState';
66
import { bool, float, funcref, listExpr, int, str, list, funcCall, blob } from './build';
7-
import { expressionParser, numberParser } from './parser';
7+
import { expressionParser, floatParser, numberParser } from './parser';
88
import {
99
BinaryOp,
1010
ComparisonOp,
@@ -1276,7 +1276,12 @@ export class EvaluationContext {
12761276
const [x] = getArgs(1);
12771277
return float(Math.sqrt(toFloat(x!)));
12781278
}
1279-
// TODO: str2float()
1279+
case 'str2float': {
1280+
// TODO: There are differences. See `:help str2float`
1281+
const [s, quoted] = getArgs(1, 2);
1282+
const result = floatParser.parse(toString(s!));
1283+
return result.status === true ? result.value : float(0);
1284+
}
12801285
case 'str2list': {
12811286
const [s, _ignored] = getArgs(1, 2);
12821287
const result: number[] = [];

src/vimscript/expression/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const decimalOrOctalNumberParser: Parser<NumberValue> = regexp(/\d+/).map((x) =>
7171
return int(Number.parseInt(x, base));
7272
});
7373

74-
const floatParser: Parser<FloatValue> = seq(
74+
export const floatParser: Parser<FloatValue> = seq(
7575
regexp(/\d+\.\d+/).map((x) => Number.parseFloat(x)),
7676
alt(string('e'), string('E'))
7777
.then(

0 commit comments

Comments
 (0)