Skip to content

Commit

Permalink
updating everything
Browse files Browse the repository at this point in the history
formating

formatting

spacing at end of comments
  • Loading branch information
Pat-Lafon committed Jun 1, 2020
1 parent e32bf25 commit 7823812
Show file tree
Hide file tree
Showing 29 changed files with 2,796 additions and 2,267 deletions.
1 change: 1 addition & 0 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile=compact
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.PHONY: all clean repl run build
BUILD_ARGS=$(if (command -v ocamlformat),@install @fmt --auto-promote,@install)

.PHONY: all clean repl run build test

all:
dune exec bin/gatorc.bc
dune exec bin/gatorc.ml

repl:
dune utop src
Expand All @@ -13,8 +15,13 @@ run:
cd examples/; SRC=$(src) yarn run start

build:
dune build bin/gatorc.bc
dune build && dune install
dune build ${BUILD_ARGS} bin/gatorc.ml
dune build ${BUILD_ARGS} && dune install

clean:
dune clean
rm test/**/*.out || true
# rm test-u/**/*.out || true

test:
python3 test.py
119 changes: 61 additions & 58 deletions bin/gatorc.ml
Original file line number Diff line number Diff line change
@@ -1,78 +1,81 @@
open Gatorl

let program : GatorAst.prog option ref = ref None

let program_file : string option ref = ref None
let set_program_file (arg : string) : unit =
match !program_file with
| None -> program_file := Some arg
| Some _ -> () (* Don't overwrite program_file *)

let set_program_file (arg : string) : unit =
match !program_file with None -> program_file := Some arg | Some _ -> ()

(* Don't overwrite program_file *)

let run_interp : bool ref = ref false
let emit_ts : bool ref = ref false
let debug_flag : bool ref = ref false
let pretty_printer : bool ref = ref false

let usage_msg = "Gator Help Center\n"

let spec_list : (Arg.key * Arg.spec * Arg.doc) list =
[
("-i", Arg.Set run_interp,
"Runs the given file with the gator interpreter (replaces standard output)");
("-t", Arg.Set emit_ts,
"Emits Typescript (replaces standard output)");
("-d", Arg.Set debug_flag,
"Enable debug output");
("-p", Arg.Set pretty_printer, "Enable pretty printing")
]
[ ( "-i"
, Arg.Set run_interp
, "Runs the given file with the gator interpreter (replaces standard \
output)" )
; ("-t", Arg.Set emit_ts, "Emits Typescript (replaces standard output)")
; ("-d", Arg.Set debug_flag, "Enable debug output")
; ("-p", Arg.Set pretty_printer, "Enable pretty printing") ]

let prog_path f = if not (String.contains f '/') then "" else
String.concat "/" (List.rev (List.tl (List.rev (String.split_on_char '/' f)))) ^ "/"
let prog_path f =
if not (String.contains f '/') then ""
else
String.concat "/"
(List.rev (List.tl (List.rev (String.split_on_char '/' f))))
^ "/"

let parse_prog f : GatorAst.prog =
let ch =
try open_in f
with Sys_error s -> failwith ("Cannot open file: " ^ s) in
let prog : GatorAst.prog =
let lexbuf = Lexing.from_channel ch in
try
Parser.main Lexer.read lexbuf
with
| _ ->
begin
close_in ch;
let pos = lexbuf.Lexing.lex_curr_p in
let tok = (Lexing.lexeme lexbuf) in
(* let line = pos.Lexing.pos_lnum in *)
let cnum = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
failwith ("Parsing error at token '" ^ tok ^ "', line "
^ (string_of_int pos.Lexing.pos_lnum) ^ ", column " ^ string_of_int cnum)
end in
close_in ch; prog
let ch =
try open_in f with Sys_error s -> failwith ("Cannot open file: " ^ s) in
let prog : GatorAst.prog =
let lexbuf = Lexing.from_channel ch in
try Parser.main Lexer.read lexbuf
with _ ->
close_in ch ;
let pos = lexbuf.Lexing.lex_curr_p in
let tok = Lexing.lexeme lexbuf in
(* let line = pos.Lexing.pos_lnum in *)
let cnum = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
failwith
( "Parsing error at token '" ^ tok ^ "', line "
^ string_of_int pos.Lexing.pos_lnum
^ ", column " ^ string_of_int cnum ) in
close_in ch ; prog

let rec search_progs path fs found : GatorAst.prog Assoc.context =
match fs with
| [] -> Assoc.empty
| f::t ->
let p = parse_prog (path ^ f) in
let to_search,found' = Check.search_prog p found in
Assoc.update f p (search_progs (path ^ (prog_path f)) to_search found')
let rec search_progs path fs found : GatorAst.prog Assoc.context =
match fs with
| [] -> Assoc.empty
| f :: t ->
let p = parse_prog (path ^ f) in
let to_search, found' = Check.search_prog p found in
Assoc.update f p (search_progs (path ^ prog_path f) to_search found')

let _ =
Arg.parse spec_list set_program_file usage_msg;
Util.debug := !debug_flag;
Util.pretty_printer := !pretty_printer;
match !program_file with
None -> print_string (Arg.usage_string spec_list usage_msg) | Some f ->
let prog = parse_prog f in
let progname = List.hd (String.split_on_char '.' (List.hd (List.rev (String.split_on_char '/' f)))) in
let fs,found = Check.search_prog prog [progname] in
let typedProg = Check.check_prog prog (search_progs (prog_path f) fs found) in
if !run_interp then Ops.eval_prog typedProg
else if !emit_ts then print_string (EmitTS.compile_program typedProg)
else if !pretty_printer then
Arg.parse spec_list set_program_file usage_msg ;
Util.debug := !debug_flag ;
Util.pretty_printer := !pretty_printer ;
match !program_file with
| None -> print_string (Arg.usage_string spec_list usage_msg)
| Some f ->
let prog = parse_prog f in
let progname =
List.hd
(String.split_on_char '.'
(List.hd (List.rev (String.split_on_char '/' f)))) in
let fs, found = Check.search_prog prog [progname] in
let typedProg =
Check.check_prog prog (search_progs (prog_path f) fs found) in
if !run_interp then Ops.eval_prog typedProg
else if !emit_ts then print_string (EmitTS.compile_program typedProg)
else if !pretty_printer then
let compiled_program = EmitGL.compile_program typedProg in
let r = Str.regexp ";\\s*}?" in
let r = Str.regexp ";\\s*}?" in
let result = Str.global_replace r "\\0\n" compiled_program in
print_string result
else print_string (EmitGL.compile_program typedProg)

else print_string (EmitGL.compile_program typedProg)
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(lang dune 1.0)
(using menhir 1.0)
(lang dune 2.0)
(using menhir 2.0)
2 changes: 1 addition & 1 deletion dune-workspace
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(lang dune 1.0)
(lang dune 2.0)
(profile default)
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "main.js",
"license": "MIT",
"scripts": {
"start": "parcel serve $SRC/index.html"
"start": "if test -f $SRC/main.lgl; then gatorc -t $SRC/main.lgl > $SRC/main.ts; fi; parcel serve $SRC/index.html"
},
"devDependencies": {
"parcel-bundler": "^1.9.7",
Expand Down
2 changes: 1 addition & 1 deletion examples/phong_ts/fragment.lgl
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ void main() {

scalar specular = pow(max(dot(normalize(-camPos), reflectDir), 0.), 32.);

vec4 gl_FragColor = vec4(ambient + diffuse * diffColor + specular * specColor, 1.0);
gl_FragColor = vec4(ambient + diffuse * diffColor + specular * specColor, 1.0);
}
3 changes: 0 additions & 3 deletions modd.conf

This file was deleted.

14 changes: 8 additions & 6 deletions src/assoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,30 @@ let union c1 c2 = c1 @ c2

(* Look up a variable by name and return the associated letue. *)
(* Raises Not_found if no binding *)
let lookup x c = try List.assoc x c with _ -> failwith ("Undefined association member: " ^ x)
let lookup x c =
try List.assoc x c with _ -> failwith ("Undefined association member: " ^ x)

(* Remove var from context *)
let remove x c = List.remove_assoc x c

(* Rebind var to value in context. *)
let update x v c = (x, v) :: (List.remove_assoc x c)
let update x v c = (x, v) :: List.remove_assoc x c

(* Produce bindings as an association list. *)
let bindings c = c
let keys c = List.map fst c
let values c = List.map snd c

(* Generates a context from a list *)
let create l = List.fold_left (fun acc (s, v) -> update s v acc) empty (List.rev l)
let create l =
List.fold_left (fun acc (s, v) -> update s v acc) empty (List.rev l)

(* Check var exists in context *)
let mem x c = List.mem_assoc x c

let map f c = List.map (fun (x, v) -> (x, f v)) c

let size c = List.length c

let to_string_sep f sep c = String.concat sep (List.map (fun (l, r) -> l ^ " : " ^ f r) c)
let to_string_sep f sep c =
String.concat sep (List.map (fun (l, r) -> l ^ " : " ^ f r) c)

let to_string f c = to_string_sep f ", " c
3 changes: 1 addition & 2 deletions src/assoc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ val create : (string * 'a) list -> 'a context
(* val state_to_string : ('a, 'a) context -> string *)

val map : ('a -> 'b) -> 'a context -> 'b context

val size : 'a context -> int

(* Gives a string resprentation of this association list, seperated by sep *)
val to_string_sep : ('a -> string) -> string -> 'a context -> string

(* String representation seperated by ", " *)
val to_string : ('a -> string) -> 'a context -> string
val to_string : ('a -> string) -> 'a context -> string
Loading

0 comments on commit 7823812

Please sign in to comment.