Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler]: compile match expr #20

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyBreakComment: 80
PenaltyBreakFirstLessLess: 80
PenaltyBreakString: 80
PenaltyExcessCharacter: 80
PenaltyReturnTypeOnItsOwnLine: 80
PointerAlignment: Middle
ReflowComments: true
SortIncludes: true
SortIncludes: "Never"
SpaceAfterCStyleCast: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
Expand Down
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ ImplementationFileExtensions:
- cc
- cpp
- cxx
AnalyzeTemporaryDtors: false
FormatStyle: none
User: muqiuhan
CheckOptions:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ compiler/parser/bison_parser.hpp
compiler/parser/location.hh

.ninja_deps
.ninja_log
.ninja_log

cmake-build-debug/
81 changes: 43 additions & 38 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,42 @@ cmake_policy(SET CMP0091 NEW)
project(swallow VERSION 0.0.1 LANGUAGES CXX)

# target
add_library(stdlib SHARED "")
set_target_properties(stdlib PROPERTIES OUTPUT_NAME "stdlib")
set_target_properties(stdlib PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/linux/x86_64/debug")
target_compile_options(stdlib PRIVATE
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
add_library(base SHARED "")
set_target_properties(base PROPERTIES OUTPUT_NAME "base")
set_target_properties(base PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/linux/x86_64/release")
target_compile_options(base PRIVATE
$<$<COMPILE_LANGUAGE:C>:-m64>
$<$<COMPILE_LANGUAGE:CXX>:-m64>
$<$<COMPILE_LANGUAGE:C>:-DNDEBUG>
$<$<COMPILE_LANGUAGE:CXX>:-DNDEBUG>
)
set_target_properties(stdlib PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(stdlib PRIVATE cxx_std_20)
set_target_properties(base PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(base PRIVATE cxx_std_20)
if(MSVC)
target_compile_options(stdlib PRIVATE $<$<CONFIG:Debug>:-Od>)
target_compile_options(base PRIVATE $<$<CONFIG:Release>:-Ox -fp:fast>)
else()
target_compile_options(stdlib PRIVATE -O0)
target_compile_options(base PRIVATE -O3)
endif()
if(MSVC)
target_compile_options(stdlib PRIVATE -Zi)
else()
target_compile_options(stdlib PRIVATE -g)
endif()
if(MSVC)
set_property(TARGET stdlib PROPERTY
set_property(TARGET base PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
target_link_options(stdlib PRIVATE
target_link_options(base PRIVATE
-m64
)
target_sources(stdlib PRIVATE
stdlib/stdlib.cpp
target_sources(base PRIVATE
base/base.cpp
)

# target
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
add_executable(swc "")
set_target_properties(swc PROPERTIES OUTPUT_NAME "swc")
set_target_properties(swc PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/linux/x86_64/debug")
add_dependencies(swc stdlib)
set_target_properties(swc PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/linux/x86_64/release")
add_dependencies(swc base)
target_include_directories(swc PRIVATE
compiler
compiler/ast
Expand All @@ -55,84 +56,88 @@ target_include_directories(swc PRIVATE
target_compile_options(swc PRIVATE
$<$<COMPILE_LANGUAGE:C>:-m64>
$<$<COMPILE_LANGUAGE:CXX>:-m64>
$<$<COMPILE_LANGUAGE:C>:-DNDEBUG>
$<$<COMPILE_LANGUAGE:CXX>:-DNDEBUG>
$<$<COMPILE_LANGUAGE:CXX>:-stdlib=libc++>
)
set_target_properties(swc PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(swc PRIVATE cxx_std_20)
if(MSVC)
target_compile_options(swc PRIVATE $<$<CONFIG:Debug>:-Od>)
target_compile_options(swc PRIVATE $<$<CONFIG:Release>:-Ox -fp:fast>)
else()
target_compile_options(swc PRIVATE -O0)
target_compile_options(swc PRIVATE -O3)
endif()
if(MSVC)
target_compile_options(swc PRIVATE -Zi)
else()
target_compile_options(swc PRIVATE -g)
target_compile_options(swc PRIVATE -fvisibility=hidden)
endif()
if(MSVC)
set_property(TARGET swc PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
target_link_libraries(swc PRIVATE
c++
stdlib
base
)
target_link_directories(swc PRIVATE
build/linux/x86_64/debug
build/linux/x86_64/release
)
target_link_options(swc PRIVATE
-m64
)
target_sources(swc PRIVATE
compiler/compiler.cpp
compiler/ast/ast.cpp
compiler/ast/dump.cpp
compiler/ast/g-machine.cpp
compiler/ast/dump.cpp
compiler/ast/ast.cpp
compiler/ast/type.cpp
compiler/diagnostics/reporter.cpp
compiler/diagnostics/utils.cpp
compiler/diagnostics/reporter.cpp
compiler/diagnostics/diagnostics.cpp
compiler/g-machine/instruction.cpp
compiler/g-machine/binop.cpp
compiler/g-machine/environment.cpp
compiler/g-machine/instruction.cpp
compiler/lexer/lexer.cpp
compiler/lexer/flex_lexer.cpp
compiler/parser/parser.cpp
compiler/parser/bison_parser.cpp
compiler/type/type.cpp
compiler/type/dump.cpp
compiler/type/environment.cpp
compiler/type/type.cpp
)

# target
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
add_executable(swi "")
set_target_properties(swi PROPERTIES OUTPUT_NAME "swi")
set_target_properties(swi PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/linux/x86_64/debug")
set_target_properties(swi PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/linux/x86_64/release")
add_dependencies(swi swc)
target_compile_options(swi PRIVATE
$<$<COMPILE_LANGUAGE:C>:-m64>
$<$<COMPILE_LANGUAGE:CXX>:-m64>
$<$<COMPILE_LANGUAGE:C>:-DNDEBUG>
$<$<COMPILE_LANGUAGE:CXX>:-DNDEBUG>
)
set_target_properties(swi PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(swi PRIVATE cxx_std_20)
if(MSVC)
target_compile_options(swi PRIVATE $<$<CONFIG:Debug>:-Od>)
target_compile_options(swi PRIVATE $<$<CONFIG:Release>:-Ox -fp:fast>)
else()
target_compile_options(swi PRIVATE -O0)
target_compile_options(swi PRIVATE -O3)
endif()
if(MSVC)
target_compile_options(swi PRIVATE -Zi)
else()
target_compile_options(swi PRIVATE -g)
target_compile_options(swi PRIVATE -fvisibility=hidden)
endif()
if(MSVC)
set_property(TARGET swi PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
target_link_libraries(swi PRIVATE
stdlib
base
)
target_link_directories(swi PRIVATE
build/linux/x86_64/debug
build/linux/x86_64/release
)
target_link_options(swi PRIVATE
-m64
Expand Down
2 changes: 1 addition & 1 deletion FAQ
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
libparser.so: undefined reference to `yy::parser::parse()'
---------------
The error message like this:
*/ld: build/linux/x86_64/release/libparser.so: undefined reference to `yy::parser::parse()'
*/ld: build/linux/x86_64/release/libparser.so: undefined reference to `yy::parser::Parse()'
*/ld: build/linux/x86_64/release/libparser.so: undefined reference to `yy::parser::~parser()'
*/ld: build/linux/x86_64/release/libparser.so: undefined reference to `Program'
*/ld: build/linux/x86_64/release/libparser.so: undefined reference to `yy::parser::parser()'
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To build and run with xmake:
Features
----------
o- Typechecking
o- Pattern match
o- Pattern Match
o- Garbage Collection
o- Polymorphism

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data Bool = [ True, False ]
data List = [ Nil, Cons Int List ]

let main(argv) = {
match argv with {
Match argv with {
| True => { 0 }
| Nil => { 1 }
}
Expand All @@ -62,7 +62,7 @@ let main(argv) = {
data List = [ Nil, Cons Int List ]

let main(argv) = {
match argv with {
Match argv with {
| Nil => { 0 }
| Cons x xs => { 0 }
}
Expand Down Expand Up @@ -93,7 +93,7 @@ let main(argv) = {
- [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)
- [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/)
Expand Down
1 change: 1 addition & 0 deletions base/base.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void a() {}
Empty file removed base/stdlib.cpp
Empty file.
3 changes: 1 addition & 2 deletions base/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
target("base")
set_kind("shared")
set_languages("c++20")
add_files("*.cpp")
add_cxxflags("clang::-stdlib=libc++")
add_files("*.cpp")
6 changes: 3 additions & 3 deletions build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ build build/.objs/swi/linux/x86_64/debug/repl/main.cpp.o: cxx repl/main.cpp

# build target: swc
build swc: phony build/linux/x86_64/debug/swc
build build/linux/x86_64/debug/swc: ld build/.objs/swc/linux/x86_64/debug/compiler/compiler.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/ast.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/dump.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/g-machine.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/type.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/diagnostics/reporter.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/diagnostics/utils.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/diagnostics/diagnostics.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/g-machine/binop.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/g-machine/environment.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/g-machine/instruction.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/lexer/lexer.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/lexer/flex_lexer.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/parser/parser.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/parser/bison_parser.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/type/dump.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/type/environment.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/type/type.cpp.o || $
build build/linux/x86_64/debug/swc: ld build/.objs/swc/linux/x86_64/debug/compiler/compiler.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/ast.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/Dump.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/g-machine.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/ast/type.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/diagnostics/reporter.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/diagnostics/utils.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/diagnostics/diagnostics.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/g-machine/binop.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/g-machine/environment.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/g-machine/instruction.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/lexer/lexer.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/lexer/flex_lexer.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/parser/parser.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/parser/bison_parser.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/type/Dump.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/type/environment.cpp.o build/.objs/swc/linux/x86_64/debug/compiler/type/type.cpp.o || $
build/linux/x86_64/debug/libstdlib.so
ARGS = -m64 -Lbuild/linux/x86_64/debug -Wl,-rpath=$ORIGIN -lc++ -lstdlib

Expand All @@ -101,7 +101,7 @@ build build/.objs/swc/linux/x86_64/debug/compiler/compiler.cpp.o: cxx compiler/c
build build/.objs/swc/linux/x86_64/debug/compiler/ast/ast.cpp.o: cxx compiler/ast/ast.cpp
ARGS = -m64 -g -O0 -std=c++20 -Icompiler -Icompiler/ast -Icompiler/type -Icompiler/lexer -Icompiler/parser -Icompiler/diagnostics -Icompiler/utils

build build/.objs/swc/linux/x86_64/debug/compiler/ast/dump.cpp.o: cxx compiler/ast/dump.cpp
build build/.objs/swc/linux/x86_64/debug/compiler/ast/Dump.cpp.o: cxx compiler/ast/Dump.cpp
ARGS = -m64 -g -O0 -std=c++20 -Icompiler -Icompiler/ast -Icompiler/type -Icompiler/lexer -Icompiler/parser -Icompiler/diagnostics -Icompiler/utils

build build/.objs/swc/linux/x86_64/debug/compiler/ast/g-machine.cpp.o: cxx compiler/ast/g-machine.cpp
Expand Down Expand Up @@ -140,7 +140,7 @@ build build/.objs/swc/linux/x86_64/debug/compiler/parser/parser.cpp.o: cxx compi
build build/.objs/swc/linux/x86_64/debug/compiler/parser/bison_parser.cpp.o: cxx compiler/parser/bison_parser.cpp
ARGS = -m64 -g -O0 -std=c++20 -Icompiler -Icompiler/ast -Icompiler/type -Icompiler/lexer -Icompiler/parser -Icompiler/diagnostics -Icompiler/utils

build build/.objs/swc/linux/x86_64/debug/compiler/type/dump.cpp.o: cxx compiler/type/dump.cpp
build build/.objs/swc/linux/x86_64/debug/compiler/type/Dump.cpp.o: cxx compiler/type/Dump.cpp
ARGS = -m64 -g -O0 -std=c++20 -Icompiler -Icompiler/ast -Icompiler/type -Icompiler/lexer -Icompiler/parser -Icompiler/diagnostics -Icompiler/utils

build build/.objs/swc/linux/x86_64/debug/compiler/type/environment.cpp.o: cxx compiler/type/environment.cpp
Expand Down
26 changes: 15 additions & 11 deletions compiler/ast/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ using namespace swallow::compiler::utils;

namespace swallow::compiler::ast
{
auto Binop::operatorsToString(const utils::Binop op) noexcept -> std::string
auto Binop::OperatorToString(utils::Binop op) noexcept -> std::string
{
switch (op)
{
Expand All @@ -52,10 +52,10 @@ namespace swallow::compiler::ast
return {"/"};
}

utils::panic("operatorsToString failed!!!");
utils::Panic("OperatorToString failed!!!");
}

void dump(const std::vector<Definition::Ptr> &Program) noexcept
void Dump(const std::vector<Definition::Ptr> &Program) noexcept
{
for (const auto &definition : Program)
{
Expand All @@ -67,14 +67,15 @@ namespace swallow::compiler::ast
std::cout << " " << param;

std::cout << ":" << '\n';
fn->Body->dump(1, std::cout);
fn->Body->Dump(1, std::cout);
}
}

} // namespace swallow::compiler::ast

namespace swallow::compiler::type
{
void typecheck(const std::vector<ast::Definition::Ptr> &program) noexcept
void TypeCheck(const std::vector<ast::Definition::Ptr> &program) noexcept
{
Manager typeManager;
Environment typeEnvironment;
Expand All @@ -83,15 +84,18 @@ namespace swallow::compiler::type
auto binopType = Type::Ptr(
new Arrow(intType, Type::Ptr(new type::Arrow(intType, intType))));

typeEnvironment.bind("+", binopType);
typeEnvironment.bind("-", binopType);
typeEnvironment.bind("*", binopType);
typeEnvironment.bind("/", binopType);
typeEnvironment.Bind("+", binopType);
typeEnvironment.Bind("-", binopType);
typeEnvironment.Bind("*", binopType);
typeEnvironment.Bind("/", binopType);

for (const auto &definition : program)
definition->PreScanTypes(typeManager, typeEnvironment);

for (const auto &definition : program)
definition->scanDefinitionType(typeManager, typeEnvironment);
definition->TypeCheck(typeManager, typeEnvironment);

for (const auto &definition : program)
definition->typecheck(typeManager, typeEnvironment);
definition->Resolve(typeManager);
}
} // namespace swallow::compiler::type
Loading
Loading