Skip to content

Commit b483785

Browse files
committed
implemented the primitive \include
1 parent 60291b9 commit b483785

File tree

5 files changed

+80
-44
lines changed

5 files changed

+80
-44
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bin/macrodown.exe : types.ml mcdlexer.mli mcdlexer.ml stacklist.mli stacklist.ml mcdparser.mli mcdparser.ml mcdabs.mli mcdabs.ml assoclist.mli assoclist.ml mcdout.mli mcdout.ml mcdsemantics.mli mcdsemantics.ml mcdmain.ml
2-
ocamlc.opt -o bin/macrodown.exe types.ml mcdlexer.mli mcdlexer.ml stacklist.mli stacklist.ml mcdparser.mli mcdparser.ml mcdabs.mli mcdabs.ml assoclist.mli assoclist.ml mcdout.mli mcdout.ml mcdsemantics.mli mcdsemantics.ml mcdmain.ml
1+
bin/macrodown.exe : types.ml files.ml mcdlexer.mli mcdlexer.ml stacklist.mli stacklist.ml mcdparser.mli mcdparser.ml mcdabs.mli mcdabs.ml assoclist.mli assoclist.ml mcdout.mli mcdout.ml mcdsemantics.mli mcdsemantics.ml mcdmain.ml
2+
ocamlc -o bin/macrodown.exe types.ml files.ml mcdlexer.mli mcdlexer.ml stacklist.mli stacklist.ml mcdparser.mli mcdparser.ml mcdabs.mli mcdabs.ml assoclist.mli assoclist.ml mcdout.mli mcdout.ml mcdsemantics.mli mcdsemantics.ml mcdmain.ml
33

files.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
open Types
2+
3+
(* string -> string *)
4+
let string_of_file_in file_name_in =
5+
let str_in = ref "" in
6+
let chnl_in = open_in file_name_in in
7+
let cat_sub () =
8+
while true do
9+
str_in := !str_in ^ (String.make 1 (input_char chnl_in))
10+
done
11+
in
12+
try
13+
( cat_sub () ; "" )
14+
with
15+
End_of_file -> ( close_in chnl_in ; !str_in )
16+
17+
let rec string_of_file_in_list file_name_in_list =
18+
match file_name_in_list with
19+
[] -> ""
20+
| head :: tail ->
21+
let str_in = string_of_file_in head in
22+
str_in ^ (string_of_file_in_list tail)
23+
24+
(* string -> string -> unit *)
25+
let file_out_of_string file_name_out content_out =
26+
let chnl_out = open_out file_name_out in
27+
output_string chnl_out content_out ;
28+
close_out chnl_out

mcdmain.ml

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,18 @@ open Types
33
let report_error errmsg =
44
print_string ("[ERROR IN MAIN] " ^ errmsg) ; print_newline ()
55

6-
(* string -> string *)
7-
let string_of_file_in file_name_in =
8-
let str_in = ref "" in
9-
let chnl_in = open_in file_name_in in
10-
let cat_sub () =
11-
while true do
12-
str_in := !str_in ^ (String.make 1 (input_char chnl_in))
13-
done
14-
in
15-
try
16-
( cat_sub () ; "" )
17-
with
18-
End_of_file -> ( close_in chnl_in ; !str_in )
19-
20-
let rec string_of_file_in_list file_name_in_list =
21-
match file_name_in_list with
22-
[] -> ""
23-
| head :: tail ->
24-
let str_in =
25-
try string_of_file_in head with
26-
Sys_error(s) -> report_error ("System error: " ^ s)
27-
in
28-
str_in ^ (string_of_file_in_list tail)
29-
30-
(* string -> string -> unit *)
31-
let file_out_of_string file_name_out content_out =
32-
let chnl_out = open_out file_name_out in
33-
output_string chnl_out content_out ;
34-
close_out chnl_out
6+
let report_detail dtlmsg =
7+
print_string dtlmsg ; print_newline ()
358

369
let main file_name_in_list file_name_out =
3710

38-
let content_in = (string_of_file_in_list file_name_in_list) in
11+
let content_in = (
12+
try Files.string_of_file_in_list file_name_in_list with
13+
Sys_error(s) -> (
14+
report_error ("System error - " ^ s) ;
15+
""
16+
)
17+
) in
3918
let lexed = Mcdlexer.mcdlex content_in in
4019
let parsed = Mcdparser.mcdparser lexed in
4120
let absed = Mcdabs.concrete_to_abstract parsed in
@@ -45,12 +24,12 @@ let main file_name_in_list file_name_out =
4524
IllegalOut -> ""
4625
in
4726
match content_out with
48-
"" -> ( print_string "No output." ; print_newline () )
27+
"" -> report_detail "No output."
4928
| _ -> (
5029
try
51-
file_out_of_string file_name_out content_out
30+
Files.file_out_of_string file_name_out content_out
5231
with
53-
Sys_error(s) -> report_error ("System error: " ^ s)
32+
Sys_error(s) -> report_error ("System error - " ^ s)
5433
)
5534

5635
let rec concat_list lsta lstb =
@@ -59,14 +38,17 @@ let rec concat_list lsta lstb =
5938
| head :: tail -> head :: (concat_list tail lstb)
6039

6140
let rec see_argv num file_name_in_list file_name_out =
62-
if num == Array.length Sys.argv then
41+
if num == Array.length Sys.argv then (
42+
report_detail ("[output] " ^ file_name_out) ;
6343
main file_name_in_list file_name_out
64-
else (
65-
if (compare Sys.argv.(num) "-o") == 0 then (
66-
print_string ("[output] " ^ Sys.argv.(num + 1)) ; print_newline () ;
67-
see_argv (num + 2) file_name_in_list (Sys.argv.(num + 1))
68-
) else (
69-
print_string ("[input] " ^ Sys.argv.(num)) ; print_newline () ;
44+
) else (
45+
if (compare Sys.argv.(num) "-o") == 0 then
46+
try (
47+
see_argv (num + 2) file_name_in_list (Sys.argv.(num + 1))
48+
) with
49+
Invalid_argument(s) -> report_error "missing file name after '-o' option"
50+
else (
51+
report_detail ("[input] " ^ Sys.argv.(num)) ;
7052
see_argv (num + 1) (concat_list file_name_in_list [Sys.argv.(num)]) file_name_out
7153
)
7254
)

mcdsemantics.ml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
let loc_replace : macro_location = ref DummyFunc in
3737
let loc_prefix : macro_location = ref DummyFunc in
3838
let loc_postfix : macro_location = ref DummyFunc in
39+
let loc_include : macro_location = ref DummyFunc in
3940
let menv_main : macro_environment ref = ref Assoclist.empty in
4041
let venv_main : var_environment ref = ref Assoclist.empty in
4142
venv_main := (Assoclist.add "~indent" loc_indent !venv_main) ;
@@ -46,6 +47,7 @@
4647
menv_main := (Assoclist.add "\\replace" loc_replace !menv_main) ;
4748
menv_main := (Assoclist.add "\\prefix" loc_prefix !menv_main) ;
4849
menv_main := (Assoclist.add "\\postfix" loc_postfix !menv_main) ;
50+
menv_main := (Assoclist.add "\\include" loc_include !menv_main) ;
4951
loc_deepen := Func([], DeepenIndent, EmptyAbsBlock, !menv_main, !venv_main) ;
5052
loc_shallow := Func([], ShallowIndent, EmptyAbsBlock, !menv_main, !venv_main) ;
5153
loc_ifempty := Func(["~subj"; "~tru"; "~fls"],
@@ -68,6 +70,10 @@
6870
PrimitivePostfix(ContentOf("~name"), ContentOf("~postfix")),
6971
EmptyAbsBlock, !menv_main, !venv_main
7072
) ;
73+
loc_include := Func(["~filename"],
74+
PrimitiveInclude(ContentOf("~filename")),
75+
EmptyAbsBlock, !menv_main, !venv_main
76+
) ;
7177
interpret menv_main venv_main abstr
7278

7379
(* (macro_environment ref) -> (var_environment ref) -> abstract_tree -> abstract_tree *)
@@ -144,6 +150,25 @@
144150
EmptyAbsBlock
145151
)
146152

153+
| PrimitiveInclude(abstr_file_name) -> (
154+
print_process "$PrimitiveInclude" ;
155+
let str_file_name = (
156+
try Mcdout.mcdout (interpret menv venv abstr_file_name) with
157+
IllegalOut -> ( report_error "illegal argument of \\include" ; "" )
158+
) in
159+
let str_content = (
160+
try Files.string_of_file_in str_file_name with
161+
Sys_error(s) -> (
162+
report_error ("System error at \\include - " ^ s) ;
163+
""
164+
)
165+
) in
166+
let lexed_content = Mcdlexer.mcdlex str_content in
167+
let parsed_content = Mcdparser.mcdparser lexed_content in
168+
let absed_content = Mcdabs.concrete_to_abstract parsed_content in
169+
interpret menv venv absed_content
170+
)
171+
147172
| DeepenIndent -> (
148173
print_process "$DeepenIndent" ;
149174
(
@@ -258,7 +283,7 @@
258283
ref (Assoclist.add_list var_list loc_list cont_venv_f)
259284
with
260285
IncorrespondenceOfLength -> (
261-
report_error "wrong number of arguments" ;
286+
report_error ("wrong number of arguments for '" ^ f ^ "'") ;
262287
ref cont_venv_f
263288
)
264289
in
@@ -291,7 +316,7 @@
291316
ref (Assoclist.add "@id" loc_id (Assoclist.add_list var_list loc_list cont_venv_f))
292317
with
293318
IncorrespondenceOfLength -> (
294-
report_error "wrong number of argument" ;
319+
report_error ("wrong number of arguments for '" ^ f ^ "'") ;
295320
ref cont_venv_f
296321
)
297322
in

types.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type abstract_tree = EmptyAbsBlock
4343
| PrimitiveReplace of abstract_tree * abstract_tree * abstract_tree
4444
| PrimitivePrefix of abstract_tree * abstract_tree
4545
| PrimitivePostfix of abstract_tree * abstract_tree
46+
| PrimitiveInclude of abstract_tree
4647
| LiteralBlock of literal_name * abstract_tree
4748
| OutputOfLiteral of letter
4849

0 commit comments

Comments
 (0)