Skip to content

Commit

Permalink
Added "create" command
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNachoBIT committed Mar 1, 2023
1 parent 55d0710 commit b10a7df
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 22 deletions.
59 changes: 58 additions & 1 deletion Language/CodeGen.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "CodeGen.hpp"
#include "AST.hpp"
#include <filesystem>

#ifdef _WIN32
#include <Windows.h>
Expand Down Expand Up @@ -41,7 +42,7 @@ void CodeGen::Build()
llvm::raw_fd_ostream dest("output.ll", EC, llvm::sys::fs::OF_None);

TheModule->print(dest, nullptr);
clangCmd = "clang++ output.ll -o result";
clangCmd = "clang++ output.ll -Wno-override-module -o result";

std::cout << "Compiling...\n";

Expand All @@ -55,6 +56,62 @@ void CodeGen::Print()
TheModule->print(llvm::outs(), nullptr);
}

void CodeGen::Run()
{
CodeGen::Build();

#ifdef _WIN32
// additional information
STARTUPINFO si;
PROCESS_INFORMATION pi;

// set the size of the structures
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

std::string resultPath = std::string(std::filesystem::current_path().string() + "/result.exe");

// start the program up
auto res = CreateProcess( NULL, // the path
(LPSTR)resultPath.c_str(), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
);
// Close process and thread handles.

if(!res)
{
std::cout << "Error: Program can't be executed.\n";
exit(1);
}
else
{
unsigned long exitCode;

WaitForSingleObject(
pi.hProcess,
INFINITE // time-out interval in milliseconds
);
GetExitCodeProcess(pi.hProcess, &exitCode);

std::cout << "Your Program Returned: " << (int)exitCode << "\n";
}

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

#else
std::cout << "The 'run' command is not supported in your current OS yet.\n";
#endif
}

llvm::Function* CodeGen::GetFunction(std::string name)
{
if(auto* F = TheModule->getFunction(name)) { return F; }
Expand Down
2 changes: 2 additions & 0 deletions Language/CodeGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct CodeGen

static void Print();

static void Run();

static llvm::Function* GetFunction(std::string name);
};

Expand Down
7 changes: 7 additions & 0 deletions MyFirstProject/Nucleus.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
name = "MyFirstProject"
version = "1.0.0"

[dependencies]


4 changes: 4 additions & 0 deletions MyFirstProject/main.nk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn i32 main() {
return 0;
}

68 changes: 68 additions & 0 deletions Tooling/Project.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "Project.hpp"
#include <iostream>
#include <fstream>
#include <filesystem>

void Project::CreateTOML(std::string name)
{
std::string path = name + "/Nucleus.toml";

std::string content = "[project]\n";
content += "name = \"" + name + "\"\n";
content += "version = \"1.0.0\"\n\n";

content += "[dependencies]\n\n";

//content += "[imports]\n";
//content += "folders = [\n";
//content += "\t\"Nucleus.Std\"\n";
//content += "]\n";

std::ofstream o(path.c_str());

o << content << "\n";
}

void Project::CreateMainNk(std::string name)
{
std::string path = name + "/main.nk";

std::string content = "fn i32 main() {\n";
content += "\treturn 0;\n";
content += "}\n";

std::ofstream o(path.c_str());

o << content << "\n";
}

void Project::Create(std::string name)
{
for(auto c : name)
{
if(c < 32)
continue;

if(!isalpha(c))
{
std::cout << "Error: Sorry, but the name of the project can't have special characters, only letters and numbers are allowed!\n";
exit(1);
}
}

if(std::filesystem::exists(name))
{
std::cout << "Error: Sorry, but the folder '" << name << "' already exists...\n";
exit(1);
}

std::cout << "Creating \"" << name << "\"...\n";

std::filesystem::create_directory(name);

CreateTOML(name);

CreateMainNk(name);

std::cout << "Project \"" << name << "\" successfully created!\n";
}
13 changes: 13 additions & 0 deletions Tooling/Project.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef PROJECT_HPP
#define PROJECT_HPP

#include <string>

struct Project
{
static void Create(std::string name);
static void CreateTOML(std::string name);
static void CreateMainNk(std::string name);
};

#endif
2 changes: 1 addition & 1 deletion compilerCommand.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/d/msys64/mingw64/bin/clang++ -g -O3 Language/*.cpp *.cpp `/d/msys64/mingw64/bin/llvm-config --cxxflags --link-static --ldflags --system-libs --libs all` -fstack-protector -lssp -frtti -std=c++20 -static -o nucleus
/d/msys64/mingw64/bin/clang++ -g -O3 Language/*.cpp Tooling/*.cpp *.cpp `/d/msys64/mingw64/bin/llvm-config --cxxflags --link-static --ldflags --system-libs --libs all` -fstack-protector -lssp -frtti -std=c++20 -static -o nucleus
9 changes: 9 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Language/Parser.hpp"
#include "Language/AST.hpp"
#include "Language/CodeGen.hpp"
#include "Tooling/Project.hpp"
#include <fstream>

void CompileToLLVMIR()
Expand Down Expand Up @@ -44,6 +45,14 @@ int main(int argc, char const *argv[])
CompileToLLVMIR();
CodeGen::Run();
}
else if(cmd == "create")
{
if(argc > 2)
{
Project::Create(argv[2]);
}
else { std::cout << "Error: Name not provided!\n"; }
}
}

return 0;
Expand Down
21 changes: 12 additions & 9 deletions main.ll
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
; ModuleID = 'Nucleus'
source_filename = "Nucleus"

define i32 @addTwo() {
entry:
ret i32 2
}

define i32 @main() {
entry:
%variableName = alloca i32, align 4
store i32 0, ptr %variableName, align 4
store i32 2, ptr %variableName, align 4
%variableName1 = load i32, ptr %variableName, align 4
%addtmp = add i32 %variableName1, 1
%addtmp = add i32 %variableName1, 2
store i32 %addtmp, ptr %variableName, align 4
%state1 = load i32, ptr %variableName, align 4
%addtmp2 = add i32 %state1, 1
%state2 = load i32, ptr %variableName, align 4
%addtmp3 = add i32 %state2, 2
%state3 = load i32, ptr %variableName, align 4
%addtmp4 = add i32 %state3, 3
ret i32 %addtmp2
%autoLoad = load i32, ptr %variableName, align 4
ret i32 %autoLoad
}
4 changes: 1 addition & 3 deletions main.nk
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ fn i32 main()
{
alloc variableName: i32 = 2;

variableName += 1;

load lastVariable: i32 = variableName;
variableName += 2;

return verify variableName;
}
Binary file modified nucleus.exe
Binary file not shown.
19 changes: 11 additions & 8 deletions output.ll
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
; ModuleID = 'Nucleus'
source_filename = "Nucleus"

define i32 @addTwo() {
entry:
ret i32 2
}

define i32 @main() {
entry:
%variable = alloca i32, align 4
store i32 2, ptr %variable, align 4
%variable1 = load i32, ptr %variable, align 4
%addtmp = add i32 %variable1, 4
%addtmp2 = add i32 %addtmp, 6
%addtmp3 = add i32 %addtmp2, 10
store i32 %addtmp3, ptr %variable, align 4
%autoLoad = load i32, ptr %variable, align 4
%variableName = alloca i32, align 4
store i32 2, ptr %variableName, align 4
%variableName1 = load i32, ptr %variableName, align 4
%addtmp = add i32 %variableName1, 2
store i32 %addtmp, ptr %variableName, align 4
%autoLoad = load i32, ptr %variableName, align 4
ret i32 %autoLoad
}
Binary file modified result.exe
Binary file not shown.

0 comments on commit b10a7df

Please sign in to comment.