Skip to content

Commit

Permalink
Fixed everything for ce-explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed Jan 17, 2024
1 parent 005fdd1 commit fc63fb6
Show file tree
Hide file tree
Showing 31 changed files with 386 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/Debug/snowball",
"args": ["run", "-O0"],
"args": ["test", "-O0"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
Expand Down
1 change: 1 addition & 0 deletions app/commands/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int bench(app::Options::BenchmarkOptions p_opts) {
auto start = high_resolution_clock::now();

// TODO: false if --no-output is passed
compiler->enamblePackageManager(true);
compiler->compile(p_opts.no_progress || p_opts.silent);

auto stop = high_resolution_clock::now();
Expand Down
4 changes: 2 additions & 2 deletions app/commands/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ int build(app::Options::BuildOptions p_opts) {
// TODO: check for output
Compiler* compiler = new Compiler(content, filename);
compiler->initialize();
std::string output = _SNOWBALL_OUT_DEFAULT(package_name, p_opts.emit_type, !compiler->getGlobalContext()->isDynamic);
std::string output = _SNOWBALL_OUT_DEFAULT(package_name, p_opts.emit_type, !compiler->getGlobalContext().isDynamic);
if (!p_opts.output.empty()) { output = p_opts.output; }
compiler->setOptimization(p_opts.opt);
if (p_opts.is_test) { compiler->enable_tests(); }

auto start = high_resolution_clock::now();

compiler->enamblePackageManager(p_opts.file.empty());
compiler->compile(p_opts.no_progress || p_opts.silent);
auto stop = high_resolution_clock::now();

Expand Down
1 change: 1 addition & 0 deletions app/commands/run.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ int run(app::Options::RunOptions p_opts) {
compiler->setOptimization(p_opts.opt);

// TODO: false if --no-output is passed
compiler->enamblePackageManager(p_opts.file.empty());
compiler->compile(p_opts.no_progress || p_opts.silent);
compiler->emitBinary(output, false);

Expand Down
2 changes: 1 addition & 1 deletion app/commands/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int test(app::Options::TestOptions p_opts) {

auto start = high_resolution_clock::now();

// TODO: false if --no-output is passed
compiler->enamblePackageManager(true);
compiler->compile(p_opts.no_progress || p_opts.silent);

auto stop = high_resolution_clock::now();
Expand Down
16 changes: 16 additions & 0 deletions src/ast/syntax/nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,10 @@ struct EnumTypeDef : public AcceptorExtend<EnumTypeDef, Base>,
std::string name;
/// @brief Defined enum fields
std::vector<std::pair<std::string, std::vector<Expression::TypeRef*>>> fields;
/// @brief Enum's extended type method's
std::vector<FunctionDef*> methods;
/// @brief Enum's extended implementations
std::vector<Expression::TypeRef*> impls;
public:
EnumTypeDef(std::string name, Privacy::Status prvc = PRIVATE);

Expand All @@ -786,6 +790,18 @@ struct EnumTypeDef : public AcceptorExtend<EnumTypeDef, Base>,
FieldIterator fieldStart();
FieldIterator fieldEnd();

/// @return A full list of declared methods
std::vector<FunctionDef*>& getMethods();
/// @brief Add a method to the enum
/// Yes, enums can have methods!
/// Methods can be defined by using `class extends` to
/// extend the enum.
void addMethod(FunctionDef* method);
/// @brief Add an implementation to the enum
void addImpl(Expression::TypeRef* impl);
/// @return Get enum's implementations
std::vector<Expression::TypeRef*> getImpls() const;

// Set an acceptance call
ACCEPT()
};
Expand Down
4 changes: 4 additions & 0 deletions src/ast/syntax/stmts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ std::vector<std::string> ImportStmt::getPath() const { return path; }
std::string ImportStmt::getExportSymbol() const { return exportSymbol; }
Comment* CommentHolder::getComment() const { return comment; }
void CommentHolder::setComment(Comment* comment) { this->comment = comment; }
std::vector<FunctionDef*>& EnumTypeDef::getMethods() { return methods; }
void EnumTypeDef::addMethod(FunctionDef* method) { methods.push_back(method); }
void EnumTypeDef::addImpl(Expression::TypeRef* impl) { impls.push_back(impl); }
std::vector<Expression::TypeRef*> EnumTypeDef::getImpls() const { return impls; }

} // namespace Statement
} // namespace Syntax
Expand Down
4 changes: 2 additions & 2 deletions src/builder/linker/Linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Linker {
/// @brief Vector to store the names of linked libraries.
std::vector<std::string> linkedLibraries;
/// @brief Pointer to the global context.
GlobalContext* ctx;
GlobalContext ctx;
/// @brief The target triple.
llvm::Triple target;
/// @brief A list of rpaths to be added to the executable.
Expand All @@ -41,7 +41,7 @@ class Linker {
*
* This constructor initializes the Linker object with default settings.
*/
Linker(GlobalContext* ctx, std::string ldPath = LD_PATH);
Linker(GlobalContext ctx, std::string ldPath = LD_PATH);
/**
* @brief Default destructor for the Linker class.
*
Expand Down
4 changes: 2 additions & 2 deletions src/builder/linker/common/ld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace snowball {
namespace linker {

Linker::Linker(GlobalContext* ctx, std::string ldPath) : ctx(ctx), ldPath(ldPath) {
Linker::Linker(GlobalContext ctx, std::string ldPath) : ctx(ctx), ldPath(ldPath) {
target = llvm::Triple(llvm::sys::getProcessTriple());
}
void Linker::addLibrary(std::string& library) { linkedLibraries.push_back(library); }
Expand All @@ -24,7 +24,7 @@ int Linker::link(std::string& input, std::string& output, std::vector<std::strin
if (ldstatus) { throw SNError(LINKER_ERR, Logger::format("Linking with " LD_PATH " failed with code %d", ldstatus)); }

#if __APPLE__
if (ctx->opt == app::Options::Optimization::OPTIMIZE_O0) os::Driver::run({"dsymutil", output});
if (ctx.opt == app::Options::Optimization::OPTIMIZE_O0) os::Driver::run({"dsymutil", output});
#endif

return EXIT_SUCCESS;
Expand Down
12 changes: 6 additions & 6 deletions src/builder/linker/linux/ld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace linker {
void Linker::constructLinkerArgs(std::string& input, std::string& output, std::vector<std::string>& args) {
const bool isIAMCU = target.isOSIAMCU();
linkerArgs.clear();
if (ctx->isDynamic) {
if (ctx.isDynamic) {
// linkerArgs.push_back("-pic");
linkerArgs.push_back("--export-dynamic");
linkerArgs.push_back("-m");
Expand All @@ -29,7 +29,7 @@ void Linker::constructLinkerArgs(std::string& input, std::string& output, std::v
linkerArgs.push_back("-z");
linkerArgs.push_back("text");
}
if (ctx->withStd) {
if (ctx.withStd) {
// TODO: check if this works for all platforms
linkerArgs.push_back("-dynamic-linker");

Expand Down Expand Up @@ -97,20 +97,20 @@ void Linker::constructLinkerArgs(std::string& input, std::string& output, std::v
}
}
linkerArgs.push_back(input);
if (ctx->isThreaded) linkerArgs.push_back("-lpthread");
if (ctx.isThreaded) linkerArgs.push_back("-lpthread");
for (auto& arg : args) linkerArgs.push_back(arg);
// TODO: should this be with ctc->withStd?
linkerArgs.push_back("--eh-frame-hdr");
if (ctx->withStd) {
if (ctx.withStd) {
for (auto llvmArg : utils::split(LLVM_LDFLAGS, " ")) { linkerArgs.push_back(llvmArg); }
}
if (!ctx->isDynamic) linkerArgs.push_back("-static");
if (!ctx.isDynamic) linkerArgs.push_back("-static");
for (auto& rpath : rpaths) linkerArgs.push_back("--rpath=" + rpath);
for (auto& lib : linkedLibraries) {
linkerArgs.push_back("-l:" + lib);
DEBUG_CODEGEN("Linking library: %s", lib.c_str());
}
if (ctx->withCXXStd) {
if (ctx.withCXXStd) {
auto libs = utils::get_lib_folder() / ".." / _SNOWBALL_LIBRARY_OBJ;
linkerArgs.push_back("-L/usr/lib/../lib64");
linkerArgs.push_back("-L/lib/../lib64");
Expand Down
6 changes: 3 additions & 3 deletions src/builder/linker/macos/ld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ void Linker::constructLinkerArgs(std::string& input, std::string& output, std::v
linkerArgs.push_back("-l:" + lib);
DEBUG_CODEGEN("Linking library: %s", lib.c_str());
}
if (ctx->withCXXStd) {
if (ctx.withCXXStd) {
auto libs = utils::get_lib_folder() / ".." / _SNOWBALL_LIBRARY_OBJ;
linkerArgs.push_back("-L" + libs.string());
linkerArgs.push_back("-lsnowballrt");
}
linkerArgs.push_back(input);
for (auto& arg : args) linkerArgs.push_back(arg);
if (ctx->withStd) {
if (ctx.withStd) {
for (auto llvmArg : utils::split(LLVM_LDFLAGS, " ")) { linkerArgs.push_back(llvmArg); }
}
if (!ctx->isDynamic)
if (!ctx.isDynamic)
linkerArgs.push_back("-static");

linkerArgs.push_back("-arch");
Expand Down
14 changes: 7 additions & 7 deletions src/builder/llvm/DIHelpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ llvm::DISubprogram* LLVMBuilder::getDISubprogramForFunc(ir::Func* x) {

llvm::DIType* LLVMBuilder::getDIType(types::Type* ty) {
if (auto intTy = cast<types::IntType>(ty)) {
return dbg.builder->createBasicType(ty->getName(), ty->sizeOf() * 8, intTy->isSigned() ? llvm::dwarf::DW_ATE_signed : llvm::dwarf::DW_ATE_unsigned);
return dbg.builder->createBasicType(ty->getName(), ty->sizeOf(), intTy->isSigned() ? llvm::dwarf::DW_ATE_signed : llvm::dwarf::DW_ATE_unsigned);
} else if (is<types::FloatType>(ty)) {
return dbg.builder->createBasicType(ty->getName(), ty->sizeOf() * 8, llvm::dwarf::DW_ATE_float);
return dbg.builder->createBasicType(ty->getName(), ty->sizeOf(), llvm::dwarf::DW_ATE_float);
} else if (cast<types::VoidType>(ty)) {
return nullptr;
} else if (auto x = cast<types::ReferenceType>(ty)) {
auto type = getDIType(x->getPointedType());
return dbg.builder->createPointerType(type, ty->sizeOf() * 8);
return dbg.builder->createReferenceType(llvm::dwarf::DW_TAG_reference_type, type, ty->sizeOf());
} else if (auto x = cast<types::PointerType>(ty)) {
auto type = getDIType(x->getPointedType());
return dbg.builder->createPointerType(type, ty->sizeOf() * 8);
return dbg.builder->createPointerType(type, ty->sizeOf());
}

else if (auto f = Syntax::Transformer::getFunctionType(ty)) {
Expand All @@ -63,7 +63,7 @@ llvm::DIType* LLVMBuilder::getDIType(types::Type* ty) {
for (auto argType : f->getArgs()) { argTypes.push_back(getDIType(argType)); }

auto subroutineType = dbg.builder->createSubroutineType(llvm::MDTuple::get(*context, argTypes));
return dbg.builder->createPointerType(subroutineType, ty->sizeOf() * 8);
return dbg.builder->createPointerType(subroutineType, ty->sizeOf());
} else if (auto c = cast<types::TypeAlias>(ty)) {
return getDIType(c->getBaseType());
} else if (auto e = cast<types::EnumType>(ty)) {
Expand All @@ -75,7 +75,7 @@ llvm::DIType* LLVMBuilder::getDIType(types::Type* ty) {
e->getPrettyName(),
file,
dbgInfo->line,
ty->sizeOf(),
8,
ty->alignmentOf(),
dbg.builder->getOrCreateArray(vector_iterate<types::EnumType::EnumField, llvm::Metadata*>(
e->getFields(),
Expand Down Expand Up @@ -124,7 +124,7 @@ llvm::DIType* LLVMBuilder::getDIType(types::Type* ty) {
t->name,
file,
dbgInfo->line,
t->type->sizeOf() * 8,
t->type->sizeOf(),
/*AlignInBits=*/0,
0,
llvm::DINode::FlagZero,
Expand Down
3 changes: 3 additions & 0 deletions src/builder/llvm/buildSwitch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ void LLVMBuilder::visit(ir::Switch* switchStmt) {
if (!switchStmt->isCStyleSwitch()) {
assert(enumType != nullptr);
initExpr = build(switchStmt->getExpr().get());
if (!initExpr->getType()->isPointerTy() && llvm::isa<llvm::LoadInst>(initExpr)) {
initExpr = llvm::cast<llvm::LoadInst>(initExpr)->getPointerOperand();
}
auto exprIndex = builder->CreateStructGEP(getLLVMType(enumType), initExpr, 0);
exprValue = builder->CreateLoad(builder->getInt8Ty(), exprIndex, ".switch-index");
} else exprValue = expr(switchStmt->getExpr().get());
Expand Down
11 changes: 8 additions & 3 deletions src/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ void Compiler::initialize() {
initialized = true;
createSourceInfo();

globalContext = new GlobalContext();
globalContext->isTest = testsEnabled;
globalContext->isBench = benchmarkEnabled;
globalContext = GlobalContext();
globalContext.isTest = testsEnabled;
globalContext.isBench = benchmarkEnabled;

configFolder = cwd / ".sn";
if (!fs::exists(configFolder)) fs::create_directory(configFolder);
Expand Down Expand Up @@ -134,6 +134,7 @@ void Compiler::compile(bool silent) {
using recursive_directory_iterator = std::filesystem::recursive_directory_iterator;

void Compiler::runPackageManager(bool silent) {
if (!globalContext.packageManagerEnabled) return;
auto package = getConfiguration();
auto manager = new pm::Manager(package, silent, cwd, configFolder);
manager->runAsMain();
Expand Down Expand Up @@ -250,6 +251,10 @@ toml::parse_result Compiler::getConfiguration() {
);
}

void Compiler::enamblePackageManager(bool enable) {
globalContext.packageManagerEnabled = enable;
}

void Compiler::cleanup() { }

int Compiler::emitObject(std::string out, bool log) {
Expand Down
9 changes: 6 additions & 3 deletions src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct GlobalContext {
bool withStd = true;
bool withCXXStd = true;
bool isThreaded = false;
bool packageManagerEnabled = false;

bool isDynamic = true;
app::Options::Optimization opt = app::Options::Optimization::OPTIMIZE_O0;
Expand All @@ -57,7 +58,7 @@ class Compiler {
fs::path cwd;
app::Options::Optimization opt_level;

GlobalContext* globalContext;
GlobalContext globalContext;

const SourceInfo* srcInfo = (snowball::SourceInfo*) nullptr;
bool initialized = false;
Expand Down Expand Up @@ -96,10 +97,12 @@ class Compiler {
int emitSnowballIr(std::string, bool = true);
int emitDocs(std::string, std::string, BasicPackageInfo, bool = true);

GlobalContext* getGlobalContext() { return globalContext; }
void enamblePackageManager(bool);

GlobalContext& getGlobalContext() { return globalContext; }

void setOptimization(app::Options::Optimization o) {
globalContext->opt = o;
globalContext.opt = o;
opt_level = o;
}

Expand Down
17 changes: 9 additions & 8 deletions src/parser/parseClass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {
if (isInterface) {
createError<SYNTAX_ERROR>("Interfaces can't have constants!");
} else if (extends) {
createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* constants!");
createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* constants!");
}

auto var = parseConstant();
Expand All @@ -151,7 +151,7 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {

case TokenType::KWORD_ENUM: {
if (extends) {
createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* enums!");
createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* enums!");
} else if (isInterface) {
createError<SYNTAX_ERROR>("Interfaces can't have enums!");
}
Expand Down Expand Up @@ -222,7 +222,7 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {
createError<ARGUMENT_ERROR>("Virtual methods cant be static!");
} else if (extends) {
next();
createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* virtual methods!");
createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* virtual methods!");
} else if (pk.type != TokenType::KWORD_FUNC && pk.type != TokenType::KWORD_MUTABLE) {
next();
createError<SYNTAX_ERROR>("Expected keyword \"fn\" or \"mut\" after virtual declaration!");
Expand All @@ -236,7 +236,7 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {
cls->addVariable(var);

assert_tok<TokenType::SYM_SEMI_COLLON>("a ';'");
if (extends) { createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* variables!"); }
if (extends) { createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* variables!"); }
} break;

case TokenType::BRACKET_RCURLY: {
Expand All @@ -250,7 +250,7 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {
case TokenType::KWORD_TYPEDEF: {
assertNoAttributes("a type alias");
if (extends) {
createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* type aliases!");
createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* type aliases!");
} else if (isInterface) {
createError<SYNTAX_ERROR>("Interfaces can't have type aliases!");
}
Expand All @@ -265,7 +265,7 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {
case TokenType::KWORD_CLASS:
case TokenType::KWORD_INTER: {
if (extends) {
createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* classes!");
createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* classes!");
} else if (isInterface) {
createError<SYNTAX_ERROR>("Interfaces can't have classes!");
}
Expand All @@ -277,7 +277,7 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {

case TokenType::KWORD_STRUCT: {
if (extends) {
createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* structs!");
createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* structs!");
} else if (isInterface) {
createError<SYNTAX_ERROR>("Interfaces can't have structs!");
}
Expand All @@ -294,7 +294,8 @@ Syntax::Statement::DefinedTypeDef* Parser::parseClass() {
if (isInterface) {
createError<SYNTAX_ERROR>("Interfaces can't have constructors!");
} else if (extends) {
createError<SYNTAX_ERROR>("Classes that extend other types can't have *new* constructors!");
// NOTE: If we end up allowing this, we need to make sure enums can't have constructors!
createError<SYNTAX_ERROR>("Classes that extend other types cant have *new* constructors!");
}
hasConstructor = true;
auto func = parseFunction(true);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parseGlobal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Parser::NodeVec Parser::parseGlobal(TokenType terminator) {
createError<SYNTAX_ERROR>("expected keyword \"func\", \"static\", \"unsafe\", \"class\", "
"\"let\", \"const\", \"enum\" "
"or "
"\"extern\" after public/private declaration");
"\"external\" after public/private declaration");
}

break;
Expand Down
Loading

0 comments on commit fc63fb6

Please sign in to comment.