Skip to content
460 changes: 460 additions & 0 deletions chpl-src/announcing-chapel-2.7.chpl

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions content/posts/announcing-chapel-2.7/code/Print.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use Types;
use CTypes;

extern {
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdbool.h>

void printMyChar(int8_t x);
void printMyInt64(int64_t x);
void printMyUint64(uint64_t x);
void printMyReal64(double x);
void printMyBool(bool x);
void printStr(const char* str);

void printMyInt64(int64_t x) {
printf("%" PRId64, x);
}

void printMyUint64(uint64_t x) {
printf("%" PRIu64, x);
}

void printMyReal64(double x) {
printf("%lf", x);
}

void printMyChar(int8_t x) {
printf("%c", x);
}

void printMyBool(bool x) {
if (x) {
printf("true");
} else {
printf("false");
}
}

void printStr(const char* str) {
printf("%s", str);
}
}

extern proc printMyChar(x: real(64));
extern proc printMyInt64(x: int(64));
extern proc printMyUint64(x: uint(64));
extern proc printMyReal64(x: real(64));
extern proc printMyBool(x: bool);
extern proc printStr(str: c_ptrConst(c_char));

proc doPrintSpace() do printMyChar(32);
proc doPrintNewline() do printMyChar(10);

proc print(x: ?t) {
if t == bool {
printMyBool(x);
} else if isIntType(t) {
printMyInt64(x);
} else if isUintType(t) {
printMyUint64(x);
} else if isRealType(t) {
printMyReal64(x);
} else if t == string {
printStr(x.c_str());
} else if t == c_ptrConst(c_char) {
printStr(x);
} else if isTupleType(t) {
// TODO: Param loop this...
// TODO: Move this to 'print'...
if x.size >= 1 then print(x[0]);
if x.size >= 2 { doPrintSpace(); print(x[1]); }
if x.size >= 3 { doPrintSpace(); print(x[2]); }
if x.size >= 4 { doPrintSpace(); print(x[3]); }
if x.size >= 5 { doPrintSpace(); print(x[4]); }
if x.size >= 6 { doPrintSpace(); print(x[5]); }
if x.size >= 7 { doPrintSpace(); print(x[6]); }
if x.size >= 8 { doPrintSpace(); print(x[7]); }
if x.size >= 9 { doPrintSpace(); print(x[8]); }
} else if isEnumType(t) {
var temp : int = chpl__enumToOrder(x);
printMyInt64(temp);
} else {
// TODO: 'halt()'
}
}

proc println(x) {
print(x);
doPrintNewline();
}
14 changes: 14 additions & 0 deletions content/posts/announcing-chapel-2.7/code/array-formals.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
proc foo(A: [?D] ?t) param {
param isRect =
if A.isDefaultRectangular() then "rectangular" else "not rectangular";
param eltType = A.eltType:string;
param dim = D.rank:string;
return isRect + " " + dim + "-dimensional array of " + eltType;
}

use BlockDist;
var A: [1..10] int;
var B = blockDist.createArray(1..10, 1..10, real);

param infoA = foo(A);
param infoB = foo(B);
5 changes: 5 additions & 0 deletions content/posts/announcing-chapel-2.7/code/casts.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum color { red = 1, green, blue }

var myTup = (42, "hello");
var castTup = myTup : (real, int);
param redBytes = color.red : bytes;
32 changes: 32 additions & 0 deletions content/posts/announcing-chapel-2.7/code/converter.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use IO;
use Print; // helper module while we work toward resolving more of IO

// utilize generic varargs and param for-loops to mimic 'writeln' behavior
proc myWriteln(const args...?n) {
for param i in 0..<n {
print(args(i));
}
println("");
}

// return a tuple from a procedure
proc double(arg: int) {
return (arg, arg*2);
}

proc main() {
// uses generic varargs and param for-loop to mimic 'writeln' behavior
myWriteln(1, " != ", 2.0);

// grouped variable initialization, unpacking the result of 'double'
var (a, b) = double(5);
myWriteln("a = ", a, ", b = ", b);

// Open 'stdout' and use the IO module to write a string!
//
// Utilizes shared objects, ranges, generic types, enums, interoperability,
// and a great deal of string manipulating-module code.
var s = new file(chpl_cstdout());
var w = s.writer();
w.writeLiteral("Hello, World!\n");
}
12 changes: 12 additions & 0 deletions content/posts/announcing-chapel-2.7/code/debug.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use Debugger;

proc main() {
on Locales[1] {
var myArr = [i in 1..10] i;
breakpoint;
on Locales[0] {
writeln(myArr);
breakpoint;
}
}
}
17 changes: 17 additions & 0 deletions content/posts/announcing-chapel-2.7/code/outOfBounds.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
proc main() {
var arr = [i in -9..9] i,
s1 = sliceToString(arr, -5..#6),
s2 = sliceToString(arr, 0.. by 2 # 6),
s3 = sliceToString(arr, 5..#5 by -1);
writeln("Slice 1: ", s1);
writeln("Slice 2: ", s2);
writeln("Slice 3: ", s3);
}

proc sliceToString(arr, slice) {
var s: string;
for i in slice {
s += arr[i]:string + " ";
}
return s.strip();
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.