Skip to content

Commit

Permalink
Initial implementation of SCode in C++ (#11428)
Browse files Browse the repository at this point in the history
- Implements classes for the SCode/Absyn records used by the frontend,
  so far mostly just handling the conversion from MetaModelica to C++
  and implementing some simple printing. Everything is merged into one
  Absyn namespace since the long term goal is to get rid of SCode and
  only have a single abstract syntax representation.
  • Loading branch information
perost committed Oct 23, 2023
1 parent d0d1bac commit 7d33402
Show file tree
Hide file tree
Showing 70 changed files with 5,831 additions and 30 deletions.
23 changes: 23 additions & 0 deletions OMCompiler/Compiler/FrontEndCpp/Absyn/Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "Util.h"
#include "Algorithm.h"

using namespace OpenModelica;
using namespace OpenModelica::Absyn;

Algorithm::Algorithm(MetaModelica::Record value)
: _statements{value[0].mapVector<Statement>()}
{

}

std::ostream& OpenModelica::Absyn::operator<< (std::ostream &os, const Algorithm &algorithm)
{
os << "algorithm\n";

for (auto &s: algorithm.statements()) {
s.print(os, " ");
os << ";\n";
}

return os;
}
23 changes: 23 additions & 0 deletions OMCompiler/Compiler/FrontEndCpp/Absyn/Algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef ABSYN_ALGORITHM_H
#define ABSYN_ALGORITHM_H

#include "MetaModelica.h"
#include "Statement.h"

namespace OpenModelica::Absyn
{
class Algorithm
{
public:
Algorithm(MetaModelica::Record value);

const std::vector<Statement>& statements() const noexcept { return _statements; }

private:
std::vector<Statement> _statements;
};

std::ostream& operator<< (std::ostream &os, const Algorithm &algorithm);
};

#endif /* ABSYN_ALGORITHM_H */
24 changes: 24 additions & 0 deletions OMCompiler/Compiler/FrontEndCpp/Absyn/Annotation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <ostream>

#include "Annotation.h"

using namespace OpenModelica::Absyn;

Annotation::Annotation(MetaModelica::Record value)
: _modifier{value[0]}
{

}

void Annotation::print(std::ostream &os, std::string_view indent) const noexcept
{
if (!_modifier.isEmpty()) {
os << indent << "annotation" << _modifier;
}
}

std::ostream& OpenModelica::Absyn::operator<< (std::ostream &os, const Annotation &annotation) noexcept
{
annotation.print(os);
return os;
}
29 changes: 29 additions & 0 deletions OMCompiler/Compiler/FrontEndCpp/Absyn/Annotation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef ABSYN_ANNOTATION_H
#define ABSYN_ANNOTATION_H

#include <string_view>
#include <iosfwd>

#include "MetaModelica.h"
#include "Modifier.h"

namespace OpenModelica::Absyn
{
class Annotation
{
public:
Annotation() = default;
Annotation(MetaModelica::Record value);

const Modifier& modifier() const noexcept { return _modifier; }

void print(std::ostream &os, std::string_view indent = {}) const noexcept;

private:
Modifier _modifier;
};

std::ostream& operator<< (std::ostream &os, const Annotation &annotation) noexcept;
}

#endif /* ANNOTATION_H */
35 changes: 35 additions & 0 deletions OMCompiler/Compiler/FrontEndCpp/Absyn/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
set(OMC_FRONTEND_CPP_ABSYN_SOURCES
Algorithm.cpp
Annotation.cpp
Class.cpp
ClassDef.cpp
Comment.cpp
Component.cpp
ComponentRef.cpp
ConstrainingClass.cpp
DefineUnit.cpp
Element.cpp
ElementAttributes.cpp
ElementPrefixes.cpp
Equation.cpp
Expression.cpp
Extends.cpp
ExternalDecl.cpp
FunctionArgs.cpp
FunctionArgsList.cpp
FunctionArgsIter.cpp
Import.cpp
ImportPath.cpp
Iterator.cpp
Modifier.cpp
Operator.cpp
Statement.cpp
Subscript.cpp
TypeSpec.cpp)

add_library(omcfrontendcppabsyn STATIC)
add_library(omc::compiler::frontendcpp::absyn ALIAS omcfrontendcppabsyn)

target_sources(omcfrontendcppabsyn PRIVATE ${OMC_FRONTEND_CPP_ABSYN_SOURCES})

target_include_directories(omcfrontendcppabsyn PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
40 changes: 40 additions & 0 deletions OMCompiler/Compiler/FrontEndCpp/Absyn/Class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <ostream>

#include "Class.h"

using namespace OpenModelica::Absyn;

Class::Class(MetaModelica::Record value)
: Element::Base(SourceInfo{value[7]}),
_name{value[0].toString()},
_prefixes{value[1]},
_encapsulated{value[2]},
_partial{value[3]},
_restriction{value[4]},
_classDef{value[5]},
_comment{value[6]}
{

}

const std::string& Class::name() const noexcept
{
return _name;
}

const Comment& Class::comment() const noexcept
{
return _comment;
}

std::unique_ptr<Element::Base> Class::clone() const noexcept
{
return std::make_unique<Class>(*this);
}

void Class::print(std::ostream &os, Each each) const noexcept
{
_prefixes.print(os, each);
os << _encapsulated.unparse() << _partial.unparse() << _restriction << ' ';
_classDef.print(os, *this);
}
37 changes: 37 additions & 0 deletions OMCompiler/Compiler/FrontEndCpp/Absyn/Class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef ABSYN_CLASS_H
#define ABSYN_CLASS_H

#include <string>

#include "MetaModelica.h"
#include "Element.h"
#include "ElementPrefixes.h"
#include "Restriction.h"
#include "ClassDef.h"
#include "Comment.h"

namespace OpenModelica::Absyn
{
class Class : public Element::Base
{
public:
Class(MetaModelica::Record value);

const std::string& name() const noexcept;
const Comment& comment() const noexcept;

std::unique_ptr<Element::Base> clone() const noexcept override;
void print(std::ostream &os, Each each) const noexcept override;

private:
std::string _name;
ElementPrefixes _prefixes;
Encapsulated _encapsulated;
Partial _partial;
Restriction _restriction;
ClassDef _classDef;
Comment _comment;
};
}

#endif /* ABSYN_CLASS_H */

0 comments on commit 7d33402

Please sign in to comment.