Skip to content

Commit

Permalink
Dynamic Polymorphism (#58)
Browse files Browse the repository at this point in the history
* Use Static[] for static inheritance

* Support .seq extension

* Fix #36

* Polymorphic typechecking; vtables [wip]

* v-table dispatch [wip]

* vtable routing [wip; bug]

* vtable routing [MVP]

* Fix texts

* Add union type support

* Update FAQs

* Clarify

* Add BSL license

* Add makeUnion

* Add IR UnionType

* Update union representation in LLVM

* Update README

* Update README.md

* Update README

* Update README.md

* Add benchmarks

* Add more benchmarks and README

* Add primes benchmark

* Update benchmarks

* Fix cpp

* Clean up list

* Update faq.md

* Add binary trees benchmark

* Add fannkuch benchmark

* Fix paths

* Add PyPy

* Abort on fail

* More benchmarks

* Add cpp word_count

* Update set_partition cpp

* Add nbody cpp

* Add TAQ cpp; fix word_count timing

* Update CODEOWNERS

* Update README

* Update README.md

* Update CODEOWNERS

* Fix bench script

* Update binary_trees.cpp

* Update taq.cpp

* Fix primes benchmark

* Add mandelbrot benchmark

* Fix OpenMP init

* Add Module::unsafeGetUnionType

* UnionType [wip] [skip ci]

* Integrate IR unions and Union

* UnionType refactor [skip ci]

* Update README.md

* Update docs

* UnionType [wip] [skip ci]

* UnionType and automatic unions

* Add Slack

* Update faq.md

* Refactor types

* New error reporting [wip]

* New error reporting [wip]

* peglib updates [wip] [skip_ci]

* Fix parsing issues

* Fix parsing issues

* Fix error reporting issues

* Make sure random module matches Python

* Update releases.md

* Fix tests

* Fix #59

* Fix #57

* Fix #50

* Fix #49

* Fix #26; Fix #51; Fix #47; Fix #49

* Fix collection extension methods

* Fix #62

* Handle *args/**kwargs with Callable[]; Fix #43

* Fix #43

* Fix Ptr.__sub__; Fix polymorphism issues

* Add typeinfo

* clang-format

* Upgrade fmtlib to v9; Use CPM for fmtlib; format spec support; __format__ support

* Use CPM for semver and toml++

* Remove extension check

* Revamp str methods

* Update str.zfill

* Fix thunk crashes [wip] [skip_ci]

* Fix str.__reversed__

* Fix count_with_max

* Fix vtable memory allocation issues

* Add poly AST tests

* Use PDQsort when stability does not matter

* Fix dotted imports; Fix  issues

* Fix kwargs passing to Python

* Fix #61

* Fix #37

* Add isinstance support for unions; Union methods return Union type if different

* clang-format

* Nicely format error tracebacks

* Fix build issues; clang-format

* Fix OpenMP init

* Fix OpenMP init

* Update README.md

* Fix tests

* Update license [skip ci]

* Update license [ci skip]

* Add copyright header to all source files

* Fix super(); Fix error recovery in ClassStmt

* Clean up whitespace [ci skip]

* Use Python 3.9 on CI

* Print info in random test

* Fix single unions

* Update random_test.codon

* Fix polymorhic thunk instantiation

* Fix random test

* Add operator.attrgetter and operator.methodcaller

* Add code documentation

* Update documentation

* Update README.md

* Fix tests

* Fix random init

Co-authored-by: A. R. Shajii <ars@ars.me>
  • Loading branch information
inumanag and arshajii committed Dec 5, 2022
1 parent 2f26884 commit 5de233a
Show file tree
Hide file tree
Showing 360 changed files with 9,303 additions and 35,115 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.8'
python-version: '3.9'

- name: Linux Setup
if: startsWith(matrix.os, 'ubuntu')
Expand Down
98 changes: 38 additions & 60 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(LLVM REQUIRED)

if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include(${CMAKE_SOURCE_DIR}/cmake/deps.cmake)
Expand All @@ -50,8 +54,9 @@ else()
set(STATIC_LIBCPP "-static-libstdc++")
endif()

add_executable(peg2cpp codon/util/peg2cpp.cpp codon/util/cpp-peglib/peglib.h)
target_link_libraries(peg2cpp Threads::Threads)
add_executable(peg2cpp codon/util/peg2cpp.cpp)
target_include_directories(peg2cpp PRIVATE ${peglib_SOURCE_DIR})
target_link_libraries(peg2cpp PRIVATE Threads::Threads fmt)
add_custom_command(
OUTPUT codon_rules.cpp
COMMAND peg2cpp ${CMAKE_SOURCE_DIR}/codon/parser/peg/grammar.peg
Expand All @@ -75,7 +80,7 @@ endif()
target_include_directories(codonrt PRIVATE ${backtrace_SOURCE_DIR}
${re2_SOURCE_DIR}
"${gc_SOURCE_DIR}/include" runtime)
target_link_libraries(codonrt PRIVATE omp backtrace ${STATIC_LIBCPP}
target_link_libraries(codonrt PRIVATE fmt omp backtrace ${STATIC_LIBCPP}
LLVMSupport)
if(APPLE)
target_link_libraries(
Expand Down Expand Up @@ -134,6 +139,13 @@ set(CODON_HPPFILES
codon/parser/ast/expr.h
codon/parser/ast/stmt.h
codon/parser/ast/types.h
codon/parser/ast/types/type.h
codon/parser/ast/types/link.h
codon/parser/ast/types/class.h
codon/parser/ast/types/function.h
codon/parser/ast/types/union.h
codon/parser/ast/types/static.h
codon/parser/ast/types/traits.h
codon/parser/cache.h
codon/parser/common.h
codon/parser/ctx.h
Expand Down Expand Up @@ -203,46 +215,6 @@ set(CODON_HPPFILES
codon/sir/value.h
codon/sir/var.h
codon/util/common.h
codon/util/cpp-peglib/peglib.h
codon/util/fmt/chrono.h
codon/util/fmt/color.h
codon/util/fmt/compile.h
codon/util/fmt/core.h
codon/util/fmt/format-inl.h
codon/util/fmt/format.h
codon/util/fmt/locale.h
codon/util/fmt/ostream.h
codon/util/fmt/posix.h
codon/util/fmt/printf.h
codon/util/fmt/ranges.h
codon/util/semver/semver.h
codon/util/toml++/toml.h
codon/util/toml++/toml_default_formatter.hpp
codon/util/toml++/toml_node.hpp
codon/util/toml++/toml_preprocessor.h
codon/util/toml++/toml_utf8_streams.hpp
codon/util/toml++/toml_array.h
codon/util/toml++/toml_formatter.h
codon/util/toml++/toml_node_view.h
codon/util/toml++/toml_print_to_stream.h
codon/util/toml++/toml_value.h
codon/util/toml++/toml_array.hpp
codon/util/toml++/toml_instantiations.hpp
codon/util/toml++/toml_parse_error.h
codon/util/toml++/toml_table.h
codon/util/toml++/toml_version.h
codon/util/toml++/toml_common.h
codon/util/toml++/toml_json_formatter.h
codon/util/toml++/toml_parse_result.h
codon/util/toml++/toml_table.hpp
codon/util/toml++/toml_date_time.h
codon/util/toml++/toml_json_formatter.hpp
codon/util/toml++/toml_parser.h
codon/util/toml++/toml_utf8.h
codon/util/toml++/toml_default_formatter.h
codon/util/toml++/toml_node.h
codon/util/toml++/toml_parser.hpp
codon/util/toml++/toml_utf8_streams.h
extra/jupyter/jupyter.h
extra/python/codon/jit.h)
set(CODON_CPPFILES
Expand All @@ -255,7 +227,13 @@ set(CODON_CPPFILES
codon/dsl/plugins.cpp
codon/parser/ast/expr.cpp
codon/parser/ast/stmt.cpp
codon/parser/ast/types.cpp
codon/parser/ast/types/type.cpp
codon/parser/ast/types/link.cpp
codon/parser/ast/types/class.cpp
codon/parser/ast/types/function.cpp
codon/parser/ast/types/union.cpp
codon/parser/ast/types/static.cpp
codon/parser/ast/types/traits.cpp
codon/parser/cache.cpp
codon/parser/common.cpp
codon/parser/peg/peg.cpp
Expand Down Expand Up @@ -338,9 +316,9 @@ set(CODON_CPPFILES
codon/sir/value.cpp
codon/sir/var.cpp
codon/util/common.cpp
codon/util/fmt/format.cpp
extra/jupyter/jupyter.cpp)
add_library(codonc SHARED ${CODON_HPPFILES})
target_include_directories(codonc PRIVATE ${peglib_SOURCE_DIR} ${toml_SOURCE_DIR}/include ${semver_SOURCE_DIR}/include)
target_sources(codonc PRIVATE ${CODON_CPPFILES} codon_rules.cpp omp_rules.cpp)
if(CODON_JUPYTER)
add_compile_definitions(CODON_JUPYTER)
Expand Down Expand Up @@ -389,9 +367,9 @@ llvm_map_components_to_libnames(
Vectorize
Passes)
if(APPLE)
target_link_libraries(codonc PRIVATE ${LLVM_LIBS} dl codonrt)
target_link_libraries(codonc PRIVATE ${LLVM_LIBS} fmt dl codonrt)
else()
target_link_libraries(codonc PRIVATE ${STATIC_LIBCPP} ${LLVM_LIBS} dl codonrt)
target_link_libraries(codonc PRIVATE ${STATIC_LIBCPP} ${LLVM_LIBS} fmt dl codonrt)
endif()

# Gather headers
Expand Down Expand Up @@ -429,22 +407,22 @@ add_custom_target(
add_dependencies(libs codonrt codonc)

if(APPLE AND APPLE_ARM)
add_custom_target(
libs_apple ALL
COMMENT "Collecting Apple-specific libraries"
BYPRODUCTS "${CMAKE_BINARY_DIR}/lib"
VERBATIM
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/lib/codon"
COMMAND
${CMAKE_COMMAND} -E copy
"${CMAKE_BINARY_DIR}/libunwind${CMAKE_SHARED_LIBRARY_SUFFIX}"
"${CMAKE_BINARY_DIR}/lib/codon")
add_dependencies(libs_apple codonrt)
# add_custom_target(
# libs_apple_arm ALL
# COMMENT "Collecting Apple-specific libraries"
# BYPRODUCTS "${CMAKE_BINARY_DIR}/lib"
# VERBATIM
# COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/lib/codon"
# COMMAND
# ${CMAKE_COMMAND} -E copy
# "${CMAKE_BINARY_DIR}/libunwind${CMAKE_SHARED_LIBRARY_SUFFIX}"
# "${CMAKE_BINARY_DIR}/lib/codon")
# add_dependencies(libs_apple_arm codonrt)
endif()

# Codon command-line tool
add_executable(codon codon/app/main.cpp)
target_link_libraries(codon PUBLIC ${STATIC_LIBCPP} codonc Threads::Threads)
target_link_libraries(codon PUBLIC ${STATIC_LIBCPP} fmt codonc Threads::Threads)

# Codon test Download and unpack googletest at configure time
include(FetchContent)
Expand Down Expand Up @@ -477,6 +455,6 @@ set(CODON_TEST_CPPFILES
add_executable(codon_test ${CODON_TEST_CPPFILES})
target_include_directories(codon_test PRIVATE test/sir
"${gc_SOURCE_DIR}/include")
target_link_libraries(codon_test codonc codonrt gtest_main)
target_link_libraries(codon_test fmt codonc codonrt gtest_main)
target_compile_definitions(codon_test
PRIVATE TEST_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test")
5 changes: 2 additions & 3 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
* @arshajii @inumanag
/compiler/ @arshajii
/compiler/parser/ @inumanag
/docs/ @markhend
/codon/ @arshajii
/codon/parser/ @inumanag
37 changes: 18 additions & 19 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
# Contributing to Codon

Thank you for considering contributing to Codon! This document contains some helpful information for getting started. The best place to ask questions or get feedback is [our Gitter chatroom](https://gitter.im/seq-lang/Seq). For a high-level outline of the features we aim to add in the future, check the [roadmap](https://github.com/seq-lang/seq/wiki/Roadmap).
Thank you for considering contributing to Codon! This document contains some helpful information for getting started.
The best place to ask questions or get feedback is [our Slack](https://join.slack.com/t/exaloop/shared_invite/zt-1jusa4kc0-T3rRWrrHDk_iZ1dMS8s0JQ).

## Development workflow

All development is done on the [`develop`](https://github.com/exaloop/codon/tree/develop) branch. Just before release, we bump the version number, merge into [`master`](https://github.com/exaloop/codon/tree/master) and tag the build with a tag of the form `vX.Y.Z` where `X`, `Y` and `Z` are the [SemVer](https://semver.org) major, minor and patch numbers, respectively. Our Travis CI build script automatically builds and deploys tagged commits as a new GitHub release via our trusty [@SeqBot](https://github.com/seqbot). It also builds and deploys the documentation to our website.
All development is done on the [`develop`](https://github.com/exaloop/codon/tree/develop) branch. Just before release,
we bump the version number, merge into [`master`](https://github.com/exaloop/codon/tree/master) and tag the build with
a tag of the form `vX.Y.Z` where `X`, `Y` and `Z` are the [SemVer](https://semver.org) major, minor and patch numbers,
respectively. Our CI build process automatically builds and deploys tagged commits as a new GitHub release.

## Coding standards

All C++ code should be formatted with [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) using the `.clang-format` file in the root of the repository.
All C++ code should be formatted with [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html) using the `.clang-format`
file in the root of the repository.

## Writing tests

Tests are written as Codon programs. The [`test/core/`](https://github.com/exaloop/codon/tree/master/test/core) directory contains some examples. If you add a new test file, be sure to add it to [`test/main.cpp`](https://github.com/exaloop/codon/blob/master/test/main.cpp) so that it will be executed as part of the test suite. There are two ways to write tests for Codon:
Tests are written as Codon programs. The [`test/core/`](https://github.com/exaloop/codon/tree/master/test/core) directory
contains some examples. If you add a new test file, be sure to add it to
[`test/main.cpp`](https://github.com/exaloop/codon/blob/master/test/main.cpp) so that it will be executed as part of the test
suite. There are two ways to write tests for Codon:

#### New style

Expand All @@ -25,7 +33,8 @@ def my_test():
my_test()
```

**Semantics:** `assert` statements in functions marked `@test` are not compiled to standard assertions: they don't terminate the program when the condition fails, but instead print source information, fail the test, and move on.
**Semantics:** `assert` statements in functions marked `@test` are not compiled to standard assertions: they don't terminate
the program when the condition fails, but instead print source information, fail the test, and move on.

#### Old style

Expand All @@ -35,7 +44,9 @@ Example:
print(2 + 2) # EXPECT: 4
```

**Semantics:** The source file is scanned for `EXPECT`s, executed, then the output is compared to the "expected" output. Note that if you have, for example, an `EXPECT` in a loop, you will need to duplicate it however many times the loop is executed. Using `EXPECT` is helpful mainly in cases where you need to test control flow, **otherwise prefer the new style**.
**Semantics:** The source file is scanned for `EXPECT`s, executed, then the output is compared to the "expected" output. Note
that if you have, for example, an `EXPECT` in a loop, you will need to duplicate it however many times the loop is executed.
Using `EXPECT` is helpful mainly in cases where you need to test control flow, **otherwise prefer the new style**.

## Pull requests

Expand All @@ -45,16 +56,4 @@ Pull requests should generally be based on the `develop` branch. Before submitti
- ... to include tests for any new or changed code.
- ... that all code is formatted as per the guidelines above.

Please be patient with pull request reviews, as our throughput is limited!

## Issues

We use [GitHub's issue tracker](https://github.com/exaloop/codon/issues), so that's where you'll find the most recent list of open bugs, feature requests, and general issues. If applicable, we try to tag each issue with at least one of the following tags:

- `Build`: Issues related to building Codon
- `Codegen`: Issues related to code generation (i.e. after parsing and type checking)
- `Parser`: Issues related to lexing/parsing
- `Library`: Issues related to the Codon standard library
- `Interop`: Issues related to interoperability with other languages or systems
- `Docs`: Issues related to documentation
- `Feature`: New language feature proposals
Please be patient with pull request reviews, as our throughput is limited.
91 changes: 91 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
Business Source License 1.1

License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
"Business Source License" is a trademark of MariaDB Corporation Ab.

-----------------------------------------------------------------------------

Parameters

Licensor: Exaloop, Inc.

Licensed Work: Codon compiler, runtime, and standard library
The Licensed Work is (c) 2022 Exaloop Inc.

Additional Use Grant: None

Change Date: 2025-11-01

Change License: Apache License, Version 2.0

For information about alternative licensing arrangements for the Software,
please visit: https://exaloop.io/

-----------------------------------------------------------------------------

Terms

The Licensor hereby grants you the right to copy, modify, create derivative
works, redistribute, and make non-production use of the Licensed Work. The
Licensor may make an Additional Use Grant, above, permitting limited
production use.

Effective on the Change Date, or the fourth anniversary of the first publicly
available distribution of a specific version of the Licensed Work under this
License, whichever comes first, the Licensor hereby grants you rights under
the terms of the Change License, and the rights granted in the paragraph
above terminate.

If your use of the Licensed Work does not comply with the requirements
currently in effect as described in this License, you must purchase a
commercial license from the Licensor, its affiliated entities, or authorized
resellers, or you must refrain from using the Licensed Work.

All copies of the original and modified Licensed Work, and derivative works
of the Licensed Work, are subject to this License. This License applies
separately for each version of the Licensed Work and the Change Date may vary
for each version of the Licensed Work released by Licensor.

You must conspicuously display this License on each original or modified copy
of the Licensed Work. If you receive the Licensed Work in original or
modified form from a third party, the terms and conditions set forth in this
License apply to your use of that work.

Any use of the Licensed Work in violation of this License will automatically
terminate your rights under this License for the current and all other
versions of the Licensed Work.

This License does not grant you any right in any trademark or logo of
Licensor or its affiliates (provided that you may use a trademark or logo of
Licensor as expressly required by this License).

TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
TITLE.

MariaDB hereby grants you permission to use this License's text to license
your works, and to refer to it using the trademark "Business Source License",
as long as you comply with the Covenants of Licensor below.

Covenants of Licensor

In consideration of the right to use this License's text and the "Business
Source License" name and trademark, Licensor covenants to MariaDB, and to all
other recipients of the licensed work to be provided by Licensor:

1. To specify as the Change License the GPL Version 2.0 or any later version,
or a license that is compatible with GPL Version 2.0 or a later version,
where "compatible" means that software provided under the Change License can
be included in a program with software provided under GPL Version 2.0 or a
later version. Licensor may specify additional Change Licenses without
limitation.

2. To either: (a) specify an additional grant of rights to use that does not
impose any additional restriction on the right granted in this License, as
the Additional Use Grant; or (b) insert the text "None".

3. To specify a Change Date.

4. Not to modify this License in any other way.

0 comments on commit 5de233a

Please sign in to comment.