Skip to content

Commit

Permalink
Restructure type modules
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Feb 16, 2025
1 parent a94b5dc commit a97c7a0
Show file tree
Hide file tree
Showing 12 changed files with 674 additions and 350 deletions.
36 changes: 29 additions & 7 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
type T int|dyn|double;
import "std/type/type-conversion";

f<int> main() {

f<int> foo<T>(T t) {
return 1;
}

/*import "std/text/format";

f<int> main() {
foo(1);
foo(1.0);
foo("test");
}
printf("toUpper: %c\n", toUpper('j'));
printf("toUpper: %s\n", toUpper(String("This is a test.")));
printf("toLower: %c\n", toLower('L'));
printf("toLower: %s\n", toLower(String("This is a test.")));
printf("capizalize: %s\n", capitalize(String("word")));

printf("formatThousandsDelimiter: %s\n", formatThousandsDelimiter(12345678));
printf("formatThousandsDelimiter: %s\n", formatThousandsDelimiter(12345678, '.'));

printf("formatStorageSize: %s\n", formatStorageSize(1l));
printf("formatStorageSize: %s\n", formatStorageSize(1l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234l));
printf("formatStorageSize: %s\n", formatStorageSize(1234l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456l, true));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456789l));
printf("formatStorageSize: %s\n", formatStorageSize(1234567890123456789l, true));
}*/
33 changes: 0 additions & 33 deletions std/type/bool.spice

This file was deleted.

43 changes: 0 additions & 43 deletions std/type/byte.spice

This file was deleted.

39 changes: 0 additions & 39 deletions std/type/char.spice

This file was deleted.

16 changes: 0 additions & 16 deletions std/type/common.spice

This file was deleted.

32 changes: 0 additions & 32 deletions std/type/double.spice

This file was deleted.

53 changes: 6 additions & 47 deletions std/type/int.spice
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
public const int INT_SIZE = 32;
public const int INT_MIN_VALUE = -2147483648;
public const int INT_MAX_VALUE = 2147483647;
public const unsigned int UINT_MIN_VALUE = 0;
public const unsigned int UINT_MAX_VALUE = 4294967295u;

// External functions
ext f<unsigned int> snprintf(char*, unsigned long, string, int);

// Converts an int to a double
public f<double> toDouble(int input) {
return 0.0 + input;
}

// Converts an int to a short
public f<short> toShort(int input) {
return cast<short>(input);
}

// Converts an int to a long
public f<long> toLong(int input) {
return cast<long>(input);
}

// Converts an int to a byte
public f<byte> toByte(int input) {
return cast<byte>(input);
}

// Converts an int to a char
public f<char> toChar(int input) {
return cast<char>(input);
}

// Converts an int to a string
public f<String> toString(const int input) {
const unsigned int length = snprintf(nil<char*>, 0l, "%d", input);
result = String(length);
snprintf(cast<char*>(result.getRaw()), length + 1l, "%d", input);
}

// Converts an int to a boolean
public f<bool> toBool(int input) {
return input >= 1;
}

// Check if the input is a power of two
/**
* Check if an int is a power of two
*
* @param input Input number
* @return Is power of two
*/
public f<bool> isPowerOfTwo(int input) {
return (input & (input - 1)) == 0;
}
53 changes: 6 additions & 47 deletions std/type/long.spice
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
public const unsigned int SIZE = 64;
public const long MIN_VALUE = -9223372036854775808l;
public const long MAX_VALUE = 9223372036854775807l;
public const unsigned long ULONG_MIN_VALUE = 0ul;
public const unsigned long ULONG_MAX_VALUE = 18446744073709551615ul;

// External functions
ext f<unsigned int> snprintf(char*, unsigned long, string, long);

// Converts a long to a double
public f<double> toDouble(long input) {
return 0.0 + input;
}

// Converts a long to an int
public f<int> toInt(long input) {
return cast<int>(input);
}

// Converts a long to a short
public f<short> toShort(long input) {
return cast<short>(input);
}

// Converts a long to a byte
public f<byte> toByte(long input) {
return cast<byte>(cast<int>(input));
}

// Converts a long to a char
public f<char> toChar(long input) {
return cast<char>(input);
}

// Converts a long to a string
public f<String> toString(long input) {
const unsigned int length = snprintf(nil<char*>, 0l, "%ld", input);
result = String(length);
snprintf(cast<char*>(result.getRaw()), length + 1l, "%ld", input);
}

// Converts a long to a boolean
public f<bool> toBool(long input) {
return input >= 1;
}

// Check if the input is a power of two
/**
* Check if a long is a power of two
*
* @param input Input number
* @return Is power of two
*/
public f<bool> isPowerOfTwo(long input) {
return (input & (input - 1l)) == 0l;
}
53 changes: 6 additions & 47 deletions std/type/short.spice
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
public const unsigned int SHORT_SIZE = 16;
public const short SHORT_MIN_VALUE = -32768s;
public const short SHORT_MAX_VALUE = 32767s;
public const unsigned short USHORT_MIN_VALUE = 0s;
public const unsigned short USHORT_MAX_VALUE = 65535us;

// External functions
ext f<unsigned int> snprintf(char*, unsigned long, string, short);

// Converts a short to a double
public f<double> toDouble(short input) {
return 0.0 + input;
}

// Converts a short to an int
public f<int> toInt(short input) {
return cast<int>(input);
}

// Converts a short to a long
public f<long> toLong(short input) {
return cast<long>(input);
}

// Converts a short to a byte
public f<byte> toByte(short input) {
return cast<byte>(cast<int>(input));
}

// Converts a short to a char
public f<char> toChar(short input) {
return cast<char>(input);
}

// Converts a short to a string
public f<String> toString(short input) {
const unsigned int length = snprintf(nil<char*>, 0l, "%hd", input);
result = String(length);
snprintf(cast<char*>(result.getRaw()), length + 1l, "%hd", input);
}

// Converts a short to a boolean
public f<bool> toBool(short input) {
return input >= 1;
}

// Check if the input is a power of two
/**
* Check if a short is a power of two
*
* @param input Input number
* @return Is power of two
*/
public f<bool> isPowerOfTwo(short input) {
return (input & (input - 1s)) == 0s;
}
Loading

0 comments on commit a97c7a0

Please sign in to comment.