Support scientific-notation float literals (1.5e10, 1e6)#82
Merged
Conversation
Writing a float in exponent form (1.5e10, 2.0e-3, 1e6) did not produce the expected value. The tokenizer scanned only mantissa digits, so 1.5e10 lexed as FLOATCONST(1.5) followed by IDENT(e10) -- which the parser then read as a function call, yielding the misdirecting "Function 'e30' not found". A real downstream consumer hit this on `Local huge# = 1.0e30` and had to rewrite every constant as a plain decimal. The value pipeline was already exponent-correct (FloatConstNode converts via atof, which parses "1.5e10"/"1e30" fine); the gap was purely tokenization. Scan an optional [eE][+-]?<digits> suffix on all three numeric paths and emit FLOATCONST. A bare-integer mantissa with an exponent is promoted to float (1e6 is a float, not an int). The scan is conservative: the e/E is consumed only when a full [eE][+-]?<digit> follows, so 1.0e / 1.0eq / 1.0e+x keep their existing behavior (float 1.0 followed by a separate identifier) -- no legacy program can depend on the old tokenization because every newly-accepted form was previously a hard compile error. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add acceptance tests to MathsTest.bb (written clean-room from the criteria) covering the canonical forms (1.5e3, 2.0e-3, 1.0e+2), the risky no-dot promotion (1e6) and leading-dot (.5e2) paths, exponents in arithmetic, a large-magnitude case (asserted by magnitude/round-trip since FloatClose's absolute tolerance is meaningless at 1e30), and a conservative-guard case proving a bare e is not swallowed. Document exponent notation on the float-literals reference page, and record the working note for the change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary (non-technical)
You can now write floating-point numbers in exponent / scientific notation —
1.5e10,2.0e-3,1e6— and they evaluate to the value you'd expect. Before this change, writing1.0e30didn't just fail; it failed with a bafflingFunction 'e30' not found, sending you hunting for a typo in a function that doesn't exist. A real downstream consumer (rcce2) hit exactly this onLocal huge# = 1.0e30and had to rewrite every constant as a plain decimal and record the gotcha. Exponent literals are table stakes for a language; this closes the gap and makes more legacy/standard BASIC drop-in compatible.Technical summary
The value pipeline was already exponent-correct:
FloatConstNodeconverts its token viaatof()(parser.cpp:1106,exprnode.cpp:385), andatof("1.5e10")/atof("1e30")parse correctly. The entire gap was in tokenization —toker.cpp's numeric paths scanned only mantissa digits, so1.5e10lexed asFLOATCONST(1.5)+IDENT(e10)and the parser reade10as a call.src/blitzrc/compiler/toker.cpp: add a smallscanExphelper that consumes an optional[eE][+-]?<digits>suffix, applied to all three numeric paths (.-leading, digit-with-dot, digit-no-dot). A bare-integer mantissa with an exponent is promoted toFLOATCONST(1e6is a float, not an int).e/Eis consumed only when a full[eE][+-]?<digit>follows.1.0e,1.0eq,1.0e+xkeep their current behavior (1.0+ a separate identifier). No legacy program can depend on the old tokenization, because every newly-accepted form was previously a hard compile error.No breaking changes.
Acceptance criteria & results
1.5e3=1500,2.0e-3=0.002,1.0e+2=100,1e6=1000000,.5e2=50 — pinned byAssert(FloatClose(...))intests/MathsTest.bb;blitzcc -t tests/MathsTest.bbpasses (all 5 new blocks).1.5eq→Function 'eq' not found,1.5e+q→Function 'e' not found(theeis not swallowed);1.5e10compiles. Verified at the CLI and pinned in a Test block.Local huge# = 1.0e30now compiles (exit 0).test.batsuite green ("Tests passed").lang_ref_basicdatatypes.htmldocuments exponent notation with examples.Trade-offs / deferred
1e30to float deliberately (the common case; leaving it out would be a second sharp edge). Safe: an identifier can't start with a digit, so1e30was alwaysINTCONST(1)+IDENT(e30)= a parse error before.Release/Recast/Referencecontextual-keyword work (still the top legacy-compat backlog item).🤖 Generated with Claude Code