-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsyntax.qon
116 lines (100 loc) · 4.13 KB
/
fsyntax.qon
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
((includes q/base.qon q/boolean.qon q/lists.qon q/newparser.qon q/compiler.qon q/macros.qon)
(types)
(functions
(int indexOfHelper (string haystack string needle int start int current) (declare)
(body
(if (greaterthan (add current (string-length needle)) (string-length haystack))
(then (return -1))
(else
(if (equalString (sub-string haystack current (string-length needle)) needle)
(then (return current))
(else (return (indexOfHelper haystack needle start (add1 current)))))))))
(int indexOf (string haystack string needle int start) (declare)
(body
(if (equal (string-length needle) 0)
(then (return start))
(else (return (indexOfHelper haystack needle start start))))))
(bool lessThan (int a int b) (declare)
(body
(return (andBool (notBool (equal a b)) (notBool (greaterthan a b))))))
(string addParens (string line) (declare)
(body
(return (stringConcatenate "(" (stringConcatenate line ")")))))
(int countLeadingSpaces (string line) (declare)
(body
(if (andBool (greaterthan (string-length line) 0) (equalString (sub-string line 0 1) " "))
(then (return (add1 (countLeadingSpaces (sub-string line 1 (sub (string-length line) 1))))))
(else (return 0)))))
(string addClosingParens (int count string line) (declare)
(body
(if (equal count 0)
(then (return line))
(else (return (addClosingParens (sub1 count) (stringConcatenate line ")") ))))))
(string processLine (string line int prevIndent) (declare
(int currentIndent 0)
(string processed ""))
(body
(set currentIndent (countLeadingSpaces line))
(set processed (sub-string line currentIndent (sub (string-length line) currentIndent)))
(set processed (addParens processed))
(if (lessThan currentIndent prevIndent)
(then (set processed (addClosingParens (sub prevIndent currentIndent) processed)))
(else))
(return processed)))
(list splitLines (string input int start) (declare
(int newlinePos 0))
(body
(if (notBool (lessThan start (string-length input)))
(then (return (emptyList)))
(else
(set newlinePos (indexOf input "\n" start))
(if (equal newlinePos -1)
(then (return (cons (boxString (sub-string input start (sub (string-length input) start))) (emptyList))))
(else (return (cons (boxString (sub-string input start (sub newlinePos start)))
(splitLines input (add1 newlinePos))))))))))
(list processLines (list lines int prevIndent) (declare
(string line "")
(string processed ""))
(body
(if (isEmpty lines)
(then (return (emptyList)))
(else
(set line (unBoxString (car lines)))
(if (equalString line "")
(then (return (processLines (cdr lines) prevIndent)))
(else
(set processed (processLine line prevIndent))
(return (cons (boxString processed)
(processLines (cdr lines) (countLeadingSpaces line))))))))))
(string convertToSexpr (string input) (declare
(list lines nil)
(list processedLines nil)
(string result ""))
(body
(set lines (splitLines input 0))
(set processedLines (processLines lines 0))
(set result (StringListJoin processedLines "\n"))
(return (addClosingParens (countLeadingSpaces (unBoxString (car lines))) result))))
(int start () (declare
(string filename "")
(string inputContent "")
(string convertedContent ""))
(body
(if (lessThan globalArgsCount 2)
(then
(printf "Usage: new_converter <input_file>\n")
(return 1))
(else
(set filename (getStringArray 1 globalArgs))
(printf "Reading file: %s\n" filename)
(set inputContent (read-file filename))
(if (equalString inputContent "")
(then
(printf "Error: Could not read file or file is empty.\n")
(return 1))
(else
(printf "%s\n" inputContent)
(set convertedContent (convertToSexpr inputContent))
(printf "%s\n" convertedContent)
(return 0)))))))
))