Skip to content

Commit

Permalink
fixed a cons bug and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spartango committed Oct 24, 2011
1 parent 0fb04db commit c10bf8e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 4 deletions.
13 changes: 11 additions & 2 deletions ps4/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# change to a different ocamlc if you prefer (e.g., ocamlopt)
COMPILER=ocamlc

all: clean cish scish
all: clean cish cish_stdin scish scish_stdin

mips:
$(COMPILER) -c word32.ml
Expand All @@ -23,6 +23,10 @@ cish: mips
$(COMPILER) -c cish.ml
$(COMPILER) -o ps4_cish cish_ast.cmo cish_parse.cmo cish_lex.cmo cish_eval.cmo word32.cmo mips.cmo utility.cmo cish_compile.cmo cish.cmo

cish_stdin: cish
$(COMPILER) -c cish_stdin.ml
$(COMPILER) -o ps4_cish_stdin cish_ast.cmo cish_parse.cmo cish_lex.cmo cish_eval.cmo word32.cmo mips.cmo utility.cmo cish_compile.cmo cish_stdin.cmo

scish: cish mips
$(COMPILER) -c cish_ast.ml
$(COMPILER) -c scish_ast.ml
Expand All @@ -37,6 +41,11 @@ scish: cish mips
$(COMPILER) -c scish.ml
$(COMPILER) -o ps4_scish cish_ast.cmo cish_parse.cmo cish_lex.cmo scish_ast.cmo scish_parse.cmo scish_lex.cmo scish_eval.cmo utility.cmo word32.cmo mips.cmo environment.cmo scish_compile.cmo scish.cmo

scish_stdin: scish
$(COMPILER) -c scish_stdin.ml
$(COMPILER) -o ps4_scish_stdin cish_ast.cmo cish_parse.cmo cish_lex.cmo scish_ast.cmo scish_parse.cmo scish_lex.cmo scish_eval.cmo utility.cmo word32.cmo mips.cmo environment.cmo scish_compile.cmo scish_stdin.cmo


clean:
rm -rf *.cmo *.cmi ps4_cish ps4_scish cish_parse.ml cish_parse.mli cish_lex.ml scish_parse.ml scish_parse.mli scish_lex.ml
rm -rf *.cmo *.cmi ps4_cish ps4_scish ps4_cish_stdin ps4_scish_stdin cish_parse.ml cish_parse.mli cish_lex.ml scish_parse.ml scish_parse.mli scish_lex.ml

27 changes: 27 additions & 0 deletions ps4/cish_stdin.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
open Cish_ast
open Cish_eval

(* This magic is used to glue the generated lexer and parser together.
* Expect one command-line argument, a file to parse.
* You do not need to understand this interaction with the system. *)
let parse_file() =
let argv = Sys.argv in
let _ =
if Array.length argv != 2
then (prerr_string ("usage: " ^ argv.(0) ^ " [file-to-parse]\n");
exit 1) in
let ch = open_in argv.(1) in
Cish_parse.program Cish_lex.lexer (Lexing.from_channel ch)

let parse_stdin() =
Cish_parse.program Cish_lex.lexer (Lexing.from_channel stdin)

let compile_prog prog =
Cish_compile.result2string (Cish_compile.compile prog )

(* Reads in cish code and evaluates it *)
let _ =
let prog = parse_stdin() in
let ans = eval prog in
print_string ("answer = "^(val2string ans)^"\n")
(* print_string (compile_prog prog) *)
5 changes: 3 additions & 2 deletions ps4/scish_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ let rec compile_exp_r ( t_expr : Scish_ast.exp )
let store_stmt1 = cish_stmt_from_str ("*(" ^ tuple_address ^ ") = " ^ result_name ^ ";") in
(* Compile second expression, placing result in result *)
let (f_list2, scope2, stmt2) = compile_exp_r (List.nth exps 1) f_list1 scope1 in
(* Store result in second word at tupele's address *)
(* Store result in second word at tuple's address *)
let store_stmt2 = cish_stmt_from_str ("*(" ^ tuple_address ^ "+4) = " ^ result_name ^ ";") in
(f_list2, scope2, (init_var tuple_address (seqs [init_stmt; stmt1; store_stmt1; stmt2; store_stmt2])))
let mv_result = cish_stmt_from_str (result_name^" = "^tuple_address^";") in
(f_list2, scope2, (init_var tuple_address (seqs [init_stmt; stmt1; store_stmt1; stmt2; store_stmt2; mv_result])))
(* create a pair *)
| Fst -> access_mem (List.hd exps) f_list scope 0
| Snd -> access_mem (List.hd exps) f_list scope 4
Expand Down
31 changes: 31 additions & 0 deletions ps4/scish_stdin.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
open Scish_ast
open Scish_eval

(* This magic is used to glue the generated lexer and parser together.
* Expect one command-line argument, a file to parse.
* You do not need to understand this interaction with the system. *)
let parse_file () =
let argv = Sys.argv in
let _ =
if Array.length argv != 2
then (prerr_string ("usage: " ^ argv.(0) ^ " [file-to-parse]\n");
exit 1) in
let ch = open_in argv.(1) in
Scish_parse.program Scish_lex.lexer (Lexing.from_channel ch)

let parse_stdin () =
Scish_parse.program Scish_lex.lexer (Lexing.from_channel stdin)

let compile_prog prog = Scish_compile.compile_exp prog

let run_prog prog = Scish_eval.run prog

let dump p = print_string (Cish_ast.prog2string p)

let _ =
let prog = parse_stdin() in
(*
let _ = print_string ("answer = "^(string_of_int ans)^"\n") in
*)

dump (compile_prog prog)
1 change: 1 addition & 0 deletions ps4/test/if.scish
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
((lambda (x) (if (< x 5) (* x 5) (* x 2))) 10)
1 change: 1 addition & 0 deletions ps4/test/sample_add.scish
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(+ 5 4)
1 change: 1 addition & 0 deletions ps4/test/sample_cons.scish
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(fst (cons 5 4))
1 change: 1 addition & 0 deletions ps4/test/shadow.scish
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(((lambda (x) (lambda (x) (+ x x))) 42) 3)
1 change: 1 addition & 0 deletions ps4/test/square.scish
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
((lambda (x) (* x x)) 5)

0 comments on commit c10bf8e

Please sign in to comment.