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

"module style" chialisp. moves toplevel forms to the top level (instead of enclosed) and allows import to be more intelligent ala es2015. Introduces namespaces. #62

Draft
wants to merge 79 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
9945a64
Staging + import
prozacchiwawa Nov 8, 2023
1c656a5
Some fixes, WIP
prozacchiwawa Nov 8, 2023
e657c2d
more tests passing
prozacchiwawa Nov 8, 2023
2c1e8c3
tests passing
prozacchiwawa Nov 8, 2023
0d06ae3
individual module outputs are written
prozacchiwawa Nov 8, 2023
131aa04
despam
prozacchiwawa Nov 8, 2023
5f8098f
Output now written to fs for module mains
prozacchiwawa Nov 8, 2023
70f53ec
Add some state that keeps track of where helpers were defined when we…
prozacchiwawa Nov 30, 2023
d44d1e6
Very rough prototype of full namespacing
prozacchiwawa Dec 9, 2023
93f260d
Add prelude
prozacchiwawa Dec 9, 2023
7ec229f
First full test set up for the module system
prozacchiwawa Dec 10, 2023
b9e5ee8
checkpoint: wip working on pulling in lambdas. there is a bug in nam…
prozacchiwawa Dec 12, 2023
544b950
Add more tests, elaborate lambdas in the context of rewriting.
prozacchiwawa Dec 12, 2023
9340b42
Add another lambda test
prozacchiwawa Dec 12, 2023
189b787
Fix
prozacchiwawa Dec 12, 2023
730374a
Fix
prozacchiwawa Dec 12, 2023
26320b4
more fixing
prozacchiwawa Dec 12, 2023
6237cde
Introduce LongNameTranslation, which keeps track of how we want the p…
prozacchiwawa Dec 14, 2023
8d534d4
Add imports from compiled programs
prozacchiwawa Dec 14, 2023
559f9a3
Ensure that we pass on a clean opts to subcompiles via modules, ensur…
prozacchiwawa Dec 14, 2023
7d814d9
Clean some clippy
prozacchiwawa Dec 14, 2023
5b2e02f
Classic compile via import
prozacchiwawa Dec 14, 2023
58f2785
test files
prozacchiwawa Dec 14, 2023
2563796
Add new tests
prozacchiwawa Dec 14, 2023
cc18e37
Support namespaced types fully
prozacchiwawa Dec 18, 2023
d4162be
Type tests
prozacchiwawa Dec 18, 2023
cb39773
Types are now properly resolved
prozacchiwawa Dec 19, 2023
6d2cc0e
first part of modules-as-preprocessing refactor
prozacchiwawa Dec 22, 2023
236183f
Move to preprocessor
prozacchiwawa Dec 22, 2023
b3ec175
Working tests again
prozacchiwawa Dec 22, 2023
3e729a8
Compiler output that differentiates between module and simple output
prozacchiwawa Dec 22, 2023
d7217b4
advance
prozacchiwawa Dec 23, 2023
d110170
More transformation. everything in compilation is properly routed by…
prozacchiwawa Dec 23, 2023
1d07990
revert
prozacchiwawa Dec 23, 2023
43fc160
revert
prozacchiwawa Dec 23, 2023
6af9ead
Keep moving forward
prozacchiwawa Jan 2, 2024
783a9ab
More moves
prozacchiwawa Jan 2, 2024
2b61a50
Moving
prozacchiwawa Jan 2, 2024
ab3e009
Move context into more places and add includes to it
prozacchiwawa Jan 3, 2024
1cd2051
Introduce FrontendOutput
prozacchiwawa Jan 3, 2024
7441734
move match_export_form
prozacchiwawa Jan 3, 2024
f987d2f
silence
prozacchiwawa Jan 4, 2024
2b4764a
Ok everything passes with properly layered integration of frontend an…
prozacchiwawa Jan 4, 2024
0ba4c0e
fmt
prozacchiwawa Jan 4, 2024
859b82d
fmt + clippy
prozacchiwawa Jan 4, 2024
2c83b62
Additional
prozacchiwawa Jan 4, 2024
2d176e8
test updates
prozacchiwawa Jan 4, 2024
289d522
Add new arg
prozacchiwawa Jan 4, 2024
7639c47
fmt
prozacchiwawa Jan 4, 2024
f3d1165
API movement
prozacchiwawa Jan 4, 2024
c19b621
Fix cldb print only
prozacchiwawa Jan 4, 2024
289ce76
fmt
prozacchiwawa Jan 4, 2024
e147e85
Add a test exercising the profiler wrapper
prozacchiwawa Jan 4, 2024
c3b73a9
opps
prozacchiwawa Jan 4, 2024
0d24c89
reverse unintended change
prozacchiwawa Jan 4, 2024
0dc0473
Fix allowing use of bare 'a' as a variable name to be interpreted as …
prozacchiwawa Jan 13, 2024
0a3a51a
fmt + clippy
prozacchiwawa Jan 13, 2024
fdda6aa
Add test for using prim names as variable names not getting broken du…
prozacchiwawa Jan 14, 2024
0eb6176
Add test_handcalc with cards. namespace. Fixes resolution of macros …
prozacchiwawa Jan 17, 2024
91f754f
More cards
prozacchiwawa Jan 17, 2024
bfc8238
fmt
prozacchiwawa Jan 17, 2024
1a77cd7
Move handcalc, properly support imports against the root namespace
prozacchiwawa Jan 17, 2024
b116bec
Add factorial test
prozacchiwawa Jan 17, 2024
f14e39f
Fix argument naming clashing with prims
prozacchiwawa Jan 17, 2024
105eb6f
Quiet some of this diagnostic to make deinlining a bit faster when an…
prozacchiwawa Jan 17, 2024
b1028e5
Add a test for importing a constant
prozacchiwawa Jan 18, 2024
2682351
opps
prozacchiwawa Jan 18, 2024
5b3b6a9
Add constant export
prozacchiwawa Jan 18, 2024
adae72e
Add different constant tests, resolve export helpers using find_helpe…
prozacchiwawa Jan 19, 2024
e047b38
Ensure sigils
prozacchiwawa Jan 19, 2024
ef0c94f
more sigil
prozacchiwawa Jan 19, 2024
091267c
fmt + clippy
prozacchiwawa Jan 19, 2024
74c770e
Add exposing renaming
prozacchiwawa Jan 19, 2024
f8470e1
clippy
prozacchiwawa Jan 19, 2024
13940ee
Fix a bug affecting macro name resolution
prozacchiwawa Jan 21, 2024
9fe6d8d
clippy
prozacchiwawa Jan 21, 2024
3b7f851
More base merge
prozacchiwawa May 8, 2024
b413e5c
More merges
prozacchiwawa May 8, 2024
c7b95ec
fmt + clippy
prozacchiwawa May 8, 2024
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
4 changes: 4 additions & 0 deletions resources/tests/module/basic_program_import.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(include *standard-cl-23*)
(import qualified programs.p1 as P1)

(export () (list P1.program_hash P1.program))
4 changes: 4 additions & 0 deletions resources/tests/module/classic_program_import.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(include *standard-cl-23*)
(import qualified programs.classic as P1)

(export () (list P1.program_hash P1.program))
238 changes: 238 additions & 0 deletions resources/tests/module/handcalc.clinc
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
(import std.map)
(import std.filtermap)
(import std.relops)
(import std.truncate)
(import std.partition)
(import std.append)
(import std.reverse)
(import onehandcalc)

; ranks are 2-14 with 2 being two, 13 being king, and 14 being ace
; suits are 1-4 with no particular labelling
; takes a list of cards (rank . suit) and returns the value of the best poker
; hand which can be made with them
; doesn't work for ten or more cards if there are multiple flushes
; all sorting is done highest to lowest
(defun find_flush (suits)
(assign
((count1 . suit1)) (group_by_count_clean (atomsort suits))
(* suit1 (>= count1 5))
)
)

(defun straight_high_inner (ranks last count)
(if (not ranks)
(if (logand (= last 2) (= count 4))
; maybe ace to five
5
0
)
(if (= last (f ranks))
; skip identical cards
(straight_high_inner (r ranks) last count)
; if the partial straight continues
(if (= (f ranks) (- last 1))
(if (= count 4)
; found a straight, add 3 to last because next and last are included
(+ last 3)
; keep looking for a straight with the count going up by one
(straight_high_inner (r ranks) (f ranks) (+ count 1))
)
; reset the count
(straight_high_inner (r ranks) (f ranks) 1)
)
)
)
)

; returns the high card of a straight or 0 if there isn't any
; ranks must be sorted in descending order
(defun straight_high_extended (ranks)
(assign
high (straight_high_inner ranks 0 0)
(if (= high 5)
(* (= (f ranks) 14) 5)
high
)
)
)

(defun group_by_count_clean (items)
(map
unflatten_card
(atomsort (group_by_count_inner items (f items) 0))
)
)

(defun find_straight_flush_indices (flush_suit straight_flush_high (@ cards ((first_rank . first_suit) . remaining)))
(if (not cards)
0
(assign
straight_to_ace_match
(logand
(= straight_flush_high 5)
(= first_rank 14)
)

rank_in_range
(logand
(<= first_rank straight_flush_high)
(> first_rank (- straight_flush_high 5))
)

hit
(logand
(= first_suit flush_suit)
(logior straight_to_ace_match rank_in_range)
)

(logior (lsh (find_straight_flush_indices flush_suit straight_flush_high remaining) 1) hit)
)
)
)

(defun flush_cards_with_index (flush_suit index (@ cards ((first_rank . first_suit))))
(if (not cards)
0
(if (= flush_suit first_suit)
(c (flatten_card (c first_rank index)) (flush_cards_with_index flush_suit (+ index 1) (r cards)))
(flush_cards_with_index flush_suit (+ index 1) (r cards))
)
)
)

(defun find_flush_indices (flush_suit cards)
(assign
myfiltered (truncate 5 (atomsort (flush_cards_with_index flush_suit 0 cards)))
(to_bitfield 0 (reverse (atomsort (map (lambda (x) (logand x 15)) myfiltered))))
)
)

; includes should be in ascending order
(defun to_bitfield (index includes)
(if (not includes)
0
(if (= index (f includes))
(logior 1 (lsh (to_bitfield (+ index 1) (r includes)) 1))
(lsh (to_bitfield (+ index 1) includes) 1)
)
)
)

(defun find_straight_includes (ranks with_index)
(if (all ranks with_index)
(if (= (f ranks) (lsh (f with_index) -4))
(c (logand 15 (f with_index)) (find_straight_includes (r ranks) (r with_index)))
(find_straight_includes ranks (r with_index))
)
0
)
)

(defun find_straight_indices (my_straight_high cards)
(assign
with_index (atomsort (ranks_with_indices 0 cards))
my_ranks
(if (= my_straight_high 5)
(list 14 5 4 3 2)
(list
my_straight_high
(- my_straight_high 1)
(- my_straight_high 2)
(- my_straight_high 3)
(- my_straight_high 4)
)
)

includes (reverse (atomsort (find_straight_includes my_ranks with_index)))
(to_bitfield 0 includes)
)
)

(defun ranks_with_indices (index cards)
(if (not cards)
0
(c (flatten_card (c (f (f cards)) index)) (ranks_with_indices (+ index 1) (r cards)))
)
)

(defun find_hand_indices ((@ numbered-cards ((number . card) . rest)))
(if numbered-cards
(logior (lsh 1 number) (find_hand_indices rest))
0
)
)

(defun number_cards (num cards)
(if cards
(c (c num (f cards)) (number_cards (+ num 1) (r cards)))
()
)
)

;; Sorts the cards by group size according to hand.
;; Hand has pairs of count and rank. We pull out cards based on their rank
;; until each bucket is empty in hand and then give the remaining cards.
(defun bucket_cards_by_frequency_groups (hand cards)
(if hand
(assign
(hand_freq . want_rank) (f hand)

(cards_with_rank . cards_without_rank) (partition (lambda ((& want_rank) (num . (rank . suit))) (= rank want_rank)) cards)

;; We have the cards with this rank... go to the next wanted rank.
(append cards_with_rank (bucket_cards_by_frequency_groups (r hand) cards_without_rank))
)
cards
)
)

(defun normal_full_house (firstcount secondcount hand cards)
(find_hand_indices (truncate 5 (bucket_cards_by_frequency_groups hand (number_cards 0 cards))))
)

(defun handcalc (cards)
(assign-lambda
first (lambda (x) (f x))
rest (lambda (x) (r x))
sorted_ranks (atomsort (map first cards))
hand (group_by_count_clean sorted_ranks)
((firstcount . firstrank) (secondcount . secondrank)) hand
flush_suit (find_flush (map rest cards))
(if ;; Full house
(logand (= firstcount 3) (= secondcount 2))
(normal_full_house firstcount secondcount hand cards)

;; Flush
flush_suit
(assign-lambda
flush_cards (filtermap (lambda ((& flush_suit) (@ thecard (rank . suit))) (if (= suit flush_suit) rank 0)) cards)
straight_flush_high (straight_high_extended (atomsort flush_cards))
(if straight_flush_high
(find_straight_flush_indices flush_suit straight_flush_high cards)
(if (logior (< firstcount 3) (logand (= firstcount 3) (= secondcount 1)))
(find_flush_indices flush_suit cards)
(find_hand_indices (truncate 5 (bucket_cards_by_frequency_groups hand (number_cards 0 cards))))
)
)
)

;; Else
(assign
my_straight_high (straight_high_extended sorted_ranks)
(if (all
my_straight_high
(logior
(< firstcount 3)
(logand (= firstcount 3) (= secondcount 1))
)
)

(find_straight_indices my_straight_high cards)

(find_hand_indices (truncate 5 (bucket_cards_by_frequency_groups hand (number_cards 0 cards))))
)
)
)
)
)
6 changes: 6 additions & 0 deletions resources/tests/module/modtest1.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(include *standard-cl-23*)
(import std.assert exposing assert)

(export (X)
(assert (not (= X 13)) X)
)
9 changes: 9 additions & 0 deletions resources/tests/module/modtest1_assign.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(include *strict-cl-21*)
(import std.assert exposing assert)

(export (X)
(assign
less-than-x (- X 13)
(assert less-than-x X)
)
)
6 changes: 6 additions & 0 deletions resources/tests/module/modtest1_current_module_type.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(include *strict-cl-21*)
(import std.assert exposing assert)

(deftype T (A B))

(export (X Y) (get_T_A (new_T X Y)))
6 changes: 6 additions & 0 deletions resources/tests/module/modtest1_lambda.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(include *strict-cl-21*)
(import std.assert exposing assert)

(defun F (X) (lambda ((& X) Y) (assert (not (= X 13)) (+ X Y))))

(export (X Y) (a (F X) (list Y)))
6 changes: 6 additions & 0 deletions resources/tests/module/modtest1_macro.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(include *strict-cl-21*)
(import std.assert exposing assert)

(defmacro M (X) (c 1 X))

(export (Z) (M Z))
4 changes: 4 additions & 0 deletions resources/tests/module/modtest1_other_module_type.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(include *strict-cl-21*)
(import qualified test-data.test-type as TD)

(export (X Y) (TD.get_TestType_A (TD.new_TestType X Y 1337)))
8 changes: 8 additions & 0 deletions resources/tests/module/modtest2.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(include *strict-cl-21*)
(import std.assert exposing assert)

(defun F (X) (assert (- X 1) ()))
(defun G (X) (assert (= 6 (* X 2)) ()))

(export G)
(export F)
6 changes: 6 additions & 0 deletions resources/tests/module/modtest2_lambda.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(include *strict-cl-21*)
(import std.assert exposing assert)

(defun F (X) (lambda ((& X) Y) (assert (not (= X 13)) (list (+ X Y) (* X Y)))))

(export (X Y) (a (F X) (list Y)))
5 changes: 5 additions & 0 deletions resources/tests/module/modtest_internal.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(include *strict-cl-21*)

(import test-data.use_internal exposing use-internal-constant)

(export (X) (use-internal-constant X))
Loading
Loading