commit
110c8bb56c
|
@ -0,0 +1,2 @@
|
||||||
|
language: d
|
||||||
|
script: make test
|
42
src/dfmt.d
42
src/dfmt.d
|
@ -159,7 +159,7 @@ private:
|
||||||
const i = index;
|
const i = index;
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
if (tokens[i-1].line < tokens[i].line)
|
if (tokens[i-1].line < current.line)
|
||||||
{
|
{
|
||||||
if (tokens[i-1].type != tok!"comment"
|
if (tokens[i-1].type != tok!"comment"
|
||||||
&& tokens[i-1].type != tok!"{")
|
&& tokens[i-1].type != tok!"{")
|
||||||
|
@ -171,6 +171,11 @@ private:
|
||||||
writeToken();
|
writeToken();
|
||||||
if (i >= tokens.length-1)
|
if (i >= tokens.length-1)
|
||||||
newline();
|
newline();
|
||||||
|
else if (tokens[i+1].line-1 > tokens[i].line)
|
||||||
|
{
|
||||||
|
newline();
|
||||||
|
newline();
|
||||||
|
}
|
||||||
else if (tokens[i+1].line > tokens[i].line)
|
else if (tokens[i+1].line > tokens[i].line)
|
||||||
newline();
|
newline();
|
||||||
else if (tokens[i+1].type != tok!"{")
|
else if (tokens[i+1].type != tok!"{")
|
||||||
|
@ -192,6 +197,8 @@ private:
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
tempIndent = 0;
|
tempIndent = 0;
|
||||||
|
if (current.type == tok!"comment")
|
||||||
|
break;
|
||||||
if (!(t == tok!"import" && current.type == tok!"import"))
|
if (!(t == tok!"import" && current.type == tok!"import"))
|
||||||
write("\n");
|
write("\n");
|
||||||
newline();
|
newline();
|
||||||
|
@ -226,7 +233,8 @@ private:
|
||||||
else if (current.type == tok!"return")
|
else if (current.type == tok!"return")
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
write(" ");
|
if (current.type != tok!";")
|
||||||
|
write(" ");
|
||||||
}
|
}
|
||||||
else if (current.type == tok!"switch")
|
else if (current.type == tok!"switch")
|
||||||
formatSwitch();
|
formatSwitch();
|
||||||
|
@ -310,17 +318,6 @@ private:
|
||||||
case tok!"(":
|
case tok!"(":
|
||||||
writeParens(true);
|
writeParens(true);
|
||||||
break;
|
break;
|
||||||
case tok!":":
|
|
||||||
if (!assumeSorted(astInformation.ternaryColonLocations)
|
|
||||||
.equalRange(current.index).empty)
|
|
||||||
{
|
|
||||||
write(" ");
|
|
||||||
writeToken();
|
|
||||||
write(" ");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
writeToken();
|
|
||||||
break;
|
|
||||||
case tok!"@":
|
case tok!"@":
|
||||||
case tok!"!":
|
case tok!"!":
|
||||||
case tok!"...":
|
case tok!"...":
|
||||||
|
@ -330,6 +327,10 @@ private:
|
||||||
case tok!"$":
|
case tok!"$":
|
||||||
writeToken();
|
writeToken();
|
||||||
break;
|
break;
|
||||||
|
case tok!":":
|
||||||
|
write(" : ");
|
||||||
|
index += 1;
|
||||||
|
break;
|
||||||
case tok!"]":
|
case tok!"]":
|
||||||
writeToken();
|
writeToken();
|
||||||
if (current.type == tok!"identifier")
|
if (current.type == tok!"identifier")
|
||||||
|
@ -340,6 +341,8 @@ private:
|
||||||
writeToken();
|
writeToken();
|
||||||
if (current.type != tok!"comment")
|
if (current.type != tok!"comment")
|
||||||
newline();
|
newline();
|
||||||
|
if (peekImplementation(tok!"class",0))
|
||||||
|
newline();
|
||||||
break;
|
break;
|
||||||
case tok!"{":
|
case tok!"{":
|
||||||
writeBraces();
|
writeBraces();
|
||||||
|
@ -538,7 +541,8 @@ private:
|
||||||
else if (current.type == tok!")")
|
else if (current.type == tok!")")
|
||||||
{
|
{
|
||||||
if (peekIs(tok!"identifier") || (index + 1 < tokens.length
|
if (peekIs(tok!"identifier") || (index + 1 < tokens.length
|
||||||
&& isKeyword(tokens[index + 1].type)))
|
&& (isKeyword(tokens[index+1].type)
|
||||||
|
|| tokens[index+1].type == tok!"@")))
|
||||||
{
|
{
|
||||||
writeToken();
|
writeToken();
|
||||||
if (space_afterwards)
|
if (space_afterwards)
|
||||||
|
@ -807,9 +811,6 @@ struct ASTInformation
|
||||||
|
|
||||||
/// Locations of unary operators
|
/// Locations of unary operators
|
||||||
size_t[] unaryLocations;
|
size_t[] unaryLocations;
|
||||||
|
|
||||||
/// Locations of ':' operators in ternary expressions
|
|
||||||
size_t[] ternaryColonLocations;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collects information from the AST that is useful for the formatter
|
/// Collects information from the AST that is useful for the formatter
|
||||||
|
@ -882,13 +883,6 @@ final class FormatVisitor : ASTVisitor
|
||||||
unary.accept(this);
|
unary.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
override void visit(const TernaryExpression ternary)
|
|
||||||
{
|
|
||||||
if (ternary.colon.type != tok!"")
|
|
||||||
astInformation.ternaryColonLocations ~= ternary.colon.index;
|
|
||||||
ternary.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ASTInformation* astInformation;
|
ASTInformation* astInformation;
|
||||||
alias visit = ASTVisitor.visit;
|
alias visit = ASTVisitor.visit;
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
import std.stdio;
|
||||||
|
class Foo {}
|
||||||
|
import std.conv;
|
||||||
|
void main() {return;}
|
||||||
|
const baz = 11;
|
||||||
|
class Foo2:Foo {}
|
|
@ -0,0 +1,18 @@
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
import std.conv;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baz = 11;
|
||||||
|
|
||||||
|
class Foo2 : Foo
|
||||||
|
{
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
class U0 : Exception {
|
||||||
|
this() @safe pure nothrow { super("U0 error message"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
class U1 : Exception {
|
||||||
|
this() @safe pure nothrow { super("U1 error message"); }
|
||||||
|
}
|
||||||
|
|
||||||
|
void foo() {
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
foreach (immutable i; 0 .. 2) {
|
||||||
|
try {
|
||||||
|
i.bar;
|
||||||
|
} catch (U0) {
|
||||||
|
"Function foo caught exception U0".writeln;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bar(in int i) @safe pure {
|
||||||
|
i.baz;
|
||||||
|
}
|
||||||
|
|
||||||
|
void baz(in int i) @safe pure {
|
||||||
|
throw i ? new U1 : new U0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
foo;
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
class U0 : Exception
|
||||||
|
{
|
||||||
|
this() @safe pure nothrow
|
||||||
|
{
|
||||||
|
super("U0 error message");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class U1 : Exception
|
||||||
|
{
|
||||||
|
this() @safe pure nothrow
|
||||||
|
{
|
||||||
|
super("U1 error message");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void foo()
|
||||||
|
{
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
foreach (immutable i; 0 .. 2)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
i.bar;
|
||||||
|
}
|
||||||
|
catch(U0)
|
||||||
|
{
|
||||||
|
"Function foo caught exception U0".writeln;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bar(in int i) @safe pure
|
||||||
|
{
|
||||||
|
i.baz;
|
||||||
|
}
|
||||||
|
|
||||||
|
void baz(in int i) @safe pure
|
||||||
|
{
|
||||||
|
throw i ? new U1 : new U0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
foo;
|
||||||
|
}
|
|
@ -1,45 +0,0 @@
|
||||||
import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits, std.typecons;
|
|
||||||
|
|
||||||
double hero(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
|
|
||||||
immutable s = (a + b + c) / 2.0;
|
|
||||||
immutable a2 = s * (s - a) * (s - b) * (s - c);
|
|
||||||
return (a2 > 0) ? a2.sqrt : 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isHeronian(in uint a, in uint b, in uint c) pure nothrow @safe @nogc {
|
|
||||||
immutable h = hero(a, b, c);
|
|
||||||
return h > 0 && h.floor == h.ceil;
|
|
||||||
}
|
|
||||||
|
|
||||||
T gcd3(T)(in T x, in T y, in T z) pure nothrow @safe @nogc {
|
|
||||||
return gcd(gcd(x, y), z);
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() /*@safe*/ {
|
|
||||||
enum uint maxSide = 200;
|
|
||||||
|
|
||||||
// Sort by increasing area, perimeter, then sides.
|
|
||||||
//auto h = cartesianProduct!3(iota(1, maxSide + 1))
|
|
||||||
auto r = iota(1, maxSide + 1);
|
|
||||||
const h = cartesianProduct(r, r, r)
|
|
||||||
//.filter!({a, b, c} => ...
|
|
||||||
.filter!(t => t[0] <= t[1] && t[1] <= t[2] &&
|
|
||||||
t[0] + t[1] > t[2] &&
|
|
||||||
t[].gcd3 == 1 && t[].isHeronian)
|
|
||||||
.array
|
|
||||||
.schwartzSort!(t => tuple(t[].hero, t[].only.sum, t.reverse))
|
|
||||||
.release;
|
|
||||||
|
|
||||||
static void showTriangles(R)(R ts) @safe {
|
|
||||||
"Area Perimeter Sides".writeln;
|
|
||||||
foreach (immutable t; ts)
|
|
||||||
writefln("%3s %8d %3dx%dx%d", t[].hero, t[].only.sum, t[]);
|
|
||||||
}
|
|
||||||
|
|
||||||
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide, h.length);
|
|
||||||
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:".writeln;
|
|
||||||
showTriangles(h.take(10));
|
|
||||||
|
|
||||||
"\nAll with area 210 subject to the previous ordering:".writeln;
|
|
||||||
showTriangles(h.filter!(t => t[].hero == 210));
|
|
||||||
}
|
|
|
@ -1,48 +0,0 @@
|
||||||
import std.stdio, std.math, std.range, std.algorithm, std.numeric, std.traits,
|
|
||||||
std.typecons;
|
|
||||||
|
|
||||||
double hero(in uint a, in uint b, in uint c) pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
immutable s = (a + b + c) / 2.0;
|
|
||||||
immutable a2 = s * (s - a) * (s - b) * (s - c);
|
|
||||||
return (a2 > 0) ? a2.sqrt : 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isHeronian(in uint a, in uint b, in uint c) pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
immutable h = hero(a, b, c);
|
|
||||||
return h > 0 && h.floor == h.ceil;
|
|
||||||
}
|
|
||||||
|
|
||||||
T gcd3(T)(in T x, in T y, in T z) pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return gcd(gcd(x, y), z);
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() /*@safe*/
|
|
||||||
{
|
|
||||||
enum uint maxSide = 200;
|
|
||||||
// Sort by increasing area, perimeter, then sides.
|
|
||||||
//auto h = cartesianProduct!3(iota(1, maxSide + 1))
|
|
||||||
auto r = iota(1, maxSide + 1);
|
|
||||||
const h = cartesianProduct(r, r, r)
|
|
||||||
//.filter!({a, b, c} => ...
|
|
||||||
.filter!(t => t[0] <= t[1] && t[1] <= t[2] && t[0] + t[1] > t[2] &&
|
|
||||||
t[].gcd3 == 1 && t[].isHeronian)
|
|
||||||
.array.schwartzSort!(t => tuple(t[].hero, t[].only.sum, t.reverse))
|
|
||||||
.release;
|
|
||||||
|
|
||||||
static void showTriangles(R)(R ts) @safe
|
|
||||||
{
|
|
||||||
"Area Perimeter Sides".writeln;
|
|
||||||
foreach (immutable t; ts)
|
|
||||||
writefln("%3s %8d %3dx%dx%d", t[].hero, t[].only.sum, t[]);
|
|
||||||
}
|
|
||||||
|
|
||||||
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide, h.length);
|
|
||||||
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:"
|
|
||||||
.writeln;
|
|
||||||
showTriangles(h.take(10));
|
|
||||||
"\nAll with area 210 subject to the previous ordering:".writeln;
|
|
||||||
showTriangles(h.filter!(t => t[].hero == 210));
|
|
||||||
}
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
import std.algorithm: swap; // from Phobos standard library
|
||||||
|
|
||||||
|
// The D solution uses templates and it's similar to the C++ one:
|
||||||
|
void mySwap(T)(ref T left, ref T right) {
|
||||||
|
auto temp = left;
|
||||||
|
left = right;
|
||||||
|
right = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
int[] a = [10, 20];
|
||||||
|
writeln(a);
|
||||||
|
|
||||||
|
// The std.algorithm standard library module
|
||||||
|
// contains a generic swap:
|
||||||
|
swap(a[0], a[1]);
|
||||||
|
writeln(a);
|
||||||
|
|
||||||
|
// Using mySwap:
|
||||||
|
mySwap(a[0], a[1]);
|
||||||
|
writeln(a);
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
import std.algorithm : swap; // from Phobos standard library
|
||||||
|
|
||||||
|
// The D solution uses templates and it's similar to the C++ one:
|
||||||
|
void mySwap(T)(ref T left, ref T right)
|
||||||
|
{
|
||||||
|
auto temp = left;
|
||||||
|
left = right;
|
||||||
|
right = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
int[] a = [10, 20];
|
||||||
|
writeln(a);
|
||||||
|
// The std.algorithm standard library module
|
||||||
|
// contains a generic swap:
|
||||||
|
swap(a[0], a[1]);
|
||||||
|
writeln(a);
|
||||||
|
// Using mySwap:
|
||||||
|
mySwap(a[0], a[1]);
|
||||||
|
writeln(a);
|
||||||
|
}
|
|
@ -4,5 +4,5 @@ set -e
|
||||||
for source in *.d
|
for source in *.d
|
||||||
do
|
do
|
||||||
../bin/dfmt "${source}" >"${source}.out"
|
../bin/dfmt "${source}" >"${source}.out"
|
||||||
diff -u "${source}.ref" "${source}.out" || echo "fail ${source}"
|
diff -u "${source}.ref" "${source}.out"
|
||||||
done
|
done
|
||||||
|
|
Loading…
Reference in New Issue