Skip to content

Commit 73494af

Browse files
committed
initial commit
0 parents  commit 73494af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4707
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
*~

Documentation/InterpreterUse.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
## Using the Interpreter
2+
3+
For details on the language itself, read the [manual](http://hjemmesider.diku.dk/~torbenm/Troll/manual.pdf).
4+
5+
All functionality described in the manual is implemented (*not necessarily correctly* :) with the following exceptions:
6+
7+
- Compositional functions
8+
- Probability calculations
9+
10+
As these are functions I do not currently need, their implementation is low priority. Let me know if they are important to you and I may re-consider. Of course, pull requests with implementations are appreciated!
11+
12+
### Running a Script
13+
14+
If you run troll with a filename on the command line, it will execute that script
15+
16+
```
17+
troll afile.t
18+
```
19+
20+
You can optionally define variables on the command line as well
21+
22+
```
23+
troll afile.t N=5 S=20
24+
```
25+
26+
Given this command line, the definitions can make use of the values specified for `N` and `S`. Variable definitions must be of the form `<identifier>=<integer>` where `<identifier>` follows the Troll requirements for variable names. There cannot be spaces between the equals sign and the identifier and integer value. Definitions for variables that are not used in the Troll script are ignored.
27+
28+
Troll files conventionally have names ending in `.t`, but that is not required.
29+
30+
### Interactive Usage
31+
32+
If you run troll without a filename
33+
34+
```
35+
$ troll
36+
```
37+
38+
it will start up an interactive shell (aka a REPL). You can enter a line of Troll code at the prompt and the interpreter will print the resulting value. In addition, several commands are supported (enter commands on a line by themselves):
39+
40+
- `+set` &mdash; define a variable to be used in subsequent Troll code. The format is `+set <varname> <integer>`
41+
- `-set` &mdash; remove a variable definition. The format is `-set <varname`
42+
- `+multiline` &mdash; allows you to enter multiple lines of Troll code
43+
44+
This is particularly useful for entering function definitions since currently you cannot enter them without also entering a 'main' Troll script.
45+
46+
To stop entering Toll code and have the interpreter run the script, enter `+done` on its own line.
47+
- `+quit` &mdash; quit the REPL (*Ctl-D and Ctl-C also work*)
48+
- `+version` &mdash; print the interpreter version
49+
- `+help` &mdash; print a help message
50+
51+
NOTE: Variable definitions remain in existance until either you remove them (*via `-set`*) or you quit the REPL; although you can *shadow* variables by re-defining them. Functions continue to be defined as well (*currently there is no mechanism to un-define a function, other than quitting the REPL*).
52+
53+
The following commands are (*probably*) only of interest to people working on the Troll implementation:
54+
55+
- `+parser` &mdash; print the AST
56+
- `-parser` &mdash; stop printing the AST
57+
- `+scanner` &mdash; print the token stream
58+
- `-scanner` &mdash; stop printing the token stream
59+
60+
61+
62+
63+
64+
## Installing Troll
65+
66+
Troll should build on any platform that has Swift, i.e. macOS, Linux (Ubuntu, CentOS, Amazon Linux 2), and Windows 10. Currently it has only been tested on macOS.
67+
68+
### Get a pre-compiled version
69+
70+
Download the [latest release](https://github.com/profburke/troll/releases), and copy the executable to somewhere on your PATH; `/usr/local/bin` is recommended.
71+
72+
### Build from scratch
73+
74+
Building should be fairly straight-forward:
75+
76+
```
77+
git clone https://github.com/profburke/troll
78+
cd troll
79+
swift build -c release
80+
```
81+
82+
Copy the executable file (`.build/release/troll`) to somewhere on your PATH; `/usr/local/bin` is a good choice.
83+
84+
If there are errors while building, please create an [issue](https://github.com/profburke/troll/issues/new).

Examples/Backgammon.t

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
\ Backgammon dice -- 2d6 where doubles count twice
2+
3+
x := d6;
4+
y := d6;
5+
if x=y then 4#x else {x,y}

Examples/Diff.t

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
\ The number of different values on N d6
2+
3+
count different (N d6)

Examples/Efron.t

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
\ Efron's non-transitive dice
2+
\ A beats B with p = 2/3
3+
\ B beats C with p = 2/3
4+
\ C beats E with p = 2/3
5+
\ E beats A with p = 2/3
6+
7+
A := choose{4,4,4,4,0,0};
8+
B := choose{3,3,3,3,3,3};
9+
C := choose{6,6,2,2,2,2};
10+
E := choose{5,5,5,1,1,1};
11+
12+
count A>B
13+
14+
\ modify above which pair you want to test.

Examples/Equal.t

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\ The size of the largest set of identical results on N d6s
2+
\ Explanation: Roll N d6 and for each possible number on a die
3+
\ count how many there are of that,
4+
\ finally taking the maximum of these.
5+
6+
x := N d6; max (foreach i in 1..6 do count i=x)

Examples/Ironclaw.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\ Jason Holmgren. IRONCLAW: Anthropomorphic
2+
\ Fantasy Role-Play. Sanguine Productions Ltd., 1999.
3+
4+
count T < { d M, d N, d P }

Examples/L5R.t

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
\ Die roll from Legend of the Five Rings.
2+
\ Add the M largest of N d10,
3+
\ where each die adds another d10 (recursively) when 10 is rolled.
4+
5+
sum (largest M N#(sum accumulate x:=d10 while x=10))

Examples/Match.t

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\ Match highest attack die against highest defending dice and so on
2+
\ (like in Risk) counting 1 to the victor of each match (ties = 0).
3+
\ Result is shown as attacker score minus defender score.
4+
5+
attack := M d6;
6+
defense := N d6;
7+
size := min {M,N};
8+
sum
9+
foreach i in 1..size do (
10+
aval := min largest i attack;
11+
dval := min largest i defense;
12+
if aval>dval then 1 else if dval>aval then -1 else 0
13+
)

Examples/Median3.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\ Median of 3 d N
2+
3+
max least 2 3 d N
4+

0 commit comments

Comments
 (0)