Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/main.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Conkeldurr
open Cmdliner

let version = "1.0a"
let conkeldurr_process_file file = file |> Entry.entry_point

let input_file_arg =
Expand All @@ -10,7 +11,7 @@ let input_file_arg =

let main_cmd =
let term = Term.(const conkeldurr_process_file $ input_file_arg) in
Cmd.v (Cmd.info "conkeldurr" ~version:"%%VERSION%%") term
Cmd.v (Cmd.info "conkeldurr" ~version) term
;;

let main () = Cmd.eval main_cmd
Expand Down
8 changes: 8 additions & 0 deletions lib/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ module Export = struct
;;
end

module Chdir = struct
type t = string [@@deriving sexp]

let to_string node = Printf.sprintf "Chdir to %s" node
end

module Node = struct
type t =
| ReadConstant of ReadConstant.t
| ReadVariable of ReadVariable.t
| ReadSpreadsheet of ReadSpreadsheet.t
| Import of Import.t
| Export of Export.t
| Chdir of Chdir.t
[@@deriving sexp]

let to_string node =
Expand All @@ -92,5 +99,6 @@ module Node = struct
| ReadSpreadsheet n -> ReadSpreadsheet.to_string n
| Import n -> Import.to_string n
| Export n -> Export.to_string n
| Chdir n -> Chdir.to_string n
;;
end
9 changes: 9 additions & 0 deletions lib/ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ module Export : sig
val to_string : t -> string
end

module Chdir : sig
type t = string [@@deriving sexp]

val t_of_sexp : Sexplib0.Sexp.t -> t
val sexp_of_t : t -> Sexplib0.Sexp.t
val to_string : t -> string
end

(* Node is a node in the AST tree *)
module Node : sig
type t =
Expand All @@ -66,6 +74,7 @@ module Node : sig
| ReadSpreadsheet of ReadSpreadsheet.t
| Import of Import.t
| Export of Export.t
| Chdir of Chdir.t

val t_of_sexp : Sexplib0.Sexp.t -> t
val sexp_of_t : t -> Sexplib0.Sexp.t
Expand Down
14 changes: 14 additions & 0 deletions lib/interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module T = struct
; spreadsheet_store : Spreadsheet.t Store.T.t
; interface_set : Variable.T.t Store.T.t
; import_store : Variable.T.t Store.T.t
; root_directory : string
}

let interpret_read_constant state (node : Ast.ReadConstant.t) =
Expand Down Expand Up @@ -76,6 +77,17 @@ module T = struct
Store.T.clear state.import_store
;;

let interpret_chdir state (node : Ast.Chdir.t) =
let cwd = Sys.getcwd () in
try
Io.Files.cd state.root_directory;
Io.Files.cd node
with
| Io.Files.Io_exception msg ->
Io.Files.cd cwd;
failwith msg
;;

let interpret_node state node =
match node with
| Ast.Node.ReadConstant n -> interpret_read_constant state n
Expand All @@ -85,13 +97,15 @@ module T = struct
| Ast.Node.Export n ->
interpret_export state n;
reset_state state
| Ast.Node.Chdir n -> interpret_chdir state n
;;

let[@inline] create_blank_state () =
{ variable_store = Store.T.create ()
; spreadsheet_store = Store.T.create ()
; interface_set = Store.T.create ()
; import_store = Store.T.create ()
; root_directory = Sys.getcwd ()
}
;;

Expand Down
1 change: 1 addition & 0 deletions lib/interpreter.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module T : sig
; spreadsheet_store : Spreadsheet.t Store.T.t
; interface_set : Variable.T.t Store.T.t
; import_store : Variable.T.t Store.T.t
; root_directory : string
}

val interpret_node : state -> Ast.Node.t -> unit
Expand Down
9 changes: 8 additions & 1 deletion lib/io.ml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
module Files = struct
exception Io_exception of string

let[@inline] get_absolute_file_path file =
file |> Util.T.unquote |> Filename_unix.realpath
;;

let[@inline] is_valid_path path = path |> get_absolute_file_path |> Sys.file_exists

let[@inline] cd dir =
try Sys.chdir dir with
| Sys_error msg -> raise (Io_exception msg)
;;

let set_working_directory input =
let full_path = input |> get_absolute_file_path in
full_path |> Filename.dirname |> Sys.chdir;
full_path |> Filename.dirname |> cd;
full_path
;;
end
Expand Down
3 changes: 3 additions & 0 deletions lib/io.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(* Some useful file system utils *)
module Files : sig
exception Io_exception of string

val get_absolute_file_path : string -> string
val is_valid_path : string -> bool
val cd : string -> unit
val set_working_directory : string -> string
end

Expand Down
27 changes: 27 additions & 0 deletions test/expect_tests/ast_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ let%expect_test "parses ReadSpreadsheet correctly" =
|}]
;;

let%expect_test "parses Import correctly" =
let sample_sexp =
{|
((Import ((var SomeEnum) (from some-file.ts))))
|}
in
sample_sexp |> Program.T.of_string |> Program.T.to_readable_string |> print_endline;
[%expect {| Import SomeEnum from some-file.ts |}]
;;

let%expect_test "parses Export correctly" =
let sample_sexp =
{|
Expand All @@ -59,3 +69,20 @@ let%expect_test "parses Export correctly" =
Export to stdout
|}]
;;

let%expect_test "parses Chdir correctly" =
let sample_sexp =
{|(
(Chdir "some_folder1")
(Chdir "some_folder2")
(Chdir "some_folder3")
)|}
in
sample_sexp |> Program.T.of_string |> Program.T.to_readable_string |> print_endline;
[%expect
{|
Chdir to some_folder1
Chdir to some_folder2
Chdir to some_folder3
|}]
;;
12 changes: 12 additions & 0 deletions test/expect_tests/cases/chdir_program_test.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(
(Chdir "./data/a")
(ReadSpreadsheet ((var "sheet1") (interface "interface1") (path (Csv "data.csv"))))

(Chdir "./data/b")
(ReadSpreadsheet ((var "sheet2") (interface "interface2") (path (Csv "data.csv"))))

(Chdir "./data/c")
(ReadSpreadsheet ((var "sheet3") (interface "interface3") (path (Csv "data.csv"))))

(Export Stdout)
)
2 changes: 2 additions & 0 deletions test/expect_tests/cases/data/a/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(String col)
"A"
2 changes: 2 additions & 0 deletions test/expect_tests/cases/data/b/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(String col)
"B"
2 changes: 2 additions & 0 deletions test/expect_tests/cases/data/c/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(String col)
"C"
32 changes: 32 additions & 0 deletions test/expect_tests/end_to_end_program_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,35 @@ let%expect_test "fails on duplicate imported modules" =
print_endline msg;
[%expect {| variable SomeEnum already set |}]
;;

let%expect_test "passes chdir end to end test" =
Entry.entry_point "chdir_program_test.sexp";
[%expect
{|
export interface interface1 {
col: string;
};

export const sheet1: Array<interface1> = [
{ col: "A" }
];


export interface interface2 {
col: string;
};

export const sheet2: Array<interface2> = [
{ col: "B" }
];


export interface interface3 {
col: string;
};

export const sheet3: Array<interface3> = [
{ col: "C" }
];
|}]
;;