Compare commits
2 Commits
ad29dbdc14
...
4d46fc6b4d
Author | SHA1 | Date | |
---|---|---|---|
4d46fc6b4d | |||
03a72fc583 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
.cache/
|
||||
CMakeFiles/
|
||||
CMakeCache.txt
|
||||
dub.selections.json
|
||||
|
@ -3,8 +3,7 @@ project(Elna)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
find_package(Boost COMPONENTS filesystem REQUIRED)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
add_executable(tester tests/runner.cpp include/elna/tester.hpp)
|
||||
target_include_directories(tester PRIVATE include)
|
||||
@ -13,9 +12,14 @@ add_executable(elnsh shell/main.cpp
|
||||
shell/interactive.cpp include/elna/interactive.hpp
|
||||
shell/history.cpp include/elna/history.hpp
|
||||
shell/state.cpp include/elna/state.hpp
|
||||
shell/lexer.cpp include/elna/lexer.hpp
|
||||
shell/result.cpp include/elna/result.hpp)
|
||||
target_include_directories(elnsh PRIVATE include)
|
||||
target_link_libraries(elnsh PUBLIC
|
||||
${Boost_FILESYSTEM_LIBRARY}
|
||||
)
|
||||
target_include_directories(elnsh PRIVATE include)
|
||||
|
||||
add_library(elna
|
||||
source/lexer.cpp include/elna/lexer.hpp
|
||||
source/result.cpp include/elna/result.hpp
|
||||
source/riscv.cpp include/elna/riscv.hpp
|
||||
source/ir.cpp include/elna/ir.hpp
|
||||
include/elna/parser.hpp
|
||||
)
|
||||
target_include_directories(elna PRIVATE include)
|
||||
|
37
README
Normal file
37
README
Normal file
@ -0,0 +1,37 @@
|
||||
# Elna programming language
|
||||
|
||||
Elna compiles simple mathematical operations to machine code.
|
||||
The compiled program returns the result of the operation.
|
||||
|
||||
## File extension
|
||||
|
||||
.elna
|
||||
|
||||
## Grammar PL/0
|
||||
|
||||
program = block "." ;
|
||||
|
||||
block = [ "const" ident "=" number {"," ident "=" number} ";"]
|
||||
[ "var" ident {"," ident} ";"]
|
||||
{ "procedure" ident ";" block ";" } statement ;
|
||||
|
||||
statement = [ ident ":=" expression | "call" ident
|
||||
| "?" ident | "!" expression
|
||||
| "begin" statement {";" statement } "end"
|
||||
| "if" condition "then" statement
|
||||
| "while" condition "do" statement ];
|
||||
|
||||
condition = "odd" expression |
|
||||
expression ("="|"#"|"<"|"<="|">"|">=") expression ;
|
||||
|
||||
expression = [ "+"|"-"] term { ("+"|"-") term};
|
||||
|
||||
term = factor {("*"|"/") factor};
|
||||
|
||||
factor = ident | number | "(" expression ")";
|
||||
|
||||
## Operations
|
||||
|
||||
"!" - Write a line.
|
||||
"?" - Read user input.
|
||||
"odd" - The only function, returns whether a number is odd.
|
19
Rakefile
Normal file
19
Rakefile
Normal file
@ -0,0 +1,19 @@
|
||||
require 'pathname'
|
||||
require 'rake/clean'
|
||||
require 'open3'
|
||||
|
||||
DFLAGS = ['--warn-no-deprecated', '-L/usr/lib64/gcc-12']
|
||||
BINARY = 'build/bin/elna'
|
||||
SOURCES = FileList['source/**/*.d']
|
||||
|
||||
directory 'build/riscv'
|
||||
|
||||
CLEAN.include 'build'
|
||||
CLEAN.include '.dub'
|
||||
|
||||
file BINARY => SOURCES do |t|
|
||||
sh({ 'DFLAGS' => (DFLAGS * ' ') }, 'dub', 'build', '--compiler=gdc')
|
||||
end
|
||||
|
||||
task default: 'build/riscv'
|
||||
task default: BINARY
|
30
TODO
30
TODO
@ -1,15 +1,23 @@
|
||||
- After inserting autocompletion move the cursor to the end of the insertion.
|
||||
# Completion
|
||||
|
||||
- Configure fzf to show only the current directory files
|
||||
- Support multiple selections for insertion in fzf.
|
||||
- Vi mode.
|
||||
- Don't hardcode fzf binary path.
|
||||
- Persist the history.
|
||||
- Send the word under the cursor to fzf as initial input.
|
||||
- Replace hard coded ANSI codes with constants or functions.
|
||||
- Show the hostname in the prompt.
|
||||
- Crash if the cd directory doesn't exist.
|
||||
- Home directory expansion.
|
||||
- Paste pastes only the first character.
|
||||
- When we read the key and an error occures how it should be handled?
|
||||
- Kitty protocol works with UTF8 code points for escape sequences with modifiers.
|
||||
They can be wider than an unsigned char.
|
||||
- Add a bar with additional information under the prompt (edit_bar).
|
||||
- Show files in the PATH if starting at the beginning of the prompt
|
||||
|
||||
# Appearance
|
||||
|
||||
- Add a bar with additional information under the prompt (edit_bar), like the hostname.
|
||||
- Show current time in the prompt.
|
||||
|
||||
# Input
|
||||
|
||||
- DEL handling.
|
||||
- Starting long running process and killing it with Ctrl-C kills the shell.
|
||||
|
||||
# Other
|
||||
|
||||
- Persist the history.
|
||||
- Replace hard coded ANSI codes with constants or functions.
|
||||
|
11
dub.json
Normal file
11
dub.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"tanya": "~>0.19.0"
|
||||
},
|
||||
"name": "elna",
|
||||
"targetType": "executable",
|
||||
"targetPath": "build/bin",
|
||||
"mainSourceFile": "source/main.d",
|
||||
"libs": ["elna", "stdc++"],
|
||||
"lflags": ["-Lbuild"]
|
||||
}
|
@ -5,10 +5,11 @@
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
#include "elna/lexer.hpp"
|
||||
#include "elna/state.hpp"
|
||||
#include "elna/history.hpp"
|
||||
|
||||
#define BOOST_PROCESS_USE_STD_FS
|
||||
|
||||
namespace elna
|
||||
{
|
||||
/**
|
||||
@ -42,15 +43,14 @@ namespace elna
|
||||
* \param line The command and arguments.
|
||||
* \return Whether the input shoud continued (no exit requested).
|
||||
*/
|
||||
bool execute(const std::vector<token>& line);
|
||||
bool execute(const std::string& line);
|
||||
|
||||
/**
|
||||
* Runs the binary specified in its argument.
|
||||
*
|
||||
* \param program Program name in PATH.
|
||||
* \param arguments Command arguments.
|
||||
*/
|
||||
void launch(const std::string& program, const std::vector<std::string>& arguments);
|
||||
void launch(const std::string& program);
|
||||
|
||||
/**
|
||||
* Enables the raw mode.
|
||||
@ -74,7 +74,16 @@ namespace elna
|
||||
/**
|
||||
* Calls autocompletion.
|
||||
*
|
||||
* \param User input.
|
||||
* \param Cursor position in the user input.
|
||||
* \return Selected item.
|
||||
*/
|
||||
std::string complete();
|
||||
std::string complete(const std::string& input, const std::size_t position);
|
||||
|
||||
/**
|
||||
* Prints the message from the exception.
|
||||
*
|
||||
* \param exception The exception to print.
|
||||
*/
|
||||
void print_exception(const std::exception& exception);
|
||||
}
|
||||
|
95
include/elna/ir.hpp
Normal file
95
include/elna/ir.hpp
Normal file
@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include "elna/parser.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace elna::ir
|
||||
{
|
||||
class Node;
|
||||
class Definition;
|
||||
class Operand;
|
||||
class BinaryExpression;
|
||||
class Variable;
|
||||
class VariableDeclaration;
|
||||
class Number;
|
||||
|
||||
struct IRVisitor
|
||||
{
|
||||
virtual void visit(Node *) = 0;
|
||||
virtual void visit(Definition *) = 0;
|
||||
virtual void visit(Operand *) = 0;
|
||||
virtual void visit(BinaryExpression *) = 0;
|
||||
virtual void visit(Variable *) = 0;
|
||||
virtual void visit(Number *) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* AST node.
|
||||
*/
|
||||
class Node
|
||||
{
|
||||
public:
|
||||
virtual void accept(IRVisitor *) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Definition.
|
||||
*/
|
||||
class Definition : public Node
|
||||
{
|
||||
public:
|
||||
BinaryExpression **statements;
|
||||
std::size_t statementsLength;
|
||||
Operand *result;
|
||||
|
||||
virtual void accept(IRVisitor *visitor) override;
|
||||
};
|
||||
|
||||
class Statement : public Node
|
||||
{
|
||||
};
|
||||
|
||||
class Operand : public Node
|
||||
{
|
||||
public:
|
||||
virtual void accept(IRVisitor *visitor) override;
|
||||
};
|
||||
|
||||
class Number : public Operand
|
||||
{
|
||||
public:
|
||||
std::int32_t value;
|
||||
|
||||
virtual void accept(IRVisitor *visitor) override;
|
||||
};
|
||||
|
||||
class Variable : public Operand
|
||||
{
|
||||
public:
|
||||
std::size_t counter;
|
||||
|
||||
virtual void accept(IRVisitor *visitor) override;
|
||||
};
|
||||
|
||||
class BinaryExpression : public Statement
|
||||
{
|
||||
public:
|
||||
Operand *lhs, *rhs;
|
||||
BinaryOperator _operator;
|
||||
|
||||
BinaryExpression(Operand *lhs, Operand *rhs, BinaryOperator _operator);
|
||||
|
||||
virtual void accept(IRVisitor *visitor) override;
|
||||
};
|
||||
|
||||
class BangExpression : public Statement
|
||||
{
|
||||
Operand *operand;
|
||||
|
||||
public:
|
||||
BangExpression(Operand *operand);
|
||||
|
||||
virtual void accept(IRVisitor *visitor) override;
|
||||
};
|
||||
}
|
@ -16,7 +16,8 @@ namespace elna
|
||||
std::string::const_iterator m_buffer;
|
||||
Position m_position;
|
||||
|
||||
const_iterator(std::string::const_iterator buffer, const Position& position);
|
||||
const_iterator(std::string::const_iterator buffer,
|
||||
const Position position = Position());
|
||||
|
||||
public:
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
@ -42,38 +43,61 @@ namespace elna
|
||||
const_iterator end() const;
|
||||
|
||||
private:
|
||||
const std::string& m_buffer;
|
||||
const std::string m_buffer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Union type representing a single token.
|
||||
*/
|
||||
struct token
|
||||
struct Token
|
||||
{
|
||||
/**
|
||||
* Token type.
|
||||
*/
|
||||
enum class type
|
||||
enum Type : std::uint16_t
|
||||
{
|
||||
word,
|
||||
TOKEN_NUMBER = 0,
|
||||
TOKEN_OPERATOR = 1,
|
||||
TOKEN_LET = 2,
|
||||
TOKEN_IDENTIFIER = 3,
|
||||
TOKEN_EQUALS = 4,
|
||||
TOKEN_VAR = 5,
|
||||
TOKEN_SEMICOLON = 6,
|
||||
TOKEN_LEFT_PAREN = 7,
|
||||
TOKEN_RIGHT_PAREN = 8,
|
||||
TOKEN_BANG = 9,
|
||||
TOKEN_DOT = 10,
|
||||
TOKEN_COMMA = 11,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Type of the token value.
|
||||
*/
|
||||
using value = std::string;
|
||||
union Value
|
||||
{
|
||||
std::int32_t number;
|
||||
const char *identifier;
|
||||
};
|
||||
|
||||
token(const type of, source::const_iterator begin, source::const_iterator end);
|
||||
Token(Type of, Position position);
|
||||
Token(Type of, std::int32_t value, Position position);
|
||||
Token(Type of, const char *value, Position position);
|
||||
Token(const Token& that);
|
||||
Token(Token&& that);
|
||||
~Token();
|
||||
|
||||
type of() const noexcept;
|
||||
const value& identifier() const noexcept;
|
||||
Token& operator=(const Token& that);
|
||||
Token& operator=(Token&& that);
|
||||
|
||||
Type of() const noexcept;
|
||||
const char *identifier() const noexcept;
|
||||
std::int32_t number() const noexcept;
|
||||
const Position& position() const noexcept;
|
||||
|
||||
private:
|
||||
std::string m_value;
|
||||
Type m_type;
|
||||
Value m_value;
|
||||
Position m_position;
|
||||
type m_type;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -81,5 +105,5 @@ namespace elna
|
||||
*
|
||||
* \return Tokens or error.
|
||||
*/
|
||||
result<std::vector<token>> lex(const std::string& buffer);
|
||||
Token *lex(const char *buffer, CompileError *compile_error, std::size_t *length);
|
||||
}
|
||||
|
10
include/elna/parser.hpp
Normal file
10
include/elna/parser.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace elna
|
||||
{
|
||||
enum class BinaryOperator
|
||||
{
|
||||
sum,
|
||||
subtraction
|
||||
};
|
||||
}
|
@ -23,18 +23,18 @@ namespace elna
|
||||
struct CompileError
|
||||
{
|
||||
private:
|
||||
char const *message_;
|
||||
Position position_;
|
||||
char const *message;
|
||||
Position position;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @param message Error text.
|
||||
* @param position Error position in the source text.
|
||||
*/
|
||||
CompileError(char const *message, Position position) noexcept;
|
||||
CompileError(char const *message, const Position position) noexcept;
|
||||
|
||||
/// Error text.
|
||||
char const *message() const noexcept;
|
||||
const char *what() const noexcept;
|
||||
|
||||
/// Error line in the source text.
|
||||
std::size_t line() const noexcept;
|
||||
@ -45,4 +45,18 @@ namespace elna
|
||||
|
||||
template<typename T>
|
||||
using result = boost::outcome_v2::result<T, CompileError>;
|
||||
|
||||
enum class Target
|
||||
{
|
||||
text,
|
||||
high20,
|
||||
lower12i
|
||||
};
|
||||
|
||||
struct Reference
|
||||
{
|
||||
const char* name;
|
||||
size_t offset;
|
||||
Target target;
|
||||
};
|
||||
}
|
||||
|
148
include/elna/riscv.hpp
Normal file
148
include/elna/riscv.hpp
Normal file
@ -0,0 +1,148 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include "elna/ir.hpp"
|
||||
#include "elna/result.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
enum class XRegister : std::uint8_t
|
||||
{
|
||||
zero = 0,
|
||||
ra = 1,
|
||||
sp = 2,
|
||||
gp = 3,
|
||||
tp = 4,
|
||||
t0 = 5,
|
||||
t1 = 6,
|
||||
t2 = 7,
|
||||
s0 = 8,
|
||||
s1 = 9,
|
||||
a0 = 10,
|
||||
a1 = 11,
|
||||
a2 = 12,
|
||||
a3 = 13,
|
||||
a4 = 14,
|
||||
a5 = 15,
|
||||
a6 = 16,
|
||||
a7 = 17,
|
||||
s2 = 18,
|
||||
s3 = 19,
|
||||
s4 = 20,
|
||||
s5 = 21,
|
||||
s6 = 22,
|
||||
s7 = 23,
|
||||
s8 = 24,
|
||||
s9 = 25,
|
||||
s10 = 26,
|
||||
s11 = 27,
|
||||
t3 = 28,
|
||||
t4 = 29,
|
||||
t5 = 30,
|
||||
t6 = 31,
|
||||
};
|
||||
|
||||
enum class Funct3 : std::uint8_t
|
||||
{
|
||||
addi = 0b000,
|
||||
slti = 0b001,
|
||||
sltiu = 0b011,
|
||||
andi = 0b111,
|
||||
ori = 0b110,
|
||||
xori = 0b100,
|
||||
slli = 0b000,
|
||||
srli = 0b101,
|
||||
srai = 0b101,
|
||||
add = 0b000,
|
||||
slt = 0b010,
|
||||
sltu = 0b011,
|
||||
_and = 0b111,
|
||||
_or = 0b110,
|
||||
_xor = 0b100,
|
||||
sll = 0b001,
|
||||
srl = 0b101,
|
||||
sub = 0b000,
|
||||
sra = 0b101,
|
||||
beq = 0b000,
|
||||
bne = 0b001,
|
||||
blt = 0b100,
|
||||
bltu = 0b110,
|
||||
bge = 0b101,
|
||||
bgeu = 0b111,
|
||||
fence = 0b000,
|
||||
fenceI = 0b001,
|
||||
csrrw = 0b001,
|
||||
csrrs = 0b010,
|
||||
csrrc = 0b011,
|
||||
csrrwi = 0b101,
|
||||
csrrsi = 0b110,
|
||||
csrrci = 0b111,
|
||||
priv = 0b000,
|
||||
sb = 0b000,
|
||||
sh = 0b001,
|
||||
sw = 0b010,
|
||||
lb = 0b000,
|
||||
lh = 0b001,
|
||||
lw = 0b010,
|
||||
lbu = 0b100,
|
||||
lhu = 0b101,
|
||||
jalr = 0b000,
|
||||
};
|
||||
|
||||
enum class Funct12 : std::uint8_t
|
||||
{
|
||||
ecall = 0b000000000000,
|
||||
ebreak = 0b000000000001,
|
||||
};
|
||||
|
||||
enum class Funct7 : std::uint8_t
|
||||
{
|
||||
none = 0,
|
||||
sub = 0b0100000
|
||||
};
|
||||
|
||||
enum class BaseOpcode : std::uint8_t
|
||||
{
|
||||
opImm = 0b0010011,
|
||||
lui = 0b0110111,
|
||||
auipc = 0b0010111,
|
||||
op = 0b0110011,
|
||||
jal = 0b1101111,
|
||||
jalr = 0b1100111,
|
||||
branch = 0b1100011,
|
||||
load = 0b0000011,
|
||||
store = 0b0100011,
|
||||
miscMem = 0b0001111,
|
||||
system = 0b1110011,
|
||||
};
|
||||
|
||||
struct Instruction
|
||||
{
|
||||
Instruction(BaseOpcode opcode);
|
||||
|
||||
Instruction& i(XRegister rd, Funct3 funct3, XRegister rs1, std::uint32_t immediate);
|
||||
Instruction& s(std::uint32_t imm1, Funct3 funct3, XRegister rs1, XRegister rs2);
|
||||
Instruction& r(XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, Funct7 funct7 = Funct7::none);
|
||||
Instruction& u(XRegister rd, std::uint32_t imm);
|
||||
std::uint8_t *encode();
|
||||
|
||||
private:
|
||||
std::uint32_t instruction{ 0 };
|
||||
};
|
||||
|
||||
class RiscVVisitor : public ir::IRVisitor
|
||||
{
|
||||
Instruction *instructions;
|
||||
std::size_t instructionsLength;
|
||||
bool registerInUse;
|
||||
std::uint32_t variableCounter = 1;
|
||||
Reference references[3];
|
||||
|
||||
virtual void visit(ir::Node *) override;
|
||||
virtual void visit(ir::Definition *definition) override;
|
||||
virtual void visit(ir::Operand *operand) override;
|
||||
virtual void visit(ir::Variable *variable) override;
|
||||
virtual void visit(ir::Number *number) override;
|
||||
virtual void visit(ir::BinaryExpression *expression) override;
|
||||
};
|
||||
}
|
@ -38,30 +38,28 @@ namespace elna
|
||||
num_lock = 0b10000000, ///< 128
|
||||
};
|
||||
|
||||
class key
|
||||
struct key
|
||||
{
|
||||
std::variant<special_key, unsigned char> store;
|
||||
std::uint8_t modifiers;
|
||||
using char_t = std::uint32_t;
|
||||
|
||||
public:
|
||||
key();
|
||||
explicit key(const unsigned char c, const std::uint8_t modifiers = 0);
|
||||
explicit key(const unsigned char c, const modifier modifiers);
|
||||
explicit key(const char_t c, const std::uint8_t modifiers = 0);
|
||||
explicit key(const char_t c, const modifier modifiers);
|
||||
explicit key(const special_key control, const std::uint8_t modifiers = 0);
|
||||
explicit key(const special_key control, const modifier modifiers);
|
||||
|
||||
bool operator==(const unsigned char c) const;
|
||||
friend bool operator==(const unsigned char c, const key& that);
|
||||
bool operator==(const char_t c) const;
|
||||
friend bool operator==(const char_t c, const key& that);
|
||||
bool operator==(const special_key control) const;
|
||||
friend bool operator==(const special_key control, const key& that);
|
||||
bool operator==(const key& that) const;
|
||||
bool operator!=(const unsigned char c) const;
|
||||
friend bool operator!=(const unsigned char c, const key& that);
|
||||
bool operator!=(const char_t c) const;
|
||||
friend bool operator!=(const char_t c, const key& that);
|
||||
bool operator!=(const special_key control) const;
|
||||
friend bool operator!=(const special_key control, const key& that);
|
||||
bool operator!=(const key& that) const;
|
||||
|
||||
unsigned char character() const;
|
||||
char_t character() const;
|
||||
special_key control() const;
|
||||
bool modified(const std::uint8_t modifiers) const noexcept;
|
||||
bool modified(const modifier modifiers) const noexcept;
|
||||
@ -69,6 +67,10 @@ namespace elna
|
||||
bool is_character() const noexcept;
|
||||
bool is_control() const noexcept;
|
||||
bool empty() const noexcept;
|
||||
|
||||
private:
|
||||
std::variant<special_key, char_t> store;
|
||||
std::uint8_t modifiers;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -143,12 +145,20 @@ namespace elna
|
||||
*/
|
||||
std::size_t absolute_position() const noexcept;
|
||||
|
||||
/**
|
||||
* Cursor position relative to the start of the user input.
|
||||
*
|
||||
* \returns Cursor position.
|
||||
*/
|
||||
std::size_t relative_position() const noexcept;
|
||||
|
||||
/**
|
||||
* Inserts text at cursor position.
|
||||
*
|
||||
* \param text Text to insert.
|
||||
*/
|
||||
void insert(const std::string& text);
|
||||
void insert(const key::char_t text);
|
||||
|
||||
/**
|
||||
* Replaces the user input with the given string.
|
||||
|
@ -9,8 +9,8 @@ namespace elna
|
||||
|
||||
void editor_history::push(const std::string& entry)
|
||||
{
|
||||
commands.push_front(entry);
|
||||
current_pointer = commands.cbegin();
|
||||
commands.push_back(entry);
|
||||
current_pointer = commands.cend();
|
||||
}
|
||||
|
||||
void editor_history::clear()
|
||||
@ -46,4 +46,9 @@ namespace elna
|
||||
{
|
||||
return this->commands.cend();
|
||||
}
|
||||
|
||||
editor_history::const_iterator editor_history::current() const noexcept
|
||||
{
|
||||
return this->current_pointer;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
#include "elna/interactive.hpp"
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/process.hpp>
|
||||
#include <termios.h>
|
||||
#include <filesystem>
|
||||
|
||||
#include "elna/interactive.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
static termios original_termios;
|
||||
@ -39,9 +40,8 @@ namespace elna
|
||||
break;
|
||||
}
|
||||
history.push(line.value().command_line());
|
||||
auto tokens = lex(line.value().command_line());
|
||||
|
||||
if (!execute(tokens.assume_value()))
|
||||
if (!execute(line.value().command_line()))
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -53,6 +53,7 @@ namespace elna
|
||||
{
|
||||
editor_state state;
|
||||
std::string buffer;
|
||||
std::string saved;
|
||||
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
@ -81,7 +82,7 @@ namespace elna
|
||||
break;
|
||||
case action::complete:
|
||||
write(STDOUT_FILENO, "\x1b[1E\x1b[2K", 8);
|
||||
state.insert(complete());
|
||||
state.insert(complete(state.command_line(), state.relative_position()));
|
||||
write(STDOUT_FILENO, "\x1b[1A", 4);
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
@ -104,18 +105,26 @@ namespace elna
|
||||
|
||||
if (pressed_key == key(special_key::arrow_up, modifier::ctrl))
|
||||
{
|
||||
history_iterator = history.next();
|
||||
if (history.current() == history.cend())
|
||||
{
|
||||
saved = state.command_line();
|
||||
}
|
||||
history_iterator = history.prev();
|
||||
}
|
||||
else if (pressed_key == key(special_key::arrow_down, modifier::ctrl))
|
||||
{
|
||||
history_iterator = history.prev();
|
||||
history_iterator = history.next();
|
||||
if (history_iterator == history.cend())
|
||||
{
|
||||
state.load(saved);
|
||||
}
|
||||
}
|
||||
if (history_iterator != history.cend())
|
||||
{
|
||||
state.load(*history_iterator);
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
}
|
||||
state.draw(std::back_inserter(buffer));
|
||||
write(STDOUT_FILENO, buffer.data(), buffer.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -137,7 +146,7 @@ namespace elna
|
||||
{
|
||||
return key();
|
||||
}
|
||||
auto [number, last_char] = read_number<unsigned char>();
|
||||
auto [number, last_char] = read_number<key::char_t>();
|
||||
std::uint8_t modifiers{ 0 };
|
||||
|
||||
if (last_char == ';')
|
||||
@ -185,35 +194,51 @@ namespace elna
|
||||
return key();
|
||||
}
|
||||
|
||||
bool execute(const std::vector<token>& tokens)
|
||||
void print_exception(const std::exception& exception)
|
||||
{
|
||||
if (tokens.empty())
|
||||
std::string message{ exception.what() };
|
||||
message += "\r\n";
|
||||
write(STDERR_FILENO, message.data(), message.size());
|
||||
}
|
||||
|
||||
bool execute(const std::string& line)
|
||||
{
|
||||
if (line.empty())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::string program = tokens.front().identifier();
|
||||
if (program == "exit")
|
||||
if (line == "exit")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (program == "cd")
|
||||
else if (boost::starts_with(line, "cd "))
|
||||
{
|
||||
std::filesystem::current_path(tokens[1].identifier());
|
||||
try
|
||||
{
|
||||
std::filesystem::current_path(line.substr(strlen("cd ")));
|
||||
}
|
||||
catch (const std::filesystem::filesystem_error& exception)
|
||||
{
|
||||
print_exception(exception);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
std::vector<std::string> arguments;
|
||||
for (auto argument = tokens.cbegin() + 1; argument != std::cend(tokens); ++argument)
|
||||
{
|
||||
arguments.push_back(argument->identifier());
|
||||
}
|
||||
launch(program, arguments);
|
||||
launch(line);
|
||||
return true;
|
||||
}
|
||||
|
||||
void launch(const std::string& program, const std::vector<std::string>& arguments)
|
||||
void launch(const std::string& program)
|
||||
{
|
||||
boost::process::system(boost::process::search_path(program), arguments);
|
||||
try
|
||||
{
|
||||
boost::process::system(program);
|
||||
}
|
||||
catch (const boost::process::process_error& exception)
|
||||
{
|
||||
print_exception(exception);
|
||||
}
|
||||
enable_raw_mode();
|
||||
}
|
||||
|
||||
bool enable_raw_mode()
|
||||
@ -240,10 +265,17 @@ namespace elna
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
|
||||
}
|
||||
|
||||
std::string complete()
|
||||
std::string complete(const std::string& input, const std::size_t position)
|
||||
{
|
||||
std::filesystem::path program = boost::process::search_path("fzf");
|
||||
|
||||
if (program.empty())
|
||||
{
|
||||
return "";
|
||||
}
|
||||
boost::process::ipstream output;
|
||||
boost::process::system("/usr/bin/fzf", "--height=10", "--layout=reverse",
|
||||
boost::process::system(program,
|
||||
"--height=10", "--layout=reverse", "-1", "--no-multi",
|
||||
boost::process::std_out > output);
|
||||
|
||||
std::string selections;
|
||||
|
124
shell/lexer.cpp
124
shell/lexer.cpp
@ -1,124 +0,0 @@
|
||||
#include "elna/lexer.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
source::source(const std::string& buffer)
|
||||
: m_buffer(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
source::const_iterator source::begin() const
|
||||
{
|
||||
return source::const_iterator(std::cbegin(m_buffer), Position());
|
||||
}
|
||||
|
||||
source::const_iterator source::end() const
|
||||
{
|
||||
Position end_position{ 0, 0 };
|
||||
|
||||
return source::const_iterator(std::cend(m_buffer), end_position);
|
||||
}
|
||||
|
||||
source::const_iterator::const_iterator(std::string::const_iterator buffer, const Position& start_position)
|
||||
: m_buffer(buffer), m_position(start_position)
|
||||
{
|
||||
}
|
||||
|
||||
const Position& source::const_iterator::position() const noexcept
|
||||
{
|
||||
return this->m_position;
|
||||
}
|
||||
|
||||
source::const_iterator::reference source::const_iterator::operator*() const noexcept
|
||||
{
|
||||
return *m_buffer;
|
||||
}
|
||||
|
||||
source::const_iterator::pointer source::const_iterator::operator->() const noexcept
|
||||
{
|
||||
return m_buffer.base();
|
||||
}
|
||||
|
||||
source::const_iterator& source::const_iterator::operator++()
|
||||
{
|
||||
if (*this->m_buffer == '\n')
|
||||
{
|
||||
this->m_position.column = 1;
|
||||
++this->m_position.line;
|
||||
}
|
||||
else
|
||||
{
|
||||
++this->m_position.column;
|
||||
}
|
||||
std::advance(this->m_buffer, 1);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
source::const_iterator& source::const_iterator::operator++(int)
|
||||
{
|
||||
auto tmp = *this;
|
||||
++(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool source::const_iterator::operator==(const source::const_iterator& that) const noexcept
|
||||
{
|
||||
return this->m_buffer == that.m_buffer;
|
||||
}
|
||||
|
||||
bool source::const_iterator::operator!=(const source::const_iterator& that) const noexcept
|
||||
{
|
||||
return !(*this == that);
|
||||
}
|
||||
|
||||
token::token(const type of, source::const_iterator begin, source::const_iterator end)
|
||||
: m_type(of), m_value(begin, end)
|
||||
{
|
||||
}
|
||||
|
||||
token::type token::of() const noexcept
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
const token::value& token::identifier() const noexcept
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
const Position& token::position() const noexcept
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
result<std::vector<token>> lex(const std::string& buffer)
|
||||
{
|
||||
std::vector<token> tokens;
|
||||
source input{ buffer };
|
||||
|
||||
for (auto iterator = input.begin(); iterator != input.end();)
|
||||
{
|
||||
if (*iterator == ' ' || *iterator == '\n')
|
||||
{
|
||||
}
|
||||
else if (std::isgraph(*iterator))
|
||||
{
|
||||
auto current_position = iterator;
|
||||
do
|
||||
{
|
||||
++current_position;
|
||||
}
|
||||
while (current_position != input.end() && std::isgraph(*current_position));
|
||||
token new_token{ token::type::word, iterator, current_position };
|
||||
|
||||
tokens.push_back(new_token);
|
||||
iterator = current_position;
|
||||
continue;
|
||||
}
|
||||
++iterator;
|
||||
}
|
||||
|
||||
return tokens;
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
#include "elna/result.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
CompileError::CompileError(char const *message, Position position) noexcept
|
||||
{
|
||||
this->message_ = message;
|
||||
this->position_ = position;
|
||||
}
|
||||
|
||||
char const *CompileError::message() const noexcept
|
||||
{
|
||||
return this->message_;
|
||||
}
|
||||
|
||||
std::size_t CompileError::line() const noexcept
|
||||
{
|
||||
return this->position_.line;
|
||||
}
|
||||
|
||||
std::size_t CompileError::column() const noexcept
|
||||
{
|
||||
return this->position_.column;
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
#include <algorithm>
|
||||
#include <boost/endian.hpp>
|
||||
|
||||
#include "elna/state.hpp"
|
||||
|
||||
namespace elna
|
||||
@ -12,16 +15,16 @@ namespace elna
|
||||
}
|
||||
|
||||
key::key()
|
||||
: store(static_cast<unsigned char>(0x1b))
|
||||
: store(0x1bu)
|
||||
{
|
||||
}
|
||||
|
||||
key::key(const unsigned char c, const std::uint8_t modifiers)
|
||||
key::key(const char_t c, const std::uint8_t modifiers)
|
||||
: store(c), modifiers(modifiers == 0 ? 0 : modifiers - 1)
|
||||
{
|
||||
}
|
||||
|
||||
key::key(const unsigned char c, const modifier modifiers)
|
||||
key::key(const char_t c, const modifier modifiers)
|
||||
: store(c), modifiers(static_cast<uint8_t>(modifiers))
|
||||
{
|
||||
}
|
||||
@ -36,9 +39,9 @@ namespace elna
|
||||
{
|
||||
}
|
||||
|
||||
unsigned char key::character() const
|
||||
key::char_t key::character() const
|
||||
{
|
||||
return std::get<unsigned char>(this->store);
|
||||
return std::get<char_t>(this->store);
|
||||
}
|
||||
|
||||
special_key key::control() const
|
||||
@ -56,10 +59,10 @@ namespace elna
|
||||
return this->modifiers == static_cast<std::uint8_t>(modifiers);
|
||||
}
|
||||
|
||||
bool key::operator==(const unsigned char c) const
|
||||
bool key::operator==(const char_t c) const
|
||||
{
|
||||
return std::holds_alternative<unsigned char>(this->store)
|
||||
&& std::get<unsigned char>(this->store) == c;
|
||||
return std::holds_alternative<char_t>(this->store)
|
||||
&& std::get<char_t>(this->store) == c;
|
||||
}
|
||||
|
||||
bool key::operator==(const special_key control) const
|
||||
@ -73,7 +76,7 @@ namespace elna
|
||||
return this->store == that.store;
|
||||
}
|
||||
|
||||
bool key::operator!=(const unsigned char c) const
|
||||
bool key::operator!=(const char_t c) const
|
||||
{
|
||||
return !(*this == c);
|
||||
}
|
||||
@ -90,7 +93,7 @@ namespace elna
|
||||
|
||||
bool key::is_character() const noexcept
|
||||
{
|
||||
return std::holds_alternative<unsigned char>(this->store);
|
||||
return std::holds_alternative<char_t>(this->store);
|
||||
}
|
||||
|
||||
bool key::is_control() const noexcept
|
||||
@ -131,6 +134,7 @@ namespace elna
|
||||
void editor_state::load(const std::string& text)
|
||||
{
|
||||
this->input = text;
|
||||
this->position = this->input.size() + 1;
|
||||
}
|
||||
|
||||
const std::string& editor_state::command_line() const noexcept
|
||||
@ -143,9 +147,36 @@ namespace elna
|
||||
return this->prompt.size() + this->position;
|
||||
}
|
||||
|
||||
std::size_t editor_state::relative_position() const noexcept
|
||||
{
|
||||
return this->position;
|
||||
}
|
||||
|
||||
void editor_state::insert(const std::string& text)
|
||||
{
|
||||
this->input.insert(std::end(this->input), std::cbegin(text), std::cend(text));
|
||||
this->input.insert(this->position - 1, text);
|
||||
this->position += text.size();
|
||||
}
|
||||
|
||||
void editor_state::insert(const key::char_t text)
|
||||
{
|
||||
if (text == 0u)
|
||||
{
|
||||
this->input.insert(this->position - 1, '\0', 1);
|
||||
++this->position;
|
||||
return;
|
||||
}
|
||||
key::char_t buffer = boost::endian::native_to_big<key::char_t>(text);
|
||||
|
||||
const char *begin = reinterpret_cast<char *>(&buffer);
|
||||
const char *end = begin + sizeof(key::char_t);
|
||||
const char *significant = std::find_if(begin, end,
|
||||
[](const char byte) {
|
||||
return byte != 0;
|
||||
});
|
||||
|
||||
this->input.insert(this->position - 1, significant, end - significant);
|
||||
this->position += end - significant;
|
||||
}
|
||||
|
||||
action editor_state::process_keypress(const key& press)
|
||||
@ -202,12 +233,21 @@ namespace elna
|
||||
this->input.erase(this->position - 1, 1);
|
||||
}
|
||||
return action::redraw;
|
||||
case 9: // Tab.
|
||||
return action::complete;
|
||||
case '\r':
|
||||
return action::finalize;
|
||||
default:
|
||||
++this->position;
|
||||
this->input.push_back(press.character());
|
||||
return action::write;
|
||||
if (this->position - 1 < input.size())
|
||||
{
|
||||
insert(press.character());
|
||||
return action::redraw;
|
||||
}
|
||||
else
|
||||
{
|
||||
insert(press.character());
|
||||
return action::write;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
154
source/elna/arguments.d
Normal file
154
source/elna/arguments.d
Normal file
@ -0,0 +1,154 @@
|
||||
/**
|
||||
* Argument parsing.
|
||||
*/
|
||||
module elna.arguments;
|
||||
|
||||
import std.algorithm;
|
||||
import std.range;
|
||||
import std.sumtype;
|
||||
|
||||
struct ArgumentError
|
||||
{
|
||||
enum Type
|
||||
{
|
||||
expectedOutputFile,
|
||||
noInput,
|
||||
superfluousArguments,
|
||||
}
|
||||
|
||||
private Type type_;
|
||||
private string argument_;
|
||||
|
||||
@property Type type() const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.type_;
|
||||
}
|
||||
|
||||
@property string argument() const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.argument_;
|
||||
}
|
||||
|
||||
void toString(OR)(OR range)
|
||||
if (isOutputRage!OR)
|
||||
{
|
||||
final switch (Type)
|
||||
{
|
||||
case Type.expectedOutputFile:
|
||||
put(range, "Expected an output filename after -o");
|
||||
break;
|
||||
case Type.noInput:
|
||||
put(range, "No input files specified");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported compiler arguments.
|
||||
*/
|
||||
struct Arguments
|
||||
{
|
||||
private bool assembler_;
|
||||
private string output_;
|
||||
private string inFile_;
|
||||
|
||||
@property string inFile() @nogc nothrow pure @safe
|
||||
{
|
||||
return this.inFile_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Whether to generate assembly instead of an object file.
|
||||
*/
|
||||
@property bool assembler() const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.assembler_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Output file.
|
||||
*/
|
||||
@property string output() const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.output_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse command line arguments.
|
||||
*
|
||||
* The first argument is expected to be the program name (and it is
|
||||
* ignored).
|
||||
*
|
||||
* Params:
|
||||
* arguments = Command line arguments.
|
||||
*
|
||||
* Returns: Parsed arguments or an error.
|
||||
*/
|
||||
static SumType!(ArgumentError, Arguments) parse(string[] arguments)
|
||||
@nogc nothrow pure @safe
|
||||
{
|
||||
if (!arguments.empty)
|
||||
{
|
||||
arguments.popFront;
|
||||
}
|
||||
alias ReturnType = typeof(return);
|
||||
|
||||
return parseArguments(arguments).match!(
|
||||
(Arguments parsed) {
|
||||
if (parsed.inFile is null)
|
||||
{
|
||||
return ReturnType(ArgumentError(ArgumentError.Type.noInput));
|
||||
}
|
||||
else if (!arguments.empty)
|
||||
{
|
||||
return ReturnType(ArgumentError(
|
||||
ArgumentError.Type.superfluousArguments,
|
||||
arguments.front
|
||||
));
|
||||
}
|
||||
return ReturnType(parsed);
|
||||
},
|
||||
(ArgumentError argumentError) => ReturnType(argumentError)
|
||||
);
|
||||
}
|
||||
|
||||
private static SumType!(ArgumentError, Arguments) parseArguments(ref string[] arguments)
|
||||
@nogc nothrow pure @safe
|
||||
{
|
||||
Arguments parsed;
|
||||
|
||||
while (!arguments.empty)
|
||||
{
|
||||
if (arguments.front == "-s")
|
||||
{
|
||||
parsed.assembler_ = true;
|
||||
}
|
||||
else if (arguments.front == "-o")
|
||||
{
|
||||
if (arguments.empty)
|
||||
{
|
||||
return typeof(return)(ArgumentError(
|
||||
ArgumentError.Type.expectedOutputFile,
|
||||
arguments.front
|
||||
));
|
||||
}
|
||||
arguments.popFront;
|
||||
parsed.output_ = arguments.front;
|
||||
}
|
||||
else if (arguments.front == "--")
|
||||
{
|
||||
arguments.popFront;
|
||||
parsed.inFile_ = arguments.front;
|
||||
arguments.popFront;
|
||||
break;
|
||||
}
|
||||
else if (!arguments.front.startsWith("-"))
|
||||
{
|
||||
parsed.inFile_ = arguments.front;
|
||||
}
|
||||
arguments.popFront;
|
||||
}
|
||||
return typeof(return)(parsed);
|
||||
}
|
||||
}
|
114
source/elna/backend.d
Normal file
114
source/elna/backend.d
Normal file
@ -0,0 +1,114 @@
|
||||
module elna.backend;
|
||||
|
||||
import core.stdc.stdio;
|
||||
import core.stdc.stdlib;
|
||||
import core.stdc.string;
|
||||
import elna.elf;
|
||||
import elna.ir;
|
||||
import elna.extended;
|
||||
import elna.riscv;
|
||||
import elna.lexer;
|
||||
import elna.parser;
|
||||
import elna.result;
|
||||
import std.algorithm;
|
||||
import std.sumtype;
|
||||
import std.typecons;
|
||||
import tanya.os.error;
|
||||
import tanya.container.array;
|
||||
import tanya.container.string;
|
||||
import tanya.memory.allocator;
|
||||
|
||||
private char* readSource(string source) @nogc
|
||||
{
|
||||
enum size_t bufferSize = 255;
|
||||
auto sourceFilename = String(source);
|
||||
|
||||
return readFile(sourceFilename).match!(
|
||||
(ErrorCode errorCode) {
|
||||
perror(sourceFilename.toStringz);
|
||||
return null;
|
||||
},
|
||||
(Array!ubyte contents) {
|
||||
char* cString = cast(char*) malloc(contents.length + 1);
|
||||
memcpy(cString, contents.get.ptr, contents.length);
|
||||
cString[contents.length] = '\0';
|
||||
|
||||
return cString;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
int generate(string inFile, ref String outputFilename) @nogc
|
||||
{
|
||||
auto sourceText = readSource(inFile);
|
||||
if (sourceText is null)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
CompileError compileError = void;
|
||||
size_t tokensCount;
|
||||
auto tokens = lex(sourceText, &compileError, &tokensCount);
|
||||
free(sourceText);
|
||||
if (tokens is null)
|
||||
{
|
||||
printf("%lu:%lu: %s\n", compileError.line, compileError.column, compileError.what);
|
||||
return 1;
|
||||
}
|
||||
auto ast = parse(tokens[0 .. tokensCount]);
|
||||
if (!ast.valid)
|
||||
{
|
||||
compileError = ast.error.get;
|
||||
printf("%lu:%lu: %s\n", compileError.line, compileError.column, compileError.what);
|
||||
return 2;
|
||||
}
|
||||
auto transformVisitor = cast(TransformVisitor) malloc(__traits(classInstanceSize, TransformVisitor));
|
||||
(cast(void*) transformVisitor)[0 .. __traits(classInstanceSize, TransformVisitor)] = __traits(initSymbol, TransformVisitor)[];
|
||||
|
||||
auto ir = transformVisitor.visit(ast.result);
|
||||
|
||||
transformVisitor.__xdtor();
|
||||
free(cast(void*) transformVisitor);
|
||||
|
||||
auto handle = File.open(outputFilename.toStringz, BitFlags!(File.Mode)(File.Mode.truncate));
|
||||
if (!handle.valid)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
auto program = writeNext(ir);
|
||||
auto elf = Elf!ELFCLASS32(move(handle));
|
||||
auto readOnlyData = Array!ubyte(cast(const(ubyte)[]) "%d\n".ptr[0 .. 4]); // With \0.
|
||||
|
||||
elf.addReadOnlyData(String(".CL0"), readOnlyData);
|
||||
elf.addCode(program.name, program.text);
|
||||
|
||||
elf.addExternSymbol(String("printf"));
|
||||
foreach (ref reference; program.symbols)
|
||||
{
|
||||
elf.Rela relocationEntry = {
|
||||
r_offset: cast(elf.Addr) reference.offset
|
||||
};
|
||||
elf.Rela relocationSub = {
|
||||
r_offset: cast(elf.Addr) reference.offset,
|
||||
r_info: R_RISCV_RELAX
|
||||
};
|
||||
|
||||
final switch (reference.target)
|
||||
{
|
||||
case Target.text:
|
||||
relocationEntry.r_info = R_RISCV_CALL;
|
||||
break;
|
||||
case Target.high20:
|
||||
relocationEntry.r_info = R_RISCV_HI20;
|
||||
break;
|
||||
case Target.lower12i:
|
||||
relocationEntry.r_info = R_RISCV_LO12_I;
|
||||
break;
|
||||
}
|
||||
|
||||
elf.relocate(String(reference.name[0 .. strlen(reference.name)]), relocationEntry, relocationSub);
|
||||
}
|
||||
|
||||
elf.finish();
|
||||
|
||||
return 0;
|
||||
}
|
1060
source/elna/elf.d
Normal file
1060
source/elna/elf.d
Normal file
File diff suppressed because it is too large
Load Diff
335
source/elna/extended.d
Normal file
335
source/elna/extended.d
Normal file
@ -0,0 +1,335 @@
|
||||
/**
|
||||
* File I/O that can be moved into more generic library when and if finished.
|
||||
*/
|
||||
module elna.extended;
|
||||
|
||||
import core.stdc.errno;
|
||||
import core.stdc.stdio;
|
||||
import std.sumtype;
|
||||
import std.typecons;
|
||||
import tanya.os.error;
|
||||
import tanya.container.array;
|
||||
import tanya.container.string;
|
||||
|
||||
/**
|
||||
* File handle abstraction.
|
||||
*/
|
||||
struct File
|
||||
{
|
||||
/// Plattform dependent file type.
|
||||
alias Handle = FILE*;
|
||||
|
||||
/// Uninitialized file handle value.
|
||||
enum Handle invalid = null;
|
||||
|
||||
/**
|
||||
* Relative position.
|
||||
*/
|
||||
enum Whence
|
||||
{
|
||||
/// Relative to the start of the file.
|
||||
set = SEEK_SET,
|
||||
/// Relative to the current cursor position.
|
||||
currentt = SEEK_CUR,
|
||||
/// Relative from the end of the file.
|
||||
end = SEEK_END,
|
||||
}
|
||||
|
||||
/**
|
||||
* File open modes.
|
||||
*/
|
||||
enum Mode
|
||||
{
|
||||
/// Open the file for reading.
|
||||
read = 1 << 0,
|
||||
/// Open the file for writing. The stream is positioned at the beginning
|
||||
/// of the file.
|
||||
write = 1 << 1,
|
||||
/// Open the file for writing and remove its contents.
|
||||
truncate = 1 << 2,
|
||||
/// Open the file for writing. The stream is positioned at the end of
|
||||
/// the file.
|
||||
append = 1 << 3,
|
||||
}
|
||||
|
||||
private enum Status
|
||||
{
|
||||
invalid,
|
||||
owned,
|
||||
borrowed,
|
||||
}
|
||||
|
||||
private union Storage
|
||||
{
|
||||
Handle handle;
|
||||
ErrorCode errorCode;
|
||||
}
|
||||
private Storage storage;
|
||||
private Status status = Status.invalid;
|
||||
|
||||
@disable this(scope return ref File f);
|
||||
@disable this();
|
||||
|
||||
/**
|
||||
* Closes the file.
|
||||
*/
|
||||
~this() @nogc nothrow
|
||||
{
|
||||
if (this.status == Status.owned)
|
||||
{
|
||||
fclose(this.storage.handle);
|
||||
}
|
||||
this.storage.handle = invalid;
|
||||
this.status = Status.invalid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the object with the given system handle. The won't be claused
|
||||
* in the descructor if this constructor is used.
|
||||
*
|
||||
* Params:
|
||||
* handle = File handle to be wrapped by this structure.
|
||||
*/
|
||||
this(Handle handle) @nogc nothrow pure @safe
|
||||
{
|
||||
this.storage.handle = handle;
|
||||
this.status = Status.borrowed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Plattform dependent file handle.
|
||||
*/
|
||||
@property Handle handle() @nogc nothrow pure @trusted
|
||||
{
|
||||
return valid ? this.storage.handle : invalid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: An error code if an error has occurred.
|
||||
*/
|
||||
@property ErrorCode errorCode() @nogc nothrow pure @safe
|
||||
{
|
||||
return valid ? ErrorCode() : this.storage.errorCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Whether a valid, opened file is represented.
|
||||
*/
|
||||
@property bool valid() @nogc nothrow pure @safe
|
||||
{
|
||||
return this.status != Status.invalid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers the file into invalid state.
|
||||
*
|
||||
* Returns: The old file handle.
|
||||
*/
|
||||
Handle reset() @nogc nothrow pure @safe
|
||||
{
|
||||
if (!valid)
|
||||
{
|
||||
return invalid;
|
||||
}
|
||||
auto oldHandle = handle;
|
||||
|
||||
this.status = Status.invalid;
|
||||
this.storage.errorCode = ErrorCode();
|
||||
|
||||
return oldHandle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets stream position in the file.
|
||||
*
|
||||
* Params:
|
||||
* offset = File offset.
|
||||
* whence = File position to add the offset to.
|
||||
*
|
||||
* Returns: Error code if any.
|
||||
*/
|
||||
ErrorCode seek(size_t offset, Whence whence) @nogc nothrow
|
||||
{
|
||||
if (!valid)
|
||||
{
|
||||
return ErrorCode(ErrorCode.ErrorNo.badDescriptor);
|
||||
}
|
||||
if (fseek(this.storage.handle, offset, whence))
|
||||
{
|
||||
return ErrorCode(cast(ErrorCode.ErrorNo) errno);
|
||||
}
|
||||
return ErrorCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Current offset or an error.
|
||||
*/
|
||||
SumType!(ErrorCode, size_t) tell() @nogc nothrow
|
||||
{
|
||||
if (!valid)
|
||||
{
|
||||
return typeof(return)(ErrorCode(ErrorCode.ErrorNo.badDescriptor));
|
||||
}
|
||||
auto result = ftell(this.storage.handle);
|
||||
|
||||
if (result < 0)
|
||||
{
|
||||
return typeof(return)(ErrorCode(cast(ErrorCode.ErrorNo) errno));
|
||||
}
|
||||
return typeof(return)(cast(size_t) result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* buffer = Destination buffer.
|
||||
*
|
||||
* Returns: Bytes read. $(D_PSYMBOL ErrorCode.ErrorNo.success) means that
|
||||
* while reading the file an unknown error has occurred.
|
||||
*/
|
||||
SumType!(ErrorCode, size_t) read(ubyte[] buffer) @nogc nothrow
|
||||
{
|
||||
if (!valid)
|
||||
{
|
||||
return typeof(return)(ErrorCode(ErrorCode.ErrorNo.badDescriptor));
|
||||
}
|
||||
const bytesRead = fread(buffer.ptr, 1, buffer.length, this.storage.handle);
|
||||
if (bytesRead == buffer.length || eof())
|
||||
{
|
||||
return typeof(return)(bytesRead);
|
||||
}
|
||||
return typeof(return)(ErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* buffer = Source buffer.
|
||||
*
|
||||
* Returns: Bytes written. $(D_PSYMBOL ErrorCode.ErrorNo.success) means that
|
||||
* while reading the file an unknown error has occurred.
|
||||
*/
|
||||
SumType!(ErrorCode, size_t) write(const(ubyte)[] buffer) @nogc nothrow
|
||||
{
|
||||
if (!valid)
|
||||
{
|
||||
return typeof(return)(ErrorCode(ErrorCode.ErrorNo.badDescriptor));
|
||||
}
|
||||
const bytesWritten = fwrite(buffer.ptr, 1, buffer.length, this.storage.handle);
|
||||
if (bytesWritten == buffer.length)
|
||||
{
|
||||
return typeof(return)(buffer.length);
|
||||
}
|
||||
return typeof(return)(ErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: EOF status of the file.
|
||||
*/
|
||||
bool eof() @nogc nothrow
|
||||
{
|
||||
return valid && feof(this.storage.handle) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a file object that will be closed in the destructor.
|
||||
*
|
||||
* Params:
|
||||
* filename = The file to open.
|
||||
*
|
||||
* Returns: Opened file or an error.
|
||||
*/
|
||||
static File open(const(char)* filename, BitFlags!Mode mode) @nogc nothrow
|
||||
{
|
||||
char[3] modeBuffer = "\0\0\0";
|
||||
|
||||
if (mode.truncate)
|
||||
{
|
||||
modeBuffer[0] = 'w';
|
||||
if (mode.read)
|
||||
{
|
||||
modeBuffer[1] = '+';
|
||||
}
|
||||
}
|
||||
else if (mode.append)
|
||||
{
|
||||
modeBuffer[0] = 'a';
|
||||
if (mode.read)
|
||||
{
|
||||
modeBuffer[1] = '+';
|
||||
}
|
||||
}
|
||||
else if (mode.read)
|
||||
{
|
||||
modeBuffer[0] = 'r';
|
||||
if (mode.write)
|
||||
{
|
||||
modeBuffer[1] = '+';
|
||||
}
|
||||
}
|
||||
|
||||
auto newHandle = fopen(filename, modeBuffer.ptr);
|
||||
auto newFile = File(newHandle);
|
||||
|
||||
if (newHandle is null)
|
||||
{
|
||||
newFile.status = Status.invalid;
|
||||
newFile.storage.errorCode = ErrorCode(cast(ErrorCode.ErrorNo) errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mode == BitFlags!Mode(Mode.write))
|
||||
{
|
||||
rewind(newHandle);
|
||||
}
|
||||
newFile.status = Status.owned;
|
||||
}
|
||||
|
||||
return newFile;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the whole file and returns its contents.
|
||||
*
|
||||
* Params:
|
||||
* sourceFilename = Source filename.
|
||||
*
|
||||
* Returns: File contents or an error.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL File.read)
|
||||
*/
|
||||
SumType!(ErrorCode, Array!ubyte) readFile(String sourceFilename) @nogc
|
||||
{
|
||||
enum size_t bufferSize = 255;
|
||||
auto sourceFile = File.open(sourceFilename.toStringz, BitFlags!(File.Mode)(File.Mode.read));
|
||||
|
||||
if (!sourceFile.valid)
|
||||
{
|
||||
return typeof(return)(sourceFile.errorCode);
|
||||
}
|
||||
Array!ubyte sourceText;
|
||||
size_t totalRead;
|
||||
size_t bytesRead;
|
||||
do
|
||||
{
|
||||
sourceText.length = sourceText.length + bufferSize;
|
||||
const readStatus = sourceFile
|
||||
.read(sourceText[totalRead .. $].get)
|
||||
.match!(
|
||||
(ErrorCode errorCode) => nullable(errorCode),
|
||||
(size_t bytesRead_) {
|
||||
bytesRead = bytesRead_;
|
||||
return Nullable!ErrorCode();
|
||||
}
|
||||
);
|
||||
if (!readStatus.isNull)
|
||||
{
|
||||
return typeof(return)(readStatus.get);
|
||||
}
|
||||
totalRead += bytesRead;
|
||||
}
|
||||
while (bytesRead == bufferSize);
|
||||
|
||||
sourceText.length = totalRead;
|
||||
|
||||
return typeof(return)(sourceText);
|
||||
}
|
220
source/elna/ir.d
Normal file
220
source/elna/ir.d
Normal file
@ -0,0 +1,220 @@
|
||||
module elna.ir;
|
||||
|
||||
import core.stdc.stdlib;
|
||||
import parser = elna.parser;
|
||||
import tanya.container.array;
|
||||
import tanya.container.hashtable;
|
||||
import tanya.container.string;
|
||||
import tanya.memory.allocator;
|
||||
public import elna.parser : BinaryOperator;
|
||||
|
||||
/**
|
||||
* Mapping between the parser and IR AST.
|
||||
*/
|
||||
struct ASTMapping
|
||||
{
|
||||
alias Node = .Node;
|
||||
alias Definition = .Definition;
|
||||
alias Statement = .Operand;
|
||||
alias BangStatement = .Operand;
|
||||
alias Block = .Definition;
|
||||
alias Expression = .Operand;
|
||||
alias Number = .Number;
|
||||
alias Variable = .Number;
|
||||
alias BinaryExpression = .Variable;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* IR visitor.
|
||||
*/
|
||||
extern(C++, "elna", "ir")
|
||||
abstract class IRVisitor
|
||||
{
|
||||
abstract void visit(Node) @nogc;
|
||||
abstract void visit(Definition) @nogc;
|
||||
abstract void visit(Operand) @nogc;
|
||||
abstract void visit(BinaryExpression) @nogc;
|
||||
abstract void visit(Variable) @nogc;
|
||||
abstract void visit(Number) @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* AST node.
|
||||
*/
|
||||
extern(C++, "elna", "ir")
|
||||
abstract class Node
|
||||
{
|
||||
abstract void accept(IRVisitor) @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Definition.
|
||||
*/
|
||||
extern(C++, "elna", "ir")
|
||||
class Definition : Node
|
||||
{
|
||||
BinaryExpression* statements;
|
||||
size_t statementsLength;
|
||||
Operand result;
|
||||
|
||||
override void accept(IRVisitor visitor) @nogc;
|
||||
}
|
||||
|
||||
extern(C++, "elna", "ir")
|
||||
abstract class Statement : Node
|
||||
{
|
||||
}
|
||||
|
||||
extern(C++, "elna", "ir")
|
||||
abstract class Operand : Node
|
||||
{
|
||||
override void accept(IRVisitor visitor) @nogc;
|
||||
}
|
||||
|
||||
extern(C++, "elna", "ir")
|
||||
class Number : Operand
|
||||
{
|
||||
int value;
|
||||
|
||||
override void accept(IRVisitor visitor) @nogc;
|
||||
}
|
||||
|
||||
extern(C++, "elna", "ir")
|
||||
class Variable : Operand
|
||||
{
|
||||
size_t counter;
|
||||
|
||||
override void accept(IRVisitor visitor) @nogc;
|
||||
}
|
||||
|
||||
extern(C++, "elna", "ir")
|
||||
class BinaryExpression : Statement
|
||||
{
|
||||
Operand lhs, rhs;
|
||||
BinaryOperator operator;
|
||||
|
||||
this(Operand lhs, Operand rhs, BinaryOperator operator) @nogc;
|
||||
|
||||
override void accept(IRVisitor visitor) @nogc;
|
||||
}
|
||||
|
||||
extern(C++, "elna", "ir")
|
||||
class BangExpression : Statement
|
||||
{
|
||||
Operand operand;
|
||||
|
||||
this(Operand operand);
|
||||
|
||||
override void accept(IRVisitor visitor) @nogc;
|
||||
}
|
||||
|
||||
final class TransformVisitor : parser.ParserVisitor!ASTMapping
|
||||
{
|
||||
private HashTable!(String, int) constants;
|
||||
private BinaryExpression* statements;
|
||||
private size_t statementsLength;
|
||||
|
||||
ASTMapping.Node visit(parser.Node node) @nogc
|
||||
{
|
||||
assert(false, "Not implemented");
|
||||
}
|
||||
|
||||
ASTMapping.Definition visit(parser.Definition definition) @nogc
|
||||
{
|
||||
assert(false, "Not implemented");
|
||||
}
|
||||
|
||||
ASTMapping.BangStatement visit(parser.BangStatement statement) @nogc
|
||||
{
|
||||
return statement.expression.accept(this);
|
||||
}
|
||||
|
||||
ASTMapping.Block visit(parser.Block block) @nogc
|
||||
{
|
||||
auto target = defaultAllocator.make!Definition;
|
||||
this.constants = transformConstants(block.definitions);
|
||||
|
||||
target.result = block.statement.accept(this);
|
||||
target.statements = this.statements;
|
||||
target.statementsLength = this.statementsLength;
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
ASTMapping.Expression visit(parser.Expression expression) @nogc
|
||||
{
|
||||
if ((cast(parser.Number) expression) !is null)
|
||||
{
|
||||
return (cast(parser.Number) expression).accept(this);
|
||||
}
|
||||
if ((cast(parser.Variable) expression) !is null)
|
||||
{
|
||||
return (cast(parser.Variable) expression).accept(this);
|
||||
}
|
||||
else if ((cast(parser.BinaryExpression) expression) !is null)
|
||||
{
|
||||
return (cast(parser.BinaryExpression) expression).accept(this);
|
||||
}
|
||||
assert(false, "Invalid expression type");
|
||||
}
|
||||
|
||||
ASTMapping.Number visit(parser.Number number) @nogc
|
||||
{
|
||||
auto numberExpression = defaultAllocator.make!Number;
|
||||
numberExpression.value = number.value;
|
||||
|
||||
return numberExpression;
|
||||
}
|
||||
|
||||
ASTMapping.Variable visit(parser.Variable variable) @nogc
|
||||
{
|
||||
auto numberExpression = defaultAllocator.make!Number;
|
||||
numberExpression.value = this.constants[variable.identifier];
|
||||
|
||||
return numberExpression;
|
||||
}
|
||||
|
||||
ASTMapping.BinaryExpression visit(parser.BinaryExpression binaryExpression) @nogc
|
||||
{
|
||||
auto target = defaultAllocator.make!BinaryExpression(
|
||||
binaryExpression.lhs.accept(this),
|
||||
binaryExpression.rhs.accept(this),
|
||||
binaryExpression.operator
|
||||
);
|
||||
this.statements = cast(BinaryExpression*)
|
||||
realloc(this.statements, (this.statementsLength + 1) * BinaryExpression.sizeof);
|
||||
this.statements[this.statementsLength++] = target;
|
||||
|
||||
auto newVariable = defaultAllocator.make!Variable;
|
||||
newVariable.counter = this.statementsLength;
|
||||
|
||||
return newVariable;
|
||||
}
|
||||
|
||||
private Number transformNumber(parser.Number number) @nogc
|
||||
{
|
||||
return defaultAllocator.make!Number(number.value);
|
||||
}
|
||||
|
||||
override Operand visit(parser.Statement statement) @nogc
|
||||
{
|
||||
if ((cast(parser.BangStatement) statement) !is null)
|
||||
{
|
||||
return (cast(parser.BangStatement) statement).accept(this);
|
||||
}
|
||||
assert(false, "Invalid statement type");
|
||||
}
|
||||
|
||||
private HashTable!(String, int) transformConstants(ref Array!(parser.Definition) definitions) @nogc
|
||||
{
|
||||
typeof(return) constants;
|
||||
|
||||
foreach (definition; definitions[])
|
||||
{
|
||||
constants[definition.identifier] = definition.number.value;
|
||||
}
|
||||
|
||||
return constants;
|
||||
}
|
||||
}
|
117
source/elna/lexer.d
Normal file
117
source/elna/lexer.d
Normal file
@ -0,0 +1,117 @@
|
||||
module elna.lexer;
|
||||
|
||||
import core.stdc.stdlib;
|
||||
import core.stdc.ctype;
|
||||
import core.stdc.string;
|
||||
import elna.result;
|
||||
import std.range;
|
||||
import tanya.container.array;
|
||||
import tanya.container.string;
|
||||
|
||||
extern(C++, "elna")
|
||||
struct Token
|
||||
{
|
||||
enum Type : ushort
|
||||
{
|
||||
number = 0,
|
||||
operator = 1,
|
||||
let = 2,
|
||||
identifier = 3,
|
||||
equals = 4,
|
||||
var = 5,
|
||||
semicolon = 6,
|
||||
leftParen = 7,
|
||||
rightParen = 8,
|
||||
bang = 9,
|
||||
dot = 10,
|
||||
comma = 11,
|
||||
}
|
||||
|
||||
union Value
|
||||
{
|
||||
int number;
|
||||
const(char)* identifier;
|
||||
}
|
||||
|
||||
private Type type;
|
||||
private Value value_;
|
||||
private Position position_;
|
||||
|
||||
@disable this();
|
||||
|
||||
this(Type type, Position position) @nogc nothrow pure @safe;
|
||||
this(Type type, int value, Position position) @nogc nothrow pure @trusted;
|
||||
this(Type type, const(char)* value, Position position) @nogc nothrow;
|
||||
|
||||
/**
|
||||
* Returns: Expected token type.
|
||||
*/
|
||||
Type of() const @nogc nothrow pure @safe;
|
||||
const(char)* identifier() const @nogc nothrow pure;
|
||||
int number() const @nogc nothrow pure;
|
||||
|
||||
/**
|
||||
* Returns: The token position in the source text.
|
||||
*/
|
||||
@property const(Position) position() const @nogc nothrow pure @safe;
|
||||
}
|
||||
|
||||
/**
|
||||
* Range over the source text that keeps track of the current position.
|
||||
*/
|
||||
struct Source
|
||||
{
|
||||
const(char)* buffer;
|
||||
Position position;
|
||||
|
||||
this(const(char)* buffer) @nogc nothrow pure @safe
|
||||
{
|
||||
this.buffer = buffer;
|
||||
}
|
||||
|
||||
@disable this();
|
||||
|
||||
bool empty() @nogc nothrow pure @safe
|
||||
{
|
||||
return this.buffer is null || this.buffer[0] == '\0';
|
||||
}
|
||||
|
||||
char front() @nogc nothrow pure @safe
|
||||
in (!empty)
|
||||
{
|
||||
return this.buffer[0];
|
||||
}
|
||||
|
||||
void popFront() @nogc nothrow pure
|
||||
in (!empty)
|
||||
{
|
||||
++this.buffer;
|
||||
++this.position.column;
|
||||
}
|
||||
|
||||
void breakLine() @nogc nothrow pure
|
||||
in (!empty)
|
||||
{
|
||||
++this.buffer;
|
||||
++this.position.line;
|
||||
this.position.column = 1;
|
||||
}
|
||||
|
||||
@property size_t length() const @nogc nothrow pure
|
||||
{
|
||||
return strlen(this.buffer);
|
||||
}
|
||||
|
||||
char opIndex(size_t index) @nogc nothrow pure
|
||||
{
|
||||
return this.buffer[index];
|
||||
}
|
||||
|
||||
const(char)[] opSlice(size_t i, size_t j) @nogc nothrow pure
|
||||
{
|
||||
return this.buffer[i .. j];
|
||||
}
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
Token* lex(const(char)* buffer, CompileError* compileError, size_t* length) @nogc;
|
308
source/elna/parser.d
Normal file
308
source/elna/parser.d
Normal file
@ -0,0 +1,308 @@
|
||||
module elna.parser;
|
||||
|
||||
import core.stdc.string;
|
||||
import elna.lexer;
|
||||
import elna.result;
|
||||
import tanya.container.array;
|
||||
import tanya.container.string;
|
||||
import tanya.memory.allocator;
|
||||
import std.array;
|
||||
|
||||
/**
|
||||
* Parser visitor.
|
||||
*/
|
||||
interface ParserVisitor(Mapping)
|
||||
{
|
||||
Mapping.Node visit(Node) @nogc;
|
||||
Mapping.Definition visit(Definition) @nogc;
|
||||
Mapping.Statement visit(Statement) @nogc;
|
||||
Mapping.BangStatement visit(BangStatement) @nogc;
|
||||
Mapping.Block visit(Block) @nogc;
|
||||
Mapping.Expression visit(Expression) @nogc;
|
||||
Mapping.Number visit(Number) @nogc;
|
||||
Mapping.Variable visit(Variable) @nogc;
|
||||
Mapping.BinaryExpression visit(BinaryExpression) @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* AST node.
|
||||
*/
|
||||
abstract class Node
|
||||
{
|
||||
Mapping.Node accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constant definition.
|
||||
*/
|
||||
class Definition : Node
|
||||
{
|
||||
Number number;
|
||||
String identifier;
|
||||
|
||||
Mapping.Definition accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Statement : Node
|
||||
{
|
||||
Mapping.Statement accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
class BangStatement : Statement
|
||||
{
|
||||
Expression expression;
|
||||
|
||||
Mapping.BangStatement accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
class Block : Node
|
||||
{
|
||||
Array!Definition definitions;
|
||||
Statement statement;
|
||||
|
||||
Mapping.Block accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Expression : Node
|
||||
{
|
||||
Mapping.Expression accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
class Number : Expression
|
||||
{
|
||||
int value;
|
||||
|
||||
Mapping.Number accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
class Variable : Expression
|
||||
{
|
||||
String identifier;
|
||||
|
||||
Mapping.Variable accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
enum BinaryOperator
|
||||
{
|
||||
sum,
|
||||
subtraction
|
||||
}
|
||||
|
||||
class BinaryExpression : Expression
|
||||
{
|
||||
Expression lhs, rhs;
|
||||
BinaryOperator operator;
|
||||
|
||||
this(Expression lhs, Expression rhs, String operator) @nogc
|
||||
{
|
||||
this.lhs = lhs;
|
||||
this.rhs = rhs;
|
||||
if (operator == "+")
|
||||
{
|
||||
this.operator = BinaryOperator.sum;
|
||||
}
|
||||
else if (operator == "-")
|
||||
{
|
||||
this.operator = BinaryOperator.subtraction;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(false, "Invalid binary operator");
|
||||
}
|
||||
}
|
||||
|
||||
Mapping.BinaryExpression accept(Mapping)(ParserVisitor!Mapping visitor) @nogc
|
||||
{
|
||||
return visitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
private Result!Expression parseFactor(ref Token[] tokens) @nogc
|
||||
in (!tokens.empty, "Expected factor, got end of stream")
|
||||
{
|
||||
if (tokens.front.of() == Token.Type.identifier)
|
||||
{
|
||||
auto variable = defaultAllocator.make!Variable;
|
||||
variable.identifier = tokens.front.identifier()[0 .. strlen(tokens.front.identifier())];
|
||||
tokens.popFront;
|
||||
return Result!Expression(variable);
|
||||
}
|
||||
else if (tokens.front.of() == Token.Type.number)
|
||||
{
|
||||
auto number = defaultAllocator.make!Number;
|
||||
number.value = tokens.front.number();
|
||||
tokens.popFront;
|
||||
return Result!Expression(number);
|
||||
}
|
||||
else if (tokens.front.of() == Token.Type.leftParen)
|
||||
{
|
||||
tokens.popFront;
|
||||
|
||||
auto expression = parseExpression(tokens);
|
||||
|
||||
tokens.popFront;
|
||||
return expression;
|
||||
}
|
||||
return Result!Expression("Expected a factor", tokens.front.position);
|
||||
}
|
||||
|
||||
private Result!Expression parseTerm(ref Token[] tokens) @nogc
|
||||
{
|
||||
return parseFactor(tokens);
|
||||
}
|
||||
|
||||
private Result!Expression parseExpression(ref Token[] tokens) @nogc
|
||||
in (!tokens.empty, "Expected expression, got end of stream")
|
||||
{
|
||||
auto term = parseTerm(tokens);
|
||||
if (!term.valid || tokens.empty || tokens.front.of() != Token.Type.operator)
|
||||
{
|
||||
return term;
|
||||
}
|
||||
auto operator = String(tokens.front.identifier()[0 .. strlen(tokens.front.identifier())]);
|
||||
tokens.popFront;
|
||||
|
||||
auto expression = parseExpression(tokens);
|
||||
|
||||
if (expression.valid)
|
||||
{
|
||||
auto binaryExpression = defaultAllocator
|
||||
.make!BinaryExpression(term.result, expression.result, operator);
|
||||
|
||||
return Result!Expression(binaryExpression);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Result!Expression("Expected right-hand side to be an expression", tokens.front.position);
|
||||
}
|
||||
}
|
||||
|
||||
private Result!Definition parseDefinition(ref Token[] tokens) @nogc
|
||||
in (!tokens.empty, "Expected definition, got end of stream")
|
||||
{
|
||||
auto definition = defaultAllocator.make!Definition;
|
||||
definition.identifier = tokens.front.identifier()[0 .. strlen(tokens.front.identifier())]; // Copy.
|
||||
|
||||
tokens.popFront();
|
||||
tokens.popFront(); // Skip the equals sign.
|
||||
|
||||
if (tokens.front.of() == Token.Type.number)
|
||||
{
|
||||
auto number = defaultAllocator.make!Number;
|
||||
number.value = tokens.front.number();
|
||||
definition.number = number;
|
||||
tokens.popFront;
|
||||
return Result!Definition(definition);
|
||||
}
|
||||
return Result!Definition("Expected a number", tokens.front.position);
|
||||
}
|
||||
|
||||
private Result!Statement parseStatement(ref Token[] tokens) @nogc
|
||||
in (!tokens.empty, "Expected block, got end of stream")
|
||||
{
|
||||
if (tokens.front.of() == Token.Type.bang)
|
||||
{
|
||||
tokens.popFront;
|
||||
auto statement = defaultAllocator.make!BangStatement;
|
||||
auto expression = parseExpression(tokens);
|
||||
if (expression.valid)
|
||||
{
|
||||
statement.expression = expression.result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Result!Statement(expression.error.get);
|
||||
}
|
||||
return Result!Statement(statement);
|
||||
}
|
||||
return Result!Statement("Expected ! statement", tokens.front.position);
|
||||
}
|
||||
|
||||
private Result!(Array!Definition) parseDefinitions(ref Token[] tokens) @nogc
|
||||
in (!tokens.empty, "Expected definition, got end of stream")
|
||||
{
|
||||
tokens.popFront; // Skip const.
|
||||
|
||||
Array!Definition definitions;
|
||||
|
||||
while (!tokens.empty)
|
||||
{
|
||||
auto definition = parseDefinition(tokens);
|
||||
if (!definition.valid)
|
||||
{
|
||||
return typeof(return)(definition.error.get);
|
||||
}
|
||||
definitions.insertBack(definition.result);
|
||||
if (tokens.front.of() == Token.Type.semicolon)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (tokens.front.of() == Token.Type.comma)
|
||||
{
|
||||
tokens.popFront;
|
||||
}
|
||||
}
|
||||
|
||||
return typeof(return)(definitions);
|
||||
}
|
||||
|
||||
private Result!Block parseBlock(ref Token[] tokens) @nogc
|
||||
in (!tokens.empty, "Expected block, got end of stream")
|
||||
{
|
||||
auto block = defaultAllocator.make!Block;
|
||||
if (tokens.front.of() == Token.Type.let)
|
||||
{
|
||||
auto constDefinitions = parseDefinitions(tokens);
|
||||
if (constDefinitions.valid)
|
||||
{
|
||||
block.definitions = constDefinitions.result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Result!Block(constDefinitions.error.get);
|
||||
}
|
||||
tokens.popFront;
|
||||
}
|
||||
auto statement = parseStatement(tokens);
|
||||
if (statement.valid)
|
||||
{
|
||||
block.statement = statement.result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Result!Block(statement.error.get);
|
||||
}
|
||||
|
||||
return Result!Block(block);
|
||||
}
|
||||
|
||||
Result!Block parse(Token[] tokenStream) @nogc
|
||||
{
|
||||
auto tokens = tokenStream[];
|
||||
return parseBlock(tokens);
|
||||
}
|
98
source/elna/result.d
Normal file
98
source/elna/result.d
Normal file
@ -0,0 +1,98 @@
|
||||
module elna.result;
|
||||
|
||||
import std.typecons;
|
||||
import tanya.container.array;
|
||||
import tanya.container.string;
|
||||
|
||||
/**
|
||||
* Position in the source text.
|
||||
*/
|
||||
extern(C++, "elna")
|
||||
struct Position
|
||||
{
|
||||
/// Line.
|
||||
size_t line = 1;
|
||||
|
||||
/// Column.
|
||||
size_t column = 1;
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
struct CompileError
|
||||
{
|
||||
private const(char)* message_;
|
||||
|
||||
private Position position_;
|
||||
|
||||
@disable this();
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* message = Error text.
|
||||
* position = Error position in the source text.
|
||||
*/
|
||||
this(const(char)* message, const Position position) @nogc nothrow pure @safe;
|
||||
|
||||
/// Error text.
|
||||
@property const(char)* what() const @nogc nothrow pure @safe;
|
||||
|
||||
/// Error line in the source text.
|
||||
@property size_t line() const @nogc nothrow pure @safe;
|
||||
|
||||
/// Error column in the source text.
|
||||
@property size_t column() const @nogc nothrow pure @safe;
|
||||
}
|
||||
|
||||
struct Result(T)
|
||||
{
|
||||
Nullable!CompileError error;
|
||||
T result;
|
||||
|
||||
this(T result)
|
||||
{
|
||||
this.result = result;
|
||||
this.error = typeof(this.error).init;
|
||||
}
|
||||
|
||||
this(const(char)* message, Position position)
|
||||
{
|
||||
this.result = T.init;
|
||||
this.error = CompileError(message, position);
|
||||
}
|
||||
|
||||
this(CompileError compileError)
|
||||
{
|
||||
this.result = null;
|
||||
this.error = compileError;
|
||||
}
|
||||
|
||||
@disable this();
|
||||
|
||||
@property bool valid() const
|
||||
{
|
||||
return error.isNull;
|
||||
}
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
enum Target
|
||||
{
|
||||
text,
|
||||
high20,
|
||||
lower12i
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
struct Reference
|
||||
{
|
||||
const(char)* name;
|
||||
size_t offset;
|
||||
Target target;
|
||||
}
|
||||
|
||||
struct Symbol
|
||||
{
|
||||
String name;
|
||||
Array!ubyte text;
|
||||
Array!Reference symbols;
|
||||
}
|
183
source/elna/riscv.d
Normal file
183
source/elna/riscv.d
Normal file
@ -0,0 +1,183 @@
|
||||
module elna.riscv;
|
||||
|
||||
import core.stdc.stdlib;
|
||||
import elna.ir;
|
||||
import elna.result;
|
||||
import tanya.container.array;
|
||||
import tanya.container.string;
|
||||
|
||||
extern(C++, "elna")
|
||||
enum XRegister : ubyte
|
||||
{
|
||||
zero = 0,
|
||||
ra = 1,
|
||||
sp = 2,
|
||||
gp = 3,
|
||||
tp = 4,
|
||||
t0 = 5,
|
||||
t1 = 6,
|
||||
t2 = 7,
|
||||
s0 = 8,
|
||||
s1 = 9,
|
||||
a0 = 10,
|
||||
a1 = 11,
|
||||
a2 = 12,
|
||||
a3 = 13,
|
||||
a4 = 14,
|
||||
a5 = 15,
|
||||
a6 = 16,
|
||||
a7 = 17,
|
||||
s2 = 18,
|
||||
s3 = 19,
|
||||
s4 = 20,
|
||||
s5 = 21,
|
||||
s6 = 22,
|
||||
s7 = 23,
|
||||
s8 = 24,
|
||||
s9 = 25,
|
||||
s10 = 26,
|
||||
s11 = 27,
|
||||
t3 = 28,
|
||||
t4 = 29,
|
||||
t5 = 30,
|
||||
t6 = 31,
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
enum Funct3 : ubyte
|
||||
{
|
||||
addi = 0b000,
|
||||
slti = 0b001,
|
||||
sltiu = 0b011,
|
||||
andi = 0b111,
|
||||
ori = 0b110,
|
||||
xori = 0b100,
|
||||
slli = 0b000,
|
||||
srli = 0b101,
|
||||
srai = 0b101,
|
||||
add = 0b000,
|
||||
slt = 0b010,
|
||||
sltu = 0b011,
|
||||
and = 0b111,
|
||||
or = 0b110,
|
||||
xor = 0b100,
|
||||
sll = 0b001,
|
||||
srl = 0b101,
|
||||
sub = 0b000,
|
||||
sra = 0b101,
|
||||
beq = 0b000,
|
||||
bne = 0b001,
|
||||
blt = 0b100,
|
||||
bltu = 0b110,
|
||||
bge = 0b101,
|
||||
bgeu = 0b111,
|
||||
fence = 0b000,
|
||||
fenceI = 0b001,
|
||||
csrrw = 0b001,
|
||||
csrrs = 0b010,
|
||||
csrrc = 0b011,
|
||||
csrrwi = 0b101,
|
||||
csrrsi = 0b110,
|
||||
csrrci = 0b111,
|
||||
priv = 0b000,
|
||||
sb = 0b000,
|
||||
sh = 0b001,
|
||||
sw = 0b010,
|
||||
lb = 0b000,
|
||||
lh = 0b001,
|
||||
lw = 0b010,
|
||||
lbu = 0b100,
|
||||
lhu = 0b101,
|
||||
jalr = 0b000,
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
enum Funct12 : ubyte
|
||||
{
|
||||
ecall = 0b000000000000,
|
||||
ebreak = 0b000000000001,
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
enum Funct7 : ubyte
|
||||
{
|
||||
none = 0,
|
||||
sub = 0b0100000
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
enum BaseOpcode : ubyte
|
||||
{
|
||||
opImm = 0b0010011,
|
||||
lui = 0b0110111,
|
||||
auipc = 0b0010111,
|
||||
op = 0b0110011,
|
||||
jal = 0b1101111,
|
||||
jalr = 0b1100111,
|
||||
branch = 0b1100011,
|
||||
load = 0b0000011,
|
||||
store = 0b0100011,
|
||||
miscMem = 0b0001111,
|
||||
system = 0b1110011,
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
struct Instruction
|
||||
{
|
||||
private uint instruction;
|
||||
|
||||
this(BaseOpcode opcode) @nogc;
|
||||
@disable this();
|
||||
|
||||
ref Instruction i(XRegister rd, Funct3 funct3, XRegister rs1, uint immediate)
|
||||
return scope @nogc;
|
||||
|
||||
ref Instruction s(uint imm1, Funct3 funct3, XRegister rs1, XRegister rs2)
|
||||
return scope @nogc;
|
||||
|
||||
ref Instruction r(XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, Funct7 funct7 = Funct7.none)
|
||||
return scope @nogc;
|
||||
|
||||
ref Instruction u(XRegister rd, uint imm)
|
||||
return scope @nogc;
|
||||
|
||||
ubyte* encode() return scope @nogc;
|
||||
}
|
||||
|
||||
extern(C++, "elna")
|
||||
class RiscVVisitor : IRVisitor
|
||||
{
|
||||
Instruction *instructions;
|
||||
size_t instructionsLength;
|
||||
bool registerInUse;
|
||||
uint variableCounter = 1;
|
||||
Reference[3] references;
|
||||
|
||||
override void visit(Node) @nogc;
|
||||
override void visit(Definition definition) @nogc;
|
||||
override void visit(Operand operand) @nogc;
|
||||
override void visit(Variable variable) @nogc;
|
||||
override void visit(Number number) @nogc;
|
||||
override void visit(BinaryExpression expression) @nogc;
|
||||
}
|
||||
|
||||
Symbol writeNext(Definition ast) @nogc
|
||||
{
|
||||
Array!Instruction instructions;
|
||||
auto visitor = cast(RiscVVisitor) malloc(__traits(classInstanceSize, RiscVVisitor));
|
||||
(cast(void*) visitor)[0 .. __traits(classInstanceSize, RiscVVisitor)] = __traits(initSymbol, RiscVVisitor)[];
|
||||
scope (exit)
|
||||
{
|
||||
free(cast(void*) visitor);
|
||||
}
|
||||
visitor.visit(ast);
|
||||
|
||||
auto program = Symbol(String("main"));
|
||||
|
||||
program.symbols = Array!Reference(visitor.references[]);
|
||||
foreach (ref instruction; visitor.instructions[0 .. visitor.instructionsLength])
|
||||
{
|
||||
program.text.insertBack(instruction.encode[0 .. uint.sizeof]);
|
||||
}
|
||||
return program;
|
||||
}
|
53
source/ir.cpp
Normal file
53
source/ir.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "elna/ir.hpp"
|
||||
|
||||
namespace elna::ir
|
||||
{
|
||||
/**
|
||||
* AST node.
|
||||
*/
|
||||
void Node::accept(IRVisitor *)
|
||||
{
|
||||
}
|
||||
|
||||
void Definition::accept(IRVisitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
void Operand::accept(IRVisitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
void Number::accept(IRVisitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
void Variable::accept(IRVisitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
BinaryExpression::BinaryExpression(Operand *lhs, Operand *rhs, BinaryOperator _operator)
|
||||
{
|
||||
this->lhs = lhs;
|
||||
this->rhs = rhs;
|
||||
this->_operator = _operator;
|
||||
}
|
||||
|
||||
void BinaryExpression::accept(IRVisitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
BangExpression::BangExpression(Operand *operand)
|
||||
{
|
||||
this->operand = operand;
|
||||
}
|
||||
|
||||
void BangExpression::accept(IRVisitor *visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
}
|
270
source/lexer.cpp
Normal file
270
source/lexer.cpp
Normal file
@ -0,0 +1,270 @@
|
||||
#include "elna/lexer.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
source::source(const std::string& buffer)
|
||||
: m_buffer(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
source::const_iterator source::begin() const
|
||||
{
|
||||
return source::const_iterator(std::cbegin(m_buffer));
|
||||
}
|
||||
|
||||
source::const_iterator source::end() const
|
||||
{
|
||||
Position end_position{ 0, 0 };
|
||||
|
||||
return source::const_iterator(std::cend(m_buffer), end_position);
|
||||
}
|
||||
|
||||
source::const_iterator::const_iterator(std::string::const_iterator buffer,
|
||||
const Position start_position)
|
||||
: m_buffer(buffer), m_position(start_position)
|
||||
{
|
||||
}
|
||||
|
||||
const Position& source::const_iterator::position() const noexcept
|
||||
{
|
||||
return this->m_position;
|
||||
}
|
||||
|
||||
source::const_iterator::reference source::const_iterator::operator*() const noexcept
|
||||
{
|
||||
return *m_buffer;
|
||||
}
|
||||
|
||||
source::const_iterator::pointer source::const_iterator::operator->() const noexcept
|
||||
{
|
||||
return m_buffer.base();
|
||||
}
|
||||
|
||||
source::const_iterator& source::const_iterator::operator++()
|
||||
{
|
||||
if (*this->m_buffer == '\n')
|
||||
{
|
||||
this->m_position.column = 1;
|
||||
++this->m_position.line;
|
||||
}
|
||||
else
|
||||
{
|
||||
++this->m_position.column;
|
||||
}
|
||||
std::advance(this->m_buffer, 1);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
source::const_iterator& source::const_iterator::operator++(int)
|
||||
{
|
||||
auto tmp = *this;
|
||||
++(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool source::const_iterator::operator==(const source::const_iterator& that) const noexcept
|
||||
{
|
||||
return this->m_buffer == that.m_buffer;
|
||||
}
|
||||
|
||||
bool source::const_iterator::operator!=(const source::const_iterator& that) const noexcept
|
||||
{
|
||||
return !(*this == that);
|
||||
}
|
||||
|
||||
Token::Token(const Type of, const char *value, Position position)
|
||||
: m_type(of), m_position(position)
|
||||
{
|
||||
std::size_t value_length = strlen(value);
|
||||
char *buffer = reinterpret_cast<char *>(malloc(value_length + 1));
|
||||
|
||||
std::memcpy(buffer, value, value_length);
|
||||
buffer[value_length] = 0;
|
||||
|
||||
m_value.identifier = buffer;
|
||||
}
|
||||
|
||||
Token::Token(const Type of, std::int32_t number, Position position)
|
||||
: m_type(of), m_position(position)
|
||||
{
|
||||
m_value.number = number;
|
||||
}
|
||||
|
||||
Token::Token(const Type of, Position position)
|
||||
: m_type(of), m_position(position)
|
||||
{
|
||||
}
|
||||
|
||||
Token::Token(const Token& that)
|
||||
: m_type(that.of()), m_position(that.position())
|
||||
{
|
||||
*this = that;
|
||||
}
|
||||
|
||||
Token::Token(Token&& that)
|
||||
: m_type(that.of()), m_position(that.position())
|
||||
{
|
||||
*this = std::move(that);
|
||||
}
|
||||
|
||||
Token::~Token()
|
||||
{
|
||||
if (m_type == TOKEN_IDENTIFIER || m_type == TOKEN_OPERATOR)
|
||||
{
|
||||
std::free(const_cast<char*>(m_value.identifier));
|
||||
}
|
||||
}
|
||||
|
||||
Token& Token::operator=(const Token& that)
|
||||
{
|
||||
m_type = that.of();
|
||||
m_position = that.position();
|
||||
if (that.of() == TOKEN_IDENTIFIER || that.of() == TOKEN_OPERATOR)
|
||||
{
|
||||
std::size_t value_length = strlen(that.identifier());
|
||||
char *buffer = reinterpret_cast<char *>(malloc(value_length + 1));
|
||||
|
||||
std::memcpy(buffer, that.identifier(), value_length);
|
||||
buffer[value_length] = 0;
|
||||
|
||||
m_value.identifier = buffer;
|
||||
}
|
||||
else if (that.of() == TOKEN_NUMBER)
|
||||
{
|
||||
m_value.number = that.number();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Token& Token::operator=(Token&& that)
|
||||
{
|
||||
m_type = that.of();
|
||||
m_position = that.position();
|
||||
if (that.of() == TOKEN_IDENTIFIER || that.of() == TOKEN_OPERATOR)
|
||||
{
|
||||
m_value.identifier = that.identifier();
|
||||
that.m_value.identifier = nullptr;
|
||||
}
|
||||
else if (that.of() == TOKEN_NUMBER)
|
||||
{
|
||||
m_value.number = that.number();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Token::Type Token::of() const noexcept
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
const char *Token::identifier() const noexcept
|
||||
{
|
||||
return m_value.identifier;
|
||||
}
|
||||
|
||||
std::int32_t Token::number() const noexcept
|
||||
{
|
||||
return m_value.number;
|
||||
}
|
||||
|
||||
const Position& Token::position() const noexcept
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
Token *lex(const char *buffer, CompileError *compile_error, std::size_t *length)
|
||||
{
|
||||
std::vector<Token> tokens;
|
||||
source input{ buffer };
|
||||
|
||||
for (auto iterator = input.begin(); iterator != input.end();)
|
||||
{
|
||||
if (*iterator == ' ' || *iterator == '\n')
|
||||
{
|
||||
}
|
||||
else if (std::isdigit(*iterator))
|
||||
{
|
||||
tokens.emplace_back(
|
||||
Token::TOKEN_NUMBER,
|
||||
static_cast<std::int32_t>(*iterator - '0'),
|
||||
iterator.position()
|
||||
);
|
||||
}
|
||||
else if (*iterator == '=')
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_EQUALS, iterator.position());
|
||||
}
|
||||
else if (*iterator == '(')
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_LEFT_PAREN, iterator.position());
|
||||
}
|
||||
else if (*iterator == ')')
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_RIGHT_PAREN, iterator.position());
|
||||
}
|
||||
else if (*iterator == ';')
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_SEMICOLON, iterator.position());
|
||||
}
|
||||
else if (*iterator == ',')
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_COMMA, iterator.position());
|
||||
}
|
||||
else if (*iterator == '!')
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_BANG, iterator.position());
|
||||
}
|
||||
else if (*iterator == '.')
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_DOT, iterator.position());
|
||||
}
|
||||
else if (std::isalpha(*iterator))
|
||||
{
|
||||
std::string word;
|
||||
auto i = iterator;
|
||||
while (i != input.end() && std::isalpha(*i))
|
||||
{
|
||||
word.push_back(*i);
|
||||
++i;
|
||||
}
|
||||
if (word == "const")
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_LET, iterator.position());
|
||||
}
|
||||
else if (word == "var")
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_VAR, iterator.position());
|
||||
}
|
||||
else
|
||||
{
|
||||
tokens.emplace_back(Token::TOKEN_IDENTIFIER, word.c_str(), iterator.position());
|
||||
}
|
||||
iterator = i;
|
||||
continue;
|
||||
}
|
||||
else if (*iterator == '+' || *iterator == '-')
|
||||
{
|
||||
std::string _operator{ *iterator };
|
||||
|
||||
tokens.emplace_back(Token::TOKEN_OPERATOR, _operator.c_str(), iterator.position());
|
||||
}
|
||||
else
|
||||
{
|
||||
*compile_error = CompileError("Unexpected next character", iterator.position());
|
||||
return nullptr;
|
||||
}
|
||||
++iterator;
|
||||
}
|
||||
Token *target = reinterpret_cast<Token *>(malloc(tokens.size() * sizeof(Token) + sizeof(Token)));
|
||||
int i = 0;
|
||||
for (auto& token : tokens)
|
||||
{
|
||||
target[i] = std::move(token);
|
||||
++i;
|
||||
}
|
||||
*length = i;
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
33
source/main.d
Normal file
33
source/main.d
Normal file
@ -0,0 +1,33 @@
|
||||
import elna.backend;
|
||||
import elna.ir;
|
||||
import elna.arguments;
|
||||
import std.path;
|
||||
import std.sumtype;
|
||||
import tanya.container.string;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.memory.mmappool;
|
||||
|
||||
int main(string[] args)
|
||||
{
|
||||
defaultAllocator = MmapPool.instance;
|
||||
|
||||
return Arguments.parse(args).match!(
|
||||
(ArgumentError argumentError) => 4,
|
||||
(Arguments arguments) {
|
||||
String outputFilename;
|
||||
if (arguments.output is null)
|
||||
{
|
||||
outputFilename = arguments
|
||||
.inFile
|
||||
.baseName
|
||||
.withExtension("o");
|
||||
}
|
||||
else
|
||||
{
|
||||
outputFilename = String(arguments.output);
|
||||
}
|
||||
|
||||
return generate(arguments.inFile, outputFilename);
|
||||
}
|
||||
);
|
||||
}
|
25
source/result.cpp
Normal file
25
source/result.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "elna/result.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
CompileError::CompileError(const char *message, const Position position) noexcept
|
||||
{
|
||||
this->message = message;
|
||||
this->position = position;
|
||||
}
|
||||
|
||||
char const *CompileError::what() const noexcept
|
||||
{
|
||||
return this->message;
|
||||
}
|
||||
|
||||
std::size_t CompileError::line() const noexcept
|
||||
{
|
||||
return this->position.line;
|
||||
}
|
||||
|
||||
std::size_t CompileError::column() const noexcept
|
||||
{
|
||||
return this->position.column;
|
||||
}
|
||||
}
|
191
source/riscv.cpp
Normal file
191
source/riscv.cpp
Normal file
@ -0,0 +1,191 @@
|
||||
#include "elna/parser.hpp"
|
||||
#include "elna/riscv.hpp"
|
||||
#include <type_traits>
|
||||
|
||||
namespace elna
|
||||
{
|
||||
Instruction::Instruction(BaseOpcode opcode)
|
||||
{
|
||||
this->instruction = static_cast<std::underlying_type<BaseOpcode>::type>(opcode);
|
||||
}
|
||||
|
||||
Instruction& Instruction::i(XRegister rd, Funct3 funct3, XRegister rs1, std::uint32_t immediate)
|
||||
{
|
||||
this->instruction |= (static_cast<std::underlying_type<XRegister>::type>(rd) << 7)
|
||||
| (static_cast<std::underlying_type<Funct3>::type>(funct3) << 12)
|
||||
| (static_cast<std::underlying_type<XRegister>::type>(rs1) << 15)
|
||||
| (immediate << 20);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Instruction& Instruction::s(std::uint32_t imm1, Funct3 funct3, XRegister rs1, XRegister rs2)
|
||||
{
|
||||
this->instruction |= ((imm1 & 0b11111) << 7)
|
||||
| (static_cast<std::underlying_type<Funct3>::type>(funct3) << 12)
|
||||
| (static_cast<std::underlying_type<XRegister>::type>(rs1) << 15)
|
||||
| (static_cast<std::underlying_type<XRegister>::type>(rs2) << 20)
|
||||
| ((imm1 & 0b111111100000) << 20);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Instruction& Instruction::r(XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, Funct7 funct7)
|
||||
{
|
||||
this->instruction |= (static_cast<std::underlying_type<XRegister>::type>(rd) << 7)
|
||||
| (static_cast<std::underlying_type<Funct3>::type>(funct3) << 12)
|
||||
| (static_cast<std::underlying_type<XRegister>::type>(rs1) << 15)
|
||||
| (static_cast<std::underlying_type<XRegister>::type>(rs2) << 20)
|
||||
| (static_cast<std::underlying_type<Funct7>::type>(funct7) << 25);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Instruction& Instruction::u(XRegister rd, std::uint32_t imm)
|
||||
{
|
||||
this->instruction |= (static_cast<std::underlying_type<XRegister>::type>(rd) << 7) | (imm << 12);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::uint8_t *Instruction::encode()
|
||||
{
|
||||
return reinterpret_cast<std::uint8_t *>(&this->instruction);
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(ir::Node *)
|
||||
{
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(ir::Definition *definition)
|
||||
{
|
||||
const uint stackSize = static_cast<std::uint32_t>(definition->statementsLength * 4 + 12);
|
||||
|
||||
this->instructionsLength += 4;
|
||||
this->instructions = reinterpret_cast<Instruction *>(
|
||||
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
|
||||
|
||||
// Prologue.
|
||||
this->instructions[instructionsLength - 4] = Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::sp, Funct3::addi, XRegister::sp, -stackSize);
|
||||
this->instructions[instructionsLength - 3] = Instruction(BaseOpcode::store)
|
||||
.s(stackSize - 4, Funct3::sw, XRegister::sp, XRegister::s0);
|
||||
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::store)
|
||||
.s(stackSize - 8, Funct3::sw, XRegister::sp, XRegister::ra);
|
||||
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::s0, Funct3::addi, XRegister::sp, stackSize);
|
||||
|
||||
for (std::size_t i = 0; i < definition->statementsLength; ++i)
|
||||
{
|
||||
definition->statements[i]->accept(this);
|
||||
}
|
||||
this->registerInUse = true;
|
||||
definition->result->accept(this);
|
||||
this->registerInUse = false;
|
||||
|
||||
this->instructions = reinterpret_cast<Instruction*>(
|
||||
realloc(this->instructions, (this->instructionsLength + 10) * sizeof(Instruction)));
|
||||
|
||||
// Print the result.
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::a1, Funct3::addi, XRegister::a0, 0);
|
||||
this->references[0] = Reference();
|
||||
this->references[0].name = ".CL0";
|
||||
this->references[0].offset = instructionsLength * 4;
|
||||
this->references[0].target = Target::high20;
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::lui).u(XRegister::a5, 0);
|
||||
this->references[1] = Reference();
|
||||
this->references[1].name = ".CL0";
|
||||
this->references[1].offset = instructionsLength * 4;
|
||||
this->references[1].target = Target::lower12i;
|
||||
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::a0, Funct3::addi, XRegister::a5, 0);
|
||||
this->references[2] = Reference();
|
||||
this->references[2].name = "printf";
|
||||
this->references[2].offset = instructionsLength * 4;
|
||||
this->references[2].target = Target::text;
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::auipc).u(XRegister::ra, 0);
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::jalr)
|
||||
.i(XRegister::ra, Funct3::jalr, XRegister::ra, 0);
|
||||
// Set the return value (0).
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::op)
|
||||
.r(XRegister::a0, Funct3::_and, XRegister::zero, XRegister::zero);
|
||||
|
||||
// Epilogue.
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::load)
|
||||
.i(XRegister::s0, Funct3::lw, XRegister::sp, stackSize - 4);
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::load)
|
||||
.i(XRegister::ra, Funct3::lw, XRegister::sp, stackSize - 8);
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::sp, Funct3::addi, XRegister::sp, stackSize);
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::jalr)
|
||||
.i(XRegister::zero, Funct3::jalr, XRegister::ra, 0);
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(ir::Operand *operand)
|
||||
{
|
||||
if (dynamic_cast<ir::Variable *>(operand) != nullptr)
|
||||
{
|
||||
return dynamic_cast<ir::Variable *>(operand)->accept(this);
|
||||
}
|
||||
if (dynamic_cast<ir::Number *>(operand) != nullptr)
|
||||
{
|
||||
return dynamic_cast<ir::Number *>(operand)->accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(ir::Variable *variable)
|
||||
{
|
||||
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
|
||||
|
||||
++this->instructionsLength;
|
||||
this->instructions = reinterpret_cast<Instruction *>(
|
||||
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
|
||||
// movl -x(%rbp), %eax; where x is a number.
|
||||
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::load)
|
||||
.i(freeRegister, Funct3::lw, XRegister::sp,
|
||||
static_cast<std::int8_t>(variable->counter * 4));
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(ir::Number *number)
|
||||
{
|
||||
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
|
||||
|
||||
++this->instructionsLength;
|
||||
this->instructions = reinterpret_cast<Instruction *>(
|
||||
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
|
||||
this->instructions[this->instructionsLength - 1] =
|
||||
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
|
||||
.i(freeRegister, Funct3::addi, XRegister::zero, number->value);
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(ir::BinaryExpression *expression)
|
||||
{
|
||||
this->registerInUse = true;
|
||||
expression->lhs->accept(this);
|
||||
this->registerInUse = false;
|
||||
expression->rhs->accept(this);
|
||||
|
||||
this->instructionsLength += 2;
|
||||
this->instructions = reinterpret_cast<Instruction *>(
|
||||
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
|
||||
// Calculate the result and assign it to a variable on the stack.
|
||||
switch (expression->_operator)
|
||||
{
|
||||
case BinaryOperator::sum:
|
||||
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::op)
|
||||
.r(XRegister::a0, Funct3::add, XRegister::a0, XRegister::t0);
|
||||
break;
|
||||
case BinaryOperator::subtraction:
|
||||
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::op)
|
||||
.r(XRegister::a0, Funct3::sub, XRegister::a0, XRegister::t0, Funct7::sub);
|
||||
break;
|
||||
}
|
||||
this->instructions[instructionsLength - 1] = // movl %eax, -x(%rbp); where x is a number.
|
||||
Instruction(BaseOpcode::store)
|
||||
.s(static_cast<std::uint32_t>(this->variableCounter * 4), Funct3::sw, XRegister::sp, XRegister::a0);
|
||||
|
||||
++this->variableCounter;
|
||||
}
|
||||
}
|
1
tests/7_member_sum.eln
Normal file
1
tests/7_member_sum.eln
Normal file
@ -0,0 +1 @@
|
||||
! 3 + 4 + 5 + 1 + 2 + 4 + 3
|
3
tests/const_list.eln
Normal file
3
tests/const_list.eln
Normal file
@ -0,0 +1,3 @@
|
||||
const a = 1, b = 2;
|
||||
! a + b
|
||||
.
|
1
tests/expectations/7_member_sum.txt
Normal file
1
tests/expectations/7_member_sum.txt
Normal file
@ -0,0 +1 @@
|
||||
22
|
1
tests/expectations/const_list.txt
Normal file
1
tests/expectations/const_list.txt
Normal file
@ -0,0 +1 @@
|
||||
3
|
1
tests/expectations/left_nested_sum.txt
Normal file
1
tests/expectations/left_nested_sum.txt
Normal file
@ -0,0 +1 @@
|
||||
8
|
1
tests/expectations/print_number.txt
Normal file
1
tests/expectations/print_number.txt
Normal file
@ -0,0 +1 @@
|
||||
3
|
1
tests/expectations/subtraction.txt
Normal file
1
tests/expectations/subtraction.txt
Normal file
@ -0,0 +1 @@
|
||||
1
|
1
tests/expectations/sum.txt
Normal file
1
tests/expectations/sum.txt
Normal file
@ -0,0 +1 @@
|
||||
8
|
1
tests/expectations/sums.txt
Normal file
1
tests/expectations/sums.txt
Normal file
@ -0,0 +1 @@
|
||||
8
|
2
tests/left_nested_sum.eln
Normal file
2
tests/left_nested_sum.eln
Normal file
@ -0,0 +1,2 @@
|
||||
! (3 + 4) + 1
|
||||
.
|
2
tests/print_number.eln
Normal file
2
tests/print_number.eln
Normal file
@ -0,0 +1,2 @@
|
||||
! 3
|
||||
.
|
@ -35,11 +35,48 @@ namespace elna
|
||||
}
|
||||
}
|
||||
|
||||
static std::string in_build_directory(const std::filesystem::path& path)
|
||||
{
|
||||
return "build/riscv" / path;
|
||||
}
|
||||
|
||||
static int build_test(const std::filesystem::directory_entry& test_entry)
|
||||
{
|
||||
const std::filesystem::path test_filename = test_entry.path().filename();
|
||||
|
||||
std::filesystem::path test_binary = in_build_directory(test_filename);
|
||||
std::filesystem::path test_object = test_binary;
|
||||
test_binary.replace_extension();
|
||||
test_object.replace_extension(".o");
|
||||
|
||||
std::filesystem::remove(test_binary);
|
||||
std::filesystem::remove(test_object);
|
||||
|
||||
auto status = boost::process::system("./build/bin/elna",
|
||||
"-o", test_object.string(), test_entry.path().string());
|
||||
if (status != 0)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
status = boost::process::system(
|
||||
"/opt/riscv/bin/riscv32-unknown-elf-ld",
|
||||
"-o", test_binary.string(),
|
||||
"-L/opt/riscv/lib/gcc/riscv32-unknown-elf/13.2.0/",
|
||||
"-L/opt/riscv/riscv32-unknown-elf/lib",
|
||||
"/opt/riscv/riscv32-unknown-elf/lib/crt0.o",
|
||||
"/opt/riscv/lib/gcc/riscv32-unknown-elf/13.2.0/crtbegin.o",
|
||||
test_object.string(),
|
||||
"--start-group", "-lgcc", "-lc", "-lgloss", "--end-group",
|
||||
"/opt/riscv/lib/gcc/riscv32-unknown-elf/13.2.0/crtend.o"
|
||||
);
|
||||
return status;
|
||||
}
|
||||
|
||||
static int run_test(const std::filesystem::directory_entry& test_entry)
|
||||
{
|
||||
const std::filesystem::path test_filename = test_entry.path().filename();
|
||||
|
||||
std::filesystem::path test_binary = "build/riscv" / test_filename;
|
||||
std::filesystem::path test_binary = in_build_directory(test_filename);
|
||||
test_binary.replace_extension();
|
||||
|
||||
std::filesystem::path expectation_path = test_entry.path().parent_path() / "expectations" / test_filename;
|
||||
@ -75,7 +112,23 @@ namespace elna
|
||||
{
|
||||
continue;
|
||||
}
|
||||
results.add_exit_code(run_test(test_entry));
|
||||
int status{ 0 };
|
||||
|
||||
try
|
||||
{
|
||||
status = build_test(test_entry);
|
||||
}
|
||||
catch (const boost::process::process_error& exception)
|
||||
{
|
||||
std::cout << exception.what() << std::endl;
|
||||
status = 3;
|
||||
continue;
|
||||
}
|
||||
if (status == 0)
|
||||
{
|
||||
status = run_test(test_entry);
|
||||
}
|
||||
results.add_exit_code(status);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
2
tests/subtraction.eln
Normal file
2
tests/subtraction.eln
Normal file
@ -0,0 +1,2 @@
|
||||
! 5 - 4
|
||||
.
|
2
tests/sum.eln
Normal file
2
tests/sum.eln
Normal file
@ -0,0 +1,2 @@
|
||||
! 1 + 7
|
||||
.
|
2
tests/sums.eln
Normal file
2
tests/sums.eln
Normal file
@ -0,0 +1,2 @@
|
||||
! 1 + (3 + 4)
|
||||
.
|
Loading…
Reference in New Issue
Block a user