Anonymous user
S-expressions: Difference between revisions
Updated D entry
(Strings like foo# wouldn't work, because /b would cut the # I really don't know why it was after the num.) |
(Updated D entry) |
||
Line 850:
=={{header|D}}==
<lang d>import std.stdio
alias Sexp = Variant;
Line 860 ⟶ 856:
struct Symbol {
private string name;
string toString() const { return name; }
}
Sexp parseSexp(
static bool isIdentChar(in char c) @safe pure /*nothrow*/ {
return c.isAlpha || "0123456789!@#-".canFind(c);
Line 869 ⟶ 865:
size_t pos = 0;
Sexp _parse() {
scope (exit)
pos = i;
if (
while (
i++;
i++;
return Sexp(
} else if (
while (
i++;
if (
i++;
while (
i++;
return aux.parse!double.Sexp;
}
return aux.parse!ulong.Sexp;
} else if (isIdentChar(raw[pos])) {▼
} else
i++;
return Sexp(Symbol(
} else if (
Sexp[] lst;
while (
while (
i++;
pos = i;
lst ~= _parse
i = pos;
while (
i++;
}
Line 909 ⟶ 907:
return Sexp(null);
}
return _parse();▼
while (txt[pos].isWhite)
}
void writeSexp(Sexp expr) {
if (expr.type == typeid(string)) {
write('"
▲ write(expr);
} else if (expr.type == typeid(Sexp[])) {
'('.write
auto arr = expr.get!(Sexp[]);
foreach (immutable i, e; arr) {
if (i + 1 < arr.length)
}
')'.write
} else {
}
}
void main() {
auto
(data (!@# (4.5) "(more" "data)")))`.parseSexp;
writeln("Parsed: ", pTest);
writeln
}</lang>
{{out}}
|