Changed number from Uint64 to Sint64.
This commit is contained in:
parent
0ceebfbdad
commit
57719279c7
3 changed files with 36 additions and 18 deletions
9
examples/signed_ints.fun
Normal file → Executable file
9
examples/signed_ints.fun
Normal file → Executable file
|
|
@ -1,5 +1,14 @@
|
||||||
#!/usr/bin/env fun
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Fun programming language.
|
||||||
|
* https://hanez.org/project/fun/
|
||||||
|
*
|
||||||
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||||
|
* Licensed under the terms of the ISC license.
|
||||||
|
* https://opensource.org/license/isc-license-txt
|
||||||
|
*/
|
||||||
|
|
||||||
// Signed integer type examples with two's complement wrapping
|
// Signed integer type examples with two's complement wrapping
|
||||||
|
|
||||||
sint8 s8 = -1 // should be -1 within sint8 range
|
sint8 s8 = -1 // should be -1 within sint8 range
|
||||||
|
|
|
||||||
37
examples/typeof_features.fun
Normal file → Executable file
37
examples/typeof_features.fun
Normal file → Executable file
|
|
@ -1,5 +1,14 @@
|
||||||
#!/usr/bin/env fun
|
#!/usr/bin/env fun
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Fun programming language.
|
||||||
|
* https://hanez.org/project/fun/
|
||||||
|
*
|
||||||
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||||
|
* Licensed under the terms of the ISC license.
|
||||||
|
* https://opensource.org/license/isc-license-txt
|
||||||
|
*/
|
||||||
|
|
||||||
// Demonstration of enhanced typeof behavior:
|
// Demonstration of enhanced typeof behavior:
|
||||||
//
|
//
|
||||||
// - typeof(<identifier>) for declared integer types returns the subtype:
|
// - typeof(<identifier>) for declared integer types returns the subtype:
|
||||||
|
|
@ -8,17 +17,17 @@
|
||||||
// Number, String, Array, Map, Function, Nil
|
// Number, String, Array, Map, Function, Nil
|
||||||
|
|
||||||
// Integer subtypes (declared)
|
// Integer subtypes (declared)
|
||||||
sint8 si8 = -1
|
sint8 si8 = -1
|
||||||
sint16 si16 = -32768
|
sint16 si16 = -32768
|
||||||
sint32 si32 = -2147483648
|
sint32 si32 = -2147483648
|
||||||
sint64 si64 = -9223372036854775808
|
sint64 si64 = -9223372036854775808
|
||||||
|
|
||||||
uint8 ui8 = 255
|
uint8 ui8 = 255
|
||||||
uint16 ui16 = 65535
|
uint16 ui16 = 65535
|
||||||
uint32 ui32 = 4294967295
|
uint32 ui32 = 4294967295
|
||||||
uint64 ui64 = 18446744073709551615
|
uint64 ui64 = 18446744073709551615
|
||||||
|
|
||||||
// 'number' maps to Uint64 behavior by design
|
// 'number' maps to Sint64 behavior by design
|
||||||
number n = 42
|
number n = 42
|
||||||
|
|
||||||
// Other runtime types
|
// Other runtime types
|
||||||
|
|
@ -27,22 +36,22 @@ arr = [1, 2, 3]
|
||||||
m = { "k": 1 }
|
m = { "k": 1 }
|
||||||
|
|
||||||
fun f()
|
fun f()
|
||||||
return 0
|
return nil
|
||||||
|
|
||||||
nil x = nil
|
nil x = f()
|
||||||
|
|
||||||
// typeof on identifiers (for integers => declared subtype)
|
// typeof on identifiers (for integers => declared subtype)
|
||||||
print("typeof(si8) = " + typeof(si8)) // Int8
|
print("typeof(si8) = " + typeof(si8)) // Sint8
|
||||||
print("typeof(si16) = " + typeof(si16)) // Int16
|
print("typeof(si16) = " + typeof(si16)) // Sint16
|
||||||
print("typeof(si32) = " + typeof(si32)) // Int32
|
print("typeof(si32) = " + typeof(si32)) // Sint32
|
||||||
print("typeof(si64) = " + typeof(si64)) // Int64
|
print("typeof(si64) = " + typeof(si64)) // Sint64
|
||||||
|
|
||||||
print("typeof(ui8) = " + typeof(ui8)) // Uint8
|
print("typeof(ui8) = " + typeof(ui8)) // Uint8
|
||||||
print("typeof(ui16) = " + typeof(ui16)) // Uint16
|
print("typeof(ui16) = " + typeof(ui16)) // Uint16
|
||||||
print("typeof(ui32) = " + typeof(ui32)) // Uint32
|
print("typeof(ui32) = " + typeof(ui32)) // Uint32
|
||||||
print("typeof(ui64) = " + typeof(ui64)) // Uint64
|
print("typeof(ui64) = " + typeof(ui64)) // Uint64
|
||||||
|
|
||||||
print("typeof(n) = " + typeof(n)) // Uint64
|
print("typeof(n) = " + typeof(n)) // Sint64
|
||||||
|
|
||||||
// typeof on identifiers for non-integers -> runtime categories
|
// typeof on identifiers for non-integers -> runtime categories
|
||||||
print("typeof(s) = " + typeof(s)) // String
|
print("typeof(s) = " + typeof(s)) // String
|
||||||
|
|
@ -60,4 +69,4 @@ ui8 = ui8 + 10
|
||||||
print("ui8 value = " + to_string(ui8) + ", typeof(ui8) = " + typeof(ui8)) // Uint8
|
print("ui8 value = " + to_string(ui8) + ", typeof(ui8) = " + typeof(ui8)) // Uint8
|
||||||
|
|
||||||
si8 = si8 - 5
|
si8 = si8 - 5
|
||||||
print("si8 value = " + to_string(si8) + ", typeof(si8) = " + typeof(si8)) // Int8
|
print("si8 value = " + to_string(si8) + ", typeof(si8) = " + typeof(si8)) // Sint8
|
||||||
|
|
|
||||||
|
|
@ -1522,7 +1522,7 @@ static void parse_simple_statement(Bytecode *bc, const char *src, size_t len, si
|
||||||
|
|
||||||
/* typed declarations:
|
/* typed declarations:
|
||||||
number|string|boolean|nil|uint8|uint16|uint32|uint64|int8|int16|int32|int64 <ident> (= expr)?
|
number|string|boolean|nil|uint8|uint16|uint32|uint64|int8|int16|int32|int64 <ident> (= expr)?
|
||||||
Note: 'number' maps to unsigned 64-bit here.
|
Note: 'number' maps to signed 64-bit here.
|
||||||
*/
|
*/
|
||||||
if (strcmp(name, "number") == 0 || strcmp(name, "string") == 0 || strcmp(name, "boolean") == 0 || strcmp(name, "nil") == 0
|
if (strcmp(name, "number") == 0 || strcmp(name, "string") == 0 || strcmp(name, "boolean") == 0 || strcmp(name, "nil") == 0
|
||||||
|| strcmp(name, "uint8") == 0 || strcmp(name, "uint16") == 0 || strcmp(name, "uint32") == 0 || strcmp(name, "uint64") == 0
|
|| strcmp(name, "uint8") == 0 || strcmp(name, "uint16") == 0 || strcmp(name, "uint32") == 0 || strcmp(name, "uint64") == 0
|
||||||
|
|
@ -1534,15 +1534,15 @@ static void parse_simple_statement(Bytecode *bc, const char *src, size_t len, si
|
||||||
int is_u8 = (strcmp(name, "uint8") == 0);
|
int is_u8 = (strcmp(name, "uint8") == 0);
|
||||||
int is_u16 = (strcmp(name, "uint16") == 0);
|
int is_u16 = (strcmp(name, "uint16") == 0);
|
||||||
int is_u32 = (strcmp(name, "uint32") == 0);
|
int is_u32 = (strcmp(name, "uint32") == 0);
|
||||||
int is_u64 = (strcmp(name, "uint64") == 0) || is_number; /* number maps to uint64 */
|
int is_u64 = (strcmp(name, "uint64") == 0);
|
||||||
int is_s8 = (strcmp(name, "int8") == 0);
|
int is_s8 = (strcmp(name, "int8") == 0);
|
||||||
int is_s16 = (strcmp(name, "int16") == 0);
|
int is_s16 = (strcmp(name, "int16") == 0);
|
||||||
int is_s32 = (strcmp(name, "int32") == 0);
|
int is_s32 = (strcmp(name, "int32") == 0);
|
||||||
int is_s64 = (strcmp(name, "int64") == 0);
|
int is_s64 = (strcmp(name, "int64") == 0) || is_number; /* number maps to int64 (signed) */
|
||||||
int decl_bits = is_u8 ? 8 : is_u16 ? 16 : is_u32 ? 32 : is_u64 ? 64
|
int decl_bits = is_u8 ? 8 : is_u16 ? 16 : is_u32 ? 32 : is_u64 ? 64
|
||||||
: is_s8 ? 8 : is_s16 ? 16 : is_s32 ? 32 : is_s64 ? 64 : 0;
|
: is_s8 ? 8 : is_s16 ? 16 : is_s32 ? 32 : is_s64 ? 64 : 0;
|
||||||
int decl_signed = (is_s8 || is_s16 || is_s32 || is_s64) ? 1 : 0;
|
int decl_signed = (is_s8 || is_s16 || is_s32 || is_s64) ? 1 : 0;
|
||||||
/* store decl bits with sign encoded: negative means signed */
|
/* store decl bits with sign encoded: negative means signed (number is signed 64-bit) */
|
||||||
if (decl_signed) decl_bits = -decl_bits;
|
if (decl_signed) decl_bits = -decl_bits;
|
||||||
free(name);
|
free(name);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue