-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser-ds.lisp
188 lines (144 loc) · 6.25 KB
/
parser-ds.lisp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;;;; -*- Mode: LISP, Syntax: COMMON-LISP, Package: MONTY.PARSER -*-
;;;;
;;;; Author: Gautham Ganapathy (gautham@lisphacker.org)
;;;;
;;;; TPTP language parser data structures.
(in-package :cl-tptp-parser.parser)
(defmacro extend-class (class base-class documentation)
(if (listp base-class)
`(defclass ,class ,base-class
()
(:documentation ,documentation))
`(defclass ,class (,base-class)
()
(:documentation ,documentation))))
(defclass ast-node ()
((token :initarg :token
:initform (error ":token must be specified")
:reader ast-token))
(:documentation "Base AST node"))
(defclass ast-named ()
((name :initarg :name
:initform (error ":name must be specified")
:reader ast-name))
(:documentation "Base named AST node"))
(defclass ast-argumented ()
((arguments :initarg :arguments
:initform (error ":arguments must be specified")
:reader arguments))
(:documentation "Node with arguments"))
(defmethod print-object ((node ast-named) stream)
(format stream "~a" (ast-name node)))
(extend-class term ast-node "Base term")
(extend-class function-term term "Function term")
(extend-class var (term ast-named) "Variable")
(extend-class conditional-term term "Conditional term")
(extend-class let-term term "Let term")
(extend-class plain-term function-term "Plain term")
(defclass constant (plain-term)
((value :initarg :value
:initform (error ":value must be specified")
:reader value))
(:documentation "Constant"))
(defmethod print-object ((constant constant) stream)
(format stream "~a" (value constant)))
(defclass function-call (plain-term ast-argumented)
((functor :initarg :functor
:initform (error ":functor must be specified")
:reader functor))
(:documentation "Plain term with arguments"))
(defmethod print-object ((term function-call) stream)
(format stream "(~a~{ ~a~})" (functor term) (arguments term)))
(extend-class defined-term function-term "Defined term")
(extend-class system-term function-term "System term")
;;(extend-class constant plain-term "Constant")
(extend-class functor constant "Functor")
;;(extend-class predicate (ast-node ast-named) "Predicate")
(extend-class defined-predicate function-call "Defined predicate")
(defmethod print-object ((term function-call) stream)
(format stream "(~a~{ ~a~})" (functor term) (arguments term)))
(extend-class proposition ast-named-node "Proposition")
(extend-class fof-formula ast-node "FOF formula")
(extend-class fof-logic-formula fof-formula "FOF logic formula")
(extend-class fof-sequent fof-formula "FOF sequent")
(defclass fof-binary-formula (fof-logic-formula)
((binop :initarg :binop
:initform (error ":binop must be specified")
:reader binop)
(left :initarg :left
:initform (error ":left must be specified")
:reader left)
(right :initarg :right
:initform (error ":right must be specified")
:reader right))
(:documentation "FOF binary formula"))
(defmethod print-object ((formula fof-logic-formula) stream)
(format stream "(~a ~a ~a)" (binop formula) (left formula) (right formula)))
(extend-class fof-unitary-formula fof-logic-formula "FOF unitary formula")
(defclass fof-quantified-formula (fof-unitary-formula)
((quantifier :initarg :quantifier
:initform (error ":quantifier must be specified (ALL or ANY)")
:reader quantifier)
(variables :initarg :variables
:initform (error ":variables must be specified")
:reader variables)
(formula :initarg :formula
:initform (error ":formula must be specified")
:reader formula))
(:documentation "FOF quantified formula"))
(defmethod print-object ((qf fof-quantified-formula) stream)
(format stream "(FOR~a ~s ~s)" (quantifier qf) (variables qf) (formula qf)))
(defclass fof-unary-formula (fof-unitary-formula)
((op :initarg :op
:initform (error ":op must be specified")
:reader op)
(formula :initarg :formula
:initform (error ":formula must be specified")
:reader formula))
(:documentation "FOF unary formula"))
(defmethod print-object ((uf fof-unary-formula) stream)
(format stream "(~a ~a)" (op uf) (formula uf)))
(defclass plain-atomic-formula (ast-node ast-argumented)
((predicate :initarg :predicate
:initform (error ":predicate must be specified")
:reader predicate))
(:documentation "Plain atomic formula"))
(defmethod print-object ((formula plain-atomic-formula) stream)
(if (null (arguments formula))
(format stream "(~a)" (predicate formula))
(format stream "(~a ~a)" (predicate formula) (arguments formula))))
(extend-class statement ast-node "Statement")
(defclass include (statement)
((file :initarg :file
:initform (error ":file must be specified")
:reader file))
(:documentation "Include directive"))
(defmethod print-object ((node include) stream)
(format stream "(INCLUDE ~a)" (file node)))
(extend-class annotated statement "Annotated formula")
(defclass fof-statement (annotated)
((name :initarg :name
:initform (error ":name must be specified")
:reader name)
(role :initarg :role
:initform (error ":role must be specified")
:reader role)
(formula :initarg :formula
:initform (error ":formula must be specified")
:reader formula))
(:documentation "FOF statement"))
(defmethod print-object ((statement fof-statement) stream)
(format stream "(FOF ~a ~a ~a)" (name statement) (role statement) (formula statement)))
(defclass file (ast-node)
((statements :initarg :statements
:initform (error ":statements must be specified")
:reader statements))
(:documentation "File"))
(defmethod print-object ((file file) stream)
(loop for statement in (statements file) do
(format stream "~a~%" statement)))
(extend-class boolean-constant (constant) "Boolean value")
(defun create-boolean-constant (a)
(make-instance 'boolean-constant
:value (token-terminal a)
:token a))