-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.pro
More file actions
148 lines (111 loc) · 3.18 KB
/
example.pro
File metadata and controls
148 lines (111 loc) · 3.18 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
:- use_module(library(clpfd)).
entity(todo, [
[id, int32]
, [text, varchar(1024)]
, [what, int32, nullable]
]).
main(Class, Ast) :- java_class_ast(Class, Ast).
:- use_module('java_syntax.pro').
test_java_syntax(Tree) :- true
, string_codes("1 + 2", Source)
, phrase(expression(Tree), Source, [])
.
bit(0).
bit(1).
bfs([]).
bfs([H|T]) :- bfs(T), bit(H).
dfs([]).
dfs([H|T]) :- bit(H), dfs(T).
/*
Prolog can handle left-recursive grammar if we ground terms as much as possible before recursing.
*/
% S ::= <empty> | S a
word([]).
word(A) :- append(B, [a], A), word(B).
% S ::= <empty> | S T S
% T ::= a
word2([]).
word2(S) :- t(T), append([S0,T,S1], S), word2(S0), word2(S1).
t([a]).
/*
Expression parsing with precedence/order.
U = unparsed
P = parsed
*/
% Read one character code from the head.
code(P, [P|U], [P], U).
digit(M, U0, P0, U1) :- code(C, U0, P0, U1), code_type(C, digit), M #= C - 0'0.
number(M, U0, P2, U2) :-
append(P2, U2, U0),
append(P0, P1, P2),
digit(MH, U1, P1, U2),
number(MT, U0, P0, U1),
M is 10 * MT + MH.
number(M, U0, P0, U1) :- digit(M, U0, P0, U1).
name(M, U0, P2, U2) :-
name_head(MH, U0, P0, U1),
append(P0, P1, P2),
name_tail(MT, U1, P1, U2),
atom_codes(M, [MH|MT]).
name_head(M, U0, P0, U1) :- code(M, U0, P0, U1), code_type(M, csymf).
name_tail([MH|MT], U0, P2, U2) :-
code(MH, U0, P0, U1),
code_type(MH, csym),
append(P0, P1, P2),
name_tail(MT, U1, P1, U2).
name_tail([], U0, [], U0).
/*
An order is an integer.
Expression with smaller order is evaluated earlier.
Problem:
Expression with equal order should be parsed from left to right, but this isn't implemented.
*/
expression(200, name(M), U0, P0, U1) :- name(M, U0, P0, U1).
expression(200, number(M), U0, P0, U1) :- number(M, U0, P0, U1).
expression(Order, paren(M), U0, P3, U3) :-
Order #= 100,
append(P3, U3, U0),
append([P0, P1, P2], P3),
code(0'(, U0, P0, U1),
code(0'), U2, P2, U3),
OP #>= Order,
expression(OP, M, U1, P1, U2).
expression(Order, infix(P1, ML, MR), U0, P3, U3) :-
Order #= 400,
append(P3, U3, U0),
append([P0, P1, P2], P3),
code(0'+, U1, P1, U2),
OL #=< Order,
OR #=< Order,
expression(OL, ML, U0, P0, U1),
expression(OR, MR, U2, P2, U3).
expression(Order, infix(P1, ML, MR), U0, P3, U3) :-
Order #= 400,
append(P3, U3, U0),
append([P0, P1, P2], P3),
code(0'-, U1, P1, U2),
OL #=< Order,
OR #=< Order,
expression(OL, ML, U0, P0, U1),
expression(OR, MR, U2, P2, U3).
expression(Order, infix(P1, ML, MR), U0, P3, U3) :-
Order #= 300,
append(P3, U3, U0),
append([P0, P1, P2], P3),
code(0'*, U1, P1, U2),
OL #=< Order,
OR #=< Order,
expression(OL, ML, U0, P0, U1),
expression(OR, MR, U2, P2, U3).
:- use_module('account_tax.pro').
example_tax(Gross_income) :-
taxation_subject(Taxation, Subject),
taxation_year(Taxation, 2018),
subject_has_npwp(Subject, true),
subject_gross_income(Subject, Income),
income_year(Income, 2018),
income_amount(Income, Gross_income),
taxation_report(Taxation, Report),
report_format(Report),
!.
:- use_module('expression.pro').