// Copyright (c) 2023 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "CodeGen.hpp" using namespace Dawn; void CodeGen::line( std::vector *out, std::string contents, std::string tabs ) { out->push_back(tabs + contents); } void CodeGen::lines( std::vector *out, std::vector lines, std::string tabs ) { auto itLine = lines.begin(); while(itLine != lines.end()) { line(out, *itLine, tabs); ++itLine; } } void CodeGen::classGen( std::vector *out, struct ClassGenInfo info ) { std::vector buffer; line(out, "#pragma once", ""); line(out, "", ""); // Includes if(info.includes.size() > 0) { // iterate over info.includes std::vector included; auto itInclude = info.includes.begin(); while(itInclude != info.includes.end()) { // skip if already included if(std::find(included.begin(), included.end(), *itInclude) != included.end()) { ++itInclude; continue; } if(itInclude->find("#include") == std::string::npos) { line(out, "#include \"" + *itInclude + "\"", ""); } else { line(out, *itInclude, ""); } included.push_back(*itInclude); ++itInclude; } line(out, "", ""); } line(out, "namespace Dawn {", ""); auto itTemplates = info.classTemplates.begin(); while(itTemplates != info.classTemplates.end()) { line(out, "template<" + itTemplates->second + " " + itTemplates->first + ">", " "); ++itTemplates; } line(out, "class " + info.clazz + (info.extend.size() == 0 ? "{" : " : public " + info.extend + " {" ), " "); if(info.protectedCode.size() > 0) { line(out, "protected:", " "); lines(out, info.protectedProperties, " "); line(out, "", " "); lines(out, info.protectedCode, " "); } if(info.publicCode.size() > 0 || info.constructorArgs.size() > 0 || info.constructorCode.size() > 0) { line(out, "public:", " "); lines(out, info.publicProperties, " "); line(out, "", " "); line(out, info.clazz + "(" + info.constructorArgs + ")" + (info.extend.size() > 0 ? " : " + info.extend + "(" + info.extendArgs + ")" : "") + " {", " "); lines(out, info.constructorCode, " "); line(out, "}", " "); if(info.publicCode.size() > 0) { line(out, "", " "); lines(out, info.publicCode, " "); } } line(out, "};", " "); line(out, "}", ""); } void CodeGen::methodGen( std::vector *out, struct MethodGenInfo info ) { line(out, (info.isStatic ? "static " : "") + info.type + " " + info.name + "(" + info.args + ") " + ( info.isOverride ? "override " : "" ) + "{", ""); lines(out, info.body, " "); line(out, "}", ""); }