June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
74
Task/S-Expressions/Julia/s-expressions.julia
Normal file
74
Task/S-Expressions/Julia/s-expressions.julia
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
function rewritequotedparen(s)
|
||||
segments = split(s, "\"")
|
||||
for i in 1:length(segments)
|
||||
if i & 1 == 0 # even i
|
||||
ret = replace(segments[i], r"\(", s"_O_PAREN")
|
||||
segments[i] = replace(ret, r"\)", s"_C_PAREN")
|
||||
end
|
||||
end
|
||||
join(segments, "\"")
|
||||
end
|
||||
|
||||
function reconsdata(n, s)
|
||||
if n > 1
|
||||
print(" ")
|
||||
end
|
||||
if s isa String && ismatch(r"[\$\%\!\$\#]", s) == false
|
||||
print("\"$s\"")
|
||||
else
|
||||
print(s)
|
||||
end
|
||||
end
|
||||
|
||||
function printAny(anyarr)
|
||||
print("(")
|
||||
for (i, el) in enumerate(anyarr)
|
||||
if el isa Array
|
||||
print("(")
|
||||
for (j, el2) in enumerate(el)
|
||||
if el2 isa Array
|
||||
print("(")
|
||||
for(k, el3) in enumerate(el2)
|
||||
if el3 isa Array
|
||||
print(" (")
|
||||
for(n, el4) in enumerate(el3)
|
||||
reconsdata(n, el4)
|
||||
end
|
||||
print(")")
|
||||
else
|
||||
reconsdata(k, el3)
|
||||
end
|
||||
end
|
||||
print(")")
|
||||
else
|
||||
reconsdata(j, el2)
|
||||
end
|
||||
end
|
||||
if i == 1
|
||||
print(")\n ")
|
||||
else
|
||||
print(")")
|
||||
end
|
||||
end
|
||||
end
|
||||
println(")")
|
||||
end
|
||||
|
||||
removewhitespace(s) = replace(replace(s, r"\n", " "), r"^\s*(\S.*\S)\s*$", s"\1")
|
||||
quote3op(s) = replace(s, r"([\$\!\@\#\%]{3})", s"\"\1\"")
|
||||
paren2bracket(s) = replace(replace(s, r"\(", s"["), r"\)", s"]")
|
||||
data2symbol(s) = replace(s, "[data", "[:data")
|
||||
unrewriteparens(s) = replace(replace(s, "_C_PAREN", ")"), "_O_PAREN", "(")
|
||||
addcommas(s) = replace(replace(s, r"\]\s*\[", "],["), r" (?![a-z])", ",")
|
||||
|
||||
inputstring = """
|
||||
((data "quoted data" 123 4.5)
|
||||
(data (!@# (4.5) "(more" "data)")))
|
||||
"""
|
||||
|
||||
println("The input string is:\n", inputstring)
|
||||
processed = (inputstring |> removewhitespace |> rewritequotedparen |> quote3op
|
||||
|> paren2bracket |> data2symbol |> unrewriteparens |> addcommas)
|
||||
nat = eval(parse("""$processed"""))
|
||||
println("The processed native structure is:\n", nat)
|
||||
println("The reconstructed string is:\n"), printAny(nat)
|
||||
98
Task/S-Expressions/Kotlin/s-expressions.kotlin
Normal file
98
Task/S-Expressions/Kotlin/s-expressions.kotlin
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
// version 1.2.31
|
||||
|
||||
const val INDENT = 2
|
||||
|
||||
fun String.parseSExpr(): List<String>? {
|
||||
val r = Regex("""\s*("[^"]*"|\(|\)|"|[^\s()"]+)""")
|
||||
val t = r.findAll(this).map { it.value }.toMutableList()
|
||||
if (t.size == 0) return null
|
||||
var o = false
|
||||
var c = 0
|
||||
for (i in t.size - 1 downTo 0) {
|
||||
val ti = t[i].trim()
|
||||
val nd = ti.toDoubleOrNull()
|
||||
if (ti == "\"") return null
|
||||
if (ti == "(") {
|
||||
t[i] = "["
|
||||
c++
|
||||
}
|
||||
else if (ti == ")") {
|
||||
t[i] = "]"
|
||||
c--
|
||||
}
|
||||
else if (nd != null) {
|
||||
val ni = ti.toIntOrNull()
|
||||
if (ni != null) t[i] = ni.toString()
|
||||
else t[i] = nd.toString()
|
||||
}
|
||||
else if (ti.startsWith("\"")) { // escape embedded double quotes
|
||||
var temp = ti.drop(1).dropLast(1)
|
||||
t[i] = "\"" + temp.replace("\"", "\\\"") + "\""
|
||||
}
|
||||
if (i > 0 && t[i] != "]" && t[i - 1].trim() != "(") t.add(i, ", ")
|
||||
if (c == 0) {
|
||||
if (!o) o = true else return null
|
||||
}
|
||||
}
|
||||
return if (c != 0) null else t
|
||||
}
|
||||
|
||||
fun MutableList<String>.toSExpr(): String {
|
||||
for (i in 0 until this.size) {
|
||||
this[i] = when (this[i]) {
|
||||
"[" -> "("
|
||||
"]" -> ")"
|
||||
", " -> " "
|
||||
else -> {
|
||||
if (this[i].startsWith("\"")) { // unescape embedded quotes
|
||||
var temp = this[i].drop(1).dropLast(1)
|
||||
"\"" + temp.replace("\\\"", "\"") + "\""
|
||||
}
|
||||
else this[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.joinToString("")
|
||||
}
|
||||
|
||||
fun List<String>.prettyPrint() {
|
||||
var level = 0
|
||||
loop@for (t in this) {
|
||||
var n: Int
|
||||
when(t) {
|
||||
", ", " " -> continue@loop
|
||||
"[", "(" -> {
|
||||
n = level * INDENT + 1
|
||||
level++
|
||||
}
|
||||
"]", ")" -> {
|
||||
level--
|
||||
n = level * INDENT + 1
|
||||
}
|
||||
else -> {
|
||||
n = level * INDENT + t.length
|
||||
}
|
||||
}
|
||||
println("%${n}s".format(t))
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val str = """((data "quoted data" 123 4.5)""" + "\n" +
|
||||
""" (data (!@# (4.5) "(more" "data)")))"""
|
||||
val tokens = str.parseSExpr()
|
||||
if (tokens == null)
|
||||
println("Invalid s-expr!")
|
||||
else {
|
||||
println("Native data structure:")
|
||||
println(tokens.joinToString(""))
|
||||
println("\nNative data structure (pretty print):")
|
||||
tokens.prettyPrint()
|
||||
|
||||
val tokens2 = tokens.toMutableList()
|
||||
println("\nRecovered S-Expression:")
|
||||
println(tokens2.toSExpr())
|
||||
println("\nRecovered S-Expression (pretty print):")
|
||||
tokens2.prettyPrint()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue