Skip to content

Commit

Permalink
Merge pull request muqiuhan#16 from X-FRI/main
Browse files Browse the repository at this point in the history
G-Machine model and new logo :)
  • Loading branch information
muqiuhan authored Feb 3, 2024
2 parents 35a3256 + 7c00b5e commit 81be3d3
Show file tree
Hide file tree
Showing 17 changed files with 569 additions and 92 deletions.
95 changes: 68 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

---

![](./resources/IllegalPattern.png)

## Build & Install

To build and run with [xmake](xmake.io):
Expand All @@ -31,33 +29,76 @@ To build and run with [xmake](xmake.io):
- To install: xmake install

## Introduction
- Typechecking
```
data Bool = [ True, False ]
let main(argv) = {
match argv with {
| True => { 0 }
| False => { True }
}
}
```
![./resources/BranchTypeChecking.png](./resources/BranchTypeChecking.png)

- Pattern match
```
data List = [ Nil, Cons Int List ]
let main(argv) = {
match argv with {
| Nil => { 0 }
| Cons x xs => { 0 }
}

### Typechecking
$$
\begin{prooftree}
\AXC{$\Gamma \vdash e_1 : \tau_1 \rightarrow \tau_2 \quad \Gamma \vdash e_2 : \tau_1$}
\UnaryInfC{$\Gamma \vdash e_1 e_2 : \tau_2$}
\end{prooftree}
$$

$$
\begin{prooftree}
\AXC{$\Gamma \vdash e : \tau \quad match_\tau(\tau, p_i) = b_i \quad \Gamma, b_i \vdash e_i : \tau_c $}
\UnaryInfC{$\Gamma \vdash match \enspace e \enspace with \enspace \{(p_1, e_1) ... (p_n, e_n)\} : \tau_c$}
\end{prooftree}
$$

$$
\begin{prooftree}
\AXC{}
\UnaryInfC{$match_\tau(\tau, v) = \{v : \tau\}$}
\end{prooftree}
$$

$$
\begin{prooftree}
\AXC{$\Gamma \vdash c : \tau_1 \rightarrow ... \rightarrow \tau_n \rightarrow \tau$}
\UnaryInfC{$match_\tau(\tau, c \enspace v_1...v_n) = \{v_1 : \tau_1 ...v_n : \tau_n\}$}
\end{prooftree}
$$

### Pattern Matching
```rescript
data List = [ Nil, Cons Int List ]
let main(argv) = {
match argv with {
| Nil => { 0 }
| Cons x xs => { 0 }
}
```
}
```

### Garbage Collection
...

### Polymorphism
...

## Acknowledge

### Tools
- [XMake: A cross-platform build utility based on Lua](https://xmake.io/#/)
- [GNU Bison: A general-purpose parser generator](https://github.com/akimd/bison)
- [Flex: The Fast Lexical Analyzer - scanner generator for lexing in C and C++](https://github.com/westes/flex)


### Library/Frameworks
- [optional: C++11/14/17 std::optional with functional-style extensions and reference support](https://github.com/TartanLlama/optional)
- [result: Result<T, E> for Modern C++](https://github.com/p-ranav/result)

- [WIP] Garbage Collection
- [WIP] Polymorphism
## REFERENCE
- [Theory of computation](https://en.wikipedia.org/wiki/Theory_of_computation)
- [Functional programming](https://en.wikipedia.org/wiki/Functional_programming)
- [Finite-state machine](https://en.wikipedia.org/wiki/Finite-state_machine)
- [Implementing Fun tional Languages: a tutorial](https://www.microsoft.com/en-us/research/wp-content/uploads/1992/01/student.pdf)
- [The G-machine: A fast, graph-reduction evaluator](https://link.springer.com/chapter/10.1007/3-540-15975-4_50)
- [Panic better using modern C++](https://buildingblock.ai/panic)
- [Hindley–Milner type system](https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system)
- [Unification](https://en.wikipedia.org/wiki/Unification_(computer_science))
- [Compiling a Functional Language Using C++](https://danilafe.com/blog/00_compiler_intro/)

## [LICENSE](./LICENSE)
```
Expand Down
2 changes: 0 additions & 2 deletions compiler/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,10 @@ namespace swallow::compiler::ast
DIVIDE
};

private:
const Operators Operator;
const Ptr Left;
const Ptr Right;

public:
Binop(const yy::location Location, Operators Operator, Ptr Left, Ptr Right)
: Operator(Operator)
, Left(std::move(Left))
Expand Down
2 changes: 1 addition & 1 deletion compiler/ast/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,4 @@ namespace swallow::compiler::ast
const type::Environment &typeEnvironment) const noexcept
{}

} // namespace swallow::compiler::ast
} // namespace swallow::compiler::ast
29 changes: 29 additions & 0 deletions compiler/diagnostics/code.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
// Copyright (c) 2023 Muqiu Han
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice,
// this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Terifo nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef SWALLOW_COMPILER_DIAGNOSTICS_CODE_HPP
#define SWALLOW_COMPILER_DIAGNOSTICS_CODE_HPP

Expand Down
52 changes: 26 additions & 26 deletions compiler/diagnostics/diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ namespace swallow::compiler::diagnostics
auto line_number = line_index + 1;
output << " "
<< COLOR_RGB(std::setw(spaces_prefix.length() - 3)
<< std::setfill(' ') << line_number << " ",
<< std::setfill(' ') << line_number << " | ",
COLOR_GREY);

auto labels = this->find_labels_in_line(line_index);
Expand Down Expand Up @@ -388,7 +388,7 @@ namespace swallow::compiler::diagnostics
current_label_startings[relative_span.get_start_index()] = label;
}

output << spaces_prefix << COLOR_RGB("· ", COLOR_GREY);
output << spaces_prefix << COLOR_RGB("o ", COLOR_GREY);

const Label *last_label = nullptr;
size_t last_end_index = 0U;
Expand All @@ -397,22 +397,22 @@ namespace swallow::compiler::diagnostics
if (next_label_endings.contains(index))
{
auto &next_label = next_label_endings.at(index);
COLOR_BY_TYPE(next_label->get_color(), "");
COLOR_BY_TYPE(next_label->get_color(), "|");
continue;
}
if (next_label_startings.contains(index))
{
auto &next_label = next_label_startings.at(index);
COLOR_BY_TYPE(next_label->get_color(), "");
COLOR_BY_TYPE(next_label->get_color(), "|");
continue;
}

if (!current_label_startings.contains(index))
{
if (index == last_end_index && index != 0)
COLOR_BY_TYPE(last_label->get_color(), "");
COLOR_BY_TYPE(last_label->get_color(), "^");
else if (index < last_end_index)
COLOR_BY_TYPE(last_label->get_color(), "");
COLOR_BY_TYPE(last_label->get_color(), "^");
else
output << " ";

Expand All @@ -425,19 +425,19 @@ namespace swallow::compiler::diagnostics
if (last_end_index >= index && index != 0)
{
if (label->get_message())
COLOR_BY_TYPE(label->get_color(), "");
COLOR_BY_TYPE(label->get_color(), "=");
else
COLOR_BY_TYPE(label->get_color(), "");
COLOR_BY_TYPE(label->get_color(), "+");
}
else if (relative_span.get_end_index() > index)
{
if (label->get_message())
COLOR_BY_TYPE(label->get_color(), "");
COLOR_BY_TYPE(label->get_color(), "^");
else
COLOR_BY_TYPE(label->get_color(), "");
COLOR_BY_TYPE(label->get_color(), "+");
}
else if (label->get_message())
COLOR_BY_TYPE(label->get_color(), "");
COLOR_BY_TYPE(label->get_color(), "^");
else
COLOR_BY_TYPE(label->get_color(), "^");

Expand All @@ -452,36 +452,36 @@ namespace swallow::compiler::diagnostics
if (!label->get_message())
continue;

output << spaces_prefix << COLOR_RGB("· ", COLOR_GREY);
output << spaces_prefix << COLOR_RGB("o ", COLOR_GREY);

const auto relative_span = label->get_span().relative_to(line_span);
for (auto index = 0U; index < relative_span.get_start_index(); index++)
{
if (next_label_endings.contains(index))
{
auto &next_label = next_label_endings.at(index);
COLOR_BY_TYPE(next_label->get_color(), "");
COLOR_BY_TYPE(next_label->get_color(), "|");
continue;
}

if (next_label_startings.contains(index))
{
auto &next_label = next_label_startings.at(index);
COLOR_BY_TYPE(next_label->get_color(), "");
COLOR_BY_TYPE(next_label->get_color(), "|");
continue;
}

if (current_label_startings.contains(index))
{
auto &next_label = current_label_startings.at(index);
COLOR_BY_TYPE(next_label->get_color(), "");
COLOR_BY_TYPE(next_label->get_color(), "|");
continue;
}

output << " ";
}

COLOR_BY_TYPE(label->get_color(), "╰─▶ ");
COLOR_BY_TYPE(label->get_color(), ":= ");
print_formatted_text(output, label->get_message().value());
output << "\n";
}
Expand Down Expand Up @@ -654,12 +654,12 @@ namespace swallow::compiler::diagnostics
void FileGroup::print(std::ostream &output,
const std::string &spaces_prefix) const
{
output << COLOR_RGB("[", COLOR_GREY)
output << COLOR_RGB("-[", COLOR_GREY)
<< COLOR_RGB(this->details_->get_path(), COLOR_WHITE)
<< COLOR_RGB("]", COLOR_GREY);
output << "\n";

output << spaces_prefix << COLOR_RGB("·", COLOR_GREY);
output << spaces_prefix << COLOR_RGB("o", COLOR_GREY);
output << "\n";

for (auto index = 0U; index < this->label_groups_.size(); index++)
Expand Down Expand Up @@ -734,35 +734,35 @@ namespace swallow::compiler::diagnostics
auto biggest_number_width = std::to_string(biggest_number).length();
auto spaces_prefix = std::string(biggest_number_width + 3, ' ');

output << spaces_prefix << COLOR_RGB("", COLOR_GREY);
output << spaces_prefix << COLOR_RGB("/", COLOR_GREY);
for (auto index = 0U; index < file_groups.size(); index++)
{
const auto &file_group = file_groups.at(index);
file_group.print(output, spaces_prefix);

if (index != file_groups.size() - 1)
{
output << spaces_prefix << COLOR_RGB("·", COLOR_GREY);
output << spaces_prefix << COLOR_RGB("o", COLOR_GREY);
output << "\n";

output << spaces_prefix << COLOR_RGB("", COLOR_GREY);
output << spaces_prefix << COLOR_RGB("^", COLOR_GREY);
}
}

output << spaces_prefix << COLOR_RGB("·", COLOR_GREY);
output << spaces_prefix << COLOR_RGB("o", COLOR_GREY);
output << "\n";

if (this->note_)
{
output << spaces_prefix << COLOR_RGB("", COLOR_GREY)
<< COLOR_RGB(" Note: ", COLOR_BEACH);
output << spaces_prefix << COLOR_RGB("o-- ", COLOR_GREY)
<< COLOR_RGB("Note: ", COLOR_BEACH);
print_formatted_text(output, this->note_.value());
output << "\n";
}

auto dashes_prefix = biggest_number_width + 3;
output << COLOR_RGB(repeat_string("", dashes_prefix), COLOR_GREY)
<< COLOR_RGB("", COLOR_GREY);
output << COLOR_RGB(repeat_string("-", dashes_prefix), COLOR_GREY)
<< COLOR_RGB("/", COLOR_GREY);
output << "\n";
}

Expand Down
30 changes: 29 additions & 1 deletion compiler/diagnostics/reporter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Copyright (c) 2023 Muqiu Han
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice,
// this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Terifo nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "reporter.h"
#include "panic/panic.hpp"
#include <cstdlib>

using namespace swallow::utils;
Expand Down
Loading

0 comments on commit 81be3d3

Please sign in to comment.