Skip to content

Commit

Permalink
like lawrence before me, i have conquered the dunes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgree committed Dec 15, 2023
1 parent 4928fc1 commit 7b4e2a5
Show file tree
Hide file tree
Showing 14 changed files with 550 additions and 251 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ Makefile
/stamp-h1

# generated by make
/src/builtins.h
/src/nodes.h
/src/syntax.h
/src/token.h
/src/token_vars.h

# generated by dune
_build

# Apple debug symbol bundles
*.dSYM/

Expand Down
36 changes: 36 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(data_only_dirs src)

(rule
(deps (source_tree src) configure.ac Makefile.am)
(targets libdash.a dlldash.so
builtins.h nodes.h syntax.h token.h token_vars.h
)
(action
(bash
"\
\n set -e\
\n if [ \"$(uname -s)\" = \"Darwin\" ]; then glibtoolize; else libtoolize; fi\
\n aclocal && autoheader && automake --add-missing && autoconf\
\n ./configure --prefix=\"$(pwd)\"\
\n %{make}\
\n %{make} install\
\n cp lib/libdash.a libdash.a\
\n cp lib/dlldash.so dlldash.so\
\n cp src/{builtins,nodes,syntax,token,token_vars}.h .\
\n")))

(subdir src
(rule
(deps ../builtins.h ../nodes.h ../syntax.h ../token.h ../token_vars.h)
(targets builtins.h nodes.h syntax.h token.h token_vars.h)
(action
(progn
(copy ../builtins.h builtins.h)
(copy ../nodes.h nodes.h)
(copy ../syntax.h syntax.h)
(copy ../token.h token.h)
(copy ../token_vars.h token_vars.h)))))

(library
(name dash)
(foreign_archives dash))
3 changes: 3 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 3.12)
(name libdash)
(using ctypes 0.3)
4 changes: 4 additions & 0 deletions dune-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(lang dune 3.12)
(env
(dev
(flags (:standard -warn-error -27))))
12 changes: 4 additions & 8 deletions ocaml/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,20 @@ let string_of_var_type = function


open Ctypes
open Foreign
open Dash

let rec last = function
| [] -> None
| [x] -> Some x
| x::xs -> last xs
| _::xs -> last xs

let skip = Command (-1,[],[],[])

let special_chars : char list = explode "|&;<>()$`\\\"'"

type quote_mode =
QUnquoted
| QQuoted
| QHeredoc

let needs_escaping c = List.mem c special_chars

let rec of_node (n : node union ptr) : t =
if nullptr n
then skip
Expand Down Expand Up @@ -225,7 +220,7 @@ and of_binary (n : node union ptr) =
(of_node (getf n nbinary_ch1), of_node (getf n nbinary_ch2))

and to_arg (n : narg structure) : arg =
let a,s,bqlist,stack = parse_arg ~tilde_ok:true ~assign:false (explode (getf n narg_text)) (getf n narg_backquote) [] in
let a,s,bqlist,stack = parse_arg ~assign:false (explode (getf n narg_text)) (getf n narg_backquote) [] in
(* we should have used up the string and have no backquotes left in our list *)
assert (s = []);
assert (nullptr bqlist);
Expand Down Expand Up @@ -304,6 +299,7 @@ and parse_arg ?tilde_ok:(tilde_ok=false) ~assign:(assign:bool) (s : char list) (
then (* we're in arithmetic or double quotes, so tilde is ignored *)
arg_char assign (C '~') s bqlist stack
else
let _ = tilde_ok in (* unused? *)
let uname,s' = parse_tilde [] s in
arg_char assign (T uname) s' bqlist stack
(* ordinary character *)
Expand All @@ -325,7 +321,7 @@ and parse_tilde acc s =
and arg_char assign c s bqlist stack =
let tilde_ok =
match c with
| C c -> assign && (match last s with
| C _ -> assign && (match last s with
| Some ':' -> true
| _ -> false)
| _ -> false
Expand Down
26 changes: 13 additions & 13 deletions ocaml/ast_atd.atd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
type char <ocaml predef module="Ast"> = int <ocaml repr="char">
type char <ocaml predef module="Libdash.Ast"> = int <ocaml repr="char">

type linno <ocaml predef module="Ast"> = int
type linno <ocaml predef module="Libdash.Ast"> = int

type t <ocaml predef module="Ast"> = [
type t <ocaml predef module="Libdash.Ast"> = [
Command of (linno * assign list * args * redirection list) (* assign, args, redir *)
| Pipe of (bool * t list) (* background?, commands *)
| Redir of (linno * t * redirection list)
Expand All @@ -19,37 +19,37 @@ type t <ocaml predef module="Ast"> = [
| Defun of (linno * string * t) (* name, body *)
] <ocaml repr="classic">

type assign <ocaml predef module="Ast"> = (string * arg) <ocaml repr="classic">
type assign <ocaml predef module="Libdash.Ast"> = (string * arg) <ocaml repr="classic">

type redirection <ocaml predef module="Ast"> = [
type redirection <ocaml predef module="Libdash.Ast"> = [
File of (redir_type * int * arg)
| Dup of (dup_type * int * arg)
| Heredoc of (heredoc_type * int * arg)
] <ocaml repr="classic">

type redir_type <ocaml predef module="Ast"> = [
type redir_type <ocaml predef module="Libdash.Ast"> = [
To
| Clobber
| From
| FromTo
| Append
] <ocaml repr="classic">

type dup_type <ocaml predef module="Ast"> = [
type dup_type <ocaml predef module="Libdash.Ast"> = [
ToFD
| FromFD
] <ocaml repr="classic">

type heredoc_type <ocaml predef module="Ast"> = [
type heredoc_type <ocaml predef module="Libdash.Ast"> = [
Here
| XHere (* for when in a quote... not sure when this comes up *)
] <ocaml repr="classic">

type args <ocaml predef module="Ast"> = arg list
type args <ocaml predef module="Libdash.Ast"> = arg list

type arg <ocaml predef module="Ast"> = arg_char list
type arg <ocaml predef module="Libdash.Ast"> = arg_char list

type arg_char <ocaml predef module="Ast"> = [
type arg_char <ocaml predef module="Libdash.Ast"> = [
C of char
| E of char (* escape... necessary for expansion *)
| T of string option (* tilde *)
Expand All @@ -59,7 +59,7 @@ type arg_char <ocaml predef module="Ast"> = [
| B of t (* backquote *)
] <ocaml repr="classic">

type var_type <ocaml predef module="Ast"> = [
type var_type <ocaml predef module="Libdash.Ast"> = [
Normal
| Minus
| Plus
Expand All @@ -72,7 +72,7 @@ type var_type <ocaml predef module="Ast"> = [
| Length
] <ocaml repr="classic">

type case <ocaml predef module="Ast"> = {
type case <ocaml predef module="Libdash.Ast"> = {
cpattern : arg list;
cbody : t
}
Loading

0 comments on commit 7b4e2a5

Please sign in to comment.