-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.pl
70 lines (56 loc) · 1.88 KB
/
interpreter.pl
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
% this is for test
% add_to_tree(Formatted):-
% consult('grammar.pl'),
% get_formatted(Formatted),
% consult('symbol_table.pl'),
% create_empty_table,
% initialize_function(Formatted).
% print(Formatted).
% giving Formatted list and ArgumentList to give out Result
% create_empty_table -> add source files' functions
% get Result
interpreter(Formatted, ArgumentList, Result) :-
consult('symbol_table.pl'),
create_empty_table,
initialize_function(Formatted),
eval_func('main', ArgumentList, Result), !.
eval_func(FunctionName, ArgumentList, Result) :-
% get functions information
get_FuncInfo(FunctionName, [ReturnType, ParameterList, CodeBody]),
% check type if matched
check_type_matched(ParameterList, ArgumentList),
expressionHandler(CodeBody, ReturnType, Result).
check_type_matched([], _):- !.
check_type_matched([[]], _):- !.
check_type_matched([ParaH|ParaT], [ArgH|ArgT]) :-
check_insert(ParaH, ArgH), check_type_matched(ParaT, ArgT).
check_insert([Type, Val], Arg) :-
check_type(Type, Arg), add_symbol(Val, Arg).
check_type(Type, Arg):-
(Type = 'bool', (Arg = 1; Arg = 0));
(Type = 'int' , integer(Arg)).
expressionHandler([[H|_], ExtraExpression], ReturnType, Result):-
valueHandler(H, ValueResult),
extraExpressionHandler(ExtraExpression, ExtraResult),
evaluate(ValueResult, ExtraResult, Result),
check_type(ReturnType, Result), remove_symbol(H).
valueHandler(H, ValueResult):-
get_symbol(H, ValueResult).
extraExpressionHandler(ExtraExpression, ExtraResult):-
ExtraResult = ExtraExpression.
evaluate(Value, [], Result) :-
Result is Value.
evaluate(Value1, [ '+', Value2 ], Result ) :-
(
integer(Value2)
->Result is Value1 + Value2;
atom_number(Value2,V2),
Result is Value1 + V2
).
evaluate(Value1, [ '*', Value2 ], Result ) :-
(
integer(Value2)
-> Result is Value1 * Value2;
atom_number(Value2,V2),
Result is Value1 * V2
).