-
Notifications
You must be signed in to change notification settings - Fork 0
/
toyprolog.ml
80 lines (75 loc) · 2.43 KB
/
toyprolog.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
open A6
let program =
let filename = Sys.argv.(1) in
let file = open_in filename in
let lexbuf = Lexing.from_channel file in
let rec createProgram acc =
let result = Parser.main Lexer.token lexbuf in
match result with x::xs->
(match x with
(Node(Sym "file_end",[]),[]) -> Program acc
| (a,b) ->
(createProgram (acc@[{head=a;body=b}]))
)
in
(* (printTable(createTable [])); *)
(createProgram [])
;;
let printSimpleTerm t = match t with
V(Var x)->Printf.printf "%s " x;flush stdout;
|Node (Sym x,[])->Printf.printf "%s " x;flush stdout;
;;
let _ =
Printf.printf "?-"; flush stdout;
let lexbuf = Lexing.from_channel stdin in
while true do
let result = Parser.main Lexer.token lexbuf in
let rec getGoals res acc= match res with
[]->acc
|(goal,[])::xs->
(* Printf.printf "debug \n"; flush stdout; *)
getGoals xs (acc@[goal])
(* |_-> Printf.printf "INVALID INPUT GOAL\n";Printf.printf "\n?-"; flush stdout; *)
in
let goals = getGoals result [] in
let tmpterm = Node(Sym "getvars",goals) in
let variables = vars tmpterm in
let newtbl = Hashtbl.create 10 in
let a = run (goals,[],program,program,newtbl,(false,newtbl),variables,[]) in
let rec finishGoal ans = match ans with
(boolean,table,[])->
(match boolean with
true -> Printf.printf "true.\n?-";flush stdout;
|false -> Printf.printf "false.\n?-";flush stdout;
)
|(boolean,table,[[]])->
(match boolean with
true -> Printf.printf "true.\n?-";flush stdout;
|false -> Printf.printf "false.\n?-";flush stdout;
)
|(boolean,table,x::xs)->
let rec printAnswers v a= match (v,a) with
([],[])->Printf.printf "\n";flush stdout;
|(v'::restv,a'::resta)->
printSimpleTerm v';
Printf.printf "= ";flush stdout;
printSimpleTerm a';
printAnswers restv resta
in
let rec foo lis acc = match lis with
[]->acc
|(x::xs)-> foo xs (acc@[V(x)])
in
let vvariables = foo variables [] in
printAnswers vvariables x;
let result' = Parser.main Lexer.token lexbuf in
(match result' with
[(Node(Sym "semicolon",[]),[])]->finishGoal (boolean,table,xs)
|_->finishGoal (boolean,table,x::xs)
)
in
finishGoal a
(* (true,a,b)->Printf.printf "true.\n?-";flush stdout; *)
(* |(false,a,b)->Printf.printf "false.\n?-";flush stdout; *)
done
;;