add check for memory() args to be const, added floats.str_f()

add check for memory() args to be const
This commit is contained in:
Irmen de Jong 2023-12-07 21:25:03 +01:00
parent 5c393091a0
commit a01c0a283d
4 changed files with 41 additions and 20 deletions

View File

@ -2,26 +2,37 @@ floats {
; the floating point functions shared across compiler targets ; the floating point functions shared across compiler targets
%option merge, no_symbol_prefixing %option merge, no_symbol_prefixing
sub print_f(float value) { asmsub print_f(float value @FAC1) clobbers(A,X,Y) {
; ---- prints the floating point value (without a newline). No leading space (unlike BASIC)! ; ---- prints the floating point value (without a newline). No leading space (unlike BASIC)!
%asm {{ %asm {{
lda #<value jsr str_f
ldy #>value ldy #0
jsr MOVFM ; load float into fac1 - lda (P8ZP_SCRATCH_W1),y
jsr FOUT ; fac1 to string in A/Y beq +
jsr cbm.CHROUT
iny
bne -
+ rts
}}
}
asmsub str_f(float value @FAC1) clobbers(X) -> str @AY {
; ---- converts the floating point value to a string. No leading space!
%asm {{
jsr FOUT
sta P8ZP_SCRATCH_W1 sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1 sty P8ZP_SCRATCH_W1+1
ldy #0 ldy #0
lda (P8ZP_SCRATCH_W1),y lda (P8ZP_SCRATCH_W1),y
cmp #' ' cmp #' '
beq + bne +
- lda (P8ZP_SCRATCH_W1),y inc P8ZP_SCRATCH_W1
beq ++ bne +
jsr cbm.CHROUT inc P8ZP_SCRATCH_W1+1
+ iny + lda P8ZP_SCRATCH_W1
bne - ldy P8ZP_SCRATCH_W1+1
+ rts rts
}} }}
} }
sub pow(float value, float power) -> float { sub pow(float value, float power) -> float {

View File

@ -47,9 +47,14 @@ internal class VerifyFunctionArgTypes(val program: Program, val errors: IErrorRe
else { else {
if(functionCallExpr.target.nameInSource==listOf("memory")) { if(functionCallExpr.target.nameInSource==listOf("memory")) {
val name = (functionCallExpr.args[0] as StringLiteral).value val name = (functionCallExpr.args[0] as StringLiteral).value
val size = (functionCallExpr.args[1] as NumericLiteral).number.toInt() val size = (functionCallExpr.args[1] as? NumericLiteral)?.number?.toInt()
val align = (functionCallExpr.args[2] as NumericLiteral).number.toInt() val align = (functionCallExpr.args[2] as? NumericLiteral)?.number?.toInt()
memorySlabs.add(Slab(name, size, align, functionCallExpr.position)) if(size==null)
errors.err("argument must be a constant", functionCallExpr.args[1].position)
if(align==null)
errors.err("argument must be a constant", functionCallExpr.args[2].position)
if(size!=null && align!=null)
memorySlabs.add(Slab(name, size, align, functionCallExpr.position))
} }
} }

View File

@ -338,7 +338,12 @@ point variables. This includes ``print_f``, the routine used to print floating
returns the value restricted to the given minimum and maximum. returns the value restricted to the given minimum and maximum.
``print_f (x)`` ``print_f (x)``
prints the floating point number x as a string. Prints the floating point number x as a string.
There's no leading whitespace (unlike cbm BASIC when printing a floating point number)
``str_f (x)``
Converts the floating point number x to a string (returns address of the string buffer)
There's no leading whitespace.
``rad (x)`` ``rad (x)``
Degrees to radians. Degrees to radians.

View File

@ -12,12 +12,12 @@
<option name="HAS_STRING_ESCAPES" value="true" /> <option name="HAS_STRING_ESCAPES" value="true" />
</options> </options>
<keywords keywords="&amp;;-&gt;;@;and;as;asmsub;break;clobbers;continue;do;downto;else;false;for;goto;if;if_cc;if_cs;if_eq;if_mi;if_ne;if_neg;if_nz;if_pl;if_pos;if_vc;if_vs;if_z;in;inline;not;or;repeat;return;romsub;step;sub;to;true;unroll;until;when;while;xor;~" ignore_case="false" /> <keywords keywords="&amp;;-&gt;;@;and;as;asmsub;break;clobbers;continue;do;downto;else;false;for;goto;if;if_cc;if_cs;if_eq;if_mi;if_ne;if_neg;if_nz;if_pl;if_pos;if_vc;if_vs;if_z;in;inline;not;or;repeat;return;romsub;step;sub;to;true;unroll;until;when;while;xor;~" ignore_case="false" />
<keywords2 keywords="%address;%asm;%asmbinary;%asminclude;%breakpoint;%encoding;%import;%ir;%launcher;%option;%output;%zeropage;%zpallowed;%zpreserved;iso:;petscii:;sc:" /> <keywords2 keywords="%address;%asm;%asmbinary;%asminclude;%breakpoint;%encoding;%import;%ir;%launcher;%option;%output;%zeropage;%zpallowed;%zpreserved;atascii:;default:;iso:;petscii:;sc:" />
<keywords3 keywords="@requirezp;@shared;@split;@zp;bool;byte;const;float;str;ubyte;uword;void;word" /> <keywords3 keywords="@requirezp;@shared;@split;@zp;bool;byte;const;float;str;ubyte;uword;void;word" />
<keywords4 keywords="abs;all;any;callfar;callram;callrom;clamp;cmp;divmod;len;lsb;max;memory;min;mkword;msb;peek;peekf;peekw;poke;pokef;pokew;pop;popw;push;pushw;reverse;rol;rol2;ror;ror2;rrestore;rrestorex;rsave;rsavex;setlsb;setmsb;sgn;sizeof;sort;sqrt;swap;|&gt;" /> <keywords4 keywords="abs;all;any;callfar;callram;callrom;clamp;cmp;divmod;len;lsb;max;memory;min;mkword;msb;peek;peekf;peekw;poke;pokef;pokew;pop;popw;push;pushw;reverse;rol;rol2;ror;ror2;rrestore;rrestorex;rsave;rsavex;setlsb;setmsb;sgn;sizeof;sort;sqrt;swap;|&gt;" />
</highlighting> </highlighting>
<extensionMap> <extensionMap>
<mapping ext="p8" />
<mapping ext="prog8" /> <mapping ext="prog8" />
<mapping ext="p8" />
</extensionMap> </extensionMap>
</filetype> </filetype>