the bulk of the compiler

This commit is contained in:
Vern Paxson 2021-04-19 16:32:04 -07:00
parent 158e82a2c1
commit 863be9436b
40 changed files with 7730 additions and 0 deletions

View file

@ -0,0 +1,73 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "zeek/script_opt/CPP/Compile.h"
namespace zeek::detail {
void CPPCompile::StartBlock()
{
++block_level;
Emit("{");
}
void CPPCompile::EndBlock(bool needs_semi)
{
Emit("}%s", needs_semi ? ";" : "");
--block_level;
}
std::string CPPCompile::GenString(const char* b, int len) const
{
return std::string("make_intrusive<StringVal>(") + Fmt(len) +
", " + CPPEscape(b, len) + ")";
}
std::string CPPCompile::CPPEscape(const char* b, int len) const
{
std::string res = "\"";
for ( int i = 0; i < len; ++i )
{
unsigned char c = b[i];
switch ( c ) {
case '\a': res += "\\a"; break;
case '\b': res += "\\b"; break;
case '\f': res += "\\f"; break;
case '\n': res += "\\n"; break;
case '\r': res += "\\r"; break;
case '\t': res += "\\t"; break;
case '\v': res += "\\v"; break;
case '\\': res += "\\\\"; break;
case '"': res += "\\\""; break;
default:
if ( isprint(c) )
res += c;
else
{
char buf[8192];
snprintf(buf, sizeof buf, "%03o", c);
res += "\\";
res += buf;
}
break;
}
}
return res + "\"";
}
void CPPCompile::Indent() const
{
for ( auto i = 0; i < block_level; ++i )
fprintf(write_file, "%s", "\t");
}
} // zeek::detail