forked from Xeeynamo/sonic-hybrid-rsdk
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
sonic3air-main/Oxygen/lemonscript/source/lemon/translator/SourceCodeWriter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
|
||
} |