Skip to content

Commit

Permalink
Add support for double notation eg. 1234. => 1234 0 on the stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Nov 11, 2024
1 parent c2f2180 commit 111fc85
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,13 @@ Regardless of the current value of `BASE`, it is possible to input numbers in on

When current `BASE` is ten (10) its possible to input floating point numbers (provided support is enabled) with a decimal point and/or scientific notation. For example:

0.0 = 0. = 0E0 = 0e0
1.0 = 1. = 1E0 = 1e0 = +1.E0
0.0 = 0E0 = 0e0
1.0 = 1E0 = 1e0 = +1.E0
12.3 = 123E-1
-0.123 = -123e-3

Floating point numbers are placed on the float stack, which is separate from the data and return stacks. Also the float stack is small, though at least six (6) deep. See [Floating-Point Words](./doc/float.md), in particular words [f.](./doc/float.md) and [fs.](./doc/float.md) to start with.

- *Note the double-cell input notation, `1234.` (equivalent to `1234 0`), is not supported.*
- *Note the input notation, `123E`, where there is no value following the exponent is not supported.*

It is also possible to input a character constant or backslash escape character. Simple use single-quotes around the character or backslash-escape string (see also `CHAR` and `[CHAR]`). For example:
Expand Down
19 changes: 16 additions & 3 deletions src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,11 @@ p4Base36(int digit)
}

int
p4StrNum(P4_String str, int base, P4_Cell *out, int *is_float)
p4StrNum(P4_String str, int base, P4_Cell *out, int *is_float, int *is_double)
{
size_t offset = 0;
*is_float = 0;
*is_double = 0;
if (str.length == 0) {
return -1;
}
Expand Down Expand Up @@ -254,6 +255,11 @@ p4StrNum(P4_String str, int base, P4_Cell *out, int *is_float)
for (num.n = 0; offset < str.length; offset++) {
int digit = p4Base36(str.string[offset]);
if (base <= digit) {
/* Support small integer double notation 123. => 123 0 */

This comment has been minimized.

Copy link
@ruv

ruv Nov 11, 2024

Contributor

-1. shall be interpreted as -1 -1. I.e., -1. d. shall print -1. At the moment it prints 18446744073709551615, which is the same as -1 0 d. .

It's better to throw an exception than to return an incorrect value.

This comment has been minimized.

Copy link
@SirWumpus

SirWumpus Nov 11, 2024

Author Owner

Doh!

This comment has been minimized.

Copy link
@SirWumpus

SirWumpus Nov 11, 2024

Author Owner

I really need to add some tests for this, else I would have noticed sooner. Daisy. Daisy ...

if (offset + 1 == str.length && str.string[offset] == '.') {
*is_double = 1;
break;
}
#ifdef HAVE_MATH_H
char *stop;
/* We don't accept the double-cell notation 123. 0.
Expand Down Expand Up @@ -1345,8 +1351,8 @@ _inter_loop: while (ctx->input->offset < ctx->input->length) {
}
word = p4FindName(ctx, str.string, str.length);
if (word == NULL) {
int is_float;
if (p4StrNum(str, ctx->radix, &x, &is_float)) {
int is_float, is_double;
if (p4StrNum(str, ctx->radix, &x, &is_float, &is_double)) {
/* Not a word, not a number. */
THROW(P4_THROW_UNDEFINED);
}
Expand All @@ -1367,9 +1373,16 @@ _inter_loop: while (ctx->input->offset < ctx->input->length) {
if (ctx->state == P4_STATE_COMPILE) {
p4WordAppend(ctx, (P4_Cell) &w_lit);
p4WordAppend(ctx, x);
if (is_double) {
p4WordAppend(ctx, (P4_Cell) &w_lit);
p4WordAppend(ctx, (P4_Cell) 0L);
}
} else {
p4StackIsFull(ctx, &ctx->ds, P4_THROW_DS_OVER);
P4_PUSH(ctx->ds, x);
if (is_double) {
P4_PUSH(ctx->ds, (P4_Cell) 0L);
}
}
} else if (ctx->state == P4_STATE_INTERPRET && P4_WORD_IS(word, P4_BIT_COMPILE)) {
THROW(P4_THROW_COMPILE_ONLY);
Expand Down
2 changes: 1 addition & 1 deletion src/post4.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ extern int p4CharLiteral(int ch);
*/
extern void p4StrRev(P4_Char *s, P4_Size length);

extern int p4StrNum(P4_String str, int base, P4_Cell *out, int *is_float);
extern int p4StrNum(P4_String str, int base, P4_Cell *out, int *is_float, int *is_double);

extern int p4Accept(P4_Input *source, char *buffer, size_t size);

Expand Down

0 comments on commit 111fc85

Please sign in to comment.