Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use atdgen to generate JSON bindings for OCaml #15

Closed
wants to merge 3 commits into from
Closed
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
162 changes: 112 additions & 50 deletions ocaml/ast.ml
Original file line number Diff line number Diff line change
@@ -1,51 +1,98 @@
type linno = int

(* type t =
* | Command of linno * assign list * args * redirection list (\* assign, args, redir *\)
* | Pipe of bool * t list (\* background?, commands *\)
* | Redir of linno * t * redirection list
* | Background of linno * t * redirection list
* | Subshell of linno * t * redirection list
* | And of t * t
* | Or of t * t
* | Not of t
* | Semi of t * t
* | If of t * t * t (\* cond, then, else *\)
* | While of t * t (\* test, body *\) (\* until encoded as a While . Not *\)
* | For of linno * arg * t * string (\* args, body, var *\)
* | Case of linno * arg * case list
* | Defun of linno * string * t (\* name, body *\)
* and assign = string * arg
* and redirection =
* | File of redir_type * int * arg
* | Dup of dup_type * int * arg
* | Heredoc of heredoc_type * int * arg
* and redir_type = To | Clobber | From | FromTo | Append
* and dup_type = ToFD | FromFD
* and heredoc_type = Here | XHere (\* for when in a quote... not sure when this comes up *\)
* and args = arg list
* and arg = arg_char list
* and arg_char =
* | C of char
* | E of char (\* escape... necessary for expansion *\)
* | T of string option (\* tilde *\)
* | A of arg (\* arith *\)
* | V of var_type * bool (\* VSNUL? *\) * string * arg
* | Q of arg (\* quoted *\)
* | B of t (\* backquote *\)
* and var_type =
* | Normal
* | Minus
* | Plus
* | Question
* | Assign
* | TrimR
* | TrimRMax
* | TrimL
* | TrimLMax
* | Length
* and case = { cpattern : arg list; cbody : t } *)

type t =
| Command of linno * assign list * args * redirection list (* assign, args, redir *)
| Pipe of bool * t list (* background?, commands *)
| Redir of linno * t * redirection list
| Background of linno * t * redirection list
| Subshell of linno * t * redirection list
| And of t * t
| Or of t * t
Command of (linno * assign list * args * redirection list)
| Pipe of (bool * t list)
| Redir of (linno * t * redirection list)
| Background of (linno * t * redirection list)
| Subshell of (linno * t * redirection list)
| And of (t * t)
| Or of (t * t)
| Not of t
| Semi of t * t
| If of t * t * t (* cond, then, else *)
| While of t * t (* test, body *) (* until encoded as a While . Not *)
| For of linno * arg * t * string (* args, body, var *)
| Case of linno * arg * case list
| Defun of linno * string * t (* name, body *)
and assign = string * arg
and redirection =
| File of redir_type * int * arg
| Dup of dup_type * int * arg
| Heredoc of heredoc_type * int * arg
and redir_type = To | Clobber | From | FromTo | Append
and dup_type = ToFD | FromFD
and heredoc_type = Here | XHere (* for when in a quote... not sure when this comes up *)
and args = arg list
and arg = arg_char list
and arg_char =
| C of char
| E of char (* escape... necessary for expansion *)
| T of string option (* tilde *)
| A of arg (* arith *)
| V of var_type * bool (* VSNUL? *) * string * arg
| Q of arg (* quoted *)
| B of t (* backquote *)
and var_type =
| Normal
| Minus
| Plus
| Question
| Assign
| TrimR
| TrimRMax
| TrimL
| TrimLMax
| Length
and case = { cpattern : arg list; cbody : t }
| Semi of (t * t)
| If of (t * t * t)
| While of (t * t)
| For of (linno * arg * t * string)
| Case of (linno * arg * case list)
| Defun of (linno * string * t)
and assign = (string * arg)
and redirection =
File of (redir_type * int * arg)
| Dup of (dup_type * int * arg)
| Heredoc of (heredoc_type * int * arg)
and redir_type = To | Clobber | From | FromTo | Append
and dup_type = ToFD | FromFD
and heredoc_type = Here | XHere
and args = arg list
and arg = arg_char list
and arg_char =
C of char
| E of char
| T of string option
| A of arg
| V of (var_type * bool * string * arg)
| Q of arg
| B of t
and var_type =
Normal
| Minus
| Plus
| Question
| Assign
| TrimR
| TrimRMax
| TrimL
| TrimLMax
| Length
and case = { cpattern : arg list; cbody : t; }


let var_type = function
| 0x0 -> (* VSNORMAL ${var} *) Normal
| 0x2 -> (* VSMINUS ${var-text} *) Minus
Expand Down Expand Up @@ -330,14 +377,23 @@ let show_unless expected actual =
else string_of_int actual

let background s = "{ " ^ s ^ " & }"


let is_compound = function
| Command _ -> false
| _ -> true

let is_bg = function
| Pipe (bg,_) -> bg
| Background _ -> true
| _ -> false

let rec to_string = function
| Command (_,assigns,cmds,redirs) ->
separated string_of_assign assigns ^
(if List.length assigns = 0 || List.length cmds = 0 then "" else " ") ^
separated string_of_arg cmds ^ string_of_redirs redirs
| Pipe (bg,ps) ->
let p = intercalate " | " (List.map to_string ps) in
let p = intercalate " | " (List.map inner ps) in
if bg then background p else p
| Redir (_,a,redirs) ->
to_string a ^ string_of_redirs redirs
Expand All @@ -354,13 +410,13 @@ let rec to_string = function
backgrounded?" so the braces resolve the issue. testing
indicates that they're semantically equivalent.
*)
background (to_string a ^ string_of_redirs redirs)
background (inner a ^ string_of_redirs redirs)
| Subshell (_,a,redirs) ->
parens (to_string a ^ string_of_redirs redirs)
| And (a1,a2) -> to_string a1 ^ " && " ^ to_string a2
| Or (a1,a2) -> to_string a1 ^ " || " ^ to_string a2
| Not a -> "! " ^ braces (to_string a)
| Semi (a1,a2) -> to_string a1 ^ " ; " ^ to_string a2
| And (a1,a2) -> inner a1 ^ " && " ^ inner a2
| Or (a1,a2) -> inner a1 ^ " || " ^ inner a2
| Not a -> "! " ^ braces (inner a)
| Semi (a1,a2) -> inner a1 ^ " ; " ^ to_string a2
| If (c,t,e) -> string_of_if c t e
| While (Not t,b) ->
"until " ^ to_string t ^ "; do " ^ to_string b ^ "; done "
Expand All @@ -373,6 +429,12 @@ let rec to_string = function
"case " ^ string_of_arg a ^ " in " ^
separated string_of_case cs ^ " esac"
| Defun (_,name,body) -> name ^ "() {\n" ^ to_string body ^ "\n}"

and inner a =
let bg = is_bg a in
if is_compound a
then "{ " ^ to_string a ^ " " ^ (if bg then "&" else ";") ^ " }"
else to_string a ^ (if bg then " &" else "")

and string_of_if c t e =
"if " ^ to_string c ^
Expand Down
36 changes: 18 additions & 18 deletions ocaml/ast.mli
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
type linno = int

type t =
Command of linno * assign list * args * redirection list
| Pipe of bool * t list
| Redir of linno * t * redirection list
| Background of linno * t * redirection list
| Subshell of linno * t * redirection list
| And of t * t
| Or of t * t
Command of (linno * assign list * args * redirection list)
| Pipe of (bool * t list)
| Redir of (linno * t * redirection list)
| Background of (linno * t * redirection list)
| Subshell of (linno * t * redirection list)
| And of (t * t)
| Or of (t * t)
| Not of t
| Semi of t * t
| If of t * t * t
| While of t * t
| For of linno * arg * t * string
| Case of linno * arg * case list
| Defun of linno * string * t
and assign = string * arg
| Semi of (t * t)
| If of (t * t * t)
| While of (t * t)
| For of (linno * arg * t * string)
| Case of (linno * arg * case list)
| Defun of (linno * string * t)
and assign = (string * arg)
and redirection =
File of redir_type * int * arg
| Dup of dup_type * int * arg
| Heredoc of heredoc_type * int * arg
File of (redir_type * int * arg)
| Dup of (dup_type * int * arg)
| Heredoc of (heredoc_type * int * arg)
and redir_type = To | Clobber | From | FromTo | Append
and dup_type = ToFD | FromFD
and heredoc_type = Here | XHere
Expand All @@ -30,7 +30,7 @@ and arg_char =
| E of char
| T of string option
| A of arg
| V of var_type * bool * string * arg
| V of (var_type * bool * string * arg)
| Q of arg
| B of t
and var_type =
Expand Down