Files
c3c/resources/testfragments/demo1.c3
Christoffer Lerno fc849c1440 0.6.0: init_new/init_temp removed. LinkedList API rewritten. List "pop" and "remove" function now return Optionals. RingBuffer API rewritten. Allocator interface changed. Deprecated Allocator, DString and mem functions removed. "identity" functions are now constants for Matrix and Complex numbers. @default implementations for interfaces removed. any* => any, same for interfaces. Emit local/private globals as "private" in LLVM, following C "static". Updated enum syntax. Add support [rgba] properties in vectors. Improved checks of aliased "void". Subarray -> slice. Fix of llvm codegen enum check. Improved alignment handling. Add --output-dir #1155. Removed List/Object append. GenericList renamed AnyList. Remove unused "unwrap". Fixes to cond. Optimize output in dead branches. Better checking of operator methods. Disallow any from implementing dynamic methods. Check for operator mismatch. Remove unnecessary bitfield. Remove numbering in --list* commands Old style enum declaration for params/type, but now the type is optional. Add note on #1086. Allow making distinct types out of "void", "typeid", "anyfault" and faults. Remove system linker build options. "Try" expressions must be simple expressions. Add optimized build to Mac tests. Register int. assert(false) only allowed in unused branches or in tests. Compile time failed asserts is a compile time error. Remove current_block_is_target. Bug when assigning an optional from an optional. Remove unused emit_zstring. Simplify phi code. Remove unnecessary unreachable blocks and remove unnecessary current_block NULL assignments. Proper handling of '.' and Win32 '//server' paths. Unify expression and macro blocks in the middle end. Add "no discard" to expression blocks with a return value. Detect "unsigned >= 0" as errors. Fix issue with distinct void as a member #1147. Improve callstack debug information #1184. Fix issue with absolute output-dir paths. Lambdas were not type checked thoroughly #1185. Fix compilation warning #1187. Request jump table using @jump for switches. Path normalization - fix possible null terminator out of bounds. Improved error messages on inlined macros.
2024-05-22 18:22:04 +02:00

152 lines
2.9 KiB
C

module demo1;
// Use C functions directly.
extern fn int printf(char *, ...);
extern fn void puts(char *);
struct Foo
{
int a;
int b;
}
fn Foo createFoo(int a, int b = 10)
{
// Compound initializer
return Foo {a, b};
}
struct Bar
{
int x;
int y;
}
fn void testStruct()
{
// Default arguments
Foo foo = createFoo(100);
printf("Foo a:%d b:%d\n", foo.a, foo.b);
// No need to write foo = (Foo){ 11, 22 }
// unlike C
foo = { 11, 12 };
// Named arguments
foo = createFoo(.b = 13, .a = 14);
printf("Foo a:%d b:%d\n", foo.a, foo.b);
Bar bar = bitcast(foo, Bar);
printf("Bar x:%d y:%d\n", bar.x, bar.y);
// Type functions
printf("Mult: %d\n", foo.mult());
printf("Add: %d\n", bar.add());
}
fn int Bar.add(Bar* b)
{
return b.x + b.y;
}
fn int Foo.mult(Foo* f)
{
return f.a * f.b;
}
fn void printArray(int[] array)
{
printf("[");
foreach (i, a : array)
{
if (i != 0) printf(", ");
printf("%d", a);
}
printf("]\n");
}
fn void testArrays()
{
int[5] x = { [0] = 100, [1..2] = 4 };
puts("Testing arrays---");
foreach (a : x)
{
printf("%d\n", a);
}
puts("Arrays with index---");
foreach (i, a : x)
{
printf("index[%d]: %d\n", i, a);
}
puts("Mutating arrays---");
foreach (i, int* &a : x) { *a += (int)(i); }
foreach (i, a : x)
{
printf("index[%d]: %d\n", i, a);
}
puts("Without & there is no change---");
foreach (i, a : x) { a += (int)(i); }
foreach (i, a : x)
{
printf("index[%d]: %d\n", i, a);
}
printf("Length is a compile time value: %d\n", x.len);
puts("Getting a slice---");
int[] y = x[1..3];
foreach (i, a : y)
{
printf("index[%d]: %d\n", i, a);
}
printf("Length is a runtime value: %d\n", y.len);
puts("Getting a slice from the beginning to an offset of the end---");
y = x[..^2]; // Same as x[0..^2]
foreach (i, a : y)
{
printf("index[%d]: %d\n", i, a);
}
puts("Printing arrays---");
printArray(&x);
printArray(y);
printArray(x[1..]);
puts("Array to pointer to slice---");
int* z = &x;
printArray(z[0..2]);
printf("Pointer to array: %p\nPointer to slice: %p\nPointer to first element of slice: %p\n", z, &y, &y[0]);
}
fn void testExpressionBlocks()
{
int x = {|
int j = 0;
for (int i = 0; i < 10; i++)
{
j += i + j;
if (j > 10) return j;
}
return 0;
|};
printf("x: %d\n", x);
{|
for (int i = 0; i < 10; i++)
{
printf("%d\n", i);
if (i && i % 4 == 0) return;
}
puts("Reached end");
|};
if ({| for (int i = 0; i < 10; i++) printf("--%d\n", i); puts("Done!"); return true; |})
{
puts("In if");
}
}
fn void main()
{
testStruct();
testArrays();
testExpressionBlocks();
}