UNPKG

2.6 kBtext/coffeescriptView Raw
1###
2 Add a pattern i.e. a substitution rule.
3 Substitution rule needs a template as first argument
4 and what to transform it to as second argument.
5 Optional third argument is a boolean test which
6 adds conditions to when the rule is applied.
7###
8
9# same as Eval_pattern but only leaves
10# NIL on stack at return, hence gives no
11# printout
12Eval_silentpattern = ->
13 Eval_pattern()
14 pop()
15 push_symbol(NIL)
16
17Eval_pattern = ->
18 # check that the parameters are allright
19 if !iscons(cdr(p1))
20 stop("pattern needs at least a template and a transformed version")
21 firstArgument = car(cdr(p1))
22 secondArgument = car(cdr(cdr(p1)))
23 if secondArgument == symbol(NIL)
24 stop("pattern needs at least a template and a transformed version")
25 # third argument is optional and contains the tests
26 if !iscons(cdr(cdr(p1)))
27 thirdArgument = symbol(NIL)
28 else
29 thirdArgument = car(cdr(cdr(cdr(p1))))
30
31 if equal(firstArgument, secondArgument)
32 stop("recursive pattern")
33
34 # console.log "Eval_pattern of " + cdr(p1)
35 # this is likely to create garbage collection
36 # problems in the C version as it's an
37 # untracked reference
38 stringKey = "template: " + print_list(firstArgument)
39 stringKey += " tests: " + print_list(thirdArgument)
40 if DEBUG then console.log "pattern stringkey: " + stringKey
41
42 patternPosition = userSimplificationsInStringForm.indexOf stringKey
43 # if pattern is not there yet, add it, otherwise replace it
44 if patternPosition == -1
45 #console.log "adding pattern because it doesn't exist: " + cdr(p1)
46 userSimplificationsInStringForm.push(stringKey)
47 userSimplificationsInListForm.push(cdr(p1))
48 else
49 if DEBUG then console.log "pattern already exists, replacing. " + cdr(p1)
50 userSimplificationsInStringForm[patternPosition] = stringKey
51 userSimplificationsInListForm[patternPosition] = cdr(p1)
52
53 # return the pattern node itself so we can
54 # give some printout feedback
55 push_symbol(PATTERN)
56 push cdr(p1)
57 list(2)
58
59
60###
61 Clear all patterns
62###
63
64
65do_clearPatterns = ->
66 userSimplificationsInListForm = []
67 userSimplificationsInStringForm = []
68
69Eval_clearpatterns = ->
70 # this is likely to create garbage collection
71 # problems in the C version as it's an
72 # untracked reference
73 do_clearPatterns()
74
75 # return nothing
76 push_symbol(NIL)
77
78
79Eval_patternsinfo = ->
80 patternsinfoToBePrinted = patternsinfo()
81
82 if patternsinfoToBePrinted != ""
83 new_string(patternsinfoToBePrinted)
84 else
85 push_symbol(NIL)
86
87patternsinfo = ->
88 patternsinfoToBePrinted = ""
89 for i in userSimplificationsInListForm
90 patternsinfoToBePrinted += userSimplificationsInListForm + "\n"
91 return patternsinfoToBePrinted