Proposal: Octal prefix

Informal

This page is dedicated to discussing this specific proposal

ContributeContributions

sxjh9935avatar of sxjh9935 [369] Octal prefixProposal2024-12-01 00:16:09

Author:

sxjh9935

Change Log:

2024-12-01 - First draft of proposal.

Problem:

It appears there is no octal prefix whereas prefixes for decimal, hexadecimal and binary exist (as per 3.4.1.3).

Solution:

Add octal prefix to 3.4.1.3.

Perhaps use a leading 0 so that it is consistent with other languages like C.

Implementation will be dependent on the interpreter.

Typical use: (Optional)

Valid examples

0377 0111 0777 00

Invalid examples

0888 0008 0989

Proposal:

This should enumerate the changes to the document at 3.4.1.3.

Reference implementation:

Implementation should be left to the interpreter's author.

BerndPaysanavatar of BerndPaysan

Since 0 wasn't a base switching prefix before, this will break code that has numbers starting with 0. I did a quick grep over the Gforth sources, and there are indeed places where 0-prefixed numbers are used, and only one of those is actually in a block that starts with 8 base !. The others are hex or decimal.

sxjh9935avatar of sxjh9935

Do we know which Forth standard introduced number prefixes?

PeterFalthavatar of PeterFalth

I use & as octal prefix

&10 . 8 ok

I did not invent that so there must be other systems also using it!

BR Peter

AntonErtlavatar of AntonErtl

Forth-2012 introduced number prefixes.

I dimly remember seeing & in some non-Forth place as octal prefix, and indeed, Free Pascal uses that (but my memory is certainly not from Free Pascal; probably some assembly language). However, as documented in the number prefixes proposal, there is conflicting usage in Forth systems concerning the & prefix: BigForth, PFE, Gforth, and Win32Forth 6 use it as decimal prefix, SwiftForth and ntf/lxf use it for octal. So it's unlikely that we will standardize & as octal prefix.

Searching a little around some assembly languages, I found that some 68000 and 6502 assembly languages use @ as octal prefix.

Implementing another prefix would be cheap, but OTOH, the benefit also appears to be miniscule.

sxjh9935avatar of sxjh9935

@AntonErtl Thanks, I thought it was Forth-2012 that introduced the prefixes but I'm little surprised that an octal prefix was not agreed upon considering its prevalence in computing.

I note your comments about using & as a prefix and I see how it will cause problems with earlier implementations of Forth but isn't that inconsistent with the spirit of creating a standard? I would have thought the standard would govern the implementations rather than the implementations governing the standard.

In terms of using @ as a prefix, I have some reservations considering the standalone word @ (fetch). I could see some confusion being created if such prefix was used.

Regarding your comment that the benefit of an octal prefix would be minuscule, I respectfully disagree. Some benefits of an octal prefix are-

  1. you can readily use octal whilst having a different base; and

  2. you can ensure that the number is octal notwithstanding any change to the base.

I find it quite common to mix binary, hexadecimal and octal numbers when writing low-level code; for example one would use binary when bit toggling flags, hexadecimal for memory addresses, and octal for encoding (especially x86 instructions).

Because of such, I make it a practice to ensure all binary, hexadecimal and decimal numbers are prefixed and all octal numbers are not prefixed but start with a 0.

The problem with my practice is that I cannot protect my code against inadvertent changes to the BASE and, therefore, I am frequently calling 8 BASE ! as a precaution.

In terms of & and/or 0 not being an acceptable prefix because of breaking existing Forth implementations, etc perhaps a 'q' prefix (looks like an o but won't be misread as a 0) might be a suitable alternative?

alextangentavatar of alextangent

Here's a suggestion. Use & as a prefix.

The nice feature of using & is that it is part of the ASCII sequence #$%& and lookup for the prefix is one of the values between # and & inclusive (decimal 35 thru 38). I'm not aware of any other formal or informal use of &.

\ Can be used to force number recognition in any base
\ $ -- hex prefix
\ # -- decimal
\ % -- binary
\ & -- octal
\                #     $     %    \ &    \ '
create num-bases 10 c, 16 c, 2 c, \ 8 c, \ 0 c,

: num-base! ( addr u -- addr' u' ) 
\ given a string, if prefixed set BASE and return the rest of the string
\ otherwise return the input string
    over c@ '#' - dup 4 u< if
      >r 1 /string r>
      num-bases + c@ base!
    else drop then ;

alextangentavatar of alextangent

Ah, I just saw Anton's references to prior art with &. I will continue to use it on my system, but my use of it is mainly in the assembler & disassembler where the x86 instruction set uses many octal encodings. I use @ for indirect references (eg @name is the address of name). Since I use recognizers, the & and @ prefixes could be switched in and out easily, Notwithstanding that, I wouldn't standardize any octal prefix; it's a lot of potential pain for not a lot of gain. Use recognizers if that's what you need.

ruvavatar of ruv

Notwithstanding that, I wouldn't standardize any octal prefix; it's a lot of potential pain for not a lot of gain. Use recognizers if that's what you need.

Agreed! If an octal prefix is needed, it can be easily added using the Recognizer API so that it only applies in limited lexical scopes.

By the way, in JavaScript, Go, Rust, Swift, Python the prefix «0o» is used for octal numeric literals.

Reply New Version