Initial commit
This commit is contained in:
48
examples/arrays.cm
Normal file
48
examples/arrays.cm
Normal file
@@ -0,0 +1,48 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// arrays.cm - Array operations
|
||||
// Demonstrates: arrays, loops, array initialization
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
int32 sum_array(int32 *arr, int32 len) {
|
||||
int32 total = 0;
|
||||
for (int32 i = 0; i < len; i = i + 1) {
|
||||
total = total + arr[i];
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
void reverse_array(int32 *arr, int32 len) {
|
||||
int32 i = 0;
|
||||
int32 j = len - 1;
|
||||
while (i < j) {
|
||||
int32 temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
i = i + 1;
|
||||
j = j - 1;
|
||||
}
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
int32 numbers[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
|
||||
|
||||
printf("Original array: ");
|
||||
for (int32 i = 0; i < 10; i = i + 1) {
|
||||
printf("%d ", numbers[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
int32 total = sum_array(numbers, 10);
|
||||
printf("Sum: %d\n", total);
|
||||
|
||||
reverse_array(numbers, 10);
|
||||
|
||||
printf("Reversed array: ");
|
||||
for (int32 i = 0; i < 10; i = i + 1) {
|
||||
printf("%d ", numbers[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
60
examples/bitwise.cm
Normal file
60
examples/bitwise.cm
Normal file
@@ -0,0 +1,60 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// bitwise.cm - Bitwise operations
|
||||
// Demonstrates: bitwise operators, bit manipulation, shifts
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
void print_binary(uint32 n) {
|
||||
printf("0b");
|
||||
for (int32 i = 31; i >= 0; i = i - 1) {
|
||||
printf("%d", (n >> i) & 1);
|
||||
if (i % 4 == 0 && i != 0) printf("_");
|
||||
}
|
||||
}
|
||||
|
||||
int32 count_set_bits(uint32 n) {
|
||||
int32 count = 0;
|
||||
while (n) {
|
||||
count = count + (n & 1);
|
||||
n = n >> 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
uint32 reverse_bits(uint32 n) {
|
||||
uint32 result = 0;
|
||||
for (int32 i = 0; i < 32; i = i + 1) {
|
||||
result = result << 1;
|
||||
result = result | (n & 1);
|
||||
n = n >> 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int32 is_power_of_two(uint32 n) {
|
||||
return n && ((n & (n - 1)) == 0);
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
uint32 a = 0xAB;
|
||||
uint32 b = 0x55;
|
||||
|
||||
printf("a = %d = ", a); print_binary(a); printf("\n");
|
||||
printf("b = %d = ", b); print_binary(b); printf("\n");
|
||||
|
||||
printf("\nBitwise AND: %d = ", a & b); print_binary(a & b); printf("\n");
|
||||
printf("Bitwise OR: %d = ", a | b); print_binary(a | b); printf("\n");
|
||||
printf("Bitwise XOR: %d = ", a ^ b); print_binary(a ^ b); printf("\n");
|
||||
printf("Bitwise NOT: %d = ", ~a); print_binary(~a); printf("\n");
|
||||
|
||||
printf("\nLeft shift (a << 2): %d\n", a << 2);
|
||||
printf("Right shift (a >> 2): %d\n", a >> 2);
|
||||
|
||||
printf("\nSet bits in a: %d\n", count_set_bits(a));
|
||||
printf("Set bits in b: %d\n", count_set_bits(b));
|
||||
|
||||
printf("\nIs 64 power of two? %d\n", is_power_of_two(64));
|
||||
printf("Is 63 power of two? %d\n", is_power_of_two(63));
|
||||
|
||||
return 0;
|
||||
}
|
||||
40
examples/bubblesort.cm
Normal file
40
examples/bubblesort.cm
Normal file
@@ -0,0 +1,40 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// bubblesort.cm - Bubble sort implementation
|
||||
// Demonstrates: nested loops, array manipulation, comparisons
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
void bubble_sort(int32 *arr, int32 n) {
|
||||
for (int32 i = 0; i < n - 1; i = i + 1) {
|
||||
for (int32 j = 0; j < n - i - 1; j = j + 1) {
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
// Swap
|
||||
int32 temp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(int32 *arr, int32 n) {
|
||||
for (int32 i = 0; i < n; i = i + 1) {
|
||||
printf("%d ", arr[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
int32 numbers[10] = { 64, 34, 25, 12, 22, 11, 90, 88, 45, 50 };
|
||||
int32 n = 10;
|
||||
|
||||
printf("Unsorted array: ");
|
||||
print_array(numbers, n);
|
||||
|
||||
bubble_sort(numbers, n);
|
||||
|
||||
printf("Sorted array: ");
|
||||
print_array(numbers, n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
101
examples/calculator.cm
Normal file
101
examples/calculator.cm
Normal file
@@ -0,0 +1,101 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// calculator.cm - Simple expression calculator
|
||||
// Demonstrates: global variables, function composition, control flow
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
// Global state for the calculator
|
||||
int32 last_result = 0;
|
||||
int32 operation_count = 0;
|
||||
|
||||
int32 add(int32 a, int32 b) {
|
||||
operation_count = operation_count + 1;
|
||||
last_result = a + b;
|
||||
return last_result;
|
||||
}
|
||||
|
||||
int32 subtract(int32 a, int32 b) {
|
||||
operation_count = operation_count + 1;
|
||||
last_result = a - b;
|
||||
return last_result;
|
||||
}
|
||||
|
||||
int32 multiply(int32 a, int32 b) {
|
||||
operation_count = operation_count + 1;
|
||||
last_result = a * b;
|
||||
return last_result;
|
||||
}
|
||||
|
||||
int32 divide(int32 a, int32 b) {
|
||||
if (b == 0) {
|
||||
printf("Error: Division by zero\n");
|
||||
return 0;
|
||||
}
|
||||
operation_count = operation_count + 1;
|
||||
last_result = a / b;
|
||||
return last_result;
|
||||
}
|
||||
|
||||
int32 power(int32 base, int32 exp) {
|
||||
operation_count = operation_count + 1;
|
||||
int32 result = 1;
|
||||
for (int32 i = 0; i < exp; i = i + 1) {
|
||||
result = result * base;
|
||||
}
|
||||
last_result = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
int32 factorial(int32 n) {
|
||||
operation_count = operation_count + 1;
|
||||
if (n <= 1) {
|
||||
last_result = 1;
|
||||
return 1;
|
||||
}
|
||||
int32 result = 1;
|
||||
for (int32 i = 2; i <= n; i = i + 1) {
|
||||
result = result * i;
|
||||
}
|
||||
last_result = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
void print_stats(void) {
|
||||
printf("\n=== Calculator Statistics ===\n");
|
||||
printf("Total operations: %d\n", operation_count);
|
||||
printf("Last result: %d\n", last_result);
|
||||
printf("============================\n");
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
printf("Calculator Demo\n");
|
||||
printf("===============\n\n");
|
||||
|
||||
// Basic arithmetic
|
||||
printf("10 + 5 = %d\n", add(10, 5));
|
||||
printf("10 - 5 = %d\n", subtract(10, 5));
|
||||
printf("10 * 5 = %d\n", multiply(10, 5));
|
||||
printf("10 / 5 = %d\n", divide(10, 5));
|
||||
|
||||
// Power function
|
||||
printf("\n2^10 = %d\n", power(2, 10));
|
||||
printf("5^3 = %d\n", power(5, 3));
|
||||
|
||||
// Factorial
|
||||
printf("\n5! = %d\n", factorial(5));
|
||||
printf("10! = %d\n", factorial(10));
|
||||
|
||||
// Complex expression: (5 + 3) * (10 - 2)
|
||||
int32 a = add(5, 3);
|
||||
int32 b = subtract(10, 2);
|
||||
int32 result = multiply(a, b);
|
||||
printf("\n(5 + 3) * (10 - 2) = %d\n", result);
|
||||
|
||||
// Division by zero test
|
||||
printf("\nTesting division by zero:\n");
|
||||
divide(10, 0);
|
||||
|
||||
print_stats();
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
examples/fibonacci.cm
Normal file
23
examples/fibonacci.cm
Normal file
@@ -0,0 +1,23 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// fibonacci.cm - Calculate Fibonacci numbers
|
||||
// Demonstrates: recursion, function calls, conditionals
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
int32 fibonacci(int32 n) {
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
}
|
||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
printf("Fibonacci sequence:\n");
|
||||
|
||||
for (int32 i = 0; i < 15; i = i + 1) {
|
||||
int32 fib = fibonacci(i);
|
||||
printf("fib(%d) = %d\n", i, fib);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
11
examples/hello.cm
Normal file
11
examples/hello.cm
Normal file
@@ -0,0 +1,11 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// hello.cm - Hello World program
|
||||
// Compile: ./common hello.cm hello.asm && nasm -f elf32 hello.asm && gcc -m32 hello.o -o hello
|
||||
|
||||
void putchar(int32 c);
|
||||
void puts(uint8 *s);
|
||||
|
||||
int32 main(void) {
|
||||
puts("Hello, World!");
|
||||
return 0;
|
||||
}
|
||||
159
examples/linkedlist.cm
Normal file
159
examples/linkedlist.cm
Normal file
@@ -0,0 +1,159 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// linkedlist.cm - Linked list implementation
|
||||
// Demonstrates: structs simulated with arrays, complex pointer manipulation, memory layout
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
void *malloc(uint32 size);
|
||||
void free(void *ptr);
|
||||
|
||||
// Node structure simulated as:
|
||||
// [0] = data (int32)
|
||||
// [4] = next pointer (int32*)
|
||||
// Total size: 8 bytes per node
|
||||
|
||||
int32 *create_node(int32 value) {
|
||||
int32 *node = (int32*)malloc(8);
|
||||
node[0] = value; // data
|
||||
node[1] = 0; // next = NULL
|
||||
return node;
|
||||
}
|
||||
|
||||
void insert_front(int32 **head, int32 value) {
|
||||
int32 *new_node = create_node(value);
|
||||
new_node[1] = (int32)(*head); // new->next = head
|
||||
*head = new_node; // head = new_node
|
||||
}
|
||||
|
||||
void insert_back(int32 **head, int32 value) {
|
||||
int32 *new_node = create_node(value);
|
||||
|
||||
if (*head == 0) {
|
||||
*head = new_node;
|
||||
return;
|
||||
}
|
||||
|
||||
int32 *current = *head;
|
||||
while (current[1] != 0) {
|
||||
current = (int32*)current[1];
|
||||
}
|
||||
current[1] = (int32)new_node;
|
||||
}
|
||||
|
||||
int32 list_length(int32 *head) {
|
||||
int32 count = 0;
|
||||
int32 *current = head;
|
||||
while (current != 0) {
|
||||
count = count + 1;
|
||||
current = (int32*)current[1];
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void print_list(int32 *head) {
|
||||
int32 *current = head;
|
||||
printf("[");
|
||||
while (current != 0) {
|
||||
printf("%d", current[0]);
|
||||
current = (int32*)current[1];
|
||||
if (current != 0) printf(", ");
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int32 find_value(int32 *head, int32 value) {
|
||||
int32 *current = head;
|
||||
int32 index = 0;
|
||||
while (current != 0) {
|
||||
if (current[0] == value) {
|
||||
return index;
|
||||
}
|
||||
current = (int32*)current[1];
|
||||
index = index + 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void delete_value(int32 **head, int32 value) {
|
||||
if (*head == 0) return;
|
||||
|
||||
// Check if head node contains the value
|
||||
if ((*head)[0] == value) {
|
||||
int32 *temp = *head;
|
||||
*head = (int32*)(*head)[1];
|
||||
free(temp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Search for the value in remaining nodes
|
||||
int32 *current = *head;
|
||||
while (current[1] != 0) {
|
||||
int32 *next = (int32*)current[1];
|
||||
if (next[0] == value) {
|
||||
current[1] = next[1]; // current->next = next->next
|
||||
free(next);
|
||||
return;
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
void free_list(int32 *head) {
|
||||
int32 *current = head;
|
||||
while (current != 0) {
|
||||
int32 *next = (int32*)current[1];
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
int32 *list = 0; // Empty list (NULL)
|
||||
|
||||
printf("Linked List Demo\n");
|
||||
printf("================\n\n");
|
||||
|
||||
printf("Inserting at front: 3, 2, 1\n");
|
||||
insert_front(&list, 3);
|
||||
insert_front(&list, 2);
|
||||
insert_front(&list, 1);
|
||||
print_list(list);
|
||||
|
||||
printf("\nInserting at back: 4, 5, 6\n");
|
||||
insert_back(&list, 4);
|
||||
insert_back(&list, 5);
|
||||
insert_back(&list, 6);
|
||||
print_list(list);
|
||||
|
||||
printf("\nList length: %d\n", list_length(list));
|
||||
|
||||
printf("\nSearching for values:\n");
|
||||
int32 search_vals[4] = { 1, 4, 7, 5 };
|
||||
for (int32 i = 0; i < 4; i = i + 1) {
|
||||
int32 val = search_vals[i];
|
||||
int32 pos = find_value(list, val);
|
||||
if (pos >= 0) {
|
||||
printf(" %d found at position %d\n", val, pos);
|
||||
} else {
|
||||
printf(" %d not found\n", val);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\nDeleting value 3\n");
|
||||
delete_value(&list, 3);
|
||||
print_list(list);
|
||||
|
||||
printf("\nDeleting value 1 (head)\n");
|
||||
delete_value(&list, 1);
|
||||
print_list(list);
|
||||
|
||||
printf("\nDeleting value 6 (tail)\n");
|
||||
delete_value(&list, 6);
|
||||
print_list(list);
|
||||
|
||||
printf("\nFinal list length: %d\n", list_length(list));
|
||||
|
||||
free_list(list);
|
||||
printf("\nList freed\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
38
examples/pointers.cm
Normal file
38
examples/pointers.cm
Normal file
@@ -0,0 +1,38 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// pointers.cm - Pointer operations
|
||||
// Demonstrates: pointers, pointer arithmetic, dereferencing
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
void swap(int32 *a, int32 *b) {
|
||||
int32 temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
int32 x = 42;
|
||||
int32 y = 17;
|
||||
|
||||
printf("Before swap: x=%d, y=%d\n", x, y);
|
||||
swap(&x, &y);
|
||||
printf("After swap: x=%d, y=%d\n", x, y);
|
||||
|
||||
// Pointer arithmetic with arrays
|
||||
int32 arr[5] = { 10, 20, 30, 40, 50 };
|
||||
int32 *ptr = arr;
|
||||
|
||||
printf("\nArray traversal with pointer arithmetic:\n");
|
||||
for (int32 i = 0; i < 5; i = i + 1) {
|
||||
printf("arr[%d] = %d (via pointer: %d)\n", i, arr[i], *(ptr + i));
|
||||
}
|
||||
|
||||
// Pointer to pointer
|
||||
int32 value = 99;
|
||||
int32 *p1 = &value;
|
||||
int32 **p2 = &p1;
|
||||
|
||||
printf("\nPointer to pointer: **p2 = %d\n", **p2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
68
examples/primes.cm
Normal file
68
examples/primes.cm
Normal file
@@ -0,0 +1,68 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// primes.cm - Prime number calculator
|
||||
// Demonstrates: algorithms, loops, mathematical operations
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
int32 is_prime(int32 n) {
|
||||
if (n <= 1) return 0;
|
||||
if (n <= 3) return 1;
|
||||
if (n % 2 == 0 || n % 3 == 0) return 0;
|
||||
|
||||
int32 i = 5;
|
||||
while (i * i <= n) {
|
||||
if (n % i == 0 || n % (i + 2) == 0) {
|
||||
return 0;
|
||||
}
|
||||
i = i + 6;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32 nth_prime(int32 n) {
|
||||
if (n == 1) return 2;
|
||||
|
||||
int32 count = 1;
|
||||
int32 candidate = 3;
|
||||
|
||||
while (count < n) {
|
||||
if (is_prime(candidate)) {
|
||||
count = count + 1;
|
||||
}
|
||||
candidate = candidate + 2;
|
||||
}
|
||||
|
||||
return candidate - 2;
|
||||
}
|
||||
|
||||
int32 count_primes_under(int32 limit) {
|
||||
int32 count = 0;
|
||||
for (int32 i = 2; i < limit; i = i + 1) {
|
||||
if (is_prime(i)) {
|
||||
count = count + 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
printf("First 20 prime numbers:\n");
|
||||
for (int32 i = 1; i <= 20; i = i + 1) {
|
||||
printf("%d ", nth_prime(i));
|
||||
if (i % 10 == 0) printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
int32 limit = 100;
|
||||
int32 count = count_primes_under(limit);
|
||||
printf("\nThere are %d primes less than %d\n", count, limit);
|
||||
|
||||
printf("\nPrime check:\n");
|
||||
int32 test_nums[8] = { 1, 2, 15, 17, 97, 100, 101, 121 };
|
||||
for (int32 i = 0; i < 8; i = i + 1) {
|
||||
int32 num = test_nums[i];
|
||||
printf(" %d is %s\n", num, is_prime(num) ? "prime" : "not prime");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
70
examples/strings.cm
Normal file
70
examples/strings.cm
Normal file
@@ -0,0 +1,70 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// strings.cm - String manipulation
|
||||
// Demonstrates: string literals, character arrays, string functions
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
int32 str_len(uint8 *s) {
|
||||
int32 len = 0;
|
||||
while (s[len]) {
|
||||
len = len + 1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
void str_copy(uint8 *dest, uint8 *src) {
|
||||
int32 i = 0;
|
||||
while (src[i]) {
|
||||
dest[i] = src[i];
|
||||
i = i + 1;
|
||||
}
|
||||
dest[i] = 0;
|
||||
}
|
||||
|
||||
int32 str_cmp(uint8 *s1, uint8 *s2) {
|
||||
int32 i = 0;
|
||||
while (s1[i] && s2[i]) {
|
||||
if (s1[i] != s2[i]) {
|
||||
return s1[i] - s2[i];
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
return s1[i] - s2[i];
|
||||
}
|
||||
|
||||
void str_reverse(uint8 *s) {
|
||||
int32 len = str_len(s);
|
||||
int32 i = 0;
|
||||
int32 j = len - 1;
|
||||
while (i < j) {
|
||||
uint8 temp = s[i];
|
||||
s[i] = s[j];
|
||||
s[j] = temp;
|
||||
i = i + 1;
|
||||
j = j - 1;
|
||||
}
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
uint8 *msg = "Hello, World!";
|
||||
printf("Original string: %s\n", msg);
|
||||
printf("Length: %d\n", str_len(msg));
|
||||
|
||||
uint8 buffer[100];
|
||||
str_copy(buffer, msg);
|
||||
printf("Copied string: %s\n", buffer);
|
||||
|
||||
str_reverse(buffer);
|
||||
printf("Reversed: %s\n", buffer);
|
||||
|
||||
uint8 *s1 = "apple";
|
||||
uint8 *s2 = "banana";
|
||||
uint8 *s3 = "apple";
|
||||
|
||||
printf("\nString comparison:\n");
|
||||
printf(" '%s' vs '%s': %d\n", s1, s2, str_cmp(s1, s2));
|
||||
printf(" '%s' vs '%s': %d\n", s1, s3, str_cmp(s1, s3));
|
||||
printf(" '%s' vs '%s': %d\n", s2, s1, str_cmp(s2, s1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
65
examples/switch.cm
Normal file
65
examples/switch.cm
Normal file
@@ -0,0 +1,65 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// switch.cm - Switch statement demonstration
|
||||
// Demonstrates: switch/case, default, break, fall-through
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
uint8 *get_day_name(int32 day) {
|
||||
switch (day) {
|
||||
case 0: return "Sunday";
|
||||
case 1: return "Monday";
|
||||
case 2: return "Tuesday";
|
||||
case 3: return "Wednesday";
|
||||
case 4: return "Thursday";
|
||||
case 5: return "Friday";
|
||||
case 6: return "Saturday";
|
||||
default: return "Invalid day";
|
||||
}
|
||||
}
|
||||
|
||||
int32 is_weekend(int32 day) {
|
||||
switch (day) {
|
||||
case 0:
|
||||
case 6:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint8 *get_grade(int32 score) {
|
||||
int32 category = score / 10;
|
||||
|
||||
switch (category) {
|
||||
case 10:
|
||||
case 9:
|
||||
return "A";
|
||||
case 8:
|
||||
return "B";
|
||||
case 7:
|
||||
return "C";
|
||||
case 6:
|
||||
return "D";
|
||||
default:
|
||||
return "F";
|
||||
}
|
||||
}
|
||||
|
||||
int32 main(void) {
|
||||
printf("Days of the week:\n");
|
||||
for (int32 i = 0; i < 8; i = i + 1) {
|
||||
printf(" Day %d: %s", i, get_day_name(i));
|
||||
if (is_weekend(i)) {
|
||||
printf(" (weekend!)");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\nGrade calculator:\n");
|
||||
int32 scores[6] = { 95, 87, 76, 65, 54, 100 };
|
||||
for (int32 i = 0; i < 6; i = i + 1) {
|
||||
printf(" Score %d: Grade %s\n", scores[i], get_grade(scores[i]));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
50
examples/types.cm
Normal file
50
examples/types.cm
Normal file
@@ -0,0 +1,50 @@
|
||||
// Public domain / CC0. Use freely for any purpose. RoyR 2026
|
||||
// types.cm - Type casting and different integer sizes
|
||||
// Demonstrates: different integer types, casting, overflow behavior
|
||||
|
||||
void printf(uint8 *fmt);
|
||||
|
||||
int32 main(void) {
|
||||
// Different integer sizes
|
||||
uint8 byte_val = 255;
|
||||
uint16 word_val = 65535;
|
||||
uint32 dword_val = 4294967295;
|
||||
|
||||
int8 sbyte_val = -128;
|
||||
int16 sword_val = -32768;
|
||||
int32 sdword_val = -2147483648;
|
||||
|
||||
printf("Unsigned types:\n");
|
||||
printf(" uint8: %d\n", byte_val);
|
||||
printf(" uint16: %d\n", word_val);
|
||||
printf(" uint32: %u\n", dword_val);
|
||||
|
||||
printf("\nSigned types:\n");
|
||||
printf(" int8: %d\n", sbyte_val);
|
||||
printf(" int16: %d\n", sword_val);
|
||||
printf(" int32: %d\n", sdword_val);
|
||||
|
||||
// Type casting
|
||||
printf("\nType casting:\n");
|
||||
int32 large = 1000;
|
||||
uint8 small = (uint8)large;
|
||||
printf(" (uint8)1000 = %d (truncated to 8 bits)\n", small);
|
||||
|
||||
int32 value = 298;
|
||||
uint8 truncated = (uint8)value;
|
||||
printf(" (uint8)298 = %d (298 %% 256 = 42)\n", truncated);
|
||||
|
||||
// Sign extension
|
||||
int8 neg = -1;
|
||||
int32 extended = neg;
|
||||
printf("\nSign extension:\n");
|
||||
printf(" int8(-1) extended to int32: %d\n", extended);
|
||||
|
||||
// Unsigned to signed
|
||||
uint8 unsigned_val = 200;
|
||||
int8 signed_val = (int8)unsigned_val;
|
||||
printf("\nUnsigned to signed:\n");
|
||||
printf(" (int8)200 = %d (interpreted as signed)\n", signed_val);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user