-
Notifications
You must be signed in to change notification settings - Fork 2
/
bsl_tools.rkt
259 lines (222 loc) · 6.93 KB
/
bsl_tools.rkt
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#lang racket/base
(require racket/contract)
(require racket/list)
(require racket/string)
(require racket/format)
(require scriblib/render-cond)
(require scribble/core
scribble/html-properties
(only-in xml cdata))
(require scribble/latex-properties)
(require scribble/base)
(require syntax/to-string)
(require syntax/stx)
(provide bsltree jsontree stepper)
(define implemented-language (or/c "en" "de"))
(define value (or/c boolean? string? number? '()))
(define name (or/c symbol?))
(struct/contract bsl-string-container (
[bsl-content (or/c value syntax?)])
#:transparent)
; predicate function value
(define (value? value)
(cond
[(boolean? value) #t]
[(string? value) #t]
[(number? value) #t]
[(empty? value) #t]
[(symbol? value) #t]
[else #f]
)
)
; HTML
(define
(bsl-tag-wrapper quiz lang)
(style #f (list
(alt-tag "bsltree")
(js-addition "bsl_tools.js")
(attributes (list (cons 'quiz (if quiz "true" "false"))
(cons 'lang lang)))
))
)
(define
(jsontree-tag-wrapper quiz lang)
(style #f (list
(alt-tag "jsontree")
(js-addition "bsl_tools.js")
(attributes (list (cons 'quiz (if quiz "true" "false"))
(cons 'lang lang)))
))
)
(define
(stepper-tag-wrapper lang)
(style #f (list
(alt-tag "stepper")
(js-addition "bsl_tools.js")
(attributes (list (cons 'lang lang)))
))
)
(define
style-tag-wrapper
(style #f (list
(alt-tag "style")))
)
; Either (List of Syntax) or (Value)-> Either (List of String) or (String)
(define (synlst-or-val->strlist-or-str lst)
(cond
[(or (number? lst) (boolean? lst)(symbol? lst))
(string-append (~a lst) " \n")]
[(string? lst) (string-append "\"" lst "\" \n")]
[(empty? lst) " '() \n"]
[(stx-list? lst)(stx-map
(lambda (x)
(string-append "(" (syntax->string x) ") \n "))lst)]
[(syntax? lst) (syntax->string lst)]
)
)
;Either (List-of-String) or (String) -> String
(define (strlist-or-str->str lst)
(cond
[(string? lst) lst]
[(empty? lst) ""]
[else (string-append (first lst) (strlist-or-str->str (rest lst)))]
)
)
; helper for "nothing" in order to not break pdf rendering by outputting nothing
(define nothing (nested-flow (style #f '()) '()))
; helper for inline styles
(define (inline-style s) (elem #:style style-tag-wrapper s))
; render bsl-string
(define
(bsltree stx #:quiz [quiz #f] #:lang [lang "en"])
(cond
[(not (or (syntax? stx) (value? stx)))
(raise-argument-error 'bsltree "BSL-Tree only accepts Syntax-Expressions or Values" stx)]
[(not (boolean? quiz))
(raise-argument-error 'quiz "BSL-Tree #:quiz toggle has to be a boolean!" quiz)]
[(not (implemented-language lang))
(raise-argument-error 'lang "BSL-Tree #:lang needs to be an implemented language, currently either 'en' or 'de'" lang)]
[(cond-block
[html (paragraph (bsl-tag-wrapper quiz lang)
(strlist-or-str->str
(synlst-or-val->strlist-or-str stx))
)]
[else nothing]
;[latex (paragraph (style #f '()) (strlist-or-str->str(synlst-or-val->strlist-or-str stx)))]
)]
)
)
; render bsl-string
(define
(jsontree #:quiz [quiz #f] #:lang [lang "en"] #:extrastyle [extrastyle ""] . json)
(cond
[(not (boolean? quiz))
(raise-argument-error 'quiz "JSON-Tree #:quiz toggle has to be a boolean!" quiz)]
[(not (implemented-language lang))
(raise-argument-error 'lang "JSON-Tree #:lang needs to be an implemented language, currently either 'en' or 'de'" lang)]
[(cond-block
[html (paragraph (style #f '())
(list
(inline-style extrastyle)
(elem #:style (jsontree-tag-wrapper quiz lang) json))
)]
[else nothing]
;[latex (paragraph (style #f '()) (strlist-or-str->str(synlst-or-val->strlist-or-str stx)))]
)]
)
)
; render bsl-stepper
(define
(stepper stx #:lang [lang "en"])
(cond
[(not (or (syntax? stx) (value? stx)))
(raise-argument-error 'bsltree "Stepper only accepts Syntax-Expressions or Values" stx)]
[(not (implemented-language lang))
(raise-argument-error 'lang "Stepper #:lang needs to be an implemented language, currently either 'en' or 'de'" lang)]
[(cond-block
[html (paragraph (stepper-tag-wrapper lang)
(strlist-or-str->str
(synlst-or-val->strlist-or-str stx))
)]
[else nothing]
;[latex (paragraph (style #f '()) (strlist-or-str->str(synlst-or-val->strlist-or-str stx)))]
)]
)
)
; OLD
; predicate function sexpr
;;; (define (sexpr? sexpr)
;;; (cond
;;; [(boolean? sexpr) #t]
;;; [(string? sexpr) #t]
;;; [(symbol? sexpr) #t]
;;; [(number? sexpr) #t]
;;; [(empty? sexpr) #t]
;;; [(and (string? (~a sexpr))
;;; (string-prefix? (~a sexpr) "((")
;;; (string-suffix? (~a sexpr) "))")
;;; )#t]
;;; [else #f]
;;; )
;;; )
; helper: add substring
; sexpr->string
;;; (define (sexpr->string sexpr)
;;; (cond
;;; [(boolean? sexpr) (string-append "\n" (~a sexpr) "\n")]
;;; [(string? sexpr) (string-append "\n" "\"" sexpr "\"" "\n")]
;;; [(number? sexpr) (string-append "\n" (~a sexpr) "\n")]
;;; [(empty? sexpr) (string-append "\n" "'()" "\n")]
;;; [(symbol? sexpr) (string-append "\n" (~a sexpr)"\n")]
;;; [else (string-append "\n" (substring (~a sexpr) 1 (- (string-length (~a sexpr)) 1)) "\n")]
;;; )
;;; )
;;; NOT USED
;;;
;;; #lang racket/base
;;; (require racket/contract)
;;; (require racket/list)
;;; (require scribble/core
;;; scribble/html-properties
;;; (only-in xml cdata))
;;; (require scribble/latex-properties)
;;; (require scribble/base)
;;; ;
;;; ; <bsl-tree>
;;; ; (expression-or-def)
;;; ; (expression-or-def)
;;; ; (expression-or-def)
;;; ;<\bsl-tree>
;;; ; BSL-Tree data definitions
;;; (define bsl-tree (
;;; flat-rec-contract tree-of()
;;; ))
;;; ; Value types
;;; (define v (or/c boolean? string? number? '()))
;;; ; name is a keyword
;;; (define name (or/c string?))
;;; ; Expr is call or cond or name or v
;;; ;(define expr (or/c call cond name v))
;;; (define expr (flat-rec-contract expr (or/c call cond name v)))
;;; ; clause is a pair of expr
;;; (define clause (cons/c expr expr))
;;; ; Cond: List of clauses
;;; (define cond (listof clause))
;;; ; call is a name and list of expr
;;; (define call (cons/c name (listof (or/c call cond name v))))
;;; ; funDef is a name a list of names and a expr
;;; (define funDef (cons/c name (cons/c (listof name) expr)))
;;; ; constDef is a name and a expr
;;; (define constDef (cons/c name expr))
;;; ; structDef is a name and a list of names
;;; (define structDef (cons/c name (listof name)))
;;; ; definition is either a funDef or constDef or structDef
;;; (define definition (or/c funDef constDef structDef))
;;; ;defOrExpr
;;; (define defOrExpr (or/c definition expr))
;;; ; program type
;;; (define program (listof defOrExpr))
;;; ; Example program
;;; ;
;;; (program (list (definition (funDef (cons (name "f") (list (name "x")) (expr (name ("x"))))))
;;; ))