Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions .github/workflows/ci.yml

This file was deleted.

74 changes: 0 additions & 74 deletions CMakeLists.txt

This file was deleted.

13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ uint8 b = a + 1;
uint8 c = a + b + 1;
```

## Printing

Values can be printed to standard out using a print statement, proceeded by the value wanted to print.

```
print 1;

uint8 a = 2;
print a;
```

## Examples

Examples can be found in the `/examples` folder.
Expand All @@ -90,7 +79,6 @@ String segments are converted to a list of tokens. Representing the smallest seg
| Identifier | `IDENTIFIER` | Variable identifier |
| Number | `NUMBER` | Integer |
| Return | `RETURN` | Returns a value from a function |
| Print | `PRINT` | Prints a value |

## Syntax Analyser

Expand All @@ -102,7 +90,6 @@ Combines tokens into statements.
| Assignment | `ASSIGNMENT` | Sets a variable to a value |
| Return | `RETURN` | Returns a value from a function |
| Addition | `ADDITION` | Adds two values and assigns them to an identifier |
| Print | `PRINT` | Prints the value after it |

## `LLVM` Intermediate Representation

Expand Down
21 changes: 9 additions & 12 deletions src/generation/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Program.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <iostream>
#include <llvm/CodeGen/TargetPassConfig.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Module.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Support/FileSystem.h>
Expand All @@ -56,7 +60,9 @@ class Generator {
llvm::InitializeNativeTargetAsmParser();
}

std::unique_ptr<llvm::Module> buildModule(llvm::LLVMContext& context) {
void compile() {

llvm::LLVMContext context;

std::unique_ptr<llvm::Module> module =
std::make_unique<llvm::Module>("build", context);
Expand Down Expand Up @@ -358,7 +364,6 @@ class Generator {
}

builder.store(builder.add(lhs, rhs, outName), outUint.getAlloc());
break;
}
case StatementType::PRINT: {
const PrintStatement& printStatement =
Expand Down Expand Up @@ -407,16 +412,8 @@ class Generator {
builder.createReturn(returnValue);
}

return module;
}

void print_module(std::unique_ptr<llvm::Module> module) {
std::cout << "-- LLVM IR --" << std::endl;
module->print(llvm::outs(), nullptr);
}

void compile(llvm::LLVMContext& context,
std::unique_ptr<llvm::Module> module) {

auto targetTriple = llvm::Triple(llvm::sys::getDefaultTargetTriple());
module->setTargetTriple(targetTriple);
Expand All @@ -429,7 +426,7 @@ class Generator {
auto CPU = "generic";
auto features = "";
llvm::TargetOptions opt;
std::optional<llvm::Reloc::Model> RM = llvm::Reloc::PIC_;
std::optional<llvm::Reloc::Model> RM = std::nullopt;

llvm::TargetMachine* targetMachine =
target->createTargetMachine(targetTriple, CPU, features, opt, RM);
Expand Down
3 changes: 2 additions & 1 deletion src/lexer/lexer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <iostream>
#include <string>

#include "./string_converter.hpp"
Expand Down Expand Up @@ -58,7 +59,7 @@ class Lexer {
tokens.addReturn(ReturnToken());
} else if (buffer == "print") {
tokens.addPrint(PrintToken());
} else if (buffer.size() != 0) {
} else {
tokens.addIdentifier(IdentifierToken(buffer));
}

Expand Down
27 changes: 3 additions & 24 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <llvm/Support/raw_ostream.h>

#include <iostream>
#include <memory>

#include "./io/file_reader.hpp"
#include "./lexer/lexer.hpp"
Expand All @@ -18,14 +17,6 @@ int main(const int argc, char* argv[]) {
return 1;
}

bool verbose = false;

if (argc == 3) {
if (std::string(argv[2]) == "-v") {
verbose = true;
}
}

FileReader fileReader(argv[1]);
if (!fileReader.isOpen()) {
std::cerr << "Input file failed to open\n";
Expand All @@ -34,26 +25,14 @@ int main(const int argc, char* argv[]) {

Lexer lexer(fileReader.readAll());
TokenContainer tokens = lexer.makeTokenList();
if (verbose) {
tokens.print();
}
tokens.print();

AbstractSyntaxTree ast(tokens);

Program program = ast.parse();
if (verbose) {
program.print();
}
program.print();

Generator generator(program);
generator.init();

llvm::LLVMContext context;
std::unique_ptr<llvm::Module> module = generator.buildModule(context);

if (verbose) {
generator.print_module(std::move(module));
}

generator.compile(context, std::move(module));
generator.compile();
}
8 changes: 8 additions & 0 deletions src/syntax_analyser/abstract_syntax_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ std::vector<std::unique_ptr<Statement>> AbstractSyntaxTree::evaluateOperations(
const Token& secondToken =
static_cast<const Token&>(tokens[tokensIndex + 1].get());

std::cout << secondToken.tokenType << "\n";

if (secondToken.tokenType != TokenType::NUMBER &&
secondToken.tokenType != TokenType::IDENTIFIER) {
throw std::runtime_error(
Expand All @@ -130,6 +132,9 @@ std::vector<std::unique_ptr<Statement>> AbstractSyntaxTree::evaluateOperations(
std::make_unique<IdentifierValue>(outputIdentifier),
std::make_unique<NumberValue>(
(static_cast<const NumberToken&>(secondToken)).value)));

std::cout << outputIdentifier << " + "
<< (static_cast<const NumberToken&>(secondToken)).value << "\n";
}

if (secondToken.tokenType == TokenType::IDENTIFIER) {
Expand All @@ -155,8 +160,10 @@ Program AbstractSyntaxTree::parse() {

const Token& token = this->tokenContainer.view(i);
buffer.push_back(token);
std::cout << "Reading " << token.tokenType << std::endl;

if (token.tokenType == TokenType::END_OF_LINE) {
std::cout << "Reading end of line\n";
if (buffer[0].get().tokenType == TokenType::PRIMITIVE &&
buffer[1].get().tokenType == TokenType::IDENTIFIER &&
buffer[2].get().tokenType == TokenType::OPERATOR) {
Expand Down Expand Up @@ -239,5 +246,6 @@ Program AbstractSyntaxTree::parse() {
buffer.clear();
}
}
std::cout << "program size: " << program.size() << "\n";
return program;
}
1 change: 0 additions & 1 deletion src/syntax_analyser/statement/addition/addition.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <memory>

#include "syntax_analyser/statement/statement.hpp"
#include "syntax_analyser/statement/value/identifier/identifier.hpp"
Expand Down
4 changes: 2 additions & 2 deletions src/syntax_analyser/statement/print/print.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <memory>

#include "syntax_analyser/statement/assignment/assignment_type.hpp"
#include "syntax_analyser/statement/statement.hpp"
#include "syntax_analyser/statement/value/value.hpp"
#include "syntax_analyser/statement/value/identifier/identifier.hpp"

class PrintStatement : public Statement {
public:
Expand Down
Loading