mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 06:48:48 +00:00
compiler: Env register_string
This commit is contained in:
parent
28f4dc191b
commit
44d50c4c8e
1 changed files with 44 additions and 44 deletions
|
|
@ -1033,50 +1033,50 @@ public:
|
||||||
|
|
||||||
/* registers a string constant */
|
/* registers a string constant */
|
||||||
Opnd register_string(const std::string &x) {
|
Opnd register_string(const std::string &x) {
|
||||||
// TODO
|
const auto escape = [](const std::string &y) {
|
||||||
// let escape x =
|
const size_t n = y.size();
|
||||||
// let n = String.length x in
|
std::string buffer;
|
||||||
// let buf = Buffer.create (n * 2) in
|
buffer.reserve(n * 2);
|
||||||
// let rec iterate i =
|
for (size_t i = 0; i < n; ++i) {
|
||||||
// if i < n then
|
switch (y[i]) {
|
||||||
// match x.[i] with
|
case '"':
|
||||||
// | '"' ->
|
buffer.push_back('\\');
|
||||||
// Buffer.add_char buf '\\';
|
buffer.push_back('"');
|
||||||
// Buffer.add_char buf '"';
|
break;
|
||||||
// iterate (i + 1)
|
case '\\':
|
||||||
// | '\\' -> (
|
buffer.push_back('\\');
|
||||||
// if i + 1 >= n then (
|
if (i + 1 >= n) {
|
||||||
// Buffer.add_char buf '\\';
|
buffer.push_back('\\');
|
||||||
// Buffer.add_char buf '\\')
|
} else {
|
||||||
// else
|
switch (y[i + 1]) {
|
||||||
// match x.[i + 1] with
|
case 'n':
|
||||||
// | 'n' ->
|
case 't':
|
||||||
// Buffer.add_char buf '\\';
|
buffer.push_back(y[i + 1]);
|
||||||
// Buffer.add_char buf 'n';
|
++i;
|
||||||
// iterate (i + 2)
|
break;
|
||||||
// | 't' ->
|
default:
|
||||||
// Buffer.add_char buf '\\';
|
buffer.push_back('\\');
|
||||||
// Buffer.add_char buf 't';
|
break;
|
||||||
// iterate (i + 2)
|
}
|
||||||
// | _ ->
|
}
|
||||||
// Buffer.add_char buf '\\';
|
break;
|
||||||
// Buffer.add_char buf '\\';
|
default:
|
||||||
// iterate (i + 1))
|
buffer.push_back(y[i]);
|
||||||
// | c ->
|
break;
|
||||||
// Buffer.add_char buf c;
|
}
|
||||||
// iterate (i + 1)
|
}
|
||||||
// in
|
return buffer;
|
||||||
// iterate 0;
|
};
|
||||||
// Buffer.contents buf
|
const auto y = escape(x);
|
||||||
// in
|
|
||||||
// let x = escape x in
|
const auto it = stringm.find(y);
|
||||||
// let name = M.find_opt x stringm in
|
|
||||||
// match name with
|
if (it == stringm.end()) {
|
||||||
// | Some name -> (M (D, I, A, name), self)
|
const auto name = std::format("string_{}", scount);
|
||||||
// | None ->
|
stringm.insert({y, name});
|
||||||
// let name = Printf.sprintf "string_%d" scount in
|
++scount;
|
||||||
// let m = M.add x name stringm in
|
}
|
||||||
// (M (D, I, A, name), {<scount = scount + 1; stringm = m>})
|
return M{DataKind::D, Externality::I, Addressed::A, it->second};
|
||||||
}
|
}
|
||||||
|
|
||||||
/* gets all global variables */
|
/* gets all global variables */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue