| 1 |
;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved. |
| 4 |
;;; |
| 5 |
;;; Redistribution and use in source and binary forms, with or without |
| 6 |
;;; modification, are permitted provided that the following conditions |
| 7 |
;;; are met: |
| 8 |
;;; |
| 9 |
;;; * Redistributions of source code must retain the above copyright |
| 10 |
;;; notice, this list of conditions and the following disclaimer. |
| 11 |
;;; |
| 12 |
;;; * Redistributions in binary form must reproduce the above |
| 13 |
;;; copyright notice, this list of conditions and the following |
| 14 |
;;; disclaimer in the documentation and/or other materials |
| 15 |
;;; provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
| 18 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
;;; |
| 29 |
(in-package :chaos) |
| 30 |
#|============================================================================== |
| 31 |
System: CHAOS |
| 32 |
Module: thstuff |
| 33 |
File: parse-apply.lisp |
| 34 |
==============================================================================|# |
| 35 |
#-:chaos-debug |
| 36 |
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0))) |
| 37 |
#+:chaos-debug |
| 38 |
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3))) |
| 39 |
|
| 40 |
;;; === APPLY COMMAND FAMILY PARSERS |
| 41 |
;;; ---------------------------------------------------------------------- |
| 42 |
|
| 43 |
;;; ***** |
| 44 |
;;; START |
| 45 |
;;; ***** |
| 46 |
;;; - Syntax ----------------------------------------------------------------- |
| 47 |
;;; <Start> ::= start <term> . |
| 48 |
;;; -------------------------------------------------------------------------- |
| 49 |
|
| 50 |
;;; Chaos form |
| 51 |
|
| 52 |
(defterm start (%script) |
| 53 |
:visible (target) ; target term |
| 54 |
:eval eval-start-command |
| 55 |
) |
| 56 |
|
| 57 |
;;; parser |
| 58 |
|
| 59 |
(defun parse-start-command (e) |
| 60 |
(%start* (cadr e))) |
| 61 |
|
| 62 |
;;; ************ |
| 63 |
;;; APPLY FAMILY |
| 64 |
;;; ************ |
| 65 |
|
| 66 |
;;; apply command of Chaos is the subset(but not a restriction) of OBJ(ver2.0) + |
| 67 |
;;; some extensions. |
| 68 |
;;;----------------------------------------------------------------------------- |
| 69 |
;;; (1) don't have <retract>. no problem, we have operator with error sorts as |
| 70 |
;;; their rank. |
| 71 |
;;; (2) allow rule labels with digit chars at its head. |
| 72 |
;;; only labels consisting of all digit chars are considered as rule |
| 73 |
;;; numbers. |
| 74 |
;;; (3) explicit forward direction specifier `+' allowing module with `-' |
| 75 |
;;; at head of their names and vise versa. |
| 76 |
;;; aboves are not so important. |
| 77 |
;;; (4) break down features of `apply' into more elementary ones. |
| 78 |
;;; `apply' of OBJ seems trying "do everything at once", or "to be a |
| 79 |
;;; powerful tool applicable to almost everything" like swiss army |
| 80 |
;;; knife. must agree(shouldn't we?), but this makes its syntax rather |
| 81 |
;;; heavy and prevents us from doing some simple tasks in a handy manner. |
| 82 |
;;; Chaos supports full apply command of OBJ (without <retract> of course) |
| 83 |
;;; in itself, and also provides some new command "do a simple thing at one |
| 84 |
;;; time", whose produced informations can be used in the later use of |
| 85 |
;;; apply command. |
| 86 |
;;; also have some extensions to OBJ's apply. |
| 87 |
;;; hopefully, these would be useful for those who uses Chaos system |
| 88 |
;;; interactively. |
| 89 |
;;; |
| 90 |
;;; === NEW COMMANDS & EXTENSIONS to APPLY of OBJ ========================= |
| 91 |
;;; *NOTE* ---------------------------------------------------------------- |
| 92 |
;;; like `apply', some new commands assume a term which is the result of |
| 93 |
;;; `start' or `parse' (even of `reduce') commands. we refer this term as |
| 94 |
;;; "$$term" in the sequel. a specific subterm of $$term may be also |
| 95 |
;;; refered and/or set by some commands; this subterm will be denoted by |
| 96 |
;;; "$$subterm". initially, that is, just after `start', `parse', or |
| 97 |
;;; `reduce', $$subterm is identical to $$term(the whole). |
| 98 |
;;; ----------------------------------------------------------------------- |
| 99 |
;;; (4-1) subterm selection : |
| 100 |
;;; [a] choose command |
| 101 |
;;; - SYNTAX -------------------------------------------------------- |
| 102 |
;;; <SubtermSelection> ::= choose <Selectors> |
| 103 |
;;; ----------------------------------------------------------------- |
| 104 |
;;; this selects a subterm specified by <Selectors>. |
| 105 |
;;; <Selectors> is just the same to `apply' command. here is its |
| 106 |
;;; summary (selector "subterm" is added, see `* NOTE (1)' below): |
| 107 |
;;; <Selectors> ::= { top | term | subterm } | |
| 108 |
;;; <Selector> { of <Selector>}* |
| 109 |
;;; <Selector> ::= <OccurSelection> | |
| 110 |
;;; <SubseqSelection> | <SubsetSelection> |
| 111 |
;;; <OccurSelection> ::= "(" <Nat>+ ")" |
| 112 |
;;; <SubseqSelection> ::= "[" <Nat> .. <Nat> "]" | "[" <Nat> "]" |
| 113 |
;;; <SubsetSelection> ::= "{" <Nat> [, <Nat>]* "}" |
| 114 |
;;; - BEHAVIOUR ----------------------------------------------------- |
| 115 |
;;; the meaning of <Slectors> is the same as OBJ. |
| 116 |
;;; the target term of `choose' is $$subterm(see *NOTE* above), and |
| 117 |
;;; it will be reset to the selected subterm. thus, succeeding |
| 118 |
;;; applications of choose command selects a "subterm of subterm of |
| 119 |
;;; ... subterm of $$subterm". |
| 120 |
;;; one can archive the same effect at onece by a <Slectors> form |
| 121 |
;;; like "<Selector> of <Selector> of ...", but the former way |
| 122 |
;;; is useful for selecting a subterm of a complex(large) term |
| 123 |
;;; incrementaly being with some checks that a subterm is correctly |
| 124 |
;;; selected at some steps of choices. |
| 125 |
;;; if sole "top" or "term" is given as <Selectors>, original whole |
| 126 |
;;; term ($$term) is selected as subterm and is set to $$subterm. |
| 127 |
;;; this is a handy way to cancell of preceding selections. but |
| 128 |
;;; of cource, if one `applied' some rules to selected subterm, $$term |
| 129 |
;;; would be changed, and then the $$term can be quite different from |
| 130 |
;;; its original. |
| 131 |
;;; ----------------------------------------------------------------- |
| 132 |
;;; * NOTE: (1) for combined use of `choose' and `apply', the |
| 133 |
;;; <Selector> of apply command is extended. it now accepts |
| 134 |
;;; "subterm" as a <Selector> which refers to a subterm |
| 135 |
;;; selected by the last `choose' command. |
| 136 |
;;; (2) once some rewrite rule is successfully applied |
| 137 |
;;; to somewhere in $$term, $$subterm is reset to $$term. |
| 138 |
;;; |
| 139 |
;;; [b] show command extension. |
| 140 |
;;; - SYNTAX ------------------------------------------------------- |
| 141 |
;;; <ShowSubterm> ::= show subterm [ tree ] |
| 142 |
;;; ---------------------------------------------------------------- |
| 143 |
;;; we extended `show' command so that it can print out the subterm |
| 144 |
;;; chosen by the last `choose' command ($$term will be printed if |
| 145 |
;;; there has been any selected proper subterm). |
| 146 |
;;; this can be used for cirtifying the subterm selected by choose |
| 147 |
;;; command. if optional "tree" is given, also prints out $$subterm |
| 148 |
;;; in a tree form. |
| 149 |
;;; *NOTE* ordinal "show term" will pirnt out $$term. for tree form |
| 150 |
;;; print of $$term, "show tree" (specific to Chaos) can be |
| 151 |
;;; used. also prepared "show term tree" though. |
| 152 |
;;; (4-2) listing out applicable rewrite rules. |
| 153 |
;;; [a] find command |
| 154 |
;;; - SYNTAX ------------------------------------------------------- |
| 155 |
;;; <FindRule> ::= find { rule | +rule | -rule } |
| 156 |
;;; ---------------------------------------------------------------- |
| 157 |
;;; - BEHAVIOUR ---------------------------------------------------- |
| 158 |
;;; print-outs all axioms which can be successfully applied |
| 159 |
;;; to $$subterm. the direction as a rewrite rule is specified by |
| 160 |
;;; the argument "rule", "+rule" or "-rule". |
| 161 |
;;; given "rule", `find' searches through the set of all axioms |
| 162 |
;;; of the current context(current module) including imported axioms |
| 163 |
;;; of submodules and system generated ones for associative and/or |
| 164 |
;;; commutative operators, and selects ones whose left OR right hand |
| 165 |
;;; side matches to $$subterm. "+rule" try matches only to left-hand |
| 166 |
;;; side of the axioms (forward direction), and "-rule" is used for |
| 167 |
;;; specifying backward direction. |
| 168 |
;;; ---------------------------------------------------------------- |
| 169 |
;;; printing form is arranged to be giving useful informations for |
| 170 |
;;; later `apply' commands. each printed axioms has the form |
| 171 |
;;; ------------------------------------------------------ |
| 172 |
;;; (<Nat>) {<RuleSpec>}+ : LHS {-->|<--} RHS [SUBST-INFO] |
| 173 |
;;; ------------------------------------------------------ |
| 174 |
;;; where <Nat> is a natural number assigned by find command and |
| 175 |
;;; can be directly used as rule specifier of our extended apply |
| 176 |
;;; command (see (4-3) below for the extension.) |
| 177 |
;;; <RuleSpec> is just the same to rule specifier of OBJ's apply |
| 178 |
;;; command, which is in a form of <ModId>.<Nat> or <ModId>.<Id>. |
| 179 |
;;; find always prints former form and if an axioms has a label, |
| 180 |
;;; it will also be printed. of course these also can be used for |
| 181 |
;;; rule specifier of apply command. following a separate mark `:' |
| 182 |
;;; axiom itself is printed. between left and right hand side side, |
| 183 |
;;; --> or <-- sign is printed showing the direction as a rewrite |
| 184 |
;;; rule. an axiom can have match on its both side to $$subterm |
| 185 |
;;; (this can happen as giving "rule"), the same axiom is printed 2 |
| 186 |
;;; times with different <Nat>, <RuleSpec>(backward direction has |
| 187 |
;;; preceding "-") and direction sign between LHS and RHS. optional |
| 188 |
;;; SUBST-INFO is printed when the RHS(LHS) has extra variable than |
| 189 |
;;; LHS(RHS) in the case of forward(backward) direction. |
| 190 |
;;; SUBST-INFO has the form |
| 191 |
;;; ------------------------------------------------------------- |
| 192 |
;;; with <VarId> = <PsudeVar>:<SortId> {, <VarId> = |
| 193 |
;;; <PsudeVar>:<SortId> }* |
| 194 |
;;; ------------------------------------------------------------- |
| 195 |
;;; where, <VarId> is the variable name occur in the axiom, |
| 196 |
;;; <PsudeVar> is an identifier generated by `find' command, and |
| 197 |
;;; <SortId> is a name of a sort of the variable. this provides |
| 198 |
;;; a template for <VarSubst> for apply command. one can substitute |
| 199 |
;;; `<PsudeVar>:<SortId>' with appopreate term and can give it to |
| 200 |
;;; an argument to `apply'. |
| 201 |
;;; (4-3) extensions to `apply' command: |
| 202 |
;;; [a] rule specifier of apply command |
| 203 |
;;; - Syntax ----------------------------------------------------- |
| 204 |
;;; apply <Nat> [<VarSubst>] { at | within } <Selectors> . |
| 205 |
;;; -------------------------------------------------------------- |
| 206 |
;;; apply now accepts simple natural number as a rule specifier |
| 207 |
;;; which refers the number assigned by preceding `find'command to |
| 208 |
;;; a rewrite rule (see (4-2) above). |
| 209 |
;;; [b] new <Selector> option -- "subterm" |
| 210 |
;;; as noted in (4-1)[a] above, apply accepts "subterm" as a subterm |
| 211 |
;;; selector. |
| 212 |
;;; [c] can omit <VarSubst> |
| 213 |
;;; can omit <VarSubst> even if there exists some extra variables. |
| 214 |
;;; in this case, system generates pusude variables (constant terms) |
| 215 |
;;; on the fly, and uses them instead of extra variables. |
| 216 |
;;; in a similar manner, one can `start' with a term with some |
| 217 |
;;; variables in it. also, in this case, system generates constant |
| 218 |
;;; terms and supply them istead. this is almost the same with |
| 219 |
;;; `open'ing a module and declare some constants and uses them. |
| 220 |
;;; |
| 221 |
;;; (4-4) some support commands: |
| 222 |
;;; [a] showing apply context |
| 223 |
;;; separating out the functions of apply command requires us to keep |
| 224 |
;;; some context(selected subterms, applicable rules) in mind. |
| 225 |
;;; the following extension to `show' can be used for this purpose. |
| 226 |
;;; - Syntax -------------------------------------------------------- |
| 227 |
;;; <SowConText> ::= show apply context |
| 228 |
;;; ----------------------------------------------------------------- |
| 229 |
;;; print-outs informations useful for using apply command in a form |
| 230 |
;;; like this: |
| 231 |
;;; ------------------------------------------------------- |
| 232 |
;;; current module : name of current module. |
| 233 |
;;; term : whole target term ($$term). |
| 234 |
;;; subterm : subterm selected by `choose' command, |
| 235 |
;;; also prints <Selctors>. |
| 236 |
;;; rules : set of rules found by `find' command. |
| 237 |
;;; ------------------------------------------------------- |
| 238 |
;;; |
| 239 |
;;; *LIMITATION* : we don't have `abbriviated' module names for complex |
| 240 |
;;; module expressions. thus we are restricted to perform |
| 241 |
;;; `apply' only in modules with simple name. |
| 242 |
;;; seems to need something like `abbrev' of OBJ. |
| 243 |
;;; * some extensions planned * |
| 244 |
;;; (1) `step' option: |
| 245 |
;;; must do apply process in interactive manner. |
| 246 |
;;; (2) `stop' option: |
| 247 |
;;; stop reduction process when we meet the `pattern' specified. |
| 248 |
;;; might need some interactive process, `continue',`done'or .. ? |
| 249 |
|
| 250 |
;;; *NOTE on Semantics of <RuleSpec>* |
| 251 |
;;; the semantics of rule specifier(rule identifier or rule number) of |
| 252 |
;;; <RuleSpec> in OBJ is only loosely defined. at least for me, careful reading |
| 253 |
;;; of throughout OBJ manual doesn't give me an enough infos to implement this |
| 254 |
;;; feature. |
| 255 |
;;; the aim of specifier is of course to select one specific rule to be applied, |
| 256 |
;;; but there exists some ambiguities here. |
| 257 |
;;; the follwing is the problem summarized and its treatment of current |
| 258 |
;;; Chaos implementation. |
| 259 |
;;; (1) searching context: |
| 260 |
;;; *(1-1) semantics of <ModId> is loose. |
| 261 |
;;; how about rules imported from sub-modules? <ModId> limits to the |
| 262 |
;;; OWN rules of <ModId> or including ones of submodules? |
| 263 |
;;; my understanding is that <ModId> says "search in all rewrite rules |
| 264 |
;;; in <ModId> including imported ones" limiting the search space |
| 265 |
;;; to a subset of current module. this seems reasonable, so Chaos follows |
| 266 |
;;; this line. (*note* the real implementation of OBJ is more complex one |
| 267 |
;;; reflecting the value of a global flag set by users with `set' command, |
| 268 |
;;; but this can make the behaviour of apply command magical one. we want |
| 269 |
;;; to make things more simple). |
| 270 |
;;; *(1-2) what is the rule number? |
| 271 |
;;; what does "2.MOD" mean? this says `the second' rule of module `MOD', |
| 272 |
;;; but which is the `second'? |
| 273 |
;;; *(1-3) system generated rules. |
| 274 |
;;; should we consider rules generated by the system, eg. some rules for |
| 275 |
;;; associative/commutative operators? from the user side, full control |
| 276 |
;;; of reduction needs these rules to be choosed. but generated rules has |
| 277 |
;;; no rule identifier, thus user must select them by `number'. |
| 278 |
;;; (2) multiple rules with the same identifier: |
| 279 |
;;; there is no syntactical limitation on rule identifiers, they can be |
| 280 |
;;; overloaded, and there is no automatic selection machanisms for choosing |
| 281 |
;;; one among them. Chaos asks user to select one |
| 282 |
;;; |
| 283 |
|
| 284 |
;;; APPLY FAMILY --------------------------------------------------------------- |
| 285 |
;;; forms and parsers |
| 286 |
;;; ---------------------------------------------------------------------------- |
| 287 |
|
| 288 |
;;; ****** |
| 289 |
;;; CHOOSE |
| 290 |
;;; ****** |
| 291 |
(defterm choose (%script) |
| 292 |
:visible (selectors) ; one of :top, :subterm or list of |
| 293 |
; selectors. |
| 294 |
:eval eval-choose-command) |
| 295 |
|
| 296 |
;;; ************ |
| 297 |
;;; INSPECT-TERM |
| 298 |
;;; ************ |
| 299 |
(defterm inspect-term (%script) |
| 300 |
:visible () |
| 301 |
:eval eval-inspect-term-command) |
| 302 |
|
| 303 |
;;; get-selectors : selector-token-seq -> { symbol | list(token)} |
| 304 |
;;; |
| 305 |
;;; <Selectors> ::= subterm | term | top | <Selector> { of <Selector>}* |
| 306 |
;;; <Selector> ::= <OccurSelection> | <SubseqSelection > | <SubsetSelection> } |
| 307 |
;;; <OccurSelection> ::= "(" <Nat>+ ")" |
| 308 |
;;; <SubseqSelection> ::= "[" <Nat> .. <Nat> "]" | "[" <Nat> "]" |
| 309 |
;;; <SubsetSelection> ::= "{" <Nat> [, <Nat>]* "}" |
| 310 |
;;; |
| 311 |
(defun get-selectors (selector-toks) |
| 312 |
(case-equal (car selector-toks) |
| 313 |
(("top" "term") :top) |
| 314 |
("subterm" :subterm) |
| 315 |
(("(" "{" "[") |
| 316 |
;; *NOTE* discard the first token |
| 317 |
;; because of stupid behaviour of our token reader. |
| 318 |
(cdr selector-toks)) |
| 319 |
(t (with-output-chaos-error ('invalid-selector) |
| 320 |
(format t "unknown type of selectors ~a" selector-toks) |
| 321 |
)))) |
| 322 |
|
| 323 |
(defun parse-choose-command (tok) |
| 324 |
(%choose* (get-selectors (cadr tok)))) |
| 325 |
|
| 326 |
;;; ***** |
| 327 |
;;; MATCH |
| 328 |
;;; ***** |
| 329 |
;;; - Syntax ------------------------------------------------------------------- |
| 330 |
;;; <MatchCommand> ::= match <Term1> to <Term2> . |
| 331 |
;;; <Term1> ::= { it | term | top | subterm | <Term> } |
| 332 |
;;; <Term2> ::= { rule | +rule | -rule | <Term> } |
| 333 |
;;;----------------------------------------------------------------------------- |
| 334 |
(defterm match (%script) |
| 335 |
:visible (type ; or :match, :unify, :xmatch |
| 336 |
target ; or :it pre-term |
| 337 |
pattern ; or :rule, +rule, -rule, pre-term |
| 338 |
) |
| 339 |
:eval eval-match-command) |
| 340 |
|
| 341 |
(defun parse-match-command (toks) |
| 342 |
(let (type |
| 343 |
target |
| 344 |
pattern) |
| 345 |
;; |
| 346 |
(setq type |
| 347 |
(if (equal "match" (car toks)) |
| 348 |
:match |
| 349 |
(if (equal "xmatch" (car toks)) |
| 350 |
:xmatch |
| 351 |
:unify))) |
| 352 |
(setq toks (cdr toks)) ; arguments |
| 353 |
;; get target |
| 354 |
(loop (when (or (null toks) |
| 355 |
(or (equal "to" (car toks)) |
| 356 |
(equal "with" (car toks)))) |
| 357 |
(return)) |
| 358 |
(push (car toks) target) |
| 359 |
(setq toks (cdr toks))) |
| 360 |
(setq target (car (nreverse target))) |
| 361 |
;; get pattern |
| 362 |
(setq pattern (cadr toks)) |
| 363 |
;; make ast |
| 364 |
(%match* type |
| 365 |
(case-equal target |
| 366 |
((("top") ("term")) :top) |
| 367 |
((("subterm")) :subterm) |
| 368 |
((("it")) :it) |
| 369 |
(t target)) |
| 370 |
(case-equal pattern |
| 371 |
((("rule") ("rules")) :rule) |
| 372 |
((("+rule") ("+rules")) :+rule) |
| 373 |
((("-rule") ("-rules")) :-rule) |
| 374 |
(t pattern))))) |
| 375 |
|
| 376 |
;;; **** |
| 377 |
;;; FIND |
| 378 |
;;; **** |
| 379 |
;;;- Syntax -------------------------------------------------------------------- |
| 380 |
;;; <FindCommand> ::= find <What> |
| 381 |
;;; <What> ::= { rule | +rule | -rule } |
| 382 |
;;;----------------------------------------------------------------------------- |
| 383 |
;;; *NOTE* just the alias of "match it to <What> . " |
| 384 |
;;; |
| 385 |
(defun parse-find-command (toks) |
| 386 |
(%match* :match |
| 387 |
:it |
| 388 |
(case-equal (cadr toks) |
| 389 |
(("rule" "rules") :rule) |
| 390 |
(("+rule" "+rules") :+rule) |
| 391 |
(("-rule" "+rules") :-rule) |
| 392 |
(t (with-output-chaos-error ('invalid-rule-spec) |
| 393 |
(princ "only `rule', `+rule', or `-rule' is meaningful for |
| 394 |
find,") |
| 395 |
(print-next) |
| 396 |
(format t "but ~a is given." (cadr toks))))))) |
| 397 |
;;; ***** |
| 398 |
;;; APPLY |
| 399 |
;;; ***** |
| 400 |
;;; - Syntax -------------------------------------------------------------------- |
| 401 |
;;; <ApplyCommand> ::= apply <Action> <WhereSpec> <Selectors> . |
| 402 |
;;; <Action> ::= {red | reduction | print | ? | |
| 403 |
;;; <RuleSpec> [<VarSubst>]} |
| 404 |
;;; <RuleSpec> ::= <Nat> | [ + | - ] [<ModId>].{<Nat> | <RuleId>} |
| 405 |
;;; <VarSubst> ::= with <VarId> = <Term> {, <VarId> = <Term>}* |
| 406 |
;;; <WhereSpec> ::= { at | within } |
| 407 |
;;; <Selectors> ::= subterm | term | top | <Selector> { of <Selector>}* |
| 408 |
;;; <Selector> ::= <OccurSelection> | <SubseqSelection > | <SubsetSelection> } |
| 409 |
;;; <OccurSelection> ::= "(" <Nat>+ ")" |
| 410 |
;;; <SubseqSelection> ::= "[" { <Nat> .. <Nat> | <Nat> } "]" |
| 411 |
;;; <SubsetSelection> ::= "{" <Nat> [, <Nat>]* "}" |
| 412 |
;;; |
| 413 |
;;; *note* the syntax of <Selectors> of OBJ is defined as |
| 414 |
;;; <Selectors> ::= <Slector> { of <Selector> }* |
| 415 |
;;; <Selector> ::= { term | top } | <OccurSelection> | <SubseqSelection> | |
| 416 |
;;; <SubseSelection> |
| 417 |
;;; this seemes to be strange for me. |
| 418 |
;;; how about "top of top of top" or "top of { 1, 3 } of top of [ 1 .. 2 ]"? |
| 419 |
;;; these are meaningful, but unneccessarily complex. |
| 420 |
;;;----------------------------------------------------------------------------- |
| 421 |
|
| 422 |
(defterm apply (%script) |
| 423 |
:visible (action ; action to be performed, one-of |
| 424 |
; :apply, :reduce, :print, :help. |
| 425 |
rule-spec ; rule specifier to be applied. |
| 426 |
substitution ; list of variable bindings. |
| 427 |
where-spec ; one of :at, :within. |
| 428 |
selectors) ; list of selectors. |
| 429 |
:eval eval-apply-command) |
| 430 |
|
| 431 |
;;; get-apply-action : <Action> -> action keyword |
| 432 |
;;; |
| 433 |
;;; <Action> ::= { red | reduction | print | help | <RuleSpec> [<VarSubst>]} |
| 434 |
;;; * "help" & <VarSubst> are specially treated elsewhere. |
| 435 |
;;; |
| 436 |
(defun get-apply-action (tok) |
| 437 |
;; tok ::= { red | reduction | reduce | print | <RuleSpec> } |
| 438 |
(case-equal tok |
| 439 |
(("red" "reduction" "reduce") :reduce) |
| 440 |
(("bred" "breduce" "behavioural-reduction") :breduce) |
| 441 |
(("execute" "exec") :exec) |
| 442 |
("print" :print) |
| 443 |
(t :apply))) |
| 444 |
|
| 445 |
;;; NOTE: rules labels cannot contain . |
| 446 |
;;; parse-rule-spec : <RuleSpec> -> (Module Rule Direction) |
| 447 |
;;; |
| 448 |
;;; <RuleSpec> ::= <Nat> | [ - | + ][<ModId>].{<Nat> | <Id>} |
| 449 |
;;; <RuleSpec> ::= [ - | + ][<ModId>.]{<Nat> | <Id>} |
| 450 |
;;; |
| 451 |
#|| |
| 452 |
(defun parse-rule-spec (tok) |
| 453 |
(declare (type string tok)) |
| 454 |
(if (every #'digit-char-p tok) |
| 455 |
(let ((val (parse-integer tok))) |
| 456 |
(unless (> val 0) |
| 457 |
(with-output-chaos-error ('invalid-rule-number) |
| 458 |
(princ "rule index must be greater than 0.") |
| 459 |
)) |
| 460 |
val) |
| 461 |
(let* ((fwd (eql #\+ (char tok 0))) |
| 462 |
(rev (eql #\- (char tok 0))) |
| 463 |
(i (if (or rev fwd) 1 0)) |
| 464 |
(dot-pos (position-if #'(lambda (x) (char= #\. x)) tok))) |
| 465 |
(if dot-pos |
| 466 |
(list (subseq tok i dot-pos) (subseq tok (1+ dot-pos)) rev) |
| 467 |
(list "" (subseq tok i) rev))))) |
| 468 |
||# |
| 469 |
|
| 470 |
(defun parse-rule-spec (tok) |
| 471 |
(declare (type string tok)) |
| 472 |
(let* ((fwd (eql #\+ (char tok 0))) |
| 473 |
(rev (eql #\- (char tok 0))) |
| 474 |
(i (if (or rev fwd) 1 0)) |
| 475 |
(dot-pos (position-if #'(lambda (x) (char= #\. x)) tok))) |
| 476 |
(if dot-pos |
| 477 |
(list (subseq tok i dot-pos) (subseq tok (1+ dot-pos)) rev) |
| 478 |
(list "" (subseq tok i) rev)))) |
| 479 |
|
| 480 |
;;; get-apply-where : <WhereSpec> -> where keyword |
| 481 |
;;; <WhereSpec> ::= { at | within } |
| 482 |
;;; |
| 483 |
(defun get-apply-where (where) |
| 484 |
(if (equal where "at") |
| 485 |
:at |
| 486 |
(if (equal where "within") |
| 487 |
:within |
| 488 |
(with-output-chaos-error ('invalid-apply-place) |
| 489 |
(format t "<WhereSpec> must be either \"at\" or \"within\", but ~a is specified" |
| 490 |
where))))) |
| 491 |
|
| 492 |
;;; parse-apply-command |
| 493 |
;;; parse whole form |
| 494 |
|
| 495 |
(defun parse-apply-command (e) |
| 496 |
(when (equal '(("?")) (cdr e)) |
| 497 |
(return-from parse-apply-command (%apply* :help nil nil nil nil))) |
| 498 |
(let* ((ee (cadr e)) ; eliminate "apply" and the last "." |
| 499 |
(act (nth 0 ee))) |
| 500 |
(let* ((action (get-apply-action act)) |
| 501 |
(rule-spec (if (eq action :apply) |
| 502 |
(parse-rule-spec act)))) |
| 503 |
(let* ((no-subst (stringp (nth 1 ee))) |
| 504 |
; "at" "within" -- no substitution. |
| 505 |
(substtoks (if no-subst |
| 506 |
nil |
| 507 |
(nth 1 ee))) ; we don't syntactic check here. |
| 508 |
(where-spec (get-apply-where (nth (if no-subst 1 2) ee))) |
| 509 |
(selectors (get-selectors (car (subseq ee (if no-subst 2 3)))))) |
| 510 |
(%apply* action rule-spec substtoks where-spec selectors))))) |
| 511 |
|
| 512 |
;;; EOF |