Skip to content

Commit

Permalink
Add executable for interactive collider
Browse files Browse the repository at this point in the history
  • Loading branch information
engboris committed Aug 13, 2024
1 parent 50cf1ec commit 6f6a7cf
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 10 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ Build the project
dune build
```

The executable is `_build/default/bin/lsc.exe`.
Executables are in `_build/default/bin/`.

## Commands

### Constellation collider

Assume the executable is named `lsc.exe`. Execute the program with:

```
Expand All @@ -69,6 +71,24 @@ or if you use Dune:
dune exec lsc -- [-options] <inputfile>
```

### Interactive collider

Dynamically add, remove, clear stars and make them collide.

Assume the executable is named `ilsc.exe`. Execute the program with:

```
./ilsc.exe
```

or if you use Dune:

```
dune exec ilsc
```

The instructions are provided in the program.

# Examples

Some example files with the `.stellar` extension in `/examples` are ready to be
Expand Down
8 changes: 4 additions & 4 deletions bin/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(executable
(public_name lsc)
(name lsc)
(libraries lsc))
(executables
(public_names lsc ilsc)
(names lsc ilsc)
(libraries lsclib base))
(env
(dev
(flags (:standard -warn-error -A))))
75 changes: 75 additions & 0 deletions bin/ilsc.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
open Base
open Lsclib.Stellar
open Lsclib.Parser

let welcome () =
Stdlib.print_string "Commands :--------------------------------------\n";
Stdlib.print_string "<constellation>\t\tMake a new constellation interacts
\t\t\twith the previous interaction space\n\t\t\t({} at beginning)\n";
Stdlib.print_string "exit|e|q\t\tquit the program\n";
Stdlib.print_string "clear|c\t\t\tclear interaction space\n";
Stdlib.print_string "delete|del|d\t\tdelete a star at some index\n";
Stdlib.print_string "add|a\t\t\tadd stars\n";
Stdlib.print_string "------------------------------------------------\n"

let prompt mode =
Stdlib.print_string (mode ^ " >> ");
Stdlib.read_line ()

let rec delete cs =
let input = prompt "Delete star of index" in
try
let n = Int.of_string input in
List.filteri ~f:(fun i _ -> not (equal_int i n)) cs
with Failure _ ->
Stdlib.print_string "This is not a positive integer. Please retry.\n";
delete cs

let unmark = function
| Marked s -> s
| Unmarked s -> s

let rec add cs =
let input = prompt "Add stars" in
try
let lexbuf = Lexing.from_string input in
let mcs = marked_constellation Lsclib.Lexer.read lexbuf in
cs @ (List.map ~f:unmark mcs)
with _ ->
Stdlib.print_string "Error. Please retry.\n";
add cs

let rec loop (cs : constellation) =
cs |> string_of_constellation |> Stdlib.print_string;
Stdlib.print_newline ();
let input = prompt "Send" in
if (List.mem ~equal:equal_string ["exit"; "e"; "q"] input) then
Stdlib.exit 0
else if (List.mem ~equal:equal_string ["clear"; "c"] input) then
loop []
else if (List.mem ~equal:equal_string ["delete"; "del"; "d"] input) then
loop (delete cs)
else if (List.mem ~equal:equal_string ["add"; "a"] input) then
loop (add cs)
else
begin
let base = List.map ~f:(fun s -> Marked s) cs in
let lexbuf = Lexing.from_string input in
try
let mcs = marked_constellation Lsclib.Lexer.read lexbuf in
let cs = extract_intspace (base @ mcs) in
let result =
exec ~unfincomp:true
~withloops:false
~showtrace:false
~selfint:false
~showsteps:false cs in
loop result
with _ ->
Stdlib.print_string "Error. Please retry.\n";
loop cs
end

let _ =
welcome ();
loop [];
6 changes: 3 additions & 3 deletions bin/lsc.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Base
open Lsc.Stellar
open Lsc.Parser
open Lsclib.Stellar
open Lsclib.Parser
open Out_channel

let usage_msg = "exec [-no-trivial-eq] [-allow-unfinished-computation] [-show-steps] [-show-trace] [-allow-self-interaction] <filename>"
Expand Down Expand Up @@ -37,7 +37,7 @@ let speclist =
let _ =
Stdlib.Arg.parse speclist anon_fun usage_msg;
let lexbuf = Lexing.from_channel (Stdlib.open_in !input_file) in
let mcs = marked_constellation Lsc.Lexer.read lexbuf in
let mcs = marked_constellation Lsclib.Lexer.read lexbuf in
let cs = extract_intspace mcs in
(if !showsteps
then output_string stdout "Press any key to move to the next step.\n");
Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name lsc)
(name lsclib)
(libraries base))
(env
(dev
Expand Down
2 changes: 1 addition & 1 deletion src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ func_expr:
cons_expr:
| r1 = ray; CONS; r2 = ray { Stellar.to_func ((Stellar.Null, ":"), [r1; r2]) }
| LPAR; e = cons_expr; RPAR; CONS; r = ray
{ Stellar.to_func ((Stellar.Null, ":"), [e; r]) }
{ Stellar.to_func ((Stellar.Null, ":"), [e; r]) }

0 comments on commit 6f6a7cf

Please sign in to comment.