Merge remote-tracking branch 'origin/master' into fix-1063

This commit is contained in:
Martin Nordholts
2021-01-04 21:02:51 +01:00
24 changed files with 630 additions and 55 deletions

View File

@ -1,28 +1,72 @@
#!/usr/bin/env bash
cd "$(dirname "${BASH_SOURCE[0]}")" || exit
# Check that Hyperfine is installed.
if ! command -v hyperfine > /dev/null 2>&1; then
echo "'hyperfine' does not seem to be installed."
echo "You can get it here: https://github.com/sharkdp/hyperfine"
exit 1
fi
# Determine the target directories.
get_target_dir() {
if [[ -f "$HOME/.cargo/config" ]]; then
grep 'target-dir[[:space:]]*=' "$HOME/.cargo/config" \
| sed 's/^[[:space:]]*target-dir[[:space:]]*=//; s/^[[:space:]]*"//; s/"[[:space:]]*$//' \
&& return 0
fi
echo "../../target"
}
TARGET_DIR="$(get_target_dir)"
TARGET_DEBUG="${TARGET_DIR}/debug/bat"
TARGET_RELEASE="${TARGET_DIR}/release/bat"
# Determine which target to benchmark.
BAT=''
for arg in "$@"; do
case "$arg" in
--system) BAT="bat" ;;
--debug) BAT="$TARGET_DEBUG" ;;
--release) BAT="$TARGET_RELEASE" ;;
--bat=*) BAT="${arg:6}" ;;
esac
done
if [[ -z "$BAT" ]]; then
echo "A build of 'bat' must be specified for benchmarking."
echo "You can use '--system', '--debug', or '--release'."
exit 1
fi
# Ensure that the target is built.
if ! command -v "$BAT" &>/dev/null; then
echo "Could not find the build of bat to benchmark."
case "$BAT" in
"bat") echo "Make you sure to symlink 'batcat' as 'bat'." ;;
"$TARGET_DEBUG") echo "Make you sure to 'cargo build' first." ;;
"$TARGET_RELEASE") echo "Make you sure to 'cargo build --release' first." ;;
esac
exit 1
fi
# Run the benchmark.
echo "### Startup time"
echo
hyperfine --warmup 3 bat
hyperfine --warmup 3 "$BAT"
echo
echo "### Plain text"
echo
hyperfine --warmup 3 "bat --language txt --paging=never 'test-src/jquery-3.3.1.js'"
hyperfine --warmup 3 "$(printf "%q" "$BAT") --language txt --paging=never 'test-src/jquery-3.3.1.js'"
echo
echo "### Time to syntax-highlight large files"
echo
for SRC in test-src/*; do
hyperfine --warmup 3 "bat --style=full --color=always --paging=never '$SRC'"
hyperfine --warmup 3 "$(printf "%q" "$BAT") --style=full --color=always --paging=never $(printf "%q" "$SRC")"
done

View File

@ -0,0 +1,68 @@
import data.matrix.notation
import data.vector2
/-!
Helpers that don't currently fit elsewhere...
-/
lemma split_eq {m n : Type*} (x : m × n) (p p' : m × n) :
 p = x p' = x (x p x p') := by tauto
-- For `playfield`s, the piece type and/or piece index type.
variables (X : Type*)
variables [has_repr X]
namespace chess.utils
section repr
/--
An auxiliary wrapper for `option X` that allows for overriding the `has_repr` instance
for `option`, and rather, output just the value in the `some` and a custom provided
`string` for `none`.
-/
structure option_wrapper :=
(val : option X)
(none_s : string)
instance wrapped_option_repr : has_repr (option_wrapper X) :=
λ val, s, (option.map has_repr.repr val).get_or_else s
variables {X}
/--
Construct an `option_wrapper` term from a provided `option X` and the `string`
that will override the `has_repr.repr` for `none`.
-/
def option_wrap (val : option X) (none_s : string) : option_wrapper X := val, none_s
-- The size of the "vectors" for a `fin n' → X`, for `has_repr` definitions
variables {m' n' : }
/--
For a "vector" `X^n'` represented by the type `Π n' : , fin n' → X`, where
the `X` has a `has_repr` instance itself, we can provide a `has_repr` for the "vector".
This definition is used for displaying rows of the playfield, when it is defined
via a `matrix`, likely through notation.
-/
def vec_repr : Π {n' : }, (fin n' X) string :=
λ _ v, string.intercalate ", " ((vector.of_fn v).to_list.map repr)
instance vec_repr_instance : has_repr (fin n' X) := vec_repr
/--
For a `matrix` `X^(m' × n')` where the `X` has a `has_repr` instance itself,
we can provide a `has_repr` for the matrix, using `vec_repr` for each of the rows of the matrix.
This definition is used for displaying the playfield, when it is defined
via a `matrix`, likely through notation.
-/
def matrix_repr : Π {m' n'}, matrix (fin m') (fin n') X string :=
λ _ _ M, string.intercalate ";\n" ((vector.of_fn M).to_list.map repr)
instance matrix_repr_instance :
 has_repr (matrix (fin n') (fin m') X) := matrix_repr
end repr
end chess.utils

View File

@ -0,0 +1,107 @@
//! this is a top level doc, starts with "//!"
const std = @import("std");
pub fn main() anyerror!void {
 const stdout = std.io.getStdOut().writer();
 try stdout.print("Hello, {}!\n", .{"world"});
}
const expect = std.testing.expect;
test "comments" {
 // comments start with "//" until newline
 // foo bar baz
 const x = true; // another comment
 expect(x);
}
/// a doc comment starts with "///"
/// multiple lines are merged together
const Timestamp = struct {
 /// number of seconds since epoch
 seconds: i64,
 /// number of nanoseconds past the second
 nano: u32,
 const Self = @This();
 pub fn unixEpoch() Self {
 return Self{
 .seconds = 0,
 .nanos = 0,
 };
 }
};
const my_val = switch (std.Target.current.os.tag) {
 .linux => "Linux",
 else => "not Linux",
};
const Book = enum {
 paperback,
 hardcover,
 ebook,
 pdf,
};
const TokenType = union(enum) {
 int: isize,
 float: f64,
 string: []const u8,
};
const array_lit: [4]u8 = .{ 11, 22, 33, 44 };
const sentinal_lit = [_:0]u8{ 1, 2, 3, 4 };
test "address of syntax" {
 // Get the address of a variable:
 const x: i32 = 1234;
 const x_ptr = &x;
 // Dereference a pointer:
 expect(x_ptr.* == 1234);
 // When you get the address of a const variable, you get a const pointer to a single item.
 expect(@TypeOf(x_ptr) == *const i32);
 // If you want to mutate the value, you'd need an address of a mutable variable:
 var y: i32 = 5678;
 const y_ptr = &y;
 expect(@TypeOf(y_ptr) == *i32);
 y_ptr.* += 1;
 expect(y_ptr.* == 5679);
}
// integer literals
const decimal_int = 98222;
const hex_int = 0xff;
const another_hex_int = 0xFF;
const octal_int = 0o755;
const binary_int = 0b11110000;
// underscores may be placed between two digits as a visual separator
const one_billion = 1_000_000_000;
const binary_mask = 0b1_1111_1111;
const permissions = 0o7_5_5;
const big_address = 0xFF80_0000_0000_0000;
// float literals
const floating_point = 123.0E+77;
const another_float = 123.0;
const yet_another = 123.0e+77;
const hex_floating_point = 0x103.70p-5;
const another_hex_float = 0x103.70;
const yet_another_hex_float = 0x103.70P-5;
// underscores may be placed between two digits as a visual separator
const lightspeed = 299_792_458.000_000;
const nanosecond = 0.000_000_001;
const more_hex = 0x1234_5678.9ABC_CDEFp-10;
fn max(comptime T: type, a: T, b: T) T {
 return if (a > b) a else b;
}

View File

@ -54,4 +54,4 @@ TEST_ESCAPE="escaped characters \n \t \r \" \' \$ or maybe a backslash \\..."
TEST_DOUBLE="Lorem {$VAR1} ${VAR2} $VAR3 ipsum dolor sit amet\n\r\t\\"
# Single Quotes
TEST_SINGLE='Lorem {$VAR1} ${VAR2} $VAR3 ipsum dolor sit amet\n\r\t\\'
TEST_SINGLE='Lorem {$VAR1} ${VAR2} $VAR3 ipsum dolor sit amet\n\r\t\\'

View File

@ -0,0 +1,68 @@
import data.matrix.notation
import data.vector2
/-!
Helpers that don't currently fit elsewhere...
-/
lemma split_eq {m n : Type*} (x : m × n) (p p' : m × n) :
p = x p' = x (x p x p') := by tauto
-- For `playfield`s, the piece type and/or piece index type.
variables (X : Type*)
variables [has_repr X]
namespace chess.utils
section repr
/--
An auxiliary wrapper for `option X` that allows for overriding the `has_repr` instance
for `option`, and rather, output just the value in the `some` and a custom provided
`string` for `none`.
-/
structure option_wrapper :=
(val : option X)
(none_s : string)
instance wrapped_option_repr : has_repr (option_wrapper X) :=
λ val, s, (option.map has_repr.repr val).get_or_else s
variables {X}
/--
Construct an `option_wrapper` term from a provided `option X` and the `string`
that will override the `has_repr.repr` for `none`.
-/
def option_wrap (val : option X) (none_s : string) : option_wrapper X := val, none_s
-- The size of the "vectors" for a `fin n' → X`, for `has_repr` definitions
variables {m' n' : }
/--
For a "vector" `X^n'` represented by the type `Π n' : , fin n' → X`, where
the `X` has a `has_repr` instance itself, we can provide a `has_repr` for the "vector".
This definition is used for displaying rows of the playfield, when it is defined
via a `matrix`, likely through notation.
-/
def vec_repr : Π {n' : }, (fin n' X) string :=
λ _ v, string.intercalate ", " ((vector.of_fn v).to_list.map repr)
instance vec_repr_instance : has_repr (fin n' X) := vec_repr
/--
For a `matrix` `X^(m' × n')` where the `X` has a `has_repr` instance itself,
we can provide a `has_repr` for the matrix, using `vec_repr` for each of the rows of the matrix.
This definition is used for displaying the playfield, when it is defined
via a `matrix`, likely through notation.
-/
def matrix_repr : Π {m' n'}, matrix (fin m') (fin n') X string :=
λ _ _ M, string.intercalate ";\n" ((vector.of_fn M).to_list.map repr)
instance matrix_repr_instance :
has_repr (matrix (fin n') (fin m') X) := matrix_repr
end repr
end chess.utils

View File

@ -0,0 +1,107 @@
//! this is a top level doc, starts with "//!"
const std = @import("std");
pub fn main() anyerror!void {
const stdout = std.io.getStdOut().writer();
try stdout.print("Hello, {}!\n", .{"world"});
}
const expect = std.testing.expect;
test "comments" {
// comments start with "//" until newline
// foo bar baz
const x = true; // another comment
expect(x);
}
/// a doc comment starts with "///"
/// multiple lines are merged together
const Timestamp = struct {
/// number of seconds since epoch
seconds: i64,
/// number of nanoseconds past the second
nano: u32,
const Self = @This();
pub fn unixEpoch() Self {
return Self{
.seconds = 0,
.nanos = 0,
};
}
};
const my_val = switch (std.Target.current.os.tag) {
.linux => "Linux",
else => "not Linux",
};
const Book = enum {
paperback,
hardcover,
ebook,
pdf,
};
const TokenType = union(enum) {
int: isize,
float: f64,
string: []const u8,
};
const array_lit: [4]u8 = .{ 11, 22, 33, 44 };
const sentinal_lit = [_:0]u8{ 1, 2, 3, 4 };
test "address of syntax" {
// Get the address of a variable:
const x: i32 = 1234;
const x_ptr = &x;
// Dereference a pointer:
expect(x_ptr.* == 1234);
// When you get the address of a const variable, you get a const pointer to a single item.
expect(@TypeOf(x_ptr) == *const i32);
// If you want to mutate the value, you'd need an address of a mutable variable:
var y: i32 = 5678;
const y_ptr = &y;
expect(@TypeOf(y_ptr) == *i32);
y_ptr.* += 1;
expect(y_ptr.* == 5679);
}
// integer literals
const decimal_int = 98222;
const hex_int = 0xff;
const another_hex_int = 0xFF;
const octal_int = 0o755;
const binary_int = 0b11110000;
// underscores may be placed between two digits as a visual separator
const one_billion = 1_000_000_000;
const binary_mask = 0b1_1111_1111;
const permissions = 0o7_5_5;
const big_address = 0xFF80_0000_0000_0000;
// float literals
const floating_point = 123.0E+77;
const another_float = 123.0;
const yet_another = 123.0e+77;
const hex_floating_point = 0x103.70p-5;
const another_hex_float = 0x103.70;
const yet_another_hex_float = 0x103.70P-5;
// underscores may be placed between two digits as a visual separator
const lightspeed = 299_792_458.000_000;
const nanosecond = 0.000_000_001;
const more_hex = 0x1234_5678.9ABC_CDEFp-10;
fn max(comptime T: type, a: T, b: T) T {
return if (a > b) a else b;
}