Skip to content

Commit

Permalink
k
Browse files Browse the repository at this point in the history
  • Loading branch information
CCIGAMES committed Mar 2, 2024
1 parent 79a85b1 commit d4b4fa8
Showing 1 changed file with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Part of the Oxygen Engine / Sonic 3 A.I.R. software distribution.
* Copyright (C) 2017-2024 by Eukaryot
*
* Published under the GNU GPLv3 open source software license, see license.txt
* or https://www.gnu.org/licenses/gpl-3.0.en.html
*/

#include "lemon/pch.h"
#include "lemon/translator/SourceCodeWriter.h"
#include "lemon/program/Function.h"


namespace lemon
{

void SourceCodeWriter::writeLine(const String& line)
{
for (int i = 0; i < mIndentation; ++i)
mOutput << "\t";
mOutput << line << "\r\n";
}

void SourceCodeWriter::writeEmptyLine()
{
mOutput << "\r\n";
}

void SourceCodeWriter::beginBlock(const char* lineContent)
{
writeLine(lineContent);
increaseIndentation();
}

void SourceCodeWriter::endBlock(const char* lineContent)
{
decreaseIndentation();
writeLine(lineContent);
}



void CppWriter::writeFunctionHeader(const Function& function)
{
String line;
addDataType(line, function.getReturnType());
line << " ";
addIdentifier(line, function.getName().getString());
line << "(";
for (size_t i = 0; i < function.getParameters().size(); ++i)
{
if (i > 0)
line << ", ";
addDataType(line, function.getParameters()[i].mDataType);
line << " ";
addIdentifier(line, function.getParameters()[i].mName.getString());
}
line << ")";
writeLine(line);
}

void CppWriter::addDataType(String& line, const DataTypeDefinition* dataType)
{
if (dataType->getClass() == DataTypeDefinition::Class::VOID)
{
line << "void";
}
else if (dataType->getClass() == DataTypeDefinition::Class::INTEGER)
{
const IntegerDataType& integerType = dataType->as<IntegerDataType>();
if (integerType.mSemantics == IntegerDataType::Semantics::BOOLEAN)
{
line << "bool";
}
else
{
switch (integerType.getBytes())
{
case 1: line << (integerType.mIsSigned ? "int8" : "uint8" ); break;
case 2: line << (integerType.mIsSigned ? "int16" : "uint16"); break;
case 4: line << (integerType.mIsSigned ? "int32" : "uint32"); break;
case 8: line << (integerType.mIsSigned ? "int64" : "uint64"); break;
}
}
}
}

void CppWriter::addIdentifier(String& line, std::string_view identifier)
{
for (size_t i = 0; i < identifier.length(); ++i)
{
if (identifier[i] == '.')
line << '_';
else
line << identifier[i];
}
}

}

0 comments on commit d4b4fa8

Please sign in to comment.