add enum syntax (sugar for list of const decls)

This commit is contained in:
Irmen de Jong
2026-03-09 00:59:50 +01:00
parent caa991e478
commit 04910ffbc0
29 changed files with 417 additions and 271 deletions
@@ -1,6 +1,6 @@
Prog8 compiler v12.2-SNAPSHOT by Irmen de Jong (irmen@razorvine.net)
Prerelease version from git commit d720eeae in branch master
Prerelease version from git commit daef1828 in branch master
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
Compiling program import-all-c128.p8
@@ -944,12 +944,12 @@ LIBRARY MODULE NAME: wavfile
----------------------------
wavfile {
const ubyte WAVE_FORMAT_ADPCM
const ubyte WAVE_FORMAT_ALAW
const ubyte WAVE_FORMAT_DVI_ADPCM
const ubyte WAVE_FORMAT_IEEE_FLOAT
const ubyte WAVE_FORMAT_MULAW
const ubyte WAVE_FORMAT_PCM
const ubyte Format::ADPCM
const ubyte Format::ALAW
const ubyte Format::DVI_ADPCM
const ubyte Format::IEEE_FLOAT
const ubyte Format::MULAW
const ubyte Format::PCM
ubyte bits_per_sample
uword block_align
uword data_offset
@@ -1,6 +1,6 @@
Prog8 compiler v12.2-SNAPSHOT by Irmen de Jong (irmen@razorvine.net)
Prerelease version from git commit d720eeae in branch master
Prerelease version from git commit daef1828 in branch master
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
Compiling program import-all-c64.p8
@@ -1056,12 +1056,12 @@ LIBRARY MODULE NAME: wavfile
----------------------------
wavfile {
const ubyte WAVE_FORMAT_ADPCM
const ubyte WAVE_FORMAT_ALAW
const ubyte WAVE_FORMAT_DVI_ADPCM
const ubyte WAVE_FORMAT_IEEE_FLOAT
const ubyte WAVE_FORMAT_MULAW
const ubyte WAVE_FORMAT_PCM
const ubyte Format::ADPCM
const ubyte Format::ALAW
const ubyte Format::DVI_ADPCM
const ubyte Format::IEEE_FLOAT
const ubyte Format::MULAW
const ubyte Format::PCM
ubyte bits_per_sample
uword block_align
uword data_offset
@@ -1,6 +1,6 @@
Prog8 compiler v12.2-SNAPSHOT by Irmen de Jong (irmen@razorvine.net)
Prerelease version from git commit 3b687412 in branch master
Prerelease version from git commit daef1828 in branch master
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
Compiling program import-all-cx16.p8
@@ -757,10 +757,10 @@ LIBRARY MODULE NAME: psg2
psg2 {
const ubyte BOTH
const ubyte DISABLED
const ubyte E_ATTACK
const ubyte E_OFF
const ubyte E_RELEASE
const ubyte E_SUSTAIN
const ubyte EnvelopeState::ATTACK
const ubyte EnvelopeState::OFF
const ubyte EnvelopeState::RELEASE
const ubyte EnvelopeState::SUSTAIN
const ubyte LEFT
const ubyte NOISE
const ubyte PULSE
@@ -1627,12 +1627,12 @@ LIBRARY MODULE NAME: wavfile
----------------------------
wavfile {
const ubyte WAVE_FORMAT_ADPCM
const ubyte WAVE_FORMAT_ALAW
const ubyte WAVE_FORMAT_DVI_ADPCM
const ubyte WAVE_FORMAT_IEEE_FLOAT
const ubyte WAVE_FORMAT_MULAW
const ubyte WAVE_FORMAT_PCM
const ubyte Format::ADPCM
const ubyte Format::ALAW
const ubyte Format::DVI_ADPCM
const ubyte Format::IEEE_FLOAT
const ubyte Format::MULAW
const ubyte Format::PCM
ubyte bits_per_sample
uword block_align
uword data_offset
@@ -1,6 +1,6 @@
Prog8 compiler v12.2-SNAPSHOT by Irmen de Jong (irmen@razorvine.net)
Prerelease version from git commit d720eeae in branch master
Prerelease version from git commit daef1828 in branch master
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
Compiling program import-all-pet32.p8
@@ -871,12 +871,12 @@ LIBRARY MODULE NAME: wavfile
----------------------------
wavfile {
const ubyte WAVE_FORMAT_ADPCM
const ubyte WAVE_FORMAT_ALAW
const ubyte WAVE_FORMAT_DVI_ADPCM
const ubyte WAVE_FORMAT_IEEE_FLOAT
const ubyte WAVE_FORMAT_MULAW
const ubyte WAVE_FORMAT_PCM
const ubyte Format::ADPCM
const ubyte Format::ALAW
const ubyte Format::DVI_ADPCM
const ubyte Format::IEEE_FLOAT
const ubyte Format::MULAW
const ubyte Format::PCM
ubyte bits_per_sample
uword block_align
uword data_offset
@@ -1,6 +1,6 @@
Prog8 compiler v12.2-SNAPSHOT by Irmen de Jong (irmen@razorvine.net)
Prerelease version from git commit d720eeae in branch master
Prerelease version from git commit daef1828 in branch master
This software is licensed under the GNU GPL 3.0, see https://www.gnu.org/licenses/gpl.html
Compiling program import-all-virtual.p8
@@ -657,12 +657,12 @@ LIBRARY MODULE NAME: wavfile
----------------------------
wavfile {
const ubyte WAVE_FORMAT_ADPCM
const ubyte WAVE_FORMAT_ALAW
const ubyte WAVE_FORMAT_DVI_ADPCM
const ubyte WAVE_FORMAT_IEEE_FLOAT
const ubyte WAVE_FORMAT_MULAW
const ubyte WAVE_FORMAT_PCM
const ubyte Format::ADPCM
const ubyte Format::ALAW
const ubyte Format::DVI_ADPCM
const ubyte Format::IEEE_FLOAT
const ubyte Format::MULAW
const ubyte Format::PCM
ubyte bits_per_sample
uword block_align
uword data_offset
+1 -1
View File
@@ -180,7 +180,7 @@ Tips for effective profiling
- A routine with high average time per call may need algorithmic improvements
- Use the flame graph to quickly identify hot paths in the call stack
- Compare profiles before and after optimizations to measure improvement
= **Recompile the program WITHOUT profiling mode after you're done** -- because the profiling logic takes up space and slows down the actual program
- **Recompile the program WITHOUT profiling mode after you're done** -- because the profiling logic takes up space and slows down the actual program
Limitations
^^^^^^^^^^^
+1
View File
@@ -112,6 +112,7 @@ Constant ``p8c_``
Label ``p8l_``
Struct ``p8t_``
Struct Field ``p8v_``
Enum Member ``p8c_EnumName_MemberName``
other ``p8_``
================ ========
-3
View File
@@ -31,7 +31,6 @@ Future Things and Ideas
- struct/ptr: support for typed function pointers? (&routine could be typed by default as well then)
- struct/ptr: really fixing the pointer dereferencing issues (cursed hybrid beween IdentifierReference, PtrDereferece and PtrIndexedDereference) may require getting rid of scoped identifiers altogether and treat '.' as a "scope or pointer following operator"
- struct/ptr: (later, nasty parser problem:) support chaining pointer dereference on function calls that return a pointer. (type checking now fails on stuff like func().field and func().next.field)
- should we have a SourceStorageKind.POINTER? (there is one for TargetStorageKind...)
- make memory mapped variables support more constant expressions such as: &uword MyHigh = &mylong1+2 (see github issue #192)
- allow memory() to occur in array initializer (maybe needed for 2 dimensional arrays?) i.e. make it a constant (see github issue #192)
- allow the value of a memory mapped variable to be address-of another variable, not just a constant number
@@ -43,7 +42,6 @@ Future Things and Ideas
- sizeof(pointer) is now always 2 (an uword), make this a variable in the ICompilationTarget so that it could be 4 at the time we might ad a 32-bits 68000 target for example. Much code assumes word size addresses though.
- Two- or even multidimensional arrays and chained indexing, purely as syntactic sugar over regular arrays?
- when a complete block is removed because unused, suppress all info messages about everything in the block being removed
- is "checkAssignmentCompatible" redundant (gets called just 1 time!) when we also have "checkValueTypeAndRange" ?
- romable: should we have a way to explicitly set the memory address for the BSS area (add a -varsaddress and -slabsaddress options?)
- romable: fix remaining codegens (some for loops, see ForLoopsAsmGen)
- Kotlin: can we use inline value classes in certain spots? (domain types instead of primitives)
@@ -62,7 +60,6 @@ Future Things and Ideas
(this is already done hardcoded for several of the builtin functions)
- more support for (64tass) SEGMENTS in the prog8 syntax itself? maybe %segment blah in blocks?
- ability to use a sub instead of only a var for @bank ? what for though? dynamic bank/overlay loading?
- enums?
- BUG: fix the c64 multiplexer example
- Zig-like try-based error handling where the V flag could indicate error condition? and/or BRK to jump into monitor on failure? (has to set BRK vector for that) But the V flag is also set on certain normal instructions
+36 -14
View File
@@ -186,6 +186,42 @@ initializing from a subroutine returning multiple result values: ``ubyte a,b,c =
into the declared variables in order. Ofcourse the number of values has to match the number of variables.
Constants
---------
.. index:: single: Constants
When using ``const``, the value of the 'variable' cannot be changed; it has become a compile-time constant value instead.
You'll have to specify the initial value expression. This value is then used
by the compiler everywhere you refer to the constant (and no memory is allocated
for the constant itself). Onlythe simple numeric types (byte, word, long, float) can be defined as a constant.
If something is defined as a constant, very efficient code can usually be generated from it.
Variables on the other hand can't be optimized as much, need memory, and more code to manipulate them.
Note that a subset of the library routines in the ``math``, ``strings`` and ``floats`` modules are recognised in
compile time expressions. For example, the compiler knows what ``math.sin8u(12)`` is and replaces it with the computed result.
Enums
-----
.. index:: single: Enums
There is a more convenient way to define a bunch of constants that belong togther: a "enum".
That is a grouped list of constants that get autonumbered for you (unless you override the numeric value yourself).
It starts numbering from zero by default. Here's an example::
enum Priority {
LOW = 1,
NORMAL,
HIGH,
EXTREME=255
}
This will define a bunch of constants like so (the "Enum::Field" syntax is specific for enumeration elements)::
const ubyte Priority::LOW = 1
const ubyte Priority::NORMAL= 2
const ubyte Priority::HIGH = 3
const ubyte Priority::EXTREME = 255
Data Types
----------
.. index:: pair: Data Types; Overview
@@ -581,20 +617,6 @@ Range expressions are most often used in for loops, but can be also be used to c
byte[] array = 100 to 199 ; initialize array with [100, 101, ..., 198, 199]
Constants
^^^^^^^^^
.. index:: pair: Data Types; Constants
When using ``const``, the value of the 'variable' cannot be changed; it has become a compile-time constant value instead.
You'll have to specify the initial value expression. This value is then used
by the compiler everywhere you refer to the constant (and no memory is allocated
for the constant itself). Onlythe simple numeric types (byte, word, long, float) can be defined as a constant.
If something is defined as a constant, very efficient code can usually be generated from it.
Variables on the other hand can't be optimized as much, need memory, and more code to manipulate them.
Note that a subset of the library routines in the ``math``, ``strings`` and ``floats`` modules are recognised in
compile time expressions. For example, the compiler knows what ``math.sin8u(12)`` is and replaces it with the computed result.
Memory-mapped
^^^^^^^^^^^^^
.. index:: pair: Data Types; Memory-mapped