Compare commits

...

723 Commits

Author SHA1 Message Date
Scott Richmond
df85be3c1e ref->box everywhere 2024-07-21 16:31:20 -04:00
Scott Richmond
60106d10f0 consider turtle-reported vs. expected-calculated state 2024-07-21 16:26:56 -04:00
Scott Richmond
e068059362 update turtle graphics protocol doc 2024-07-20 16:54:37 -04:00
Scott Richmond
dd3867968e add description of turtle graphics protocol 2024-07-20 16:34:12 -04:00
Scott Richmond
98421a9215 allow shadowing of prelude 2024-07-20 13:51:11 -04:00
Scott Richmond
7467bc8867 disallow shadowing, remove all shadowing from Prelude. 2024-07-19 16:48:11 -04:00
Scott Richmond
2ec95c8f33 add loop & recur back into the language: we do actually need it!--just not *pedagogically* 2024-07-19 16:25:18 -04:00
Scott Richmond
7afc32d9d1 remove loop & recur from the language 2024-07-19 16:11:30 -04:00
Scott Richmond
d4adc1d912 clean up 2024-07-19 16:09:31 -04:00
Scott Richmond
4a069278b8 finally fix script, block, tuple, list, dict, etc. off by one errors for last term 2024-07-19 16:00:17 -04:00
Scott Richmond
e9fee4c0e1 make some additional comments re: off-by-one error errors 2024-07-16 20:16:28 -04:00
Scott Richmond
2027490614 improve some things 2024-07-16 20:12:21 -04:00
Scott Richmond
cb7098ac4e start fixing off-by-one errors: script, block, tuple, args, tup-patt 2024-07-16 19:40:40 -04:00
Scott Richmond
d416511b48 remove repl cruft 2024-07-14 14:42:31 -04:00
Scott Richmond
a6c899a85f Pretty-patterns shows words at ends of splatterns 2024-07-14 14:41:53 -04:00
Scott Richmond
9ddb43a30f called keywords work in pipelines 2024-07-14 14:28:09 -04:00
Scott Richmond
9e50f0cbdf Fix typo 2024-07-14 14:18:10 -04:00
Scott Richmond
2f03bbb12f mismatched arity -> wrong number of arguments 2024-07-14 13:53:32 -04:00
Scott Richmond
8cf84e63d3 add char functions to doc 2024-07-14 13:49:49 -04:00
Scott Richmond
5c32d32f24 add chars to prelude 2024-07-14 13:48:47 -04:00
Scott Richmond
32b42e0242 ignore .zig-cache 2024-07-13 18:30:45 -04:00
Scott Richmond
4d1122fa58 repl comment 2024-06-24 12:57:09 -04:00
Scott Richmond
faf15bfd75 at works with strings 2024-06-24 12:56:43 -04:00
Scott Richmond
492c954655 0.1.26 2024-06-24 11:17:59 -04:00
Scott Richmond
0b67854494 build 2024-06-24 11:17:57 -04:00
Scott Richmond
af95c9800f json encode output 2024-06-24 11:17:47 -04:00
Scott Richmond
ed0a3311a4 0.1.25 2024-06-24 10:55:06 -04:00
Scott Richmond
55351da166 build 2024-06-24 10:55:02 -04:00
Scott Richmond
9752a87f27 move nil? to fix binding; use append! for turtle states 2024-06-24 10:54:54 -04:00
Scott Richmond
a30cbaebc6 at for strings, for real 2024-06-21 15:28:46 -04:00
Scott Richmond
7b56e90468 build 2024-06-21 11:37:31 -04:00
Scott Richmond
e05043e375 fix nil pattern matching 2024-06-21 11:37:23 -04:00
Scott Richmond
705be69661 validation error on empty blocks 2024-06-20 18:00:44 -04:00
Scott Richmond
3b65092267 add clause for join with empty list and separator 2024-06-20 17:50:01 -04:00
Scott Richmond
0363d5a091 calling something other than a function causes a panic 2024-06-20 17:49:27 -04:00
Scott Richmond
887d192c8a 0.1.24 2024-06-20 16:36:14 -04:00
Scott Richmond
f0176548de build 2024-06-20 16:36:11 -04:00
Scott Richmond
bf7dfe94bd fix fold/empty list bug 2024-06-20 16:36:01 -04:00
Scott Richmond
694b8adc60 0.1.23 2024-06-20 16:19:06 -04:00
Scott Richmond
f070076e2b build 2024-06-20 16:19:02 -04:00
Scott Richmond
42492c8a09 fix bug in rest with error on empty list 2024-06-20 16:18:54 -04:00
Scott Richmond
2c1de39161 0.1.22 2024-06-20 12:12:13 -04:00
Scott Richmond
58d70483cf build 2024-06-20 12:12:10 -04:00
Scott Richmond
70e8b1ede8 comment repl cruft 2024-06-20 12:12:02 -04:00
Scott Richmond
98180507e4 show and hide turtle 2024-06-20 12:10:07 -04:00
Scott Richmond
b9e721e4e2 don't draw line on turtle if pen is up 2024-06-20 12:04:45 -04:00
Scott Richmond
fe5b196404 0.1.21 2024-06-20 10:16:03 -04:00
Scott Richmond
ad0aff1cb0 build 2024-06-20 10:16:00 -04:00
Scott Richmond
7bdaf291a0 fix slice 2024-06-19 18:23:54 -04:00
Scott Richmond
27b1bb4e50 add setheading! and turtle pen tip draws in pencolor 2024-06-18 18:47:56 -04:00
Scott Richmond
b8af13106a 0.1.20 2024-06-18 12:45:29 -04:00
Scott Richmond
fc643cb561 build 2024-06-18 11:13:40 -04:00
Scott Richmond
43693e3c0e fix get, solving turtle movement hang 2024-06-18 11:13:33 -04:00
Scott Richmond
b31452db78 0.1.19 2024-06-17 13:32:01 -04:00
Scott Richmond
0a8dd3b7f7 build 2024-06-17 13:31:58 -04:00
Scott Richmond
ecc6438c3b build 2024-06-17 13:29:15 -04:00
Scott Richmond
8eed4145db fix words 2024-06-17 13:29:08 -04:00
Scott Richmond
848ba249ca add commit to build 2024-06-17 13:15:33 -04:00
Scott Richmond
d057678822 build 2024-06-17 13:12:37 -04:00
Scott Richmond
ef4a345da6 remove print!s from words 2024-06-17 13:12:26 -04:00
Scott Richmond
2203601770 0.1.18 2024-06-17 13:07:05 -04:00
Scott Richmond
2ae5f7929a build 2024-06-17 13:06:56 -04:00
Scott Richmond
3d0b58e6ed fix bug in at 2024-06-17 13:06:37 -04:00
Scott Richmond
716ecb01c5 0.1.17 2024-06-16 23:56:50 -04:00
Scott Richmond
a703a63502 build 2024-06-16 23:56:46 -04:00
Scott Richmond
bc7da38b3d final touches before computer class 2024-06-16 23:56:29 -04:00
Scott Richmond
bed1f7933e fix links 2024-06-15 23:51:09 -04:00
Scott Richmond
3bcfa520ac include link to prelude 2024-06-15 22:52:28 -04:00
Scott Richmond
600166884e include link to prelude 2024-06-15 22:51:19 -04:00
Scott Richmond
fcf3e4cdbe fix things, add things 2024-06-15 22:40:58 -04:00
Scott Richmond
84de1dae6c fix links 2024-06-15 22:30:38 -04:00
Scott Richmond
69cad142bc update 2024-06-15 22:29:06 -04:00
Scott Richmond
6d65c5abfd remove obsolete docs 2024-06-15 22:20:08 -04:00
Scott Richmond
03d5fb3450 update! 2024-06-15 22:19:50 -04:00
Scott Richmond
2bebd2261e add missing tuple fns 2024-06-15 22:19:32 -04:00
Scott Richmond
81915ed4ba cleanup 2024-06-15 22:18:58 -04:00
Scott Richmond
e3cbb4ad57 tweaks 2024-06-15 22:18:29 -04:00
Scott Richmond
2ad939b48b repl cruft 2024-06-15 21:53:40 -04:00
Scott Richmond
e899bc96f0 updated doc 2024-06-15 21:53:09 -04:00
Scott Richmond
bce3f9d7b0 properly show sets 2024-06-15 21:52:50 -04:00
Scott Richmond
6022895fa8 improve formatting and add new functions to topics 2024-06-15 21:52:19 -04:00
Scott Richmond
07c6d23587 bugfixes and improvements 2024-06-15 21:51:56 -04:00
Scott Richmond
d64467ef6d accurately describe current Ludus 2024-06-15 21:50:46 -04:00
Scott Richmond
d1c48fb0fd add hrs 2024-06-15 18:14:03 -04:00
Scott Richmond
3f030ca505 add back to top links 2024-06-15 18:12:28 -04:00
Scott Richmond
d78f7ce827 updated janet-based docs, but for real this time 2024-06-15 18:08:48 -04:00
Scott Richmond
9d66c9d697 Bugfixes! 2024-06-15 18:08:20 -04:00
Scott Richmond
68aca09de6 updated janet-based docs 2024-06-15 17:04:01 -04:00
Scott Richmond
d94eb5485d fix some bugs, prep for doc 2024-06-15 17:03:49 -04:00
Scott Richmond
98d6b1c865 fix some bugs 2024-06-15 17:03:37 -04:00
Scott Richmond
aba77569ac some pure-ludus string manipulation testing 2024-06-15 11:58:30 -04:00
Scott Richmond
223823ea68 0.1.16 2024-06-14 17:21:50 -04:00
Scott Richmond
ae6ad34364 build 2024-06-14 17:21:45 -04:00
Scott Richmond
d37e51b605 don't fold using builting functions 2024-06-14 17:21:32 -04:00
Scott Richmond
5d6d4e723d 0.1.15 2024-06-14 17:18:16 -04:00
Scott Richmond
4f84f408a7 no repl cruft + build 2024-06-14 17:18:11 -04:00
Scott Richmond
aa5795e168 improve errors, a bit 2024-06-14 17:17:23 -04:00
Scott Richmond
1841915d92 fix? bug in call-fn with incorrect function call info 2024-06-14 16:52:07 -04:00
Scott Richmond
7a7a3b8977 fix show box stack overflow 2024-06-14 15:55:57 -04:00
Scott Richmond
5a27d345eb fix goto! error 2024-06-14 15:43:52 -04:00
Scott Richmond
9584a21521 fix string escapes 2024-06-14 15:25:05 -04:00
Scott Richmond
c61981fa16 0.1.14 2024-06-14 14:54:18 -04:00
Scott Richmond
0583af819f build 2024-06-14 14:54:16 -04:00
Scott Richmond
0cbae4ce61 comment out repl cruft 2024-06-14 14:54:07 -04:00
Scott Richmond
e428fccc86 bugfixes and error improvements 2024-06-14 14:53:23 -04:00
Scott Richmond
f2f557d045 finish eliza 2024-06-11 17:25:10 -04:00
Scott Richmond
d7d9e71d67 Delete errant prints 2024-06-11 17:24:22 -04:00
Scott Richmond
eadd6e8047 fix bug in join 2024-06-11 17:19:01 -04:00
Scott Richmond
a9190995da 0.1.13 2024-06-10 18:28:23 -04:00
Scott Richmond
6dd5886896 build 2024-06-10 18:28:18 -04:00
Scott Richmond
287bf26f39 comment out repl cruft 2024-06-10 18:27:52 -04:00
Scott Richmond
de8f5c7cf4 baby's first eliza 2024-06-10 18:27:00 -04:00
Scott Richmond
5913f9b594 add string manipulation functions 2024-06-10 18:26:48 -04:00
Scott Richmond
2e7db1b969 fix typo 2024-06-10 18:25:12 -04:00
Scott Richmond
c1359e5c70 0.1.12 2024-06-07 17:26:07 -04:00
Scott Richmond
441117b966 build 2024-06-07 17:26:00 -04:00
Scott Richmond
92a1ee9010 fix mod error 2024-06-07 17:25:46 -04:00
Scott Richmond
9a3217b2c9 0.1.11 2024-06-07 17:17:00 -04:00
Scott Richmond
8c8f555d83 build 2024-06-07 17:16:52 -04:00
Scott Richmond
2f5af11527 add load_turtle_state! to prelude 2024-06-07 17:16:29 -04:00
Scott Richmond
8a4dd4b6e5 0.1.10 2024-06-07 16:46:05 -04:00
Scott Richmond
f5ce3aa72a build 2024-06-07 16:45:46 -04:00
Scott Richmond
9db10edd5c fix random(dict) bug 2024-06-07 16:45:31 -04:00
Scott Richmond
533a40bd45 runtime errors should return strings, not buffers 2024-06-07 16:41:11 -04:00
Scott Richmond
1fff319bb6 fix random bugs 2024-06-07 16:40:37 -04:00
Scott Richmond
de647097b7 0.1.9 2024-06-07 15:44:43 -04:00
Scott Richmond
5e3ffb0e1e delete print!s from prelude 2024-06-07 15:44:35 -04:00
Scott Richmond
fd3b47fa15 0.1.8 2024-06-07 15:26:03 -04:00
Scott Richmond
92498e7f80 test string interpolation 2024-06-07 15:25:03 -04:00
Scott Richmond
2fccbe96eb build things! 2024-06-07 15:20:45 -04:00
Scott Richmond
f2d76e766b integrate build scripts; build before publish 2024-06-07 15:20:36 -04:00
Scott Richmond
9498bed710 update justs 2024-06-07 15:10:17 -04:00
Scott Richmond
b3cfeb82e1 test 2024-06-07 15:06:23 -04:00
Scott Richmond
c532636acb 0.1.7 2024-06-07 15:04:46 -04:00
Scott Richmond
8dcf0b14d1 bump npm version 2024-06-07 15:04:40 -04:00
Scott Richmond
4d0a37328d fix string interpolation, prelude bugs 2024-06-07 15:04:06 -04:00
Scott Richmond
a6e2e11e4a bump version number 2024-06-07 13:42:45 -04:00
Scott Richmond
77b76430c1 finally correctly wire up all the things? 2024-06-07 13:42:11 -04:00
Scott Richmond
ed762c6079 println debugging! 2024-06-07 11:42:11 -04:00
Scott Richmond
a0678f5742 revert version to simple semver 2024-06-06 19:31:56 -04:00
Scott Richmond
940fc8ec31 fix the merge conflicts 2024-06-06 19:09:05 -04:00
Scott Richmond
cc33a2fb3d complete fucking draft of janet/wasm interpreter 2024-06-06 18:47:04 -04:00
Scott Richmond
baba0f4977 start packaging things up for wasm 2024-06-06 16:14:04 -04:00
Scott Richmond
03128441a6 start standing up a test harness, do not finish it, use judge 2024-06-06 15:43:36 -04:00
Scott Richmond
c7f99d35a6 fix a bear of a bug with accidentally persisting loop contexts 2024-06-06 15:41:33 -04:00
Scott Richmond
125a299b10 add postlude 2024-06-06 10:36:55 -04:00
Scott Richmond
d84a930073 bring in and update postlude 2024-06-05 23:23:47 -04:00
Scott Richmond
176feb5ae2 boxed console printing works, but not janet prints 2024-06-05 23:03:08 -04:00
Scott Richmond
8ac289cc9d I keep working, shit's not working 2024-06-05 20:16:29 -04:00
Scott Richmond
35a4b8e1c6 stash work: bugfixes, better errors, etc. 2024-06-05 17:47:41 -04:00
Scott Richmond
20cb689d12 prelude now passes validator 2024-06-05 15:52:03 -04:00
Scott Richmond
5874a56090 pull in most recent prelude 2024-06-05 13:21:48 -04:00
Scott Richmond
6a4e2ccd17 continue integration work: basic framework 2024-06-05 13:01:43 -04:00
Scott Richmond
721594823d start integration work: comment out prints/pps; create ludus.janet 2024-06-05 11:55:06 -04:00
Scott Richmond
fa5e298d94 ref -> box; fix box representations; add ds modifying functions to base 2024-06-04 16:57:32 -04:00
Scott Richmond
6cc7f045a2 complete draft of base 2024-06-04 16:24:54 -04:00
Scott Richmond
a25ece5a68 fixed closure/function contexts work with forward declaration & mutual recursion 2024-06-04 16:06:31 -04:00
Scott Richmond
f80aa7a8dc function contexts are fixed at declaration 2024-06-04 15:52:24 -04:00
Scott Richmond
b86a25b5bc stand up pkgs 2024-06-04 14:50:48 -04:00
Scott Richmond
27b688b96d interpret loop & recur! 2024-06-04 14:25:22 -04:00
Scott Richmond
4da846d8d7 doc! works! 2024-06-04 13:28:20 -04:00
Scott Richmond
478bc8649e add runtime doc info 2024-06-04 13:04:53 -04:00
Scott Richmond
1842923fa3 clean up little bugs 2024-06-04 13:02:15 -04:00
Scott Richmond
df274799be pretty print patterns 2024-06-04 13:00:34 -04:00
Scott Richmond
3081af60b2 parse docstrings 2024-06-04 12:13:40 -04:00
Scott Richmond
943e96203e comment on last line does not kill scanner 2024-06-04 11:59:41 -04:00
Scott Richmond
bbd41a0f74 interpret forward-declared functions, allowing mutual recursion 2024-06-04 11:54:29 -04:00
Scott Richmond
bc1eac46b8 validate forward declarations 2024-06-04 11:50:17 -04:00
Scott Richmond
d0a6cdbf54 parse fn forward declarations 2024-06-04 11:17:25 -04:00
Scott Richmond
3903f10c8d update in-code project management 2024-05-31 13:29:51 -04:00
Scott Richmond
8e03707f64 fix arity-checking bug; do not check arities in self-recursive function calls 2024-05-30 19:56:21 -04:00
Scott Richmond
524e3627fb get pkg validation access working for root pkg access, e.g. Foo :bar and Foo :bar 2024-05-30 19:38:00 -04:00
Scott Richmond
1dce69e239 work on pkgs 2024-05-23 19:33:19 -04:00
Scott Richmond
017655e8f8 add sqrt to prelude exports 2024-05-23 19:31:43 -04:00
Scott Richmond
8b0954b8ec correctly parse pkgs, actually 2024-05-20 18:24:41 -04:00
Scott Richmond
4547c0747d correctly parse pkgs 2024-05-20 18:23:00 -04:00
Scott Richmond
70b6a1dcd7 correctly parse pkg-name and pkg-kw 2024-05-20 18:11:49 -04:00
Scott Richmond
23128902bc add pkg-kw tokens 2024-05-20 18:04:24 -04:00
Scott Richmond
94adf5e9d5 called keywords take only a single argument 2024-05-20 17:43:57 -04:00
Scott Richmond
1120f21df2 dict pattern matching 2024-05-19 20:19:00 -04:00
Scott Richmond
d249ee0b21 validate dict patterns, by validating pairs 2024-05-19 20:18:39 -04:00
Scott Richmond
e767c319b1 fix dict parsing 2024-05-19 20:18:22 -04:00
Scott Richmond
010b584ef1 partial function application! 2024-05-19 19:35:41 -04:00
Scott Richmond
ba1aa8ed03 comment out repl cruft 2024-05-19 19:35:30 -04:00
Scott Richmond
e5917c6284 partially applied functions don't kill validation 2024-05-19 19:15:22 -04:00
Scott Richmond
6bf4dde487 do expressions 2024-05-19 18:38:44 -04:00
Scott Richmond
822f5c0178 {stringify, ltype} -> imports from base 2024-05-19 18:24:11 -04:00
Scott Richmond
2415f3d437 bugfixes 2024-05-19 18:23:34 -04:00
Scott Richmond
a399669197 fix bugs, oops 2024-05-19 18:13:08 -04:00
Scott Richmond
e468add325 write most of a base 2024-05-19 18:04:08 -04:00
Scott Richmond
24bbef74aa add janet functions 2024-05-19 16:18:52 -04:00
Scott Richmond
a06014270f validate with passed context; don't die on builtin functions 2024-05-19 16:17:34 -04:00
Scott Richmond
608ab4ab67 stringify fns and bugfixes 2024-05-19 14:54:51 -04:00
Scott Richmond
70bff13eea add ref to main switch 2024-05-19 14:54:37 -04:00
Scott Richmond
8d3d9a2dc5 2big commit: stand up fns, definitions and calls, lots of bugfixes 2024-05-19 01:58:10 -04:00
Scott Richmond
265f867a71 fix string interpolation 2024-05-18 18:22:49 -04:00
Scott Richmond
5deab18356 string patterns should now be working 2024-05-18 17:43:21 -04:00
Scott Richmond
014da297d0 more cleanup still 2024-05-18 17:05:47 -04:00
Scott Richmond
88aaf864ab more cleanup 2024-05-18 17:05:14 -04:00
Scott Richmond
95054ef234 add match exprs 2024-05-18 17:04:23 -04:00
Scott Richmond
41cd39df2e check + compile string patterns; some cleanup, some messes still 2024-05-18 17:04:04 -04:00
Scott Richmond
32cf7d6cc4 add expr to match to matchh 2024-05-18 17:01:12 -04:00
Scott Richmond
b5d23b26ec ensure :errors is always a tuple or array, never nil; allows (empty? (scanner :errors)) to work as a test for errors 2024-05-18 17:00:18 -04:00
Scott Richmond
b0c912b16c don't die when trying to check arity of a nonexistent function 2024-05-16 13:57:23 -04:00
Scott Richmond
d5f593b0f3 tuple patterns, with splats! 2024-05-15 12:33:52 -04:00
Scott Richmond
e0919e771d bugfix 2024-05-15 12:33:34 -04:00
Scott Richmond
399f1fd4c7 make progress; WIP: interpreting pattern matching 2024-05-15 00:05:25 -04:00
Scott Richmond
3a8a236f01 start work on interpreter 2024-05-14 20:44:54 -04:00
Scott Richmond
c68d08e8b2 fix imports 2024-05-14 20:44:43 -04:00
Scott Richmond
3e9f38ef5c update notes 2024-05-14 18:56:18 -04:00
Scott Richmond
b6e1d0e6ec clean up files 2024-05-14 18:56:06 -04:00
Scott Richmond
bc17fe5006 successfully flag tail calls 2024-05-14 18:52:11 -04:00
Scott Richmond
5fbafbac94 make progress: many things 2024-05-14 18:41:21 -04:00
Scott Richmond
ec43aa3c67 accept newlines after arrows in fn clauses; make some asts mutable for validation 2024-05-14 18:41:07 -04:00
Scott Richmond
3225ea2472 improve pkg 2024-05-14 13:46:13 -04:00
Scott Richmond
67cd9d479b keywords must start with lower case 2024-05-14 13:45:41 -04:00
Scott Richmond
3b3071adb0 capture unvalidated notes from parser as todos 2024-05-13 21:07:27 -04:00
Scott Richmond
7018949845 a whole lot of a validator 2024-05-13 20:55:36 -04:00
Scott Richmond
68e96bf223 many bugfixes; desugar pairs with word shorthand in dicts 2024-05-13 20:55:21 -04:00
Scott Richmond
2cfe9fdffc complete draft of parsing 2024-05-11 23:25:36 -04:00
Scott Richmond
806ec0e8f0 moar validations 2024-05-11 23:25:20 -04:00
Scott Richmond
65eb17778c don't put break before pipeline 2024-05-11 23:25:07 -04:00
Scott Richmond
b5def30348 add a pretty-printer (that sometimes causes errors!), lots of bugs but functions for all the things 2024-05-10 16:10:57 -04:00
Scott Richmond
b0cffea71f moar idears for validation 2024-05-10 16:10:27 -04:00
Scott Richmond
064b5df2dd better name for package names 2024-05-10 15:02:55 -04:00
Scott Richmond
f1a1e9ec62 notes towards an ast-validator 2024-05-10 15:02:37 -04:00
Scott Richmond
8f284f1e65 first draft of all the things; many bugs abound 2024-05-10 15:02:22 -04:00
Scott Richmond
232261b646 add uppercase pkg to scanner 2024-05-10 14:29:12 -04:00
Scott Richmond
f3778792b3 parse interpolated strings/string patterns 2024-05-09 18:30:13 -04:00
Scott Richmond
248e424993 moar bugfixes 2024-05-09 18:29:51 -04:00
Scott Richmond
3f16e45204 fix escaping brace bug, which was fixing next-char bug; also clean some stuff up 2024-05-09 16:35:22 -04:00
Scott Richmond
c5d04ddd66 some unfinished work on string interpolation 2024-05-08 17:42:10 -04:00
Scott Richmond
3466b075af add easy patterns, start work on string interpolation 2024-05-08 17:31:47 -04:00
Scott Richmond
736f1024c3 fix nil parser bug, start work on patterns 2024-05-08 17:24:29 -04:00
Scott Richmond
4f16cf5cb0 finally get when clauses & forms right 2024-05-08 17:14:51 -04:00
Scott Richmond
942f55fb39 fix panic off-by-one-error 2024-05-08 15:56:59 -04:00
Scott Richmond
0eb212dd45 add a break before pipline 2024-05-08 15:29:31 -04:00
Scott Richmond
77bacd1367 get when expressions worked out 2024-05-08 15:29:18 -04:00
Scott Richmond
cdb71a8122 :rarrow -> :arrow 2024-05-08 13:59:46 -04:00
Scott Richmond
c36a140c6b if expressions, done 2024-05-08 13:50:26 -04:00
Scott Richmond
f3256f7d12 first draft if 2024-05-08 13:22:49 -04:00
Scott Richmond
05703a27fa complete simple expressions 2024-04-29 18:38:08 -04:00
Scott Richmond
98fcfe7eb4 get the recursive descent framework worked out 2024-04-29 16:25:24 -04:00
Scott Richmond
402b444231 add todos for computer class 2024-04-28 18:14:11 -04:00
Scott Richmond
3d570beb45 start work on a recursive descent parser 2024-04-28 18:13:49 -04:00
Scott Richmond
0de9f90f27 update .gitignore to exclude repl cruft 2024-04-28 18:10:03 -04:00
Scott Richmond
2bc32c35f2 get the thing to run, anyway 2024-04-06 19:12:44 -04:00
Scott Richmond
83ce75c6ea Clean some things up 2024-04-06 18:15:12 -04:00
Scott Richmond
42907f19d7 start debugging current state of affairs: add comments with compile errors 2024-04-06 17:00:22 -04:00
Scott Richmond
bb42ca7ca4 Add doc command to justfile 2024-04-06 16:58:11 -04:00
Scott Richmond
efffbafdba Save some work. 2024-03-25 16:04:54 -04:00
Scott Richmond
4a1e509fc4 0.1.3 2024-01-22 17:37:28 -05:00
Scott Richmond
aef33279ca Fix vector subtraction 2024-01-22 17:37:11 -05:00
Scott Richmond
1b361971bc 0.1.2 2024-01-22 17:18:46 -05:00
Scott Richmond
673a96ffb8 Update prelude 2024-01-22 17:17:29 -05:00
Scott Richmond
90c97e7cfe Fix exports & bump version 2024-01-19 17:49:26 -05:00
Scott Richmond
d253dc3d3e stash work 2024-01-19 16:50:01 -05:00
Scott Richmond
f02532ee40 Get annoyed by translating my clj parser into janet; start work on a Janet/PEG parser 2024-01-11 19:42:58 -05:00
Scott Richmond
f4d09afed6 clj->janet :: loop->clj-loop 2024-01-11 14:26:22 -05:00
Scott Richmond
a65b4ba873 Keep working on translating parser 2024-01-09 23:42:56 -05:00
Scott Richmond
70e8763dc5 Pull janet files out into their own dir; start work on parser 2024-01-07 22:59:00 -05:00
Scott Richmond
d3a1e10983 Improve repl commands 2024-01-07 22:58:03 -05:00
Scott Richmond
ff40d395f8 Repl things 2024-01-07 20:10:45 -05:00
Scott Richmond
307bebfa53 First pass at janet scanner 2024-01-07 20:10:16 -05:00
Scott Richmond
153c5a358e Take some notes clj->janet 2024-01-07 20:10:02 -05:00
Scott Richmond
2cbc39029c Janet repl things 2024-01-07 20:09:01 -05:00
Scott Richmond
1520b1d8e7 Repl cruft 2023-12-31 18:37:19 -05:00
Scott Richmond
83bfc01275 Try with kitten 2023-12-31 18:37:09 -05:00
Scott Richmond
14780bf6b8 First pass at new state model: all refs are included in results. 2023-12-27 12:24:12 -05:00
Scott Richmond
f657da57ef Bump version 2023-12-27 12:09:39 -05:00
Scott Richmond
40f66321f1 Mention the whole team, duh 2023-12-26 19:43:53 -05:00
Scott Richmond
c9a956fafb Bump version 2023-12-24 23:42:37 -05:00
47420740df Merge pull request 'test_harness' (#22) from test_harness into main
Reviewed-on: #22
2023-12-25 04:41:16 +00:00
Scott Richmond
8b47aab280 Allow test harness to expect panics 2023-12-24 19:12:04 -05:00
Scott Richmond
d12d485583 Add basic test harness, actually add files this time 2023-12-24 17:17:08 -05:00
Scott Richmond
afb8bacb25 Stand up automated js/tap-based test harness framework. 2023-12-24 17:08:02 -05:00
Scott Richmond
20ea25761a Remove conflicting repl cruft. 2023-12-24 15:59:31 -05:00
Scott Richmond
770c84d081 Merge branch 'test-exprs' 2023-12-24 15:58:52 -05:00
Scott Richmond
b5d57cd96c Testing hits all the desiderata. 2023-12-24 15:53:40 -05:00
Scott Richmond
b6eeaa1d3e Stand up basic testing 2023-12-24 15:23:53 -05:00
Scott Richmond
b723532d1a Add testing status to run 2023-12-24 14:22:16 -05:00
8375e19f1e Merge pull request 'expr-repeat' (#19) from expr-repeat into main
Reviewed-on: #19
2023-12-24 19:17:16 +00:00
Scott Richmond
7a2c404daf Light testing 2023-12-24 14:16:08 -05:00
Scott Richmond
7541d2499d Change interpreter (just change name of binding in interpret-repeat 2023-12-24 14:12:50 -05:00
Scott Richmond
a6d64ff827 Change grammar 2023-12-24 14:11:49 -05:00
Scott Richmond
46817bd4e5 Update documentation; a pull request seems like overkill 2023-12-18 01:13:26 -05:00
Scott Richmond
6c11c5139b Language documentation reflects panic! as a form. 2023-12-18 01:13:15 -05:00
a86b8f4fa4 Merge pull request 'better_panics' (#16) from better_panics into main
Reviewed-on: #16
2023-12-18 05:46:05 +00:00
Scott Richmond
7fe53a13b4 Bump version 2023-12-18 00:43:24 -05:00
Scott Richmond
3a50910e3d Fix bug where partially applied called keyword returns nil 2023-12-18 00:42:44 -05:00
34f9a08bd6 Merge pull request 'better_panics' (#15) from better_panics into main
Reviewed-on: #15
2023-12-18 05:26:57 +00:00
Scott Richmond
0b6deefd5d Bump version 2023-12-18 00:23:06 -05:00
Scott Richmond
b12b49c197 Fix cljs/clj bug 2023-12-18 00:22:42 -05:00
Scott Richmond
ff1e1345b8 Stop tracking repl-port 2023-12-18 00:20:50 -05:00
Scott Richmond
be2c91b7dc Fix line number reporting bugs: look for lines in the right place. 2023-12-18 00:20:34 -05:00
Scott Richmond
4e646101e2 Remove duplicate error messages. 2023-12-17 23:16:12 -05:00
Scott Richmond
53b71fe790 Panic is now a form, not a function. 2023-12-17 23:13:50 -05:00
6cf3fdd5f2 Merge pull request 'Light update; no struct deletion necessary.' (#14) from delete_structs into main
Reviewed-on: #14
2023-12-18 00:33:39 +00:00
Scott Richmond
dad212c3d6 Light update; no struct deletion necessary. 2023-12-17 19:25:23 -05:00
3373d28f93 Merge pull request 'delete_structs' (#13) from delete_structs into main
Reviewed-on: #13
2023-12-18 00:21:37 +00:00
Scott Richmond
84e3356758 Remove from prelude 2023-12-17 19:17:03 -05:00
Scott Richmond
e38fc47478 Remove from interpreter 2023-12-17 19:16:45 -05:00
Scott Richmond
7c7b556115 Remove from grammar 2023-12-17 19:15:25 -05:00
Scott Richmond
0e6a71348e Remove struct from scanner 2023-12-17 19:13:32 -05:00
Scott Richmond
43778f00e1 bump version 2023-12-17 19:02:34 -05:00
Scott Richmond
60c44d8923 First draft bugfix 2023-12-17 19:00:31 -05:00
Scott Richmond
feb02dc1b6 Continual improvement 2023-12-14 19:55:33 -05:00
Scott Richmond
541b6a51aa Fix typos 2023-12-14 19:24:59 -05:00
Scott Richmond
9cb735669a Update readme & logo 2023-12-14 19:18:53 -05:00
Scott Richmond
fa8ac565a6 Finish a first draft of complete language documentation. 2023-12-14 18:25:59 -05:00
Scott Richmond
0be538b087 Keep working on a language spec. 2023-12-14 00:19:25 -05:00
Scott Richmond
63fccc16cd Keep on trucking with more/better docs. 2023-12-13 20:42:50 -05:00
Scott Richmond
421114f191 Add more new fns 2023-12-13 17:08:15 -05:00
Scott Richmond
36d9ed8d69 Add some new functions 2023-12-13 17:02:39 -05:00
Scott Richmond
84fd8779e3 Update docs 2023-12-13 16:57:44 -05:00
Scott Richmond
6c773e65e9 Update docs 2023-12-13 14:15:32 -05:00
Scott Richmond
3c25fc72eb Add more docs. 2023-12-12 15:49:18 -05:00
Scott Richmond
075f7e8f9e Updates 2023-12-12 15:38:16 -05:00
Scott Richmond
adf7c4e9e3 Updated doc 2023-12-11 16:16:16 -05:00
Scott Richmond
7001c5a30a Some tweaks, fix links. 2023-12-11 16:16:03 -05:00
Scott Richmond
080f9d018c Checkin first doc output. 2023-12-11 16:02:17 -05:00
Scott Richmond
fece951f4b First draft documentation 2023-12-11 16:02:03 -05:00
Scott Richmond
576b280a0c Fix pattern printing 2023-12-11 16:01:49 -05:00
Scott Richmond
c683b73201 Start repl for documentation scene. 2023-12-11 14:42:23 -05:00
Scott Richmond
7a4ad90380 Do a bunch of work to get things mostly working on ludus-web 2023-12-09 20:30:43 -05:00
Scott Richmond
88e027b240 Remove .helix 2023-12-08 18:20:13 -05:00
Scott Richmond
37f0350fa3 Fix l/r reversal bug, doc! bug, join bug. 2023-12-08 18:19:08 -05:00
Scott Richmond
2681a365f8 Bump version 2023-12-08 17:31:00 -05:00
Scott Richmond
1b67bab4fe Actually fix turtle angles and states 2023-12-08 17:30:33 -05:00
Scott Richmond
8b593c1492 Bump version 2023-12-08 17:21:18 -05:00
Scott Richmond
b61322bbfd Fix turtle angles, states, and colors. 2023-12-08 17:20:57 -05:00
Scott Richmond
0e42d893e8 Bump version 2023-12-08 15:35:10 -05:00
Scott Richmond
ed4447d6c2 Remove repl cruft 2023-12-08 15:30:13 -05:00
Scott Richmond
bc5cc37cc1 Fully add repeat form 2023-12-08 15:27:49 -05:00
Scott Richmond
b285047d60 Fix additional bugs in the prelude 2023-12-08 15:27:33 -05:00
Scott Richmond
f5f1d4a440 Fix subtle parsing errors in prelude. 2023-12-08 15:04:44 -05:00
Scott Richmond
c88a06d447 Start adding repeat form. 2023-12-07 12:28:30 -05:00
Scott Richmond
53c4433d2a Keep working on prelude. 2023-12-06 20:29:21 -05:00
Scott Richmond
8ce97081d0 Add better error handling, improve prelude, postlude. 2023-12-06 20:02:14 -05:00
Scott Richmond
480e7abcf0 Remove repl 2023-12-04 14:06:10 -05:00
Scott Richmond
ba44ddcb3f Remove print!s 2023-12-04 14:01:32 -05:00
Scott Richmond
6a1906c1ae Fix the bugs. Not all of them. 2023-12-04 13:58:29 -05:00
Scott Richmond
efb33cc1be Reset state after each run 2023-12-04 12:12:05 -05:00
Scott Richmond
c9ccffa067 Basically wire the things up! 2023-12-04 00:41:57 -05:00
Scott Richmond
17592149f1 Finish turtle graphics? 2023-12-03 23:14:55 -05:00
Scott Richmond
14862c3ba9 Add slice, fix bugs 2023-12-03 21:48:53 -05:00
Scott Richmond
30fa4e9d97 Fix anonymous fn bug 2023-12-03 21:10:22 -05:00
Scott Richmond
314101d17d Make lots of progress on prelude & turtle graphics, fixing partial function application bug along the way. 2023-12-03 17:15:26 -05:00
Scott Richmond
7515df835e Fix bug, add show-pattern 2023-12-03 12:40:38 -05:00
Scott Richmond
ab48dfa6b3 Make lots and lots of progress; discover error in pattern matching. 2023-12-02 16:14:57 -05:00
Scott Richmond
4a84afc971 Bump version number 2023-12-02 12:18:17 -05:00
Scott Richmond
58e0e8b51c npmify the things! 2023-12-01 20:33:12 -05:00
Scott Richmond
478d0347f2 Stand up stub of npm export 2023-12-01 19:56:22 -05:00
Scott Richmond
bc7565926a Lots of progress 2023-12-01 19:08:51 -05:00
Scott Richmond
3f865a64d7 Futz 2023-12-01 14:00:15 -05:00
Scott Richmond
792ce12617 Get base/prelude system working 2023-12-01 12:38:33 -05:00
Scott Richmond
3370fbc13e Unfuck function building 2023-12-01 11:18:39 -05:00
Scott Richmond
19c237bd9d Unfuck .gitignore so that I'm not losing work 2023-11-30 15:19:20 -05:00
Scott Richmond
02d15ac5b7 Track untracked files, add back in core, add base 2023-11-30 15:18:56 -05:00
Scott Richmond
d99a454e36 Work on fn regression 2023-11-30 15:03:51 -05:00
Scott Richmond
fcf9bd76e0 Wire up base and prelude 2023-11-30 14:31:02 -05:00
Scott Richmond
f4e2171e09 Oops--commit last change on use implementation 2023-11-30 13:23:36 -05:00
Scott Richmond
252b9af358 Add use to language 2023-11-30 13:22:38 -05:00
Scott Richmond
6070b6512e Add if let back in 2023-11-30 11:58:04 -05:00
Scott Richmond
b60efbda18 Update babashka/fs version 2023-11-30 11:57:46 -05:00
Scott Richmond
5fd2ce2583 Finish adding clj stuff back in. 2023-11-29 23:09:52 -05:00
Scott Richmond
29fc3714f8 Add back in some clj stuff: core/main/repl/loader 2023-11-29 22:39:56 -05:00
Scott Richmond
a7860b4544 Break out match from when, clean up, strictify (binding, non-binding, simple) 2023-11-29 22:29:58 -05:00
Scott Richmond
86cb4c2d76 Smol change 2023-11-26 12:18:57 -05:00
Scott Richmond
baaa010721 Clean .gitignore up 2023-11-26 12:16:13 -05:00
Scott Richmond
46fdfa2a79 Merge branch 'cljs' of https://github.com/thinking-with-computers/ludus into cljs 2023-11-26 12:12:08 -05:00
Scott Richmond
246e9e5e53 clj->extern 2023-11-25 14:27:49 -05:00
Scott Richmond
fd77e1ed45 Fix clj (extern call) for js 2023-11-25 14:26:00 -05:00
1fc7560634 added .shadow-cljs to .gitignore 2023-11-25 13:57:05 -05:00
Scott Richmond
a072191081 Wire up proof of concept! 2023-11-24 18:41:26 -05:00
Scott Richmond
8c19cca9e7 Get it working! Ludus in cljs works. 2023-11-24 13:54:06 -05:00
Scott Richmond
8a456e1d02 Oops, add .gitignore 2023-11-24 13:17:44 -05:00
Scott Richmond
a23e3bfdc5 Keep working, cljs repl :((((( 2023-11-24 13:17:20 -05:00
Scott Richmond
7ec258ee24 Clean it up, wire it up. 2023-11-16 19:16:31 -05:00
Scott Richmond
e276298f4e Don't track out 2023-11-16 13:24:47 -05:00
Scott Richmond
3273ab4385 Add cljs to deps, hello world 2023-11-16 13:22:15 -05:00
Scott Richmond
3b5a789333 Make some edits. 2023-10-13 18:18:05 -04:00
7f0415954c Futz with sandbox 2023-09-16 13:48:50 -04:00
fd2a004627 Add project helix config 2023-09-16 13:48:37 -04:00
7030aa82a9 Keep tracking sandbox 2023-07-04 12:43:44 -04:00
27140802f7 Sandbox! 2023-07-04 12:42:56 -04:00
d87a0eddc5 Ignore sandbox.ld 2023-07-04 12:41:00 -04:00
919537e758 Start working on turtle graphics 2023-07-04 12:39:32 -04:00
3854116f33 Fix and/or functions 2023-07-04 12:39:18 -04:00
98dfe5c503 Add and/or special forms 2023-07-04 12:38:31 -04:00
2750833e59 Update TODOs 2023-06-02 17:29:04 -06:00
a23c779efa Fix merge conflicts 2023-06-02 16:10:40 -06:00
7d2a7061e8 Futz 2023-06-02 16:04:03 -06:00
d6981b3714 Update TODOs 2023-06-02 16:03:40 -06:00
5c8ba725a9 Bugfixes. 2023-06-01 17:27:55 -06:00
7c30b6259b Wire up repl & file interpreters. 2023-06-01 15:06:33 -06:00
c179325719 Improve prelude 2023-06-01 13:11:06 -06:00
a6ef6522c0 Add clj fn to prelude, calls arbitrary Clojure. 2023-06-01 12:46:52 -06:00
c8c74fbd49 Simplify conditional forms: when, if, etc. 2023-05-31 11:18:55 -06:00
67e230c714 Actually save work 2023-05-31 11:51:43 -04:00
82a539a112 Update struct match 2023-05-31 11:51:02 -04:00
3bd34f1269 Fix type member of prelude 2023-05-31 11:42:51 -04:00
e27e5a4c1a Add typed shorthand to dicts 2023-05-31 11:37:39 -04:00
6c38c43727 Get dict splats working, fix match-typed 2023-05-31 11:31:04 -04:00
0e9c403634 Things, mostly list & tuple splats are correct. 2023-05-31 09:30:12 -04:00
fdaf1068d3 Get stuff running again, fix missing nil pattern, play wtih unwrap, default 2023-05-27 18:05:43 -04:00
35eed84741 Constraint -> guard 2023-05-26 15:24:10 -04:00
c1b305bc1c Fix constraint -> guard 2023-05-26 15:23:12 -04:00
776f975d54 Collate TODOs; comment repl cruft 2023-05-26 15:21:55 -04:00
c6eeed4f4c Use defp macros for parsers; update grammar using defp; update interpreter with new node names 2023-05-26 15:07:41 -04:00
cff0f9b6e8 Add some todo notes 2023-05-22 17:00:10 -04:00
6cf09fb177 Complete interpreter, less process system: spawn, receive 2023-05-22 16:56:24 -04:00
618d6b856c Add TODO 2023-05-22 00:17:17 -04:00
3256565308 Add separate recur node 2023-05-22 00:16:01 -04:00
65692b611b make some notes 2023-05-22 00:14:53 -04:00
b504370d96 Keep knocking 'em down: fns, loops, pipelines work 2023-05-21 23:58:54 -04:00
8516f0e053 Start work on the interpreter 2023-05-21 16:43:26 -04:00
e02e972d27 Start work on a compiler 2023-05-20 14:25:33 -04:00
ae8f72d3b4 Remove debug printlns 2023-05-20 14:25:13 -04:00
0fe85cf2ed Remove repl cruft 2023-05-20 14:20:23 -04:00
4fd593752b Finally get it right? 2023-05-20 14:18:30 -04:00
4ea7a3a23d Keep grinding; problems now with order/repeats 2023-05-19 18:55:14 -04:00
f97453b813 Complete parser & ludus grammar! 2023-05-18 16:44:29 -04:00
23e29fdca2 Reindent 2023-05-18 16:44:14 -04:00
a7ab313a5f Broken! Argh. 2023-05-16 20:54:01 -04:00
52abde501a Add flat combinator 2023-05-16 18:29:22 -04:00
cbd78ce7f7 Get parser combinator strategy working 2023-05-16 16:06:18 -04:00
919ab5ca34 Keep grinding on parser combinators 2023-05-07 23:23:42 -04:00
5b1ff5aef3 Many iterations of parser combinator strategies. Not yet working. 2023-05-07 22:49:19 -04:00
2866ff4eb6 Make some modest moves 2023-05-02 19:44:17 -04:00
c5a72912df Dequalify token keywords 2023-05-02 19:43:57 -04:00
963c63aed3 Unwire the things for parser refactor 2023-05-02 19:15:05 -04:00
197f4772ba keep unfucking merge 2023-05-02 19:08:18 -04:00
96b3f01e7e keep unfucking merge 2023-05-02 19:07:21 -04:00
d605cbd42a unfuck merge 2023-05-02 19:06:37 -04:00
4936daa8dc dequalify token keywords 2023-05-02 19:02:28 -04:00
3a7f86e401 Clean up some stuff, add ebnf grammar, fail to get quil working 2023-05-02 18:58:36 -04:00
0c2c646012 Clean up some stuff in scanner, try and fail to get quil working 2023-05-02 18:55:56 -04:00
Scott Richmond
55d76f6854 proof of concept: add quil to ludus 2023-03-25 15:52:25 -04:00
Scott Richmond
1c2ab5182e futz with repl 2023-03-20 15:36:00 -04:00
Scott Richmond
e535c0bf9c add ludus binary to .gitignore 2023-01-28 17:49:16 -05:00
Scott Richmond
b3e970933b fix graal compile opts for vthreads 2023-01-28 17:48:43 -05:00
Scott Richmond
7dca7ad3cc add a quick thought about spawn implementation 2023-01-28 17:42:34 -05:00
Scott Richmond
ccddfa07c5 add reference to vthread source 2023-01-28 17:42:17 -05:00
Scott Richmond
b7f0d17afe processes use virtual threads 2023-01-28 17:18:09 -05:00
Scott Richmond
6169f4f462 bump graalvm version number 2023-01-28 16:34:34 -05:00
Scott Richmond
a51ba2a2bc add TODO re: expression terminators 2023-01-28 14:31:40 -05:00
Scott Richmond
1caddfacbc Make (_) a parsing error 2022-11-03 17:51:33 -04:00
Scott Richmond
e248be16cf Comment out <- from scanner 2022-11-03 17:33:06 -04:00
Scott Richmond
01d7dc0ab1 Fix bug where if let leaked context 2022-11-03 17:19:46 -04:00
Scott Richmond
06c1b55b62 Update compile script for new graal version 2022-11-03 17:19:15 -04:00
Scott Richmond
238910e68e Complete single-branch datatypes, start work on sum types 2022-08-04 19:24:09 -04:00
Scott Richmond
cd0cad8ac3 cljfmt 2022-08-04 18:45:56 -04:00
Scott Richmond
9684c2f4c5 Add datatype declarations 2022-08-04 18:43:20 -04:00
Scott Richmond
5300ca5e3b Move ns & import to parse-script 2022-08-04 16:54:13 -04:00
Scott Richmond
969c321ffb Work on datatypes 2022-08-04 16:49:20 -04:00
Scott Richmond
18e4826960 Add datatype tokens 2022-07-26 18:25:11 -04:00
Scott Richmond
72aac19bd7 Add upper? and lower? fns 2022-07-26 18:19:16 -04:00
Scott Richmond
88c79b2b31 Comment out REPL cruft 2022-07-26 18:18:57 -04:00
Scott Richmond
08d91dd9de Add useful TODO, re: tuple splat patterns 2022-06-25 22:01:32 -04:00
Scott Richmond
272e46551e Cleanup todos 2022-06-25 18:20:54 -04:00
Scott Richmond
d2cefb79f0 Fix bug allowing repeated names everywhere 2022-06-25 18:18:54 -04:00
Scott Richmond
80fe4d370d Prevent duplicate keys in struct/dict patterns 2022-06-25 11:55:17 -04:00
Scott Richmond
df511ce5eb Fix bug allowing repeated name in tuples 2022-06-19 16:21:54 -04:00
Scott Richmond
a56a4b565a ::ast/splatted -> ::ast/splat (standardize) 2022-06-19 16:21:12 -04:00
Scott Richmond
e30a8b973b Add splats to dict & struct patterns in parser 2022-06-19 15:46:58 -04:00
Scott Richmond
07c21494cf Add splats to tuple patterns in parser 2022-06-19 15:01:00 -04:00
Scott Richmond
24f85966a2 Improve error message. 2022-06-19 14:58:14 -04:00
Scott Richmond
cdfcd1249e Fix copypasta error 2022-06-19 14:57:44 -04:00
Scott Richmond
c3c5c390e5 Unroll copypasta. 2022-06-19 14:57:04 -04:00
Scott Richmond
4e7590226b Copypasta splat patterns to dict, struct, tuple in parser 2022-06-19 14:52:38 -04:00
Scott Richmond
68bc37ef61 Add splats to list patterns in parser 2022-06-19 14:48:53 -04:00
Scott Richmond
4254359934 Complete if-let 2022-06-19 14:01:55 -04:00
Scott Richmond
689f3f23bc Add interpret-if-let 2022-06-19 13:46:11 -04:00
Scott Richmond
ed14b97a00 REPL investigate if-let ast 2022-06-19 13:45:52 -04:00
Scott Richmond
5653912f0e Fix file loading from REPL 2022-06-19 13:34:16 -04:00
Scott Richmond
f8322412b7 Remove REPL stuff 2022-06-19 13:15:00 -04:00
Scott Richmond
61bdeab5a6 REPL stuff 2022-06-19 13:12:50 -04:00
Scott Richmond
c17d210b43 Fix scanning bug: = is now a terminator 2022-06-04 14:49:49 -04:00
Scott Richmond
0aa7efcbb1 Add process:flush, fix printing 2022-06-02 19:10:41 -04:00
Scott Richmond
8752ee0da1 Add some process functions 2022-06-02 18:42:02 -04:00
Scott Richmond
4603dcf980 Fix import+process bug 2022-06-02 17:51:30 -04:00
Scott Richmond
2de1d94a4a Fix unbound name errors, import bug, process:in->process:inbox 2022-06-02 17:38:16 -04:00
Scott Richmond
2f5ebe85f7 Improve error messages in processes 2022-06-02 14:57:16 -04:00
Scott Richmond
1123d82503 Add conj and assoc 2022-06-02 14:56:13 -04:00
Scott Richmond
015d079d57 hashmap->dict cleanup 2022-06-02 14:56:03 -04:00
Scott Richmond
3f44c64802 Fix tuple/list/set parsing bug 2022-06-01 18:00:29 -04:00
Scott Richmond
b249817aa9 Fix print-function stack overflow 2022-06-01 17:28:55 -04:00
Scott Richmond
46581e103d Add actors to repl 2022-06-01 10:44:34 -04:00
Scott Richmond
1e28556baa First stab at 'actors': using threads. 2022-05-31 14:21:19 -04:00
Scott Richmond
09696c212a Fix bug when last line is comment 2022-05-31 14:12:52 -04:00
Scott Richmond
0055056142 Parse receive & self 2022-05-30 18:02:24 -04:00
Scott Richmond
b0d5b30124 Parse spawn & send 2022-05-30 17:40:27 -04:00
Scott Richmond
bb1f2a0086 Fix botched reserved word update 2022-05-30 17:24:10 -04:00
Scott Richmond
4b5a549561 Fix .gitignore 2022-05-30 17:22:01 -04:00
Scott Richmond
5f3ec99cb4 Exclude sublime workspace file 2022-05-30 17:21:22 -04:00
Scott Richmond
33d12898ec Clean up reserved words 2022-05-30 17:20:57 -04:00
Scott Richmond
18bcf6d8c6 More minor repl improvements 2022-05-27 20:02:32 -04:00
Scott Richmond
bad0591408 Add post-save cljfmt 2022-05-27 19:40:40 -04:00
Scott Richmond
173f5756a7 cljfmt 2022-05-27 19:18:00 -04:00
Scott Richmond
874dacf791 Littl repl improvements 2022-05-27 19:09:25 -04:00
Scott Richmond
a57446db93 Add basic repl session management 2022-05-26 20:24:41 -04:00
Scott Richmond
059fc16df2 Stand up super basic repl 2022-05-26 19:50:55 -04:00
Scott Richmond
698a07d085 Add interpret-repl 2022-05-26 19:50:45 -04:00
Scott Richmond
ef97cf16e4 Baby repl stub 2022-05-26 18:59:28 -04:00
Scott Richmond
a3c60e0196 Hashmap -> dict in scanner 2022-05-26 18:54:51 -04:00
Scott Richmond
64c8f30964 Hashmap -> dict in parser 2022-05-26 18:49:52 -04:00
Scott Richmond
3292c0676c Hashmap -> dict 2022-05-26 18:44:40 -04:00
Scott Richmond
a5b1763324 Add import error handling 2022-05-26 18:40:26 -04:00
Scott Richmond
8f1701dabb Add minimal error handling 2022-05-26 18:39:37 -04:00
Scott Richmond
7beca7e0f6 Fix line number reporting 2022-05-26 18:12:23 -04:00
Scott Richmond
c5a7b5ad46 Add a little comment 2022-05-26 18:06:37 -04:00
Scott Richmond
4a850f837d Typofix 2022-05-26 18:06:05 -04:00
Scott Richmond
916c38a0f4 Update readme w/ release 2022-05-25 12:58:05 -04:00
Scott Richmond
54fb9de263 Eclipse->GPL 2022-05-24 23:47:27 -04:00
Scott Richmond
3361f08aa6 Add build tooling 2022-05-24 23:18:41 -04:00
Scott Richmond
907d4e2b31 Use the little file loader 2022-05-24 18:52:45 -04:00
Scott Richmond
d8d1bf0858 Make a little file loader 2022-05-24 18:52:32 -04:00
Scott Richmond
96ef8f098f Remove binary 2022-05-24 17:33:16 -04:00
Scott Richmond
0887c0d830 Comment out repl stuff 2022-05-24 17:32:17 -04:00
Scott Richmond
3798dba9dc Comment out repl stuff 2022-05-24 17:31:52 -04:00
Scott Richmond
6cced0dd8a Tidy up 2022-05-24 17:30:38 -04:00
Scott Richmond
372103a3ee Add or and and 2022-05-24 17:29:06 -04:00
Scott Richmond
1ca434b308 Remove old show cruft 2022-05-24 17:02:50 -04:00
Scott Richmond
c0e88021f2 Remove unused clojure/string require 2022-05-24 17:02:09 -04:00
Scott Richmond
1079bb8028 Remove unused (and incorrect) 2-arity of token fn 2022-05-24 17:01:44 -04:00
Scott Richmond
b1284f3338 Interpret splats in sets 2022-05-20 19:01:13 -04:00
Scott Richmond
6e4fd22fd4 Pull #()s out into fns 2022-05-20 18:54:14 -04:00
Scott Richmond
355b9f9949 Remove println crud 2022-05-20 18:50:43 -04:00
Scott Richmond
f0fe8005e9 Interpret splats in hashmaps 2022-05-20 18:46:40 -04:00
Scott Richmond
f70aa0f4ce Parse splats in hashmaps *correctly* 2022-05-20 18:46:24 -04:00
Scott Richmond
ea5d9fa948 Parse splats in hashmaps and sets 2022-05-20 18:14:22 -04:00
Scott Richmond
7390461f2b Interpret list splats 2022-05-20 17:59:39 -04:00
Scott Richmond
9d645e3b4b Add list splats 2022-05-20 17:35:16 -04:00
Scott Richmond
bb3d145e5d Fiddle repl source 2022-05-20 17:35:02 -04:00
Scott Richmond
4d7b12eb34 Add struct patterns 2022-05-19 20:03:39 -04:00
Scott Richmond
34da4dd6fe Actually add hashmap patterns. Whoops. 2022-05-19 20:00:16 -04:00
Scott Richmond
bc2f6f8317 REPL cruft 2022-05-19 19:59:40 -04:00
Scott Richmond
bd9a408e07 Add hash patterns; add hashmap data tag & update show 2022-05-19 19:59:20 -04:00
Scott Richmond
9984a6c8ec Add basic list matching 2022-05-19 19:27:32 -04:00
Scott Richmond
a899ee776d Parse basic list, hash, struct patterns. 2022-05-19 19:14:25 -04:00
Scott Richmond
ad778f7104 Play around 2022-05-19 18:55:38 -04:00
Scott Richmond
35cbe352a8 Update reserved words list & impl 2022-05-19 18:55:27 -04:00
Scott Richmond
55c1edbb15 Add features 2022-05-19 18:55:00 -04:00
Scott Richmond
0e66dd9b0b Update readme, fix github url, add fib 2022-05-19 18:45:41 -04:00
Scott Richmond
9dd963e5b2 Interpret panic! not as fn but as rw. 2022-05-19 18:12:55 -04:00
Scott Richmond
4aeaa9e889 Fix parse-list :token typo 2022-05-19 18:11:31 -04:00
Scott Richmond
bd09dfa459 Remove panic! from prelude 2022-05-19 18:01:20 -04:00
Scott Richmond
4966271aa3 Parse panic! 2022-05-19 18:00:31 -04:00
Scott Richmond
1415461876 Fix panic! keyword 2022-05-19 18:00:18 -04:00
Scott Richmond
a36920e97e Add panic! reserved word 2022-05-19 17:54:10 -04:00
Scott Richmond
62a0fa2429 Add additional asts to errors. 2022-05-19 17:53:22 -04:00
Scott Richmond
8d2993637e Missed an ast add; fix it 2022-05-19 17:16:12 -04:00
Scott Richmond
cfe790b9c5 Add asts to thrown errors. 2022-05-19 17:14:20 -04:00
Scott Richmond
1fb41d8b71 Add tokens (and thus locations) to all AST nodes 2022-05-19 16:58:38 -04:00
Scott Richmond
ee4438bf1e Testing, small typo fix 2022-05-19 16:58:13 -04:00
Scott Richmond
959927f7ce Play with circular refs around closures 2022-05-19 16:17:30 -04:00
Scott Richmond
f419c390af Update reserved words list & impl 2022-05-19 16:16:55 -04:00
Scott Richmond
0939304518 Add TODO, re: closures 2022-05-18 20:45:14 -04:00
Scott Richmond
5a72125d1f Testing/playing 2022-05-18 20:30:27 -04:00
Scott Richmond
3b5e91c1bb Fix closure bug. 2022-05-18 20:23:43 -04:00
Scott Richmond
a5914076b0 Interpret loop/recur 2022-05-18 19:35:41 -04:00
Scott Richmond
b62fa6d074 Fix off-by-paren loop tup bug 2022-05-18 19:35:30 -04:00
Scott Richmond
0b7687024f Comment out testing 2022-05-18 18:33:32 -04:00
Scott Richmond
362e76c323 Parse loop/recur, and reindent all the things 2022-05-18 18:32:57 -04:00
Scott Richmond
e7eccca000 Update reserved words list & impl 2022-05-18 18:32:34 -04:00
Scott Richmond
5c07f43713 pprint refs 2022-05-18 18:32:15 -04:00
Scott Richmond
9df37cca4d Remove circular dependency 2022-05-18 18:31:35 -04:00
Scott Richmond
fb89508bef Play with base ref forms 2022-05-18 18:31:11 -04:00
Scott Richmond
9e25f24b3f Move testing to sandbox dir 2022-05-18 18:28:44 -04:00
Scott Richmond
8cbc516a66 Native ref fns 2022-05-17 19:13:13 -04:00
Scott Richmond
b1022ad832 Interpret refs 2022-05-17 19:13:00 -04:00
Scott Richmond
b0212b7e41 Parse refs 2022-05-17 18:34:07 -04:00
Scott Richmond
4c7ed5a8ef Update reserved words list 2022-05-17 17:30:33 -04:00
Scott Richmond
5f51d362d9 add signed binary 2022-04-30 15:26:51 -04:00
Scott Richmond
91cb0126fc Revert "clj -> cljc"
This reverts commit 214e94465d.
2022-04-30 14:08:04 -04:00
Scott Richmond
855b7a9a77 Revert "Add clojurescript"
This reverts commit b277a1bf2a.
2022-04-30 14:07:42 -04:00
Scott Richmond
082cf9c78c rm cljs 2022-04-30 13:57:25 -04:00
Scott Richmond
214e94465d clj -> cljc 2022-04-30 13:56:14 -04:00
Scott Richmond
b277a1bf2a Add clojurescript 2022-04-30 13:55:52 -04:00
Scott Richmond
8029a12f13
Update URL for releases after rename 2022-04-29 20:44:46 -04:00
Scott Richmond
bf624374ab Move ludus binary to target 2022-04-28 22:14:40 -04:00
Scott Richmond
70c63f041c Add ludus binary 2022-04-28 19:29:04 -04:00
Scott Richmond
76a87bf2d8 Improve readme 2022-04-28 19:28:32 -04:00
Scott Richmond
410dff327e Remove old script 2022-04-28 19:28:20 -04:00
Scott Richmond
2f4be6f6a5 Play with scripts 2022-04-28 19:27:59 -04:00
Scott Richmond
3d2c4719a6 Set up for GraalVM native-image 2022-04-28 19:27:35 -04:00
Scott Richmond
ea8c9ea049 Add logo 2022-04-28 17:07:26 -04:00
Scott Richmond
c107289920 play around 2022-04-25 20:37:52 -04:00
Scott Richmond
3c51394278 run uses show as printer, not pprint 2022-04-25 20:37:29 -04:00
Scott Richmond
cf7e739a93 delete cruft 2022-04-25 20:37:08 -04:00
Scott Richmond
85a8e85799 test show 2022-04-25 20:21:43 -04:00
Scott Richmond
19a0e7e3d5 Factor show out to its own package. 2022-04-25 20:21:23 -04:00
Scott Richmond
6f16495aab Improve ns access error message 2022-04-25 19:23:32 -04:00
Scott Richmond
4633941e08 Rename test scripts 2022-04-25 19:22:44 -04:00
Scott Richmond
a84e232cc7 Test imports, panics 2022-04-25 19:08:24 -04:00
Scott Richmond
6d81175b37 Wrap interpret in try/catch for more graceful panics 2022-04-25 19:08:05 -04:00
Scott Richmond
f7809d5ddf Improve panic message reporting 2022-04-25 19:07:18 -04:00
Scott Richmond
9cd4fccfb9 Remove comment cruft 2022-04-25 19:06:30 -04:00
Scott Richmond
8a36b1e4e2 Comment out testing 2022-04-25 18:46:12 -04:00
Scott Richmond
0c77de908c Make context volatile!, not atom 2022-04-25 18:44:44 -04:00
Scott Richmond
ceb8a2aa63 Make test for keyword more robust 2022-04-11 13:08:40 -04:00
Scott Richmond
2f6e81edb1 Add a quick TODO; interpret-import in progress 2022-04-08 20:48:09 -04:00
Scott Richmond
3749d2ea50 Very first draft of import expression 2022-04-08 19:13:57 -04:00
Scott Richmond
5df9c71bcc Play with piplines 2022-04-08 19:13:38 -04:00
Scott Richmond
fd832ac38e Allow _ as else in cond 2022-04-08 18:26:36 -04:00
Scott Richmond
969bbc548f Parse import statements 2022-04-08 15:59:47 -04:00
Scott Richmond
9dc9e95e85 Parse ignored words as placeholder 2022-04-08 15:23:08 -04:00
Scott Richmond
be2d291fd3 Interpret ns 2022-04-07 18:51:48 -04:00
Scott Richmond
9eecce77bc Fix ns name bug 2022-04-07 18:47:05 -04:00
Scott Richmond
df59b84321 Comment out testing 2022-04-07 18:31:36 -04:00
Scott Richmond
1317e52bbf Parse ns 2022-04-07 18:31:13 -04:00
Scott Richmond
ac83d3070b Update keyword impls 2022-04-07 18:09:53 -04:00
Scott Richmond
917e4f7ab5 Interpret cond; allow else in cond & match 2022-04-06 19:41:36 -04:00
Scott Richmond
03713c67bc Parse cond 2022-04-06 19:21:49 -04:00
Scott Richmond
e6419f6b6a Comment out testing 2022-04-05 20:31:46 -04:00
Scott Richmond
d8e8a1b17e Interpret partially applied functions 2022-04-05 20:27:22 -04:00
Scott Richmond
7309622a2b :partially-applied -> :partial 2022-04-05 20:27:04 -04:00
Scott Richmond
0544a44758 Actually comment out testing 2022-04-05 19:47:38 -04:00
Scott Richmond
bee8c250d4 Parse partial-application placeholder 2022-04-05 19:46:44 -04:00
Scott Richmond
fa5e51a179 Change some testing stuff 2022-04-05 19:11:44 -04:00
Scott Richmond
5a95f58ec1 Interpret do expressions 2022-04-05 19:09:38 -04:00
Scott Richmond
0a34e22a98 Remove debug pps 2022-04-05 19:09:22 -04:00
Scott Richmond
89f813c2f3 Parse do expressions/pipelines 2022-04-05 15:54:28 -04:00
Scott Richmond
174eb2d6a5 Pipeline is now >; add helpful impl comments 2022-04-05 15:54:12 -04:00
Scott Richmond
8cd905b8eb Test around panics. 2022-04-04 19:08:19 -04:00
Scott Richmond
4c2d05cbe1 A little demo for Bree 2022-04-04 19:07:54 -04:00
Scott Richmond
d663f48b6f Remove panic! keyword (it's an fn) 2022-04-04 19:07:33 -04:00
Scott Richmond
df784f7294 hello, world: just output 2022-04-03 22:24:41 -04:00
Scott Richmond
91fccb7be4 hello, world tweak 2022-04-03 22:23:28 -04:00
Scott Richmond
bf8e1d1924 Fix prelude/print bug 2022-04-03 22:23:12 -04:00
Scott Richmond
aa3692a141 Get rid of core shadow warnings. 2022-04-03 22:20:31 -04:00
Scott Richmond
faca700cae foo.ld -> hello.ld 2022-04-03 22:10:56 -04:00
Scott Richmond
54414ece03 Fix link to status in ludus-spec 2022-04-03 22:09:51 -04:00
Scott Richmond
8222e4119d track-changes: Update /Users/scott/git/cludus/README.md 2022-04-03 20:51:26 -04:00
Scott Richmond
e0161372e3 Wire up CLI. 2022-04-03 20:48:22 -04:00
Scott Richmond
12487b390e Remove testing cruft 2022-04-03 20:11:23 -04:00
Scott Richmond
69118ed4e0 Use ex-info everywhere 2022-04-03 20:08:28 -04:00
Scott Richmond
b8e7777841 Called KW enforce one arg 2022-04-03 19:13:54 -04:00
Scott Richmond
f93eacf5f5 Fix keyword function calling 2022-04-03 17:03:22 -04:00
Scott Richmond
7a4034d920 Keywords can be called as passed fns 2022-04-03 15:39:41 -04:00
Scott Richmond
9fd69b57f3 Don't throw in test code 2022-04-03 14:49:20 -04:00
Scott Richmond
31d407c181 Interpret structs 2022-04-03 14:48:23 -04:00
Scott Richmond
7c4751dda8 Actually scan structs. 2022-04-03 14:48:08 -04:00
Scott Richmond
21af8acf2e Parse structs 2022-04-03 14:34:30 -04:00
Scott Richmond
6b9fc590a6 Add struct to parse-expr 2022-04-03 14:25:14 -04:00
Scott Richmond
33a086b312 Copy-paste struct parse from hashmap 2022-04-03 14:24:28 -04:00
Scott Richmond
811a57be1c Add struct to scanner; new reserved words. 2022-04-03 14:22:01 -04:00
Scott Richmond
98530b0f0f Add a static analysis note 2022-03-20 20:15:51 -04:00
Scott Richmond
41e9b4d9dd Some light speed testing: Ludus is 1000x slower than Clojure. 2022-03-20 20:15:43 -04:00
Scott Richmond
52f357765d Light cleanup 2022-03-20 19:39:01 -04:00
Scott Richmond
b7d49cbf5d Test recursion. It works! 2022-03-20 19:35:41 -04:00
Scott Richmond
272951a411 Add several functions to the prelude 2022-03-20 19:35:29 -04:00
Scott Richmond
3f8207fa41 Add ns for ludus data 2022-03-20 19:35:16 -04:00
Scott Richmond
9ce1f29977 Update notes 2022-03-20 19:25:20 -04:00
Scott Richmond
b5a5be9dd0 Change tuple representation 2022-03-20 19:21:55 -04:00
Scott Richmond
d02bc05209 Finally: add functions! 2022-03-20 19:17:15 -04:00
Scott Richmond
3b1f2460a8 Write basic test case 2022-03-20 18:32:31 -04:00
Scott Richmond
1f19a0ea1e Decide against refactoring beyond cljfmt, write basic fn parser 2022-03-20 18:30:30 -04:00
Scott Richmond
bfef7a8e66 cljfmt all the things 2022-03-20 17:58:38 -04:00
Scott Richmond
59ccc00963 Add cljfmt to lein project 2022-03-20 17:41:26 -04:00
Scott Richmond
5da8090d2b Reindent things; start work on named fns; decide to refactor things 2022-03-20 17:37:44 -04:00
Scott Richmond
0f64307ba8 Add happy path for anon simple fns 2022-03-20 17:01:21 -04:00
Scott Richmond
3fde297e07 Import prelude 2022-03-20 16:41:02 -04:00
Scott Richmond
14b6e31081 Move prelude functions out to prelude ns 2022-03-20 16:40:50 -04:00
Scott Richmond
1d9559402c Add panic 2022-03-20 16:33:59 -04:00
Scott Richmond
0f2e30a8de Update more notes 2022-03-20 16:15:27 -04:00
Scott Richmond
d924b5fa6f Update notes 2022-03-20 16:15:07 -04:00
Scott Richmond
85f7bc8c08 Add hashmaps, synthetics, native functions 2022-03-20 15:46:49 -04:00
Scott Richmond
11a51cf708 Interpret match expressions 2022-03-20 14:14:15 -04:00
Scott Richmond
069e3b4a7b Parse multi-clause match expressions 2022-03-20 13:28:05 -04:00
Scott Richmond
e4e984bacd Parse single-clause match expressions 2022-03-19 20:52:13 -04:00
Scott Richmond
584ddaf705 Skip comments, the dumb solution. 2022-03-19 19:21:29 -04:00
Scott Richmond
2a099298f0 Reindent things 2022-03-19 19:09:21 -04:00
Scott Richmond
90f6fa8b19 Few extra points 2022-03-19 19:08:55 -04:00
Scott Richmond
270f6100b7 Add a stub of a static analyzer. 2022-03-19 19:08:12 -04:00
Scott Richmond
7a2fabd131 Clean up notes to reflect current state of affairs 2022-03-19 19:07:46 -04:00
Scott Richmond
35d398c2e0 Add hashmap parsing. It's basic. 2022-03-19 19:01:32 -04:00
Scott Richmond
8368d6362d Add tuple patterns & matching 2022-03-19 18:23:15 -04:00
Scott Richmond
a4c5927ec4 Notes to selves 2022-03-19 17:30:41 -04:00
Scott Richmond
3f0feab83b Start work on the interpreter 2022-03-19 17:28:48 -04:00
Scott Richmond
5baa4fe3ba Expose parse function 2022-03-19 17:28:30 -04:00
Scott Richmond
fc035eb1ba Simplify forward declarations 2022-03-19 17:28:18 -04:00
Scott Richmond
e890c44fbd Improve and expose parse-script 2022-03-19 17:26:51 -04:00
Scott Richmond
4c186bae3a Better syncing in collection literals 2022-03-19 17:26:13 -04:00
Scott Richmond
faba605329 if* -> if, let* -> let 2022-03-19 12:30:59 -04:00
Scott Richmond
85d4f99030 Delete old/broken versions of let & if 2022-03-19 12:30:05 -04:00
Scott Richmond
3d080403dc Reindent--clean up 2022-03-19 12:29:15 -04:00
Scott Richmond
aa7f2ac8fc Get if-exprs fully wired up 2022-03-19 12:28:13 -04:00
Scott Richmond
e616dcd453 Fix bug with empty blocks 2022-03-19 12:27:47 -04:00
Scott Richmond
37b9560ee3 Fix typo 2022-03-19 12:27:30 -04:00
Scott Richmond
83392e3ac4 Start work on if errors 2022-03-13 19:43:57 -04:00
Scott Richmond
a05feea32a Notes on getting to a working if 2022-03-12 14:42:16 -05:00
Scott Richmond
21bef073d0 Fully fix lets; re-discover unfinished business with ifs 2022-03-12 14:39:27 -05:00
Scott Richmond
1a1f720358 Fix infinite hang bug with panics 2022-03-12 13:30:33 -05:00
Scott Richmond
0e4673a1a1 Make some progress notes 2022-02-21 22:30:27 -05:00
Scott Richmond
f96ec45d1b Improve parse-script; start work on let and pattern 2022-02-21 15:38:47 -05:00
Scott Richmond
8ec432c7a0 Tidy up expr-sync 2022-02-21 15:03:32 -05:00
Scott Richmond
33fad7656c Improve parse-block 2022-02-21 14:57:23 -05:00
Scott Richmond
3357248b3c Add do and import reserved words 2022-02-21 14:54:14 -05:00
Scott Richmond
4edfbcb04d Improve parse-set; do not swallow errors on unterminated collections 2022-02-21 14:03:58 -05:00
Scott Richmond
d23a561eda Improve parse-list (based on parse-tuple) 2022-02-21 13:56:46 -05:00
Scott Richmond
61b25ca22d Clean up parse-tuple 2022-02-21 13:46:41 -05:00
Scott Richmond
e6b204a54a Better error handling, in script & tuple 2022-02-21 13:43:30 -05:00
Scott Richmond
9414109312 Finally get error handling right? 2022-02-21 13:16:22 -05:00
Scott Richmond
a4cecc3d01 Make some progress on good parser errors 2022-02-20 19:33:24 -05:00
Scott Richmond
3fde2cb52b Atoms keep tokens. Add panic mode, begin better errors. 2022-02-20 18:40:16 -05:00
Scott Richmond
2751b7428f Whitespace should not return errors 2022-02-20 18:39:18 -05:00
Scott Richmond
23e2e95e77 Better errors? WIP 2022-02-19 14:34:07 -05:00
Scott Richmond
40367d7e57 Update number lexing comments 2022-02-19 14:20:09 -05:00
Scott Richmond
5d78d5f823 Tonksy-fmt 2022-02-18 17:49:02 -05:00
Scott Richmond
d6a547bdcb Odds & ends 2022-02-18 17:25:36 -05:00
Scott Richmond
a71db0e640 Delete prints; next -> peek; remove loop in parse-expr 2022-02-15 22:48:36 -05:00
Scott Richmond
96b30ece05 if exprs; add expect, accept, accept-many helpers 2022-02-15 19:45:39 -05:00
Scott Richmond
a35da1498e Remove expect fns; declare parse-word. 2022-02-15 18:27:22 -05:00
Scott Richmond
531c85054c Stub of let works. 2022-02-15 13:24:09 -05:00
Scott Richmond
6b61a10349 Stub of let 2022-02-15 13:17:01 -05:00
Scott Richmond
84663d0952 Add = (as replacement for <- now that we have let) 2022-02-15 13:16:46 -05:00
Scott Richmond
25da37d093 Start, but do not finish, work on let expressions 2022-02-14 20:09:10 -05:00
Scott Richmond
3fa280402c Enforce non-empty blocks 2022-02-14 18:29:29 -05:00
Scott Richmond
dd47244b78 Fix bug in script & block 2022-02-14 18:13:56 -05:00
Scott Richmond
05f55e6a84 Working synthetic parsing; simplify 2022-02-14 18:05:29 -05:00
Scott Richmond
98b147f5b8 Factor out keyword parsing, parse bare words 2022-02-14 17:32:29 -05:00
48 changed files with 75438 additions and 897 deletions

29
.gitignore vendored
View File

@ -1,14 +1,16 @@
/target
/classes
/checkouts
target/stale
target/js
target/classes
classes/
checkouts/
profiles.clj
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
/.prepl-port
.lein-*
.nrepl-port
.prepl-port
.hgignore
.hg/
.clj-condo/
@ -17,3 +19,18 @@ pom.xml.asc
.clj-kondo/
.cpcache/
**/.DS_Store
sandbox
ludus.sublime-workspace
ludus
!src/ludus
out/
node_modules/
.shadow-cljs
.cljs_node_repl/
.helix/
target/repl-port
.repl-buffer
.repl-buffer.janet
.env
src/jpm_tree
.zig-cache

View File

@ -1,24 +0,0 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
## [Unreleased]
### Changed
- Add a new arity to `make-widget-async` to provide a different widget shape.
## [0.1.1] - 2021-10-23
### Changed
- Documentation on how to make the widgets.
### Removed
- `make-widget-sync` - we're all async, all the time.
### Fixed
- Fixed widget maker to keep working when daylight savings switches over.
## 0.1.0 - 2021-10-23
### Added
- Files from the new template.
- Widget maker public API - `make-widget-sync`.
[Unreleased]: https://sourcehost.site/your-name/cludus/compare/0.1.1...HEAD
[0.1.1]: https://sourcehost.site/your-name/cludus/compare/0.1.0...0.1.1

858
LICENSE
View File

@ -1,280 +1,676 @@
Eclipse Public License - v 2.0
Ludus is copyrighted by Scott Richmond, (c) 2022, and distributed under the GNU GPL:
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE
PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION
OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
1. DEFINITIONS
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
"Contribution" means:
Preamble
a) in the case of the initial Contributor, the initial content
Distributed under this Agreement, and
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
b) in the case of each subsequent Contributor:
i) changes to the Program, and
ii) additions to the Program;
where such changes and/or additions to the Program originate from
and are Distributed by that particular Contributor. A Contribution
"originates" from a Contributor if it was added to the Program by
such Contributor itself or anyone acting on such Contributor's behalf.
Contributions do not include changes or additions to the Program that
are not Modified Works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
"Contributor" means any person or entity that Distributes the Program.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
"Licensed Patents" mean patent claims licensable by a Contributor which
are necessarily infringed by the use or sale of its Contribution alone
or when combined with the Program.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
"Program" means the Contributions Distributed in accordance with this
Agreement.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
"Recipient" means anyone who receives the Program under this Agreement
or any Secondary License (as applicable), including Contributors.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
"Derivative Works" shall mean any work, whether in Source Code or other
form, that is based on (or derived from) the Program and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
"Modified Works" shall mean any work in Source Code or other form that
results from an addition to, deletion from, or modification of the
contents of the Program, including, for purposes of clarity any new file
in Source Code form that contains any contents of the Program. Modified
Works shall not include works that contain only declarations,
interfaces, types, classes, structures, or files of the Program solely
in each case in order to link to, bind by name, or subclass the Program
or Modified Works thereof.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
"Distribute" means the acts of a) distributing or b) making available
in any manner that enables the transfer of a copy.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
"Source Code" means the form of a Program preferred for making
modifications, including but not limited to software source code,
documentation source, and configuration files.
The precise terms and conditions for copying, distribution and
modification follow.
"Secondary License" means either the GNU General Public License,
Version 2.0, or any later versions of that license, including any
exceptions or additional permissions as identified by the initial
Contributor.
TERMS AND CONDITIONS
2. GRANT OF RIGHTS
0. Definitions.
a) Subject to the terms of this Agreement, each Contributor hereby
grants Recipient a non-exclusive, worldwide, royalty-free copyright
license to reproduce, prepare Derivative Works of, publicly display,
publicly perform, Distribute and sublicense the Contribution of such
Contributor, if any, and such Derivative Works.
"This License" refers to version 3 of the GNU General Public License.
b) Subject to the terms of this Agreement, each Contributor hereby
grants Recipient a non-exclusive, worldwide, royalty-free patent
license under Licensed Patents to make, use, sell, offer to sell,
import and otherwise transfer the Contribution of such Contributor,
if any, in Source Code or other form. This patent license shall
apply to the combination of the Contribution and the Program if, at
the time the Contribution is added by the Contributor, such addition
of the Contribution causes such combination to be covered by the
Licensed Patents. The patent license shall not apply to any other
combinations which include the Contribution. No hardware per se is
licensed hereunder.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
c) Recipient understands that although each Contributor grants the
licenses to its Contributions set forth herein, no assurances are
provided by any Contributor that the Program does not infringe the
patent or other intellectual property rights of any other entity.
Each Contributor disclaims any liability to Recipient for claims
brought by any other entity based on infringement of intellectual
property rights or otherwise. As a condition to exercising the
rights and licenses granted hereunder, each Recipient hereby
assumes sole responsibility to secure any other intellectual
property rights needed, if any. For example, if a third party
patent license is required to allow Recipient to Distribute the
Program, it is Recipient's responsibility to acquire that license
before distributing the Program.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
d) Each Contributor represents that to its knowledge it has
sufficient copyright rights in its Contribution, if any, to grant
the copyright license set forth in this Agreement.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
e) Notwithstanding the terms of any Secondary License, no
Contributor makes additional grants to any Recipient (other than
those set forth in this Agreement) as a result of such Recipient's
receipt of the Program under the terms of a Secondary License
(if permitted under the terms of Section 3).
A "covered work" means either the unmodified Program or a work based
on the Program.
3. REQUIREMENTS
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
3.1 If a Contributor Distributes the Program in any form, then:
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
a) the Program must also be made available as Source Code, in
accordance with section 3.2, and the Contributor must accompany
the Program with a statement that the Source Code for the Program
is available under this Agreement, and informs Recipients how to
obtain it in a reasonable manner on or through a medium customarily
used for software exchange; and
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
b) the Contributor may Distribute the Program under a license
different than this Agreement, provided that such license:
i) effectively disclaims on behalf of all other Contributors all
warranties and conditions, express and implied, including
warranties or conditions of title and non-infringement, and
implied warranties or conditions of merchantability and fitness
for a particular purpose;
1. Source Code.
ii) effectively excludes on behalf of all other Contributors all
liability for damages, including direct, indirect, special,
incidental and consequential damages, such as lost profits;
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
iii) does not attempt to limit or alter the recipients' rights
in the Source Code under section 3.2; and
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
iv) requires any subsequent distribution of the Program by any
party to be under a license that satisfies the requirements
of this section 3.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
3.2 When the Program is Distributed as Source Code:
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
a) it must be made available under this Agreement, or if the
Program (i) is combined with other material in a separate file or
files made available under a Secondary License, and (ii) the initial
Contributor attached to the Source Code the notice described in
Exhibit A of this Agreement, then the Program may be made available
under the terms of such Secondary Licenses, and
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
b) a copy of this Agreement must be included with each copy of
the Program.
The Corresponding Source for a work in source code form is that
same work.
3.3 Contributors may not remove or alter any copyright, patent,
trademark, attribution notices, disclaimers of warranty, or limitations
of liability ("notices") contained within the Program from any copy of
the Program which they Distribute, provided that Contributors may add
their own appropriate notices.
2. Basic Permissions.
4. COMMERCIAL DISTRIBUTION
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
Commercial distributors of software may accept certain responsibilities
with respect to end users, business partners and the like. While this
license is intended to facilitate the commercial use of the Program,
the Contributor who includes the Program in a commercial product
offering should do so in a manner which does not create potential
liability for other Contributors. Therefore, if a Contributor includes
the Program in a commercial product offering, such Contributor
("Commercial Contributor") hereby agrees to defend and indemnify every
other Contributor ("Indemnified Contributor") against any losses,
damages and costs (collectively "Losses") arising from claims, lawsuits
and other legal actions brought by a third party against the Indemnified
Contributor to the extent caused by the acts or omissions of such
Commercial Contributor in connection with its distribution of the Program
in a commercial product offering. The obligations in this section do not
apply to any claims or Losses relating to any actual or alleged
intellectual property infringement. In order to qualify, an Indemnified
Contributor must: a) promptly notify the Commercial Contributor in
writing of such claim, and b) allow the Commercial Contributor to control,
and cooperate with the Commercial Contributor in, the defense and any
related settlement negotiations. The Indemnified Contributor may
participate in any such claim at its own expense.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
For example, a Contributor might include the Program in a commercial
product offering, Product X. That Contributor is then a Commercial
Contributor. If that Commercial Contributor then makes performance
claims, or offers warranties related to Product X, those performance
claims and warranties are such Commercial Contributor's responsibility
alone. Under this section, the Commercial Contributor would have to
defend claims against the other Contributors related to those performance
claims and warranties, and if a court requires any other Contributor to
pay any damages as a result, the Commercial Contributor must pay
those damages.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
5. NO WARRANTY
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT
PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF
TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE. Each Recipient is solely responsible for determining the
appropriateness of using and distributing the Program and assumes all
risks associated with its exercise of rights under this Agreement,
including but not limited to the risks and costs of program errors,
compliance with applicable laws, damage to or loss of data, programs
or equipment, and unavailability or interruption of operations.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
6. DISCLAIMER OF LIABILITY
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT
PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS
SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST
PROFITS), 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 OR DISTRIBUTION OF THE PROGRAM OR THE
EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
4. Conveying Verbatim Copies.
7. GENERAL
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
If any provision of this Agreement is invalid or unenforceable under
applicable law, it shall not affect the validity or enforceability of
the remainder of the terms of this Agreement, and without further
action by the parties hereto, such provision shall be reformed to the
minimum extent necessary to make such provision valid and enforceable.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
If Recipient institutes patent litigation against any entity
(including a cross-claim or counterclaim in a lawsuit) alleging that the
Program itself (excluding combinations of the Program with other software
or hardware) infringes such Recipient's patent(s), then such Recipient's
rights granted under Section 2(b) shall terminate as of the date such
litigation is filed.
5. Conveying Modified Source Versions.
All Recipient's rights under this Agreement shall terminate if it
fails to comply with any of the material terms or conditions of this
Agreement and does not cure such failure in a reasonable period of
time after becoming aware of such noncompliance. If all Recipient's
rights under this Agreement terminate, Recipient agrees to cease use
and distribution of the Program as soon as reasonably practicable.
However, Recipient's obligations under this Agreement and any licenses
granted by Recipient relating to the Program shall continue and survive.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
Everyone is permitted to copy and distribute copies of this Agreement,
but in order to avoid inconsistency the Agreement is copyrighted and
may only be modified in the following manner. The Agreement Steward
reserves the right to publish new versions (including revisions) of
this Agreement from time to time. No one other than the Agreement
Steward has the right to modify this Agreement. The Eclipse Foundation
is the initial Agreement Steward. The Eclipse Foundation may assign the
responsibility to serve as the Agreement Steward to a suitable separate
entity. Each new version of the Agreement will be given a distinguishing
version number. The Program (including Contributions) may always be
Distributed subject to the version of the Agreement under which it was
received. In addition, after a new version of the Agreement is published,
Contributor may elect to Distribute the Program (including its
Contributions) under the new version.
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
Except as expressly stated in Sections 2(a) and 2(b) above, Recipient
receives no rights or licenses to the intellectual property of any
Contributor under this Agreement, whether expressly, by implication,
estoppel or otherwise. All rights in the Program not expressly granted
under this Agreement are reserved. Nothing in this Agreement is intended
to be enforceable by any entity that is not a Contributor or Recipient.
No third-party beneficiary rights are created under this Agreement.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
Exhibit A - Form of Secondary Licenses Notice
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
"This Source Code may also be made available under the following
Secondary Licenses when the conditions for such availability set forth
in the Eclipse Public License, v. 2.0 are satisfied: GNU General Public
License as published by the Free Software Foundation, either version 2
of the License, or (at your option) any later version, with the GNU
Classpath Exception which is available at
https://www.gnu.org/software/classpath/license.html."
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
Simply including a copy of this Agreement, including this Exhibit A
is not sufficient to license the Source Code under Secondary Licenses.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to
look for such a notice.
6. Conveying Non-Source Forms.
You may add additional accurate notices of copyright ownership.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

101
README.md
View File

@ -1,22 +1,93 @@
# cludus
![Ludus logo](logo.png)
## Ludus: A friendly, dynamic, functional language
Ludus is a scripting programming language that is designed to be friendly, dynamic, and functional.
A Clojure library designed to ... well, that part is up to you.
This repo currently contains a work-in-progress implementation of an interpreter for the Ludus programming language, using [Janet](https://janet-lang.org) as a host language.
Ludus is part of the [_Thinking with Computers_ project](https://alea.ludus.dev/twc/), run by Scott Richmond at the University of Toronto, with collaborator Matt Nish-Lapidus; Bree Lohman and Mynt Marsellus are the RAs for the project. Ludus is our research language, which aspires to be a free translation of Logo for the 2020s.
## Usage
Here are our design goals:
FIXME
#### Friendly
Ludus, like Logo, is meant to be a teaching language, often for students who don't think of themselves as "computer people." Our intended audience are humanities and art people at the university level (undergrads, grads, faculty). Everything is kept as simple as possible, but no simpler. Everything is consistent as possible. We aspire to the best error messages we can muster, which is important for a language to be teachable. That means being as strict as we can muster, _in order to be friendlier_.
## License
Our current development target is Ludus on the web: https://web.ludus.dev. That wires what we do on the langauge interpreter (here in this repo) to a web frontend.
Copyright © 2021 FIXME
Naturally, it starts with Logo's famed turtle graphics.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0.
#### Dynamic
Statically typed programming languages generally give more helpful error messages than dynamic ones, but learning a type system (even one with robust type inference) requires learning two parallel systems: the type system and the expression system (well, and the pattern system). Type systems only really make sense once you've learned why they're necessary. And their benefits seem (to us, anyway) to be largely necessary when writing long-lived, maintainable, multi-author code. Ludus code is likely to be one-off, expressive, and single-authored.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the Eclipse
Public License, v. 2.0 are satisfied: GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or (at your
option) any later version, with the GNU Classpath Exception which is available
at https://www.gnu.org/software/classpath/license.html.
To stay friendly, Ludus is dynamic. But: despite the dynamism, we aim to be as strict as possible. Certainly, we want to avoid the type conversion shenanigans of a language like JavaScript.
#### Functional
Ludus is emphatically functional: it uses functions for just about everything. This is both because your humble PI had his world reordered when he learned his first functional language (Elixir), and because the research into Logo and the programming cultures of MIT in the 1970s revolve around extremely functional Lisp code (i.e., Scheme). Logo is a weird little language, but it is a descendant of Lisp. So is Ludus.
Also, we believe that Ludus's immutable bindings and persistent or immutable data structures and careful approach to manipulating state lead to a lot of good pedagogical results. Learning a programming language involves learning how to model what's going on inside the computer; Ludus, we think, makes that both simpler and easier.
If you're looking for cognate languages, Ludus takes a _lot_ of design inspiration from Clojure and Elixir (which itself took a lot from Clojure). (The current--quick, dirty, and slow--version of Ludus is written in [Janet](https://janet-lang.org).) Clojure and Elixir are great! If you're asking why you should use Ludus instead of them, you're already at the point where you should be using them. Ludus is, maybe, for the people whom you'd like to work with in 5 years at your Pheonix shop (but even then, probably not).
### Status
Pre-alpha, still under active development. Lots of things change all the time.
The current version of Ludus is a pure function that runs in JavaScript as a WASM blob. We have plans for more and better things.
### Use
Current emphasis is on the web version: https://web.ludus.dev.
### Main features
* Expression-oriented: everything returns a value
* Pattern matching in all the places
* No operators: everything is called as a function
* Easy-peasy partial application with placeholders
* Function pipelines
* Persistent or immutable data structures
* Careful, explicit state management using `box`es
* Clean, concise, expressive syntax
* Value-based equality; only functions are reference types
#### Under construction
* Actor-model style concurrency.
* Faster, bytecode-based VM written in a systems language, for better performance.
* Performant persistent, immutable data structures, à la Clojure.
### `Hello, world!`
Ludus is a scripting language. At current it does not have a good REPL. Our aim is to get interactive coding absolutely correct, and our efforts in [ludus-web](https://github.com/thinking-with-computers/ludus-web) are currently under way to surface the right interactivity models for Ludus.
Either:
```
"Hello, world!"
```
`=> "Hello, world!"`
Ludus scripts (and blocks) simply return their last expression; this script returns the bare string and exits.
Or:
```
print! ("Hello, world!")
```
```
=> Hello, world!
=> :ok
```
Here, we use the `print!` function, which sends a string to a console (`stdout` on Unix, or a little console box on the web). Because `print!` returns the keyword `:ok` when it completes, that is the result of the last expression in the script--and so Ludus also prints this.
### Some code
Fibonacci numbers:
```
& fibonacci!, with multi-clause fns/pattern matching
fn fib {
"Returns the nth fibonacci number."
(1) -> 1
(2) -> 1
(n) -> add (
fib (sub (n, 1))
fib (sub (n, 2)))
}
fib (10) &=> 55
```
### More on Ludus
See the [language reference](language.md) and [the documentation for the prelude](prelude.md).

130
build/driver.cpp Normal file
View File

@ -0,0 +1,130 @@
#include <cstdint>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <string>
#include <stdio.h>
#include "janet.h"
using std::string;
// set all our exported Janet functions as null pointers
static JanetFunction *janet_ludus = NULL;
// these let us look up functions
Janet env_lookup(JanetTable *env, const char *name) {
Janet out;
janet_resolve(env, janet_csymbol(name), &out);
return out;
}
JanetFunction *env_lookup_function(JanetTable *env, const char *name) {
Janet value = env_lookup(env, name);
if (!janet_checktype(value, JANET_FUNCTION)) {
janet_panicf("expected %s to be a function, got %q\n", name, value);
}
return janet_unwrap_function(value);
}
// this lets us call a function
bool call_fn(JanetFunction *fn, int argc, const Janet *argv, Janet *out) {
JanetFiber *fiber = NULL;
if (janet_pcall(fn, argc, argv, out, &fiber) == JANET_SIGNAL_OK) {
return true;
} else {
janet_stacktrace(fiber, *out);
return false;
}
}
// this is darkish magic, reads an embedded file
// do not fuck with this, fellas
unsigned char *read_file(const char *filename, size_t *length) {
size_t capacity = 2 << 17;
unsigned char *src = (unsigned char *)malloc(capacity * sizeof(unsigned char));
assert(src);
size_t total_bytes_read = 0;
FILE *file = fopen(filename, "r");
assert(file);
size_t bytes_read;
do {
size_t remaining_capacity = capacity - total_bytes_read;
if (remaining_capacity == 0) {
capacity <<= 1;
src = (unsigned char*)realloc(src, capacity * sizeof(unsigned char));
assert(src);
remaining_capacity = capacity - total_bytes_read;
}
bytes_read = fread(&src[total_bytes_read], sizeof(unsigned char), remaining_capacity, file);
total_bytes_read += bytes_read;
} while (bytes_read > 0);
fclose(file);
*length = total_bytes_read;
return src;
}
// finally, getting a string back
// this is our result type
struct StringResult {
string value;
};
// this is our result constructor
// Janet's getcstring resturns const char*
StringResult string_result(const char* cstr) {
// ...which we have to cast to a std::string
return (StringResult) {.value = (string) cstr };
}
// and this is a function that takes and returns a string
// it returns a StringResult, tho
StringResult ludus(string source) {
Janet result;
const Janet args[1] = {janet_cstringv(source.c_str())};
call_fn(janet_ludus, 1, args, &result);
// get the cstring in the result
// the 0 passed here is the index in the result of the string
const char* cstr = janet_getcstring(&result, 0);
// return a constructed StringResult
return string_result(cstr);
}
// This function sets up our Janet interpreter, and fixes the null pointers
EMSCRIPTEN_KEEPALIVE
int main() {
janet_init(); // start the interpreter
JanetTable *core_env = janet_core_env(NULL); // get a core env
JanetTable *lookup = janet_env_lookup(core_env); // and get an env table
// load the janet image into memory
// note that the image is hardcoded here
size_t image_length;
unsigned char *image = read_file("ludus.jimage", &image_length);
// load the image into the Janet environment
Janet env = janet_unmarshal(image, image_length, 0, lookup, NULL);
if(!janet_checktype(env, JANET_TABLE)) {
janet_panicf("invalid image %q", env);
}
// fix the null pointers, as above
// note that the bound symbols are just the normal fn names
// no namespacing
janet_ludus = env_lookup_function(janet_unwrap_table(env), "ludus");
janet_gcroot(janet_wrap_function(janet_ludus));
}
// these bindings are exported into javascript
EMSCRIPTEN_BINDINGS(module) {
using namespace emscripten;
// these are the functions that will be available
function("ludus", &ludus, allow_raw_pointers());
// we also want a wrapper for our StringResult
// we won't access it directly, but emcc makes it nice
value_object<StringResult>("StringResult")
.field("value", &StringResult::value);
}

51563
build/janet.c Normal file

File diff suppressed because it is too large Load Diff

2277
build/janet.h Normal file

File diff suppressed because it is too large Load Diff

13
build/justfile Normal file
View File

@ -0,0 +1,13 @@
build:
# the complex emscripten invocation
# note we have the stack size set to 1024*1024 (1 MB)
emcc \
-o out.mjs \
janet.c driver.cpp \
--embed-file ludus.jimage \
-lembind \
-s "EXPORTED_FUNCTIONS=['_main']" \
-s EXPORT_ES6 \
-s ALLOW_MEMORY_GROWTH=1 \
-s STACK_SIZE=1048576 \
-s MODULARIZE

BIN
build/ludus.jimage Normal file

Binary file not shown.

9
build/ludus.mjs Normal file
View File

@ -0,0 +1,9 @@
import init from "./out.mjs"
const mod = await init()
export function run (source) {
const result = mod.ludus(source).value
console.log(result)
return JSON.parse(result)
}

7170
build/out.mjs Normal file

File diff suppressed because it is too large Load Diff

BIN
build/out.wasm Executable file

Binary file not shown.

3
build/test.mjs Normal file
View File

@ -0,0 +1,3 @@
import {run} from "./ludus.mjs"
console.log(run(`let foo = 42; "{foo} bar"`))

View File

@ -1,7 +0,0 @@
{
:dependencies [nrepl "0.9.0"]
:aliases {:nREPL
{:extra-deps
{nrepl/nrepl {:mvn/version "0.9.0"}}}}
}

View File

@ -1,3 +0,0 @@
# Introduction to cludus
TODO: write [great documentation](http://jacobian.org/writing/what-to-write/)

31
justfile Normal file
View File

@ -0,0 +1,31 @@
# open a janet repl in a different os window
repl:
kitten @ launch --type=os-window --allow-remote-control --cwd=current --title=hx_repl:ludus --keep-focus
kitten @ send-text -m "title:hx_repl:ludus" "janet -s\n"
# restart the repl server
restart:
kitten @ send-text -m "title:hx_repl:ludus" "\04"
kitten @ send-text -m "title:hx_repl:ludus" "janet -s\n"
# send what's selected to the repl and evaluate it
eval:
sd "$" "\n" | sd "\n\n" "\n" | kitten @ send-text -m "title:hx_repl:ludus" --stdin
# get documentation for a symbol in janet/clojure
doc:
sd "$" "\n" | sd "\n\n" "\n" | xargs -I _ echo "(doc " _ ")" | kitten @ send-text -m "title:hx_repl:ludus" --stdin
# publish to npm (did you build things first?)
publish:
npm version patch
npm publish
# build the ludus jimage
build:
rm build/out.mjs
rm build/out.wasm
rm build/ludus.jimage
janet -c src/ludus.janet build/ludus.jimage
cd build && just build
git commit -am "build"

513
language.md Normal file
View File

@ -0,0 +1,513 @@
# Ludus language reference
This is not intended for beginners, but to be a language overview for experienced programmers. That said, it may help beginners orient themselves in the language.
## Comments
Ludus's comment character is `&`. Anything after an ampersand on a line is ignored. There are no multiline comments.
## Atomic values
Ludus has four types of atomic values.
### `nil`
`nil` is Ludus's representation of nothing. In the grand Lisp tradition, Ludus can, and occasionally does, use `nil`-punning. Its type is `:nil`.
### Booleans
`true` and `false`. That said, in all conditional constructs, `nil` and `false` are "falsy," and everything else is "truthy." Their type is `:boolean`.
### Numbers
Ludus has numbers, which are IEEE-754 64-bit floats. Numbers are more complicated than you think, probably.
Number literals in Ludus are either integers or decimal floating point numbers, e.g. `32.34`, `42`, `-0.23`. Underscores in numbers are ignored, and can be used to separate long numbers, e.g. `1_234_567_890`.
Numbers' type is `:number`.
### Keywords
Ludus keywords begin with a colon and a letter, e.g. `:keyword`. Types are represented as keywords. Some functions take an optional units argument as a keyword, e.g. `:radians`. Keywords are also used as keys for associative collections. Keywords' type is `:keyword`.
Keywords must begin with an ASCII upper- or lower-case letter, and can then include any letter character, as well as `_`, `/`, `!`, `?`, and `*`.
## Strings
Ludus strings are UTF-8 strings, and only use double quotes. Strings may be multiline. For example, this is a string: `"foo"`. So is this:
```
"foo
bar baz"
```
Strings use backslashes for escapes, including `\n` for newline, `\t` for tab, `\"` for a double quote, and `\{` for an open curly brace (see below on interpolation).
Strings' type is `:string`.
### String interpolation
Strings may also insert a string representation of any Ludus value that is bound to a name, by inserting that name in curly braces. To wit,
```
let foo = :foo
let bar = 42
let baz = [1, 2, 3]
"{foo} {bar} {baz}" &=> ":foo 42 1, 2, 3"
```
Interpolations may _not_ be arbitrary expressions: only bound names may be used in interpolations.
## Collections
Ludus has a few different types of collections, in increasing order of complexity: tuples, lists, sets, dicts, and packages. All collections are immutable.
#### Separators
In all collection literals, members are written with a separator between them. On the same line, use a comma; or a newline will also separate elements. You may use as many separators as you wish at any point inside a collection or pattern. `(,,,,,,,3,,4,,,,,,)` and `(3, 4)` are the same value.
#### Efficiency
At the current moment, Ludus collections are all copy-on-write; this means that Ludus is _not_ performant with large collections. Eventually, Ludus will have Clojure-style persistent, immutable collections.
### Tuples
Tuples are fully-immutable, ordered collections of any kinds of values, delimited by parentheses, e.g. `(1, :a, "foo")`. At current, they have no length limit (although they eventually will). Unlike in some languages, tuples can be empty or contain a single element: `()` and `(:foo)` are both just fine. Tuples largely cannot be manipulated functionally; they must be written as literals and unpacked using pattern matching. They can, however, be converted to lists, either through pattern matching or the `list` function. Their type is `:tuple`.
### Lists
Lists are persistent and immutable ordered collections of any kinds of values, delimited by square braces, e.g. `[1, :a, "foo"]`. Their type is `:list`.
Lists may be combined using splats, written with ellipses, e.g., `[...foo, ...bar]`.
### Sets
Sets are persistent and immutable unordered collections of any kinds of values, which can only contain one instance of any given value. They are written similarly to ordered collections: `${1, :a, "foo"}`. Their type is `:set`.
### Dictionaries, or dicts
Dicts are persistent and immutable associative collections of any kinds of values. Dicts use keywords as keys (and cannot use any other kind of Ludus value as a key, not even strings), but can store any values. Dict literals are written as keyword-value pairs: `#{:a 1, :b false}`. Single words may be used as a shorthand for a key-value pair. Accessing a key that holds no value returns `nil`. Their type is `:dict`.
### Packages
Packages are immutable collections of bindings. They may only be described at the top level of a script, and their names must begin with a capital letter. Accessing a key that has no value on a package results in a validation error. They may not be accessed using functions, but only direct keyword access. Their type is `:pkg`.
They are written with the form `pkg`, then a package name, beginning with a capital letter, that will be bound as their name, and then an associative structure (pairs or word shorthands), delimited by `{}`, e.g.:
```
pkg Foo {
:bar "bar"
:baz 42
quux
}
```
### Working with collections
Ludus names are bound permanently and immutably. Collections are immutable. How do you add something to a list or a dict? How do you get things out of them?
Ludus provides functions that allow working with persistent collections. They're detailed in [the Prelude](prelude.md). That said, all functions that modify collections take a collection and produce the modified collection _as a return value_, without changing the original collection. E.g., `append ([1, 2, 3], 4)` will produce `[1, 2, 3, 4]`, but the original list is unchanged. (For dicts, the equivalent is `assoc`.)
## Expressions
Ludus is an expression-based language: all forms in the language are expressions and return values, except `panic!`. That said, not all expressions may be used everywhere.
### Terminating expressions
Expressions in scripts and blocks are terminated by a newline or a semicolon. In compound forms, like, `if`, the terminator comes after the `else` expression.
In forms with multiple clauses surrounded by curly braces (i.e., function bodies, `match`, `when`, etc.), you may separate clauses with semicolons as well as newlines.
### Toplevel expressions
Some expressions may only be used in the "top level" of a script. Because they are the toplevel, they are assured to be statically knowable. These include: `pkg`, `ns`, `use`, `import`, and `test`. (NB: not all of these are yet implemented.)
### Non-binding expressions
Some forms may take any expression that does _not_ [bind a name](#words-and-bindings), for example, any entry in a collection, or the right-hand side of a `let` binding. This is because binding a name in some positions is ambiguous, or nonsensical, or leads to unwarranted complications.
### Simple expressions
Many compound forms will only accept "simple" expressions. Formally, simple expressions are either literal (atomic, string, or collection literals) or synthetic expressions. They are expressions which do not take sub-expressions: no `if`, `when`, `match`, etc. (`do` expressions are currently not simple, but that may be revised.)
## Words and bindings
Ludus uses _words_ to bind values to names. Words must start with a lower case ASCII letter, and can subsequently include any letter character (modulo backing character encoding), as well as , `_`, `/`, `?`, `!`, and `*`.
Ludus binds values to names immutably and permanently: no name in the same scope may ever be re-bound to a different value. (Although see [boxes](#boxes-and-state), below.
Attempting to use an unbound name (a word that has not had a value bound to it) will result in a validation error, and the script will not run.
### `let` bindings: a very short introduction
Ludus's basic binding form is `let`:
```
let foo = :bar & `foo` is now bound to `bar` for the rest of the scope.
let foo = :baz & Validation error: name foo was bound in line 1
```
`let` bindings are more complex; we will return to these below.
## Patterns
Ludus makes extensive use of pattern-matching. Patterns do two jobs at once: they match values (or don't); and they bind names. The left-hand side of the examples just above in the `let` binding is not just a word: it is a pattern. Patterns also arise in conditional forms and function declarations.
### The placeholder: `_`
The simplest pattern is the placeholder: it matches against anything, and does not bind a name. It is written as a single underscore: `_`, e.g., `let _ = :foo`.
#### Ignored names
If you wish to be a bit more explict than using a placeholder, you can use an ignored name, which is a name that starts with an underscore: `_foo`. This is not bound, is not a valid name, and can be used however much you wish, even multiple times in the same pattern. It is, in fact, a placeholder, plus a reader-facing description.
### Literal patterns
Patterns can be literal atomic values or strings: `0`, `false`, `nil`, `:foo`, etc. That means you can write `let 0 = 0` or `let :foo = :foo`, and, while nothing will happen, everything will be just fine.
Literals match against, well, literal values: if the pattern and the value are the same, they match! If not, they don't match.
Literal values do not bind anything.
### Word patterns
Word patterns match against any value, and bind that value to the word as a name. The scope of that binding depends on the form the pattern is in. `let foo = :bar` binds `:bar` to `foo` for the rest of the scope.
#### Typed patterns
Word patterns can, optionally, take a type, using the `as` reserved word, and the keyword representing the desired type: `let foo as :number = 42`.
### String patterns
Ludus has a simple but powerful form of string pattern matching that mirrors string interpolation. Any word inside curly braces in a string will match against a substring of a string passed into a pattern.
```
let i_am = "I am the walrus"
let "I {verb} the {noun}" = i_am
(verb, noun) &=> ("am", "walrus")
```
Note that such names may well be bound to empty strings: a match does not guarantee that there will be anything in the string. This is particularly relevant at the beginning and end of string patterns:
```
let we_are = "We are the eggmen"
let "{first}We {what}" = we_are
(first, what) &=> ("", "are the eggmen")
```
### Collection patterns
Tuples, lists, and dicts can be destructured using patterns. They are written nearly identically to their literal counterparts. Collection patterns are composed of any number of simpler patterns or other collection patterns. They bind any names nested in them, match literals in them, etc.
#### Tuple patterns
Tuple patterns are delimited using parens, using commas or newlines to separate any number of other patterns. Consider `let (x, y, z) = (1, 2, 3)`. `x`, `y`, and `z` are now bound to 1, 2, and 3, respectively.
The last item in a tuple pattern can be a splat--`...`--which either discards any remaining unenumerated values in a tuple, or binds them to a list. Without a splat, tuples patterns only match against tuples of the same length.
```
let mytup = (1, 2, 3)
let (x, _, y) = mytup & x is now 1, y is now 3
let (a, ...) = mytup & a is now 1; a bare splat (without a name) is just fine
let (_, ...cs) = mytup & cs is now [2, 3]
let (p, q) = mytup & panic! no match
let () = () & empty tuples are also patterns
```
#### List patterns
List patterns are identical to tuple patterns, but they are written using square braces. They also match against a specific number of members, and may take a splat in the last position, e.g. `let [first, ...rest] = [1, 2, 3]`.
Note that list patterns, like tuple patterns, match on explicit length. That means that if you are matching only the first items of a list, you must explicitly include a splat pattern, e.g. `let [first, second, ...] = [1, 2, 3, 4]`.
#### Dict patterns
Dict patterns are written either with shorthand words, or keyword-pattern pairs. Consider: `let #{:a foo, :b 12, c} = #{:a 1, :b 12, :c 4}`. `foo` is now 1; `b` is now 12, `c` is now 4. If a dict does not hold a value at a particular key, there is no match.
Dict patterns may also use a splat as their last member: `let #{:a 1, ...b} = #{:a 1, :b 2, :c 3}` will bind `b` to `#{:b 2, :c 3}`.
Like tuple and list patterns, dict patterns without a splat at the end match only on exact equivalence on all keys.
## `let` bindings
`let` bindings are the basic form of matching and binding in Ludus. It is written `let {pattern} = {non-binding expression}`. The pattern can be arbitrarily complex. If the left-hand side of a `let` binding does not match, Ludus will raise a panic, halting evaluation of the script.
## Scope and blocks
Ludus is lexically scoped. Bindings are valid for the remainder of the scope they act on. To introduce a new scope, Ludus uses a block, a collection of expressions delimited by curly braces and separated by semicolons or newlines. The value of a block, as well as the value of a script, is the last expression in it. In `let foo = {:this; :set; :of; :expressions; "is actually"; :ignored }`, `foo` will be bound to `:ignored`.
That said, you can use bindings in blocks, which will not be available outside that block--but blocks can use bidnings from their parent scope:
```
let outer = 42
let first = {
let inner = 23
add (outer, inner)
} & first is now bound to 65
inner & Validation error: unbound name inner
```
### Shadowing
Even though names are bound permanently in Ludus, it is perfectly possible (and occasionally quite useful) to "shadow" names from an enclosing scope.
```
let x = 42
let y = {
let first = x
let x = 23 & this is fine
let second = x
add (first, second)
} & y is now 65
```
## Conditional forms
Ludus has a robust set of conditional forms, all of which are expressions and resolve to a single value.
### `if`
`if` evaluates a condition; if the result of the condition is truthy, it evaluates is `then` branch; if the condition is falsy, it evaluates its `else` branch. Both branches must always be present. Newlines may come before `then` and `else`.
`if {simple expression} then {non-binding expression} else {non-binding expression}`
### `when`
`when` is like Lisp's `cond`: it takes a series of clauses, separated by semicolons or newlines, delimited by curly braces. Clauses are written `lhs -> rhs`. `when` expressions are extremely useful for avoiding nested `if`s.
The left hand of a clause is a simple expression; the right hand of a clause is any expression. When the left hand is truthy, the right hand is evaluated, and the result of that evaluation is returned; no further clauses are evaluated. If no clause has a truthy left-hand value, then a panic is raised. In the example below, not the use of literal `true` as an always-matching clause.
```
when {
maybe () -> :something
mabye_not () -> :something_else
true -> :always
}
```
### `match`
A `match` form is the most powerful conditional form in Ludus. It consists of a test expression, and a series of clauses. The test expression must be a simple expression, followed by `with`, and then a series of clauses separated by a semicolon or newline, delimited by curly braces.
```
match may_fail () with {
(:ok, value) -> calculate_result (value)
(:err, msg) -> { log! (msg); recover_somehow () }
}
```
The left hand of a match clause is a pattern; the right hand is an expression: `pattern -> expression`. If the pattern matches the test expression of a clause, the expression is evaluated with any bindings from the pattern, and `match` form evaluates to the result of that expression.
If a test expression does not match against any clause's pattern, a panic is raised.
Ludus does not attempt to do any exhaustiveness checking on match forms; match errors are always runtime errors.
#### Guards
`match` clauses may have a _guard expression_, which allows a clause only to match if the expression's result is truthy. In the previous example, consider that we might want different behaviour depending on the value of the number:
```
match may_fail () with {
(:ok, value) if pos? (value) -> calculate_positive_result (value)
(:ok, value) if neg? (value) -> calculate_negative_result (value)
(:ok, 0) -> do_something_with_zero ()
(:err, msg) -> { log! (msg); recover_somehow () }
}
```
## Functions
Ludus is an emphatically functional language. Almost everything in Ludus is accomplished by applying functions to values, or calling functions with arguments. (These are precise synonyms.)
Functions have the type `:fn`.
### Calling functions
Functions are called by placing a tuple with arguments immediately after a function name, e.g. `add (1, 2)` adds `1` and `2`. Because they are represented as tuples, arguments must be explicitly written; splats cannot be used to pass an arbitrary number of arguments to a function.
### Defining functions
Functions have three increasingly complex forms to define them. All of them include the concept of a function clause, which is just a match clause whose left hand side must be a _tuple_ pattern.
#### Anonymous lambda
An anonymous lambda is written `fn {tuple pattern} -> {expression}`, `fn (x, y) -> if gt? (x, y) then x else add (x, y)`. Lambdas may only have one clause.
#### Named functions
A named function is identical to a lambda, with the one change that a word follows the `fn` reserved word: `fn {name} {tuple pattern} -> {expression}`. E.g., `fn add_1 (x) -> add (x, 1)`. The name of the function is bound for the remainder of the scope.
#### Compound functions
Compound functions are functions that have multiple clauses. They must be named, and in place of a single clause after a name, they consist in one or more clauses, separated by semicolons or newlines, delimited by curly braces. Optionally, compound functions may have a docstring as their first element after the opening curly brace. The docstring explains the function's purpose and use, before any of the function clauses.
An example from Ludus's Prelude:
```
fn some {
"Takes a possibly `nil` value and a default value. Returns the value if it's not `nil`, returns the default if it's `nil`."
(nil, default) -> default
(value, _) -> value
}
```
### Closures
Functions in Ludus are closures: function bodies have access not only to their specific scope, but any enclosing scope. That said, functions only have access to names bound _before_ they are defined; nothing is hoisted in Ludus.
### Mutual recursion and forward declaration
If you try the following, you'll get a validation error:
```
fn stupid_odd? {
(0) -> false
(x) -> supid_even? (dec (x)) & Validation error: unbound name stupid_even?
}
fn stupid_even? {
(0) -> true
(x) -> stupid_odd? (dec (x))
}
```
To allow for mutual recursion, Ludus allows forward declarations, which are written `fn name` without any clauses. In the example above, we would simply put `fn stupid_even?` before we define `stupid_odd?`.
If you declare a function without defining it, however, Ludus will raise a validation error.
### The Prelude
The Prelude is a substantial set of functions that is available in any given Ludus script. (It is, itself, just a Ludus file that has special access to host functions.) Because of that, a large number of functions are always available. The prelude documentation is [here](prelude.md).
### Partial application
Functions in Ludus can be partially applied by using a placeholder in the arguments. Partial application may only use a single placeholder (partially applied functions are always unary), but it can be anywhere in the arguments: `let add_1 = add(1, _)` or `let double = mult(_, 2)`.
Unary functions and called keywords may _not_ be partially applied: it is redundant.
Because of "partial application," Ludus has a concept of an "argument tuple" (which may include a single placeholder) in addition to a tuple literal (which may not include a placeholder).
### Function pipelines, or `do` forms
In place of nesting function calls inside other function calls, Ludus allows for a more streamlined version of function application: the `do` form or function pipeline. `do` is followed by an initial expression. `do` expressions use `>` as an operator: whatever is on the left hand side of the `>` is passed in as a single argument to whatever is on its right hand side. For example:
```
let silly_result = do 23 >
mult (_, 2) > add (1, _) >
sub (_, 2) > div (_, 9) & silly_result is 5
```
Newlines may appear after any instance of `>` in a `do` expression. That does, however, mean that you must be careful not to accidentally include any trailing `>`s.
### Called keywords
Keywords may be called as functions, in which case they extract the value stored at that key in the value passed in:
```
let foo = #{:a 1, :b 2}
let bar = :a (foo) & `bar` is now 1
```
Called keywords can be used in pipelines.
In addition, keywords may be called when they are bound to names:
```
let foo = #{:a 1, :b 2}
let bar = :a
bar (foo) & => 1
```
## Synthetic expressions
Synthetic expressions are valid combinations of words, keywords, package names, and argument tuples which allow for calling functions and extracting values from associative collections. The root--first term--of a synthetic expression must be a word or a keyword; subsequent terms must be either argument tuples or keywords.
```
let foo = #{:a 1, :b #{:c "bar" :d "baz"}}
let bar = foo :b :c & `bar` is now "bar"
let baz = :b (foo) :d & `baz` is now "baz"
```
## Looping forms
Ludus has optimized tail calls--the most straightforward way to accomplish repeating behaviour is function recursion. There are two additional looping forms, `repeat` and `loop`.
### `repeat`
`repeat` is a help to learners, and is useful for executing side effects multiple times. It is written `repeat {number|word} { {exprs} }`. From turtle graphics:
```
repeat 4 {
forward! (100)
right! (0.25)
}
```
Note that `repeat` does two interesting things:
1. It never returns a value other than `nil`. If it's in the block, it disappears. This is a unique (and frankly, undesirable) property in Ludus.
2. Unlike everything else in Ludus, repeate _requires_ a block, and not simply an expression. You cannot write `repeat 4 forward! (100)`.
### `loop`/`recur`
`loop` and `recur` are largely identical to recursive functions for repetition, but use a special form to allow an anonymous construction and a few guard rails.
The syntax here is `loop <tuple> with { <function clauses> }`. (Or, you can have a single function clause instead of a set of clauses.) The tuple is passed in as the first set of arguments.
```
let xs = [1, 2, 3, 4]
loop (xs, 0) with {
([x], sum) -> add (x, sum) & matches against the last element of the list
([x, ...xs], sum) -> recur (xs, add (x, sum)) & recurs with the tail
} &=> 10
```
`recur` is the recursive call. It must be in tail position--`recur` must be the root of a synthetic expression, in return position. If `recur` is not in tail position, a validation error will be raised.
In addition, `recur` calls must have the same number of arguments as the original tuple passed to `loop`. While Ludus will allow you to write clauses in `loop` forms with a different arity than the original tuple, those will never match.
`recur` calls return to the nearest `loop`. Nested `loop`s are probably a bad idea and should be avoided when possible.
## Environment and context: the toplevel
The "toplevel" of a script are the expressions that are not embedded in other expressions or forms: not inside a block, not a member of a collection, not on the right hand side of a binding, not inside a function body. The toplevel-only forms:
### `import`
`import` allows for the evaluation of other Ludus scripts: `import "path/to/file" as name`. `import` just evaluates that file, and then binds a name to the result of evaluating that script. This, right now, is quite basic: circular imports are currently allowed but will lead to endless recursion; results are not cached, so each `import` in a chain re-evaluates the file; and so on.
Status: not yet implemented.
### `use`
`use` loads the contents of a namespace into a script's context. To ensure that this is statically checkable, this must be at the toplevel.
Status: not yet implemented.
### `pkg`
Packages, `pkg`es, may only be described at the toplevel of a script. This is to ensure they can be statically evaluatable.
### `test`
A `test` expression is a way of ensuring things behave the way you want them to. Run the script in test mode, and these are evaluated. If the expression under `test` returns a truthy value, you're all good! If the expression under `test` returns a falsy value or raises a panic, then Ludus will report which test(s) failed.
```
test "something goes right" eq? (:foo, :foo)
test "something goes wrong" {
let foo = :foo
let bar = :bar
eq? (foo, bar)
} &=> test failed: "something goes wrong" on line 3
```
`test`s must be at the toplevel--or embedded within other tests in _their_ highest level.
Formally: `test <string> <expression>`.
Status: not yet implemented.
## Changing things: `box`es
Ludus does not let you re-bind names. It does, however, have a type that allows for changing values over time: `box`. A box is a place to put things, it has its own identity, it can store whatever you put in it, but to get what's in it, you have to `unbox` it.
Syntactically and semantically, `box`es are straightforward, but do require a bit more overhead than `let` bindings. The idea is that Ludus makes it obvious where mutable state is in a program, as well as where that mutable state may change. It does so elegantly, but with some guardrails that may take a little getting used to.
The type of a `box` is, predictably, `:box`.
```
box foo = 42 & foo is now bound to a _box that contains 42_
add (1, foo) & panic! no match: foo is _not_ a number
store! (foo, 23) & foo is now a box containing 23
update! (foo, inc) & foo is now a ref containing 24
unbox (foo) &=> 23; use unbox to get the value contained in a box
```
### Ending with a bang!
Ludus has a strong naming convention that functions that change state or could cause an explicit panic end in an exclamation point (or, in computer nerd parlance, a "bang"). So anything function that mutates the value held in a reference ends with a bang: `store!` and `update!` take bangs; `unbox` does not.
This convention also includes anything that prints to the console: `print!`, `report!`, `doc!`, `update!`, `store!`, etc.
(Note that there are a few counter-examples to this: math functions that could cause a panic [in place of returning NaN] do not end with bangs: `div`, `inv`, and `mod`; each of these has variants that allow for more graceful error handling).
### Ending with a whimper?
Relatedly, just about any function that returns a boolean value is a predicate function--and has a name that ends with a question mark: `eq?` tests for equality; `box?` tells you if something is a ref or not; `lte?` is less-than-or-equal.
## Errors: panic! in the Ludus script
A special form, `panic!`, halts script execution with the expression that follows as an error message.
```
panic! :oops
if true then :ok else panic! "It's false!"
```
Panics also happen in the following cases:
* a `let` binding pattern has no match against the value of its expression
* a `match` or `when` form has no matching clause
* a function is called with arguments that do not match any of its clauses
* something that is not a function or keyword is called as a function
* a called keyword is partially applied
* `div`, `inv`, or `mod` divides by zero
* `sqrt` takes the square root of a negative number
* certain error handling functions, like `unwrap!` or `assert!`, are invoked on values that cause them to panic
In fact, the only functions in the Prelude which explicitly cause panics are, at current, `div`, `inv`, `mod`, `sqrt`, `unwrap!`, and `assert!`.
### `nil`s, not errors
Ludus, however, tries to return `nil` instead of panicking where it seems prudent. So, for example, attempting to get access a value at a keyword off a number or `nil`, while nonsensical, will return `nil` rather than panicking:
```
let a = true
a :b :c :d :e &=> nil
let b = [1, 2, 3]
at (b, 12) &=> nil
```
### Result tuples
Operations that could fail--especially when you want some information about why--don't always return `nil` on failures. Instead of exceptions or special error values, recoverable errors in Ludus are handled instead by result tuples: `(:ok, value)` and `(:err, msg)`. So, for example, `unwrap!` takes a result tuple and either returns the value in the `:ok` case, or panics in the `:err` case.
Variants of some functions that may have undesirably inexplicit behaviour are written as `{name}/safe`. So, for example, you can get a variant of `div` that returns a result tuple in `div/safe`, which returns `(:ok, result)` when everything's good; and `(:err, "division by zero")` when the divisor is 0.

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

15
notary.json Normal file
View File

@ -0,0 +1,15 @@
{
"source": ["./target/ludus"],
"bundle_id": "dev.ludus.core",
"apple_id": {
"username": "s.richmond@utoronto.ca",
"password": "@keychain:Notary"
},
"sign": {
"application_identity": "9F27C69TKX"
},
"dmg": {
"output_path": "./target/ludus.dmg",
"volume_name": "Ludus"
}
}

5220
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "@ludus/ludus-js-pure",
"version": "0.1.26",
"description": "A Ludus interpreter in a pure JS function.",
"type": "module",
"main": "build/ludus.mjs",
"directories": {},
"keywords": [],
"author": "Scott Richmond",
"license": "GPL-3.0",
"files": [
"build/out.wasm",
"build/out.mjs",
"build/ludus.mjs"
],
"devDependencies": {}
}

25
postlude.ld Normal file
View File

@ -0,0 +1,25 @@
& this file runs after any given interpretation
& even if the original interpretation panics
& the goal is to output any global state held in Ludus
& this does not have base loaded into it, only prelude: must be pure Ludus
if turtle_state () :visible? then render_turtle! () else nil
reset_turtle! ()
& let console_msgs = flush! ()
let (r, g, b, a) = unbox (bgcolor)
store! (bgcolor, colors :black)
let draw_calls = unbox (p5_calls)
store! (p5_calls, [])
#{
& :result result is provided elsewhere
& :errors [] & if we get here there are no errors
& :console console_msgs
:draw concat (
[(:background, r, g, b, a), (:stroke, 255, 255, 255, 255)]
draw_calls)
}

1549
prelude.ld Normal file

File diff suppressed because it is too large Load Diff

1549
prelude.md Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,8 +0,0 @@
(defproject ludus "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.3"]]
:repl-options {:init-ns ludus.core}
:main ludus.core)

285
sandbox.ld Normal file
View File

@ -0,0 +1,285 @@
let input = "I remember my mother"
print! ("DOCTOR")
print! ("> ", input)
let sanitized = do input > trim > downcase
& ensuring we have spaces at the beginning and end
& this lets us match patterns as written below
let padded = join ([" ", sanitized, " "])
fn switch_persons {
("i") -> "you"
("you") -> "i"
("am") -> "are"
("me") -> "you"
("my") -> "your"
(x) -> x
}
fn repersonalize (x) -> do x >
trim >
split (_, " ") >
map (switch_persons, _) >
join (_, " ")
fn one_of {
(str as :string) -> str
(strs as :list) -> random (strs)
}
let output = match padded with {
"{x} hello {y}" -> "How do you do. Please state your problem"
"{x} hi {y}" -> "How do you do. Please state your problem"
"{x} computer {y}" -> [
"Do computers worry you"
"What do you think about machines"
"Why do you mention computers"
"What do you think machines have to do with your problem"
]
"{x} name {y}" -> "I am not interested in names"
"{x} sorry {y}" -> [
"Please don't apologize"
"Apologies are not necessary"
"What feelings do you have when you apologize"
]
"{x} i remember {y}" -> {
let switched = repersonalize (y)
[
"Do you often think of {switched}"
"Does thinking of {switched} bring anything else to mind"
"What else do you remember"
"Why do you recall {switched} right now"
"What in the present situation reminds you of {switched}"
"What is the connection between me and {switched}"
]
}
"{x} do you remember {y}" -> {
let switched = repersonalize (y)
[
"Did you think I would forget {switched}"
"Why do you think I should recall {switched} now"
"What about {switched}"
"You mentioned {switched}"
]
}
"{x} if {y}" -> {
let switched = repersonalize (y)
[
"Do you reall think that its likely that {switched}"
"Do you wish that {switched}"
"What do you think about {switched}"
"Really--if {switched}"
]
}
"{x} i dreamt {y}" -> {
let switched = repersonalize (y)
[
"Really--{y}"
"Have you ever fantasized {y} while you were awake"
"Have you dreamt {y} before"
]
}
"{x} dream about {y}" -> {
let switched = repersonalize (y)
"How do you feel about {switched} in reality"
}
"{x} dream {y}" -> [
"What does this dream suggest to you"
"Do you dream often"
"What persons appear in your dreams"
"Don't you believe that dream has to do with your problem"
]
"{x} my mother {y}" -> {
let switched = repersonalize (y)
[
"Who else in your family {y}"
"Tell me more about your family"
]
}
"{x} my father {y}" -> [
"Your father"
"Does he influence you strongly"
"What else comes to mind when you think of your father"
]
"{x} i want {y}" -> {
let switched = repersonalize (y)
[
"What would it mean if you got {y}"
"Why do you want {y}"
"Suppose you got {y} soon"
]
}
"{x} i am glad {y}" -> {
let switched = repersonalize (y)
[
"How have I helped you to be {y}"
"What makes you happy just now"
"Can you explain why you are suddenly {y}"
]
}
"{x} i am sad {y}" -> {
let switched = repersonalize (y)
[
"I am sorry to hear you are depressed"
"I'm sure it's not pleasant to be sad"
]
}
"{x} are like {y}" -> {
let switched_x = repersonalize (x)
let switched_y = repersonalize (y)
"What resemblance to you see between {switched_x} and {switched_y}"
}
"{x} is like {y}" -> {
let switched_x = repersonalize (x)
let switched_y = repersonalize (y)
[
"In what way is it that {switched_x} is like {switched_y}"
"What resemblance do you see"
"Could there really be some connection"
"How"
]
}
"{x} alike {y}" -> [
"In what way"
"What similarities are there"
]
"{x} same {y}" -> "What other connections do you see"
"{x} i was {y}" -> {
let switched = repersonalize (y)
[
"Were you really"
"Perhaps I already knew you were {switched}"
"Why do you tell me you were {switched} now"
]
}
"{x} was i {y}" -> {
let switched = repersonalize (y)
[
"What if you were {switched}"
"Do you think you were {switched}"
"What wouuld it mean if you were {switched}"
]
}
"{x} i am {y}" -> {
let switched = repersonalize (y)
[
"In what way are you {switched}"
"Do you want to be {switched}"
]
}
"{x} am i {y}" -> {
let switched = repersonalize (y)
[
"Do you believe you are {switched}"
"Would you want to be {switched}"
"You wish I would tell you you are {switched}"
"What would it mean if you were {switched}"
]
}
"{x} am {y}" -> [
"Why do you say *AM*"
"I don't understand that"
]
"{x} are you {y}" -> {
let switched = repersonalize (y)
[
"Why are you interested in whether I am {switched} or not"
"Would you prefer if I weren't {switched}"
"Perhaps I am {switched} in your fantasies"
]
}
"{x} you are {y}" -> {
let switched = repersonalize (y)
"What makes you think I am {y}"
}
"{x} because {y}" -> [
"Is that the real reason"
"What other reasons might there be"
"Does that reason seem to explain anything else"
]
"{x} were you {y}" -> {
let switched = repersonalize (y)
[
"Perhaps I was {switched}"
"What od you think"
"What if I had been {switched}"
]
}
"{x} i can't {y}" -> {
let switched = repersonalize (y)
[
"Maybe you could {switched} now"
"What if you could {switched}"
]
}
"{x} i feel {y}" -> {
let switched = repersonalize (y)
"Do you often feel {switched}"
}
"{x} i felt {y}" -> "What other feelings do you have"
"{x} i {y} you {z}" -> {
let switched = repersonalize (y)
"Perhaps in your fantasy we {switched} each other"
}
"{x} why don't you {y}" -> {
let switched = repersonalize (y)
[
"Should you {y} yourself"
"Do you believe I don't {y}"
"Perhaps I will {y} in good time"
]
}
"{x} yes {y}" -> [
"You seem quite positive"
"You are sure"
"I understand"
]
"{x} no {y}" -> [
"Why not"
"You are being a bit negative"
"Are you saying *NO* just to be negative"
]
"{x} someone {y}" -> "Can you be more specific"
"{x} everyone {y}" -> [
"Surely not everyone"
"Can you think of anyone in particular"
"Who for example"
"You are thinking of a special person"
]
"{x} always {y}" -> [
"Can you think of a specific example"
"When"
"What incident are you thinking of"
"Really--always"
]
"{x} what {y}" -> [
"Why do you ask"
"Does that question interest you"
"What is it you really want to know"
"What do you think"
"What comes to your mind when you ask that"
]
"{x} perhaps {y}" -> "You do not seem quite certain"
"{x} are {y}" -> {
let switched = repersonalize (y)
[
"Did you think they might not be {switched}"
"Possibly they are {switched}"
]
}
_ -> [
"Very interesting"
"I am not sure I understand you fully"
"What does that suggest to you"
"Please continue"
"Go on"
"Do you feel strongly about discussing such things"
]
}
print! (">>> ", do output > one_of > upcase)

320
src/base.janet Normal file
View File

@ -0,0 +1,320 @@
# A base library for Ludus
# Only loaded in the prelude
(defn bool [x] (if (= :^nil x) nil x))
(defn ludus/and [& args] (every? (map bool args)))
(defn ludus/or [& args] (some bool args))
(defn ludus/type [value]
(when (= :^nil value) (break :nil))
(def typed? (when (dictionary? value) (value :^type)))
(def the-type (if typed? typed? (type value)))
(case the-type
:buffer :string
:boolean :bool
:array :list
:table :dict
:cfunction :function
the-type))
(var stringify nil)
(defn- dict-str [dict]
(string/join
(map
(fn [[k v]] (string (stringify k) " " (stringify v)))
(pairs dict))
", "))
(defn- stringish? [x] (or (string? x) (buffer? x)))
(defn- stringify* [value]
(when (stringish? value) (break value))
(def type (ludus/type value))
(case type
:nil ""
:number (string value)
:bool (string value)
:keyword (string ":" value)
:tuple
(string/join (map stringify value) ", ")
:list
(string/join (map stringify value) ", ")
:dict (dict-str value)
:set
(string/join (map stringify (keys value)) ", ")
:box (stringify (value :^value))
:fn (string "fn " (value :name))
:function (string "builtin " (string value))
:pkg (dict-str value)
))
(set stringify stringify*)
(var show nil)
(defn- show-pkg [x]
(def tab (struct/to-table x))
(set (tab :^name) nil)
(set (tab :^type) nil)
(string "pkg " (x :^name) " {" (stringify tab) "}")
)
(defn- dict-show [dict]
(string/join
(map
(fn [[k v]] (string (show k) " " (show v)))
(pairs dict))
", "))
(defn- set-show [sett]
(def prepped (merge sett))
(set (prepped :^type) nil)
(def shown (map show (keys prepped)))
(string/join shown ", ")
)
(defn- show* [x]
(case (ludus/type x)
:nil "nil"
:string (string "\"" x "\"")
:tuple (string "(" (string/join (map show x) ", ") ")")
:list (string "[" (string/join (map show x) ", ") "]")
:dict (string "#{" (dict-show x) "}")
:set (string "${" (set-show x) "}")
:box (string "box " (x :name) " [ " (show (x :^value)) " ]")
:pkg (show-pkg x)
(stringify x)))
(set show show*)
# (var json nil)
# (defn- dict-json [dict]
# (string/join
# (map
# (fn [[k v]] (string (json k) ": " (json v)))
# (pairs dict))
# ", "))
# (defn- json* [x]
# (case (ludus/type x)
# :nil "\"null\""
# :number (string x)
# :bool (if true "\"true\"" "\"false\"")
# :string (string "\"" x "\"")
# :keyword (string "\"" x "\"")
# :tuple (string "[" (string/join (map json x) ", ") "]")
# :list (string "[" (string/join (map json x) ", ")"]")
# :dict (string "{" (dict-json x) "}")
# :set (string "[" (string/join (map json (keys x)) ", ") "]")
# (show x)))
# (set json json*)
(defn show-patt [x]
(case (x :type)
:nil "nil"
:bool (string (x :data))
:number (string (x :data))
:keyword (string ":" (x :data))
:word (x :data)
:placeholder (get-in x [:token :lexeme])
:tuple (string "(" (string/join (map show-patt (x :data)) ", ") ")")
:list (string "[" (string/join (map show-patt (x :data)) ", ")"]")
:dict (string "#{" (string/join (map show-patt (x :data)) ", ") "}")
:pair (string (show-patt (get-in x [:data 0])) " " (show-patt (get-in x [:data 1])))
:typed (string (show-patt (get-in x [:data 1])) " as " (show-patt (get-in x [:data 0])))
:interpolated (get-in x [:token :lexeme])
:string (get-in x [:token :lexeme])
:splat (string "..." (when (x :data) (show-patt (x :data))))
(error (string "cannot show pattern of unknown type " (x :type)))))
(defn pretty-patterns [fnn]
(def {:body clauses} fnn)
(string/join (map (fn [x] (-> x first show-patt)) clauses) "\n"))
(defn doc [fnn]
(when (not= :fn (ludus/type fnn)) (break "No documentation available."))
(def {:name name :doc docstring} fnn)
(string/join [name
(pretty-patterns fnn)
(if docstring docstring "No docstring available.")]
"\n"))
(defn- conj!-set [sett value]
(set (sett value) true)
sett)
(defn- conj-set [sett value]
(def new (merge sett))
(conj!-set new value))
(defn- conj!-list [list value]
(array/push list value))
(defn- conj-list [list value]
(def new (array/slice list))
(conj!-list new value))
(defn conj! [x value]
(case (ludus/type x)
:list (conj!-list x value)
:set (conj!-set x value)))
(defn conj [x value]
(case (ludus/type x)
:list (conj-list x value)
:set (conj-set x value)
(error (string "cannot conj onto " (show x)))))
(defn disj! [sett value]
(set (sett value) nil)
sett)
(defn disj [sett value]
(def new (merge sett))
(set (new value) nil)
new)
(defn assoc! [dict key value]
(set (dict key) value)
dict)
(defn assoc [dict key value]
(merge dict {key value}))
(defn dissoc! [dict key]
(set (dict key) nil)
dict)
(defn dissoc [dict key]
(def new (merge dict))
(set (new key) nil)
new)
(defn ludus/get [key dict &opt def]
(default def :^nil)
(get dict key def))
(defn rest [indexed]
(array/slice indexed 1))
(defn to_list [x]
(case (ludus/type x)
:list x
:tuple @[;x]
:dict (pairs x)
:set (-> x (dissoc :^type) keys)
@[x]))
(defn showprint [x]
(if (= :string (ludus/type x))
x
(show x)))
(defn print! [args]
(print ;(map showprint args)))
(defn prn [x]
(pp x)
x)
(defn concat [x y & zs]
(case (ludus/type x)
:string (string x y ;zs)
:list (array/concat @[] x y ;zs)
:set (merge x y ;zs)))
(defn unbox [b] (get b :^value))
(defn store! [b x] (set (b :^value) x))
(defn mod [x y]
(% x y))
(defn- byte->ascii [c i]
(if (< c 128)
(string/from-bytes c)
(error (string "non-ASCII character at index" i))))
(defn chars [str]
(def out @[])
(try
(for i 0 (length str)
(array/push out (byte->ascii (str i) i)))
([e] (break [:err e])))
[:ok out])
(def ctx {
"add" +
"and" ludus/and
"assoc!" assoc!
"assoc" assoc
"atan_2" math/atan2
"bool" bool
"ceil" math/ceil
"chars" chars
"concat" concat
"conj!" conj!
"conj" conj
"cos" math/cos
"count" length
"dec" dec
"disj!" disj!
"disj" disj
"dissoc!" dissoc!
"dissoc" dissoc
"div" /
"doc" doc
"downcase" string/ascii-lower
"eq?" deep=
"first" first
"floor" math/floor
"get" ludus/get
"gt" >
"gte" >=
"inc" inc
"last" last
"lt" <
"lte" <=
"mod" mod
"mult" *
"not" not
"nth" ludus/get
"or" ludus/or
"pi" math/pi
"print!" print!
"prn" prn
"push" array/push
"random" math/random
"range" range
"rest" rest
"round" math/round
"show" show
"sin" math/sin
"slice" array/slice
"split" string/split
"sqrt" math/sqrt
"store!" store!
"str_slice" string/slice
"stringify" stringify
"sub" -
"tan" math/tan
"to_list" to_list
"trim" string/trim
"triml" string/triml
"trimr" string/trimr
"type" ludus/type
"unbox" unbox
"upcase" string/ascii-upper
})
(def base (let [b @{:^type :dict}]
(each [k v] (pairs ctx)
(set (b (keyword k)) v))
b))

130
src/doc.janet Normal file
View File

@ -0,0 +1,130 @@
(import /src/base :as base)
(import /src/prelude :as prelude)
(defn map-values [f dict]
(from-pairs (map (fn [[k v]] [k (f v)]) (pairs dict))))
(def with-docs (map-values base/doc prelude/ctx))
(def sorted-names (-> with-docs keys sort))
(defn escape-underscores [str] (string/replace "_" "\\_" str))
(defn escape-punctuation [str] (->> str
(string/replace "?" "")
(string/replace "!" "")))
(defn toc-entry [name]
(def escaped (escape-underscores name))
(string "[" escaped "](#" (escape-punctuation escaped) ")"))
(def alphabetical-list
(string/join (map toc-entry sorted-names) "&nbsp;&nbsp;&nbsp; "))
(def topics {
"math" ["abs" "add" "angle" "atan/2" "between?" "ceil" "cos" "dec" "deg/rad" "deg/turn" "dist" "div" "div/0" "div/safe" "even?" "floor" "gt?" "gte?" "heading/vector" "inc" "inv" "inv/0" "inv/safe" "lt?" "lte?" "max" "min" "mod" "mod/0" "mod/safe" "mult" "neg" "neg?" "odd?" "pi" "pos?" "rad/deg" "rad/turn" "random" "random_int" "range" "round" "sin" "sqrt" "sqrt/safe" "square" "sub" "sum_of_squares" "tan" "tau" "turn/deg" "turn/rad" "zero?"]
"boolean" ["and" "bool" "bool?" "false?" "not" "or" "true?"]
"dicts" ["any?" "assoc" "assoc?" "coll?" "count" "dict" "dict?" "diff" "dissoc" "empty?" "get" "keys" "random" "update" "values"]
"lists" ["any?" "append" "at" "butlast" "coll?" "concat" "count" "each!" "empty?" "filter" "first" "fold" "join" "keep" "last" "list" "list?" "map" "ordered?" "random" "range" "rest" "second" "sentence" "slice"]
"sets" ["any?" "append" "coll?" "concat" "contains?" "count" "empty?" "omit" "random" "set" "set?"]
"tuples" ["any?" "at" "coll?" "count" "empty?" "first" "last" "ordered?" "rest" "second" "tuple?"]
"strings" ["any?" "chars" "chars/safe" "concat" "count" "downcase" "empty?" "join" "sentence" "show" "slice" "split" "string" "string?" "strip" "trim" "upcase" "words"]
"types and values" ["assoc?" "bool?" "box?" "coll?" "dict?" "eq?" "fn?" "keyword?" "list?" "neq?" "nil?" "number?" "ordered?" "set?" "show" "some" "some?" "string?" "tuple?" "type"]
"boxes and state" ["box?" "unbox" "store!" "update!"]
"results" ["err" "err?" "ok" "ok?" "unwrap!" "unwrap_or"]
"errors" ["assert!"]
"turtle graphics" ["back!" "background!" "bk!" "clear!" "colors" "fd!" "forward!" "goto!" "heading" "heading/vector" "home!" "left!" "lt!" "pc!" "pd!" "pencolor" "pencolor!" "pendown!" "pendown?" "penup!" "penwidth" "penwidth!" "position" "pu!" "pw!" "render_turtle!" "reset_turtle!" "right!" "rt!" "turtle_state"]
"environment and i/o" ["doc!" "print!" "report!" "state"]
})
(defn capitalize [str]
(def fst (slice str 0 1))
(def rest (slice str 1))
(def init_cap (string/ascii-upper fst))
(def lower_rest (string/ascii-lower rest))
(string init_cap lower_rest))
(defn topic-entry [topic]
(string "### " (capitalize topic) "\n"
(as-> topic _ (topics _) (array/slice _) (sort _) (map toc-entry _)
(string/join _ "&nbsp;&nbsp;&nbsp; "))
"\n"))
(def by-topic (let [the-topics (-> topics keys sort)
topics-entries (map topic-entry the-topics)]
(string/join topics-entries "\n")))
(defn compose-entry [name]
(def header (string "\n### " name "\n"))
(def the-doc (get with-docs name))
(when (= "No documentation available." the-doc)
(break (string header the-doc "\n")))
(def lines (string/split "\n" the-doc))
(def description (last lines))
(def patterns (string/join (slice lines 1 (-> lines length dec)) "\n"))
(def backto "[Back to top.](#ludus-prelude-documentation)\n")
(string header description "\n```\n" patterns "\n```\n" backto))
(compose-entry "update")
(def entries (string/join (map compose-entry sorted-names) "\n---"))
(def doc-file (string
```
# Ludus prelude documentation
These functions are available in every Ludus script.
The documentation for any function can be found within Ludus by passing the function to `doc!`,
e.g., running `doc! (add)` will send the documentation for `add` to the console.
For more information on the syntax & semantics of the Ludus language, see [language.md](./language.md).
The prelude itself is just a Ludus file, which you can see at [prelude.ld](./prelude.ld).
## A few notes
**Naming conventions.** Functions whose name ends with a question mark, e.g., `eq?`, return booleans.
Functions whose name ends with an exclamation point, e.g., `make!`, change state in some way.
In other words, they _do things_ rather than _calculating values_.
Functions whose name includes a slash either convert from one value to another, e.g. `deg/rad`,
or they are variations on a function, e.g. `div/0` as a variation on `div`.
**How entries are formatted.** Each entry has a brief (sometimes too brief!) description of what it does.
It is followed by the patterns for each of its function clauses.
This should be enough to indicate order of arguments, types, and so on.
**Patterns often, but do not always, indicate types.** Typed patterns are written as `foo as :bar`,
where the type is indicated by the keyword.
Possible ludus types are: `:nil`, `:boolean`, `:number`, `:keyword` (atomic values);
`:string` (strings are their own beast); `:tuple` and `:list` (ordered collections), `:set`s, and `:dict`ionaries (the other collection types); `:pkg` (packages, which are quasi-collections); `:fn` (functions); and `:box`es.
**Conventional types.** Ludus has two types based on conventions.
* _Result tuples._ Results are a way of modeling the result of a calculation that might fail.
The two possible values are `(:ok, value)` and `(:err, msg)`.
`msg` is usually a string describing what went wrong.
To work with result tuples, see [`unwrap!`](#unwrap) and [`unwrap_or`](#unwrap_or).
That said, usually you work with these using pattern matching.
* _Vectors._ Vectors are 2-element tuples of x and y coordinates.
The origin is `(0, 0)`.
Many math functions take vectors as well as numbers, e.g., `add` and `mult`.
You will see vectors indicated in patterns by an `(x, y)` tuple.
You can see what this looks like in the last clause of `add`: `((x1, y1), (x2, y2))`.
## Functions by topic
```
by-topic
```
## All functions, alphabetically
```
alphabetical-list
```
## Function documentation
```
entries
))
(spit "prelude.md" doc-file)

140
src/errors.janet Normal file
View File

@ -0,0 +1,140 @@
(import /src/base :as b)
(defn- get-line [source line]
((string/split "\n" source) (dec line)))
(defn- caret [source line start]
(def lines (string/split "\n" source))
(def the-line (lines (dec line)))
(def prev-lines (slice lines 0 (dec line)))
(def char-counts (map (fn [x] (-> x length inc)) prev-lines))
(def prev-line-chars (sum char-counts))
(def offset (- start prev-line-chars))
(def indent (string/repeat "." (+ 6 offset)))
(string indent "^")
)
(defn scan-error [e]
(def {:line line-num :input input :source source :start start :msg msg} e)
(print "Syntax error: " msg)
(print " on line " line-num " in " input ":")
(def source-line (get-line source line-num))
(print " >>> " source-line)
(print (caret source line-num start))
e)
(defn parse-error [e]
(def msg (e :msg))
(def {:line line-num :input input :source source :start start} (e :token))
(def source-line (get-line source line-num))
(print "Syntax error: " msg)
(print " on line " line-num " in " input ":")
(print " >>> " source-line)
(print (caret source line-num start))
e)
(defn validation-error [e]
(def msg (e :msg))
(def {:line line-num :input input :source source :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num))
(case msg
"unbound name"
(do
(print "Validation error: " msg " " (get-in e [:node :data]))
(print " on line " line-num " in " input ":")
(print " >>> " source-line)
(print (caret source line-num start)))
(do
(print "Validation error: " msg)
(print " on line " line-num " in " input ":")
(print " >>> " source-line)
(print (caret source line-num start))))
e)
(defn- fn-no-match [e]
(print "Ludus panicked! no match")
(def {:line line-num :source source :input input :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num))
(print " on line " line-num " in " input ", ")
(def called (e :called))
(print " calling: " (slice (b/show called) 3))
(def value (e :value))
(print " with arguments: " (b/show value))
(print " expected match with one of:")
(def patterns (b/pretty-patterns called))
(def fmt-patt (do
(def lines (string/split "\n" patterns))
(def indented (map (fn [x] (string " " x)) lines))
(string/join indented "\n")
))
(print fmt-patt)
(print " >>> " source-line)
(print (caret source line-num start))
)
(defn- let-no-match [e]
(print "Ludus panicked! no match")
(def {:line line-num :source source :input input :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num))
(print " on line " line-num " in " input ", ")
(print " matching: " (b/show (e :value)))
(def pattern (get-in e [:node :data 0]))
(print " with pattern: " (b/show-patt pattern))
(print " >>> " source-line)
(print (caret source line-num start))
e)
(defn- match-no-match [e]
(print "Ludus panicked! no match")
(def {:line line-num :source source :input input :start start} (get-in e [:node :token]))
(print " on line " line-num " in " input ", ")
(def value (e :value))
(print " matching: " (b/show value))
(print " with patterns:")
(def clauses (get-in e [:node :data 1]))
(def patterns (b/pretty-patterns {:body clauses}))
(def fmt-patt (do
(def lines (string/split "\n" patterns))
(def indented (map (fn [x] (string " " x)) lines))
(string/join indented "\n")
))
(print fmt-patt)
(def source-line (get-line source line-num))
(print " >>> " source-line)
(print (caret source line-num start))
e)
(defn- generic-panic [e]
(def msg (e :msg))
(def {:line line-num :source source :input input :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num))
(print "Ludus panicked! " msg)
(print " on line " line-num " in " input ":")
(print " >>> " source-line)
(print (caret source line-num start))
e)
(defn- unbound-name [e]
(def {:line line-num :source source :lexeme name :input input :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num))
(print "Ludus panicked! unbound name " name)
(print " on line " line-num " in " input ":")
(print " >>> " source-line)
(print (caret source line-num start))
e)
(defn runtime-error [e]
(when (= :string (type e))
(print (string "Internal Ludus error: " e))
(print "Please file an issue at https://alea.ludus.dev/twc/ludus/issues")
(break e))
(def msg (e :msg))
(case msg
"no match: function call" (fn-no-match e)
"no match: let binding" (let-no-match e)
"no match: match form" (match-no-match e)
"no match: when form" (generic-panic e)
"unbound name" (unbound-name e)
(generic-panic e))
e)

657
src/interpreter.janet Normal file
View File

@ -0,0 +1,657 @@
# A tree walk interpreter for ludus
(import /src/base :as b)
(var interpret nil)
(var match-pattern nil)
(defn- todo [msg] (error (string "not yet implemented: " msg)))
(defn- resolve-name [name ctx]
# # (print "resolving " name " in:")
# # (pp ctx)
(when (not ctx) (break :^not-found))
(if (has-key? ctx name)
(ctx name)
(resolve-name name (ctx :^parent))))
(defn- match-word [word value ctx]
(def name (word :data))
# # (print "matched " (b/show value) " to " name)
(set (ctx name) value)
{:success true :ctx ctx})
(defn- typed [pattern value ctx]
(def [type-ast word] (pattern :data))
(def type (type-ast :data))
(if (= type (b/ludus/type value))
(match-word word value ctx)
{:success false :miss [pattern value]}))
(defn- match-tuple [pattern value ctx]
(when (not (tuple? value))
(break {:success false :miss [pattern value]}))
(def val-len (length value))
(var members (pattern :data))
(when (empty? members)
(break (if (empty? value)
{:success true :ctx ctx}
{:success false :miss [pattern value]})))
(def patt-len (length members))
(var splat nil)
(def splat? (= :splat ((last members) :type)))
(when splat?
(when (< val-len patt-len)
# (print "mismatched splatted tuple lengths")
(break {:success false :miss [pattern value]}))
# (print "splat!")
(set splat (last members))
(set members (slice members 0 (dec patt-len))))
(when (and (not splat?) (not= val-len patt-len))
# (print "mismatched tuple lengths")
(break {:success false :miss [pattern value]}))
(var curr-mem :^nothing)
(var curr-val :^nothing)
(var success true)
(for i 0 (length members)
(set curr-mem (get members i))
(set curr-val (get value i))
# (print "in tuple, matching " curr-val " with ")
# (pp curr-mem)
(def match? (match-pattern curr-mem curr-val ctx))
# (pp match?)
(when (not (match? :success))
(set success false)
(break)))
(when (and splat? (splat :data))
(def rest (array/slice value (length members)))
(match-word (splat :data) rest ctx))
(if success
{:success true :ctx ctx}
{:success false :miss [pattern value]}))
(defn- match-list [pattern value ctx]
(when (not (array? value))
(break {:success false :miss [pattern value]}))
(def val-len (length value))
(var members (pattern :data))
(when (empty? members)
(break (if (empty? value)
{:success true :ctx ctx}
{:success false :miss [pattern value]})))
(def patt-len (length members))
(var splat nil)
(def splat? (= :splat ((last members) :type)))
(when splat?
(when (< val-len patt-len)
# (print "mismatched splatted list lengths")
(break {:success false :miss [pattern value]}))
# (print "splat!")
(set splat (last members))
(set members (slice members 0 (dec patt-len))))
(when (and (not splat?) (not= val-len patt-len))
# (print "mismatched list lengths")
(break {:success false :miss [pattern value]}))
(var curr-mem :^nothing)
(var curr-val :^nothing)
(var success true)
(for i 0 (length members)
(set curr-mem (get members i))
(set curr-val (get value i))
# (print "in list, matching " curr-val " with ")
# (pp curr-mem)
(def match? (match-pattern curr-mem curr-val ctx))
# (pp match?)
(when (not (match? :success))
(set success false)
(break)))
(when (and splat? (splat :data))
(def rest (array/slice value (length members)))
(match-word (splat :data) rest ctx))
(if success
{:success true :ctx ctx}
{:success false :miss [pattern value]}))
(defn- match-string [pattern value ctx]
(when (not (string? value))
(break {:success false :miss [pattern value]}))
(def {:compiled compiled :bindings bindings} pattern)
# (print "matching " value " with")
# (pp (pattern :grammar))
(def matches (peg/match compiled value))
(when (not matches)
(break {:success false :miss [pattern value]}))
(when (not= (length matches) (length bindings))
(error "oops: different number of matches and bindings"))
(for i 0 (length matches)
(set (ctx (bindings i)) (matches i)))
{:success true :ctx ctx})
(defn- match-dict [pattern value ctx]
(when (not (table? value))
(break {:success false :miss [pattern value]}))
(def val-size (length value))
(var members (pattern :data))
(def patt-len (length members))
(when (empty? members)
(break (if (empty? value)
{:success true :ctx ctx}
{:success false :miss [pattern value]})))
(var splat nil)
(def splat? (= :splat ((last members) :type)))
(when splat?
(when (< val-size patt-len)
# (print "mismatched splatted dict lengths")
(break {:success false :miss [pattern value]}))
# (print "splat!")
(set splat (last members))
(set members (slice members 0 (dec patt-len))))
(when (and (not splat?) (not= val-size patt-len))
# (print "mismatched dict lengths")
(break {:success false :miss [pattern value]}))
(var success true)
(def matched-keys @[])
(for i 0 (length members)
(def curr-pair (get members i))
(def [curr-key curr-patt] (curr-pair :data))
(def key (interpret curr-key ctx))
(def curr-val (value key))
(def match? (match-pattern curr-patt curr-val ctx))
(array/push matched-keys key)
(when (not (match? :success))
(set success false)
(break)))
(when (and splat? (splat :data) success)
(def rest (merge value))
(each key matched-keys
(set (rest key) nil))
(match-word (splat :data) rest ctx))
(if success
{:success true :ctx ctx}
{:success false :miss [pattern value]}))
(defn- match-pattern* [pattern value &opt ctx]
# (print "in match-pattern, matching " value " with:")
# (pp pattern)
(default ctx @{})
(def data (pattern :data))
(case (pattern :type)
# always match
:placeholder {:success true :ctx ctx}
:ignored {:success true :ctx ctx}
:word (match-word pattern value ctx)
# match on equality
:nil {:success (= :^nil value) :ctx ctx}
:bool {:success (= data value) :ctx ctx}
:number {:success (= data value) :ctx ctx}
:string {:success (= data value) :ctx ctx}
:keyword {:success (= data value) :ctx ctx}
# TODO: lists, dicts
:tuple (match-tuple pattern value ctx)
:list (match-list pattern value ctx)
:dict (match-dict pattern value ctx)
:interpolated (match-string pattern value ctx)
:typed (typed pattern value ctx)
))
(set match-pattern match-pattern*)
(defn- lett [ast ctx]
# (print "lett!")
# (pp ast)
(def [patt expr] (ast :data))
(def value (interpret expr ctx))
(def match? (match-pattern patt value))
(if (match? :success)
(do
(merge-into ctx (match? :ctx))
value)
(error {:node ast :value value :msg "no match: let binding"})))
(defn- matchh [ast ctx]
(def [to-match clauses] (ast :data))
(def value (interpret to-match ctx))
(def len (length clauses))
(when (ast :match) (break ((ast :match) 0 value ctx)))
(defn match-fn [i value ctx]
(when (= len i)
(error {:node ast :value value :msg "no match: match form"}))
(def clause (clauses i))
(def [patt guard expr] clause)
(def match? (match-pattern patt value @{:^parent ctx}))
(when (not (match? :success))
(break (match-fn (inc i) value ctx)))
(def body-ctx (match? :ctx))
(def guard? (if guard
(b/bool (interpret guard body-ctx)) true))
(when (not guard?)
(break (match-fn (inc i) value ctx)))
(interpret expr body-ctx))
(set (ast :match) match-fn)
(match-fn 0 value ctx))
(defn- script [ast ctx]
(def lines (ast :data))
(def last-line (last lines))
(for i 0 (-> lines length dec)
(interpret (lines i) ctx))
(interpret last-line ctx))
(defn- block [ast parent]
(def lines (ast :data))
(def last-line (last lines))
(def ctx @{:^parent parent})
(for i 0 (-> lines length dec)
(interpret (lines i) ctx))
(interpret last-line ctx))
(defn- to_string [ctx] (fn [x]
(if (buffer? x)
(string x)
(b/stringify (interpret x ctx)))))
(defn- interpolated [ast ctx]
(def terms (ast :data))
(def interpolations (map (to_string ctx) terms))
(string/join interpolations))
(defn- iff [ast ctx]
(def [condition then else] (ast :data))
(if (b/bool (interpret condition ctx))
(interpret then ctx)
(interpret else ctx)))
# TODO: use a tail call here
(defn- whenn [ast ctx]
(def clauses (ast :data))
(var result :^nothing)
(each clause clauses
(def [lhs rhs] clause)
(when (b/bool (interpret lhs ctx))
(set result (interpret rhs ctx))
(break)))
(when (= result :^nothing)
(error {:node ast :msg "no match: when form"}))
result)
(defn- word [ast ctx]
(def resolved (resolve-name (ast :data) ctx))
(if (= :^not-found resolved)
(error {:node ast :msg "unbound name"})
resolved))
(defn- tup [ast ctx]
(def members (ast :data))
(def the-tup @[])
(each member members
(array/push the-tup (interpret member ctx)))
[;the-tup])
(defn- args [ast ctx]
(def members (ast :data))
(def the-args @[])
(each member members
(array/push the-args (interpret member ctx)))
(if (ast :partial)
{:^type :partial :args the-args}
[;the-args]))
(defn- sett [ast ctx]
(def members (ast :data))
(def the-set @{:^type :set})
(each member members
(def value (interpret member ctx))
(set (the-set value) true))
the-set)
(defn- list [ast ctx]
(def members (ast :data))
(def the-list @[])
(each member members
(if (= :splat (member :type))
(do
(def splatted (interpret (member :data) ctx))
(when (not= :array (type splatted))
(error {:node member :msg "cannot splat non-list into list"}))
(array/concat the-list splatted))
(array/push the-list (interpret member ctx))))
the-list)
(defn- dict [ast ctx]
(def members (ast :data))
(def the-dict @{})
(each member members
(if (= :splat (member :type))
(do
(def splatted (interpret (member :data) ctx))
(when (or
(not= :table (type splatted))
(:^type splatted))
(error {:node member :msg "cannot splat non-dict into dict"}))
(merge-into the-dict splatted))
(do
(def [key-ast value-ast] (member :data))
# (print "dict key")
# (pp key-ast)
# (print "dict value")
# (pp value-ast)
(def key (interpret key-ast ctx))
(def value (interpret value-ast ctx))
(set (the-dict key) value))))
the-dict)
(defn- box [ast ctx]
(def {:data value-ast :name name} ast)
(def value (interpret value-ast ctx))
(def box @{:^type :box :^value value :name name})
(set (ctx name) box)
box)
(defn- repeatt [ast ctx]
(def [times-ast body] (ast :data))
(def times (interpret times-ast ctx))
(when (not (number? times))
(error {:node times-ast :msg (string "repeat needs a `number` of times; you gave me a " (type times))}))
(repeat times (interpret body ctx)))
(defn- panic [ast ctx]
(def info (interpret (ast :data) ctx))
(error {:node ast :msg info}))
# TODO: add docstrings & pattern docs to fns
# Depends on: good string representation of patterns
# For now, this should be enough to tall the thing
(defn- fnn [ast ctx]
(def {:name name :data clauses :doc doc} ast)
# (print "defining fn " name)
(def closure (merge ctx))
(def the-fn @{:name name :^type :fn :body clauses :ctx closure :doc doc})
(when (not= :^not-found (resolve-name name ctx))
# (print "fn "name" was forward declared")
(def fwd (resolve-name name ctx))
(set (fwd :body) clauses)
(set (fwd :ctx) closure)
(set (fwd :doc) doc)
# (print "fn " name " has been defined")
# (pp fwd)
(break fwd))
# (pp the-fn)
(set (closure name) the-fn)
(set (ctx name) the-fn)
the-fn)
(defn- is_placeholder [x] (= x :_))
(var call-fn nil)
(defn- partial [root-ast the-fn partial-args]
(when (the-fn :applied)
(error {:msg "cannot partially apply a partially applied function"
:node root-ast :called the-fn :args partial-args}))
# (print "calling partially applied function")
(def args (partial-args :args))
# (pp args)
(def pos (find-index is_placeholder args))
(def name (string (the-fn :name) " *partial*"))
(defn partial-fn [root-ast missing]
# (print "calling function with arg " (b/show missing))
# (pp partial-args)
(def full-args (array/slice args))
(set (full-args pos) missing)
# (print "all args: " (b/show full-args))
(call-fn root-ast the-fn [;full-args]))
{:^type :fn :applied true :name name :body partial-fn})
(defn- call-fn* [root-ast the-fn args]
# (print "on line " (get-in root-ast [:token :line]))
# (print "calling " (b/show the-fn))
# (print "with args " (b/show args))
# (pp args)
(when (or
(= :function (type the-fn))
(= :cfunction (type the-fn)))
# (print "Janet function")
(break (the-fn ;args)))
(def clauses (the-fn :body))
(when (= :nothing clauses)
(error {:node root-ast :called the-fn :value args :msg "cannot call function before it is defined"}))
(when (= :function (type clauses))
(break (clauses root-ast ;args)))
(def len (length clauses))
(when (the-fn :match) (break ((the-fn :match) root-ast 0 args)))
(defn match-fn [root-ast i args]
(when (= len i)
(error {:node root-ast :called the-fn :value args :msg "no match: function call"}))
(def clause (clauses i))
(def [patt guard expr] clause)
(def match?
(match-pattern patt args @{:^parent (the-fn :ctx)}))
(when (not (match? :success))
(break (match-fn root-ast (inc i) args)))
# (print "matched!")
(def body-ctx (match? :ctx))
(def guard? (if guard
(b/bool (interpret guard body-ctx)) true))
# (print "passed guard")
(when (not guard?)
(break (match-fn root-ast (inc i) args)))
(interpret expr body-ctx))
(set (the-fn :match) match-fn)
(match-fn root-ast 0 args))
(set call-fn call-fn*)
(defn- call-partial [root-ast the-fn arg] ((the-fn :body) root-ast ;arg))
(defn- apply-synth-term [root-ast prev curr]
# (print "applying " (b/show prev))
# (print "to" (b/show curr))
(def types [(b/ludus/type prev) (b/ludus/type curr)])
# (print "typle:")
# (pp types)
(match types
[:fn :tuple] (call-fn root-ast prev curr)
[:fn :partial] (partial root-ast prev curr)
[:function :tuple] (call-fn root-ast prev curr)
# [:applied :tuple] (call-partial root-ast prev curr)
[:keyword :args] (get (first curr) prev :^nil)
[:keyword :tuple] (get (first curr) prev :^nil)
[:dict :keyword] (get prev curr :^nil)
[:nil :keyword] :^nil
[:pkg :keyword] (get prev curr :^nil)
[:pkg :pkg-kw] (get prev curr :^nil)
(error (string "cannot call " (b/ludus/type prev) " `" (b/show prev) "`"))))
(defn- synthetic [ast ctx]
(def terms (ast :data))
# (print "interpreting synthetic")
# (pp ast)
# (pp terms)
(def first-term (first terms))
(def last-term (last terms))
(var prev (interpret first-term ctx))
# (print "root term: ")
# (pp prev)
(for i 1 (-> terms length dec)
(def curr (interpret (terms i) ctx))
# (print "term " i ": " curr)
(set prev (apply-synth-term first-term prev curr)))
# (print "done with inner terms, applying last term")
(apply-synth-term first-term prev (interpret last-term ctx)))
(defn- doo [ast ctx]
(def terms (ast :data))
(var prev (interpret (first terms) ctx))
(def last-term (last terms))
(for i 1 (-> terms length dec)
(def curr (interpret (terms i) ctx))
(set prev (apply-synth-term (first terms) curr [prev])))
(def last-fn (interpret last-term ctx))
(apply-synth-term (first terms) last-fn [prev]))
(defn- pkg [ast ctx]
(def members (ast :data))
(def the-pkg @{:^name (ast :name) :^type :pkg})
(each member members
(def [key-ast value-ast] (member :data))
(def key (interpret key-ast ctx))
(def value (interpret value-ast ctx))
(set (the-pkg key) value))
# (pp the-pkg)
(def out (table/to-struct the-pkg))
(set (ctx (ast :name)) out)
out)
(defn- loopp [ast ctx]
# (print "looping!")
(def data (ast :data))
(def args (interpret (data 0) ctx))
# this doesn't work: context persists between different interpretations
# we want functions to work this way, but not loops (I think)
# (when (ast :match) (break ((ast :match) 0 args)))
(def clauses (data 1))
(def len (length clauses))
(var loop-ctx @{:^parent ctx})
(defn match-fn [i args]
(when (= len i)
(error {:node ast :value args :msg "no match: loop"}))
(def clause (clauses i))
(def [patt guard expr] clause)
(def match?
(match-pattern patt args loop-ctx))
(when (not (match? :success))
# (print "no match")
(break (match-fn (inc i) args)))
# (print "matched!")
(def body-ctx (match? :ctx))
(def guard? (if guard
(b/bool (interpret guard body-ctx)) true))
# (print "passed guard")
(when (not guard?)
(break (match-fn (inc i) args)))
(interpret expr body-ctx))
(set (ast :match) match-fn)
(set (loop-ctx :^recur) match-fn)
# (print "ATTACHED MATCH-FN")
(match-fn 0 args))
(defn- recur [ast ctx]
# (print "recurring!")
(def passed (ast :data))
(def args (interpret passed ctx))
(def match-fn (resolve-name :^recur ctx))
# (print "match fn in ctx:")
# (pp (ctx :^recur))
# (pp match-fn)
# (pp ctx)
(match-fn 0 args))
# TODO for 0.1.0
(defn- testt [ast ctx] (todo "test"))
(defn- ns [ast ctx] (todo "nses"))
(defn- importt [ast ctx] (todo "imports"))
(defn- withh [ast ctx] (todo "with"))
(defn- usee [ast ctx] (todo "use"))
(defn- interpret* [ast ctx]
# (print "interpreting node " (ast :type))
(case (ast :type)
# literals
:nil :^nil
:number (ast :data)
:bool (ast :data)
:string (ast :data)
:keyword (ast :data)
:placeholder :_
# collections
:tuple (tup ast ctx)
:args (args ast ctx)
:list (list ast ctx)
:set (sett ast ctx)
:dict (dict ast ctx)
# composite forms
:if (iff ast ctx)
:block (block ast ctx)
:when (whenn ast ctx)
:script (script ast ctx)
:panic (panic ast ctx)
# looping forms
:loop (loopp ast ctx)
:recur (recur ast ctx)
:repeat (repeatt ast ctx)
# named/naming forms
:word (word ast ctx)
:interpolated (interpolated ast ctx)
:box (box ast ctx)
:pkg (pkg ast ctx)
:pkg-name (word ast ctx)
# patterned forms
:let (lett ast ctx)
:match (matchh ast ctx)
# functions
:fn (fnn ast ctx)
# synthetic
:synthetic (synthetic ast ctx)
# do
:do (doo ast ctx)
# deferred until after computer class
# :with (withh ast ctx)
# :import (importt ast ctx)
# :ns (ns ast ctx)
# :use (usee ast ctx)
# :test (testt ast ctx)
))
(set interpret interpret*)
# # repl
# (import /src/scanner :as s)
# (import /src/parser :as p)
# (import /src/validate :as v)
# (var source nil)
# (defn- has-errors? [{:errors errors}] (and errors (not (empty? errors))))
# (defn run []
# (def scanned (s/scan source))
# (when (has-errors? scanned) (break (scanned :errors)))
# (def parsed (p/parse scanned))
# (when (has-errors? parsed) (break (parsed :errors)))
# (def validated (v/valid parsed b/ctx))
# # (when (has-errors? validated) (break (validated :errors)))
# # (def cleaned (get-in parsed [:ast :data 1]))
# # # (pp cleaned)
# (interpret (parsed :ast) @{:^parent b/lett})
# # (try (interpret (parsed :ast) @{:^parent b/ctx})
# # ([e] (if (struct? e) (error (e :msg)) (error e))))
# )
# # (do
# (comment
# (set source `
# let foo = 42
# "{foo} bar baz"
# `)
# (def result (run))
# )

131
src/json.janet Normal file
View File

@ -0,0 +1,131 @@
# pulled from cfiggers/jayson
(defmacro- letv [bindings & body]
~(do ,;(seq [[k v] :in (partition 2 bindings)] ['var k v]) ,;body))
(defn- read-hex [n]
(scan-number (string "0x" n)))
(defn- check-utf-16 [capture]
(let [u (read-hex capture)]
(if (and (>= u 0xD800)
(<= u 0xDBFF))
capture
false)))
(def- utf-8->bytes
(peg/compile
~{:double-u-esc (/ (* "\\u" (cmt (<- 4) ,|(check-utf-16 $)) "\\u" (<- 4))
,|(+ (blshift (- (read-hex $0) 0xD800) 10)
(- (read-hex $1) 0xDC00) 0x10000))
:single-u-esc (/ (* "\\u" (<- 4)) ,|(read-hex $))
:unicode-esc (/ (+ :double-u-esc :single-u-esc)
,|(string/from-bytes
;(cond
(<= $ 0x7f) [$]
(<= $ 0x7ff)
[(bor (band (brshift $ 6) 0x1F) 0xC0)
(bor (band (brshift $ 0) 0x3F) 0x80)]
(<= $ 0xffff)
[(bor (band (brshift $ 12) 0x0F) 0xE0)
(bor (band (brshift $ 6) 0x3F) 0x80)
(bor (band (brshift $ 0) 0x3F) 0x80)]
# Otherwise
[(bor (band (brshift $ 18) 0x07) 0xF0)
(bor (band (brshift $ 12) 0x3F) 0x80)
(bor (band (brshift $ 6) 0x3F) 0x80)
(bor (band (brshift $ 0) 0x3F) 0x80)])))
:escape (/ (* "\\" (<- (set "avbnfrt\"\\/")))
,|(get {"a" "\a" "v" "\v" "b" "\b"
"n" "\n" "f" "\f" "r" "\r"
"t" "\t"} $ $))
:main (+ (some (+ :unicode-esc :escape (<- 1))) -1)}))
(defn decode
``
Returns a janet object after parsing JSON. If `keywords` is truthy,
string keys will be converted to keywords. If `nils` is truthy, `null`
will become `nil` instead of the keyword `:json/null`.
``
[json-source &opt keywords nils]
(def json-parser
{:null (if nils
~(/ (<- (+ "null" "Null")) nil)
~(/ (<- (+ "null" "Null")) :json/null))
:bool-t ~(/ (<- (+ "true")) true)
:bool-f ~(/ (<- (+ "false")) false)
:number ~(/ (<- (* (? "-") :d+ (? (* "." :d+)))) ,|(scan-number $))
:string ~(/ (* "\"" (<- (to (* (> -1 (not "\\")) "\"")))
(* (> -1 (not "\\")) "\""))
,|(string/join (peg/match utf-8->bytes $)))
:array ~(/ (* "[" :s* (? (* :value (any (* :s* "," :value)))) "]") ,|(array ;$&))
:key-value (if keywords
~(* :s* (/ :string ,|(keyword $)) :s* ":" :value)
~(* :s* :string :s* ":" :value))
:object ~(/ (* "{" :s* (? (* :key-value (any (* :s* "," :key-value)))) "}")
,|(from-pairs (partition 2 $&)))
:value ~(* :s* (+ :null :bool-t :bool-f :number :string :array :object) :s*)
:unmatched ~(/ (<- (to (+ :value -1))) ,|[:unmatched $])
:main ~(some (+ :value "\n" :unmatched))})
(first (peg/match (peg/compile json-parser) json-source)))
(def- bytes->utf-8
(peg/compile
~{:four-byte (/ (* (<- (range "\xf0\xff")) (<- 1) (<- 1) (<- 1))
,|(bor (blshift (band (first $0) 0x07) 18)
(blshift (band (first $1) 0x3F) 12)
(blshift (band (first $2) 0x3F) 6)
(blshift (band (first $3) 0x3F) 0)))
:three-byte (/ (* (<- (range "\xe0\xef")) (<- 1) (<- 1))
,|(bor (blshift (band (first $0) 0x0F) 12)
(blshift (band (first $1) 0x3F) 6)
(blshift (band (first $2) 0x3F) 0)))
:two-byte (/ (* (<- (range "\x80\xdf")) (<- 1))
,|(bor (blshift (band (first $0) 0x1F) 6)
(blshift (band (first $1) 0x3F) 0)))
:multi-byte (/ (+ :two-byte :three-byte :four-byte)
,|(if (< $ 0x10000)
(string/format "\\u%04X" $)
(string/format "\\u%04X\\u%04X"
(+ (brshift (- $ 0x10000) 10) 0xD800)
(+ (band (- $ 0x10000) 0x3FF) 0xDC00))))
:one-byte (<- (range "\x20\x7f"))
:0to31 (/ (<- (range "\0\x1F"))
,|(or ({"\a" "\\u0007" "\b" "\\u0008"
"\t" "\\u0009" "\n" "\\u000A"
"\v" "\\u000B" "\f" "\\u000C"
"\r" "\\u000D"} $)
(string/format "\\u%04X" (first $))))
:backslash (/ (<- "\\") "\\\\")
:quote (/ (<- "\"") "\\\"")
:main (+ (some (+ :0to31 :backslash :quote :one-byte :multi-byte)) -1)}))
(defn- encodeone [x depth]
(if (> depth 1024) (error "recurred too deeply"))
(cond
(= x :json/null) "null"
(= x nil) "null"
(bytes? x) (string "\"" (string/join (peg/match bytes->utf-8 x)) "\"")
(indexed? x) (string "[" (string/join (map |(encodeone $ (inc depth)) x) ",") "]")
(dictionary? x) (string "{" (string/join
(seq [[k v] :in (pairs x)]
(string "\"" (string/join (peg/match bytes->utf-8 k)) "\"" ":" (encodeone v (inc depth)))) ",") "}")
(case (type x)
:nil "null"
:boolean (string x)
:number (string x)
(error "type not supported"))))
(defn encode
``
Encodes a janet value in JSON (utf-8). If `buf` is provided, the formated
JSON is append to `buf` instead of a new buffer. Returns the modifed buffer.
``
[x &opt buf]
(letv [ret (encodeone x 0)]
(if (and buf (buffer? buf))
(buffer/push ret)
(thaw ret))))

75
src/ludus.janet Normal file
View File

@ -0,0 +1,75 @@
# an integrated Ludus interpreter
# devised in order to run under wasm
# takes a string, returns a string with a json object
# (try (os/cd "janet") ([_] nil)) # for REPL
(import /src/scanner :as s)
(import /src/parser :as p)
(import /src/validate :as v)
(import /src/interpreter :as i)
(import /src/errors :as e)
(import /src/base :as b)
(import /src/prelude :as prelude)
(import /src/json :as j)
(defn ludus [source]
(when (= :error prelude/pkg) (error "could not load prelude"))
(def ctx @{:^parent prelude/ctx})
(def errors @[])
(def draw @[])
(var result @"")
(def console @"")
(setdyn :out console)
(def out @{:errors errors :draw draw :result result :console console})
(def scanned (s/scan source))
(when (any? (scanned :errors))
(each err (scanned :errors)
(e/scan-error err))
(break (-> out j/encode string)))
(def parsed (p/parse scanned))
(when (any? (parsed :errors))
(each err (parsed :errors)
(e/parse-error err))
(break (-> out j/encode string)))
(def validated (v/valid parsed ctx))
(when (any? (validated :errors))
(each err (validated :errors)
(e/validation-error err))
(break (-> out j/encode string)))
(try
(set result (i/interpret (parsed :ast) ctx))
([err]
(e/runtime-error err)
(break (-> out j/encode string))))
(setdyn :out stdout)
(set (out :result) (b/show result))
(var post @{})
(try
(set post (i/interpret prelude/post/ast ctx))
([err] (e/runtime-error err)))
(set (out :draw) (post :draw))
# out
(-> out j/encode string)
)
(comment
# (do
# (def start (os/clock))
(def source `
box foo = :bar
store! (foo, :baz)
unbox (foo)
`)
(def out (-> source
ludus
j/decode
))
# (def end (os/clock))
(setdyn :out stdout)
(pp out)
(def console (out "console"))
(print console)
(def result (out "result"))
(print result)
# (print (- end start))
)

View File

@ -1,2 +0,0 @@
(ns ludus.ast)

View File

@ -1,33 +0,0 @@
(ns ludus.core
"A tree-walk interpreter for the Ludus language."
(:require
[ludus.scanner :as scanner]))
(defn- report [line, where, message]
(println (str "[line " line "] Error" where ": " message)))
(defn- error [line, message]
(report line "" message))
(defn- run [source]
(let [tokens (scanner/scan source)]
(run! println tokens)))
(defn- run-file [path]
(let [source (slurp path)]
(run source)))
(defn- run-prompt []
(loop [_ ""]
(print "Ludus >> ")
(flush)
(when-let [line (read-line)]
(recur (run line)))))
(defn -main [& args]
(cond
(> (count args) 1) (do
(println "Usage: ludus [script]")
(System/exit 64))
(= (count args) 1) (run-file (first args))
:else (run-prompt)))

View File

@ -1,253 +0,0 @@
(ns ludus.parser
(:require
[ludus.token :as token]
[ludus.scanner :as scanner]
[ludus.ast :as ast]
[clojure.pprint :as pp]))
(defn- parser [tokens]
{::tokens tokens ::token 0 ::ast {}})
(defn- current [parser]
(nth (::tokens parser) (::token parser) nil))
(defn- next [parser]
(nth (::tokens parser) (inc (::token parser)) nil))
(defn- at-end? [parser]
(let [curr (current parser)]
(or (nil? curr) (= ::token/eof (::token/type curr)))))
(defn- advance [parser]
(update parser ::token inc))
(defn- token-type [parser]
(::token/type (current parser)))
(defn- expect [message tokens parser]
(let [curr (current parser)]
(if (contains? tokens (::token/type curr))
parser
(assoc parser ::ast {
::ast/type ::ast/poison
:message (str "Expected " message)
:token curr}))))
(defn- expect+ [message tokens parser]
(print "Epxecting ")
(pp/pprint tokens)
(let [curr (current parser)
next (next parser)]
(println "Current: " curr " Next: " next)
(if (contains? tokens (::token/type curr))
(if (contains? tokens (::token/type next))
(recur message tokens (advance parser))
parser)
(assoc parser ::ast {
::ast/type ::ast/poison
:message (str "Expected " message)
:token curr}))))
(defn- parse-atom [parser token]
(-> parser
(advance)
(assoc ::ast {
::ast/type ::ast/atom
:value (::token/literal token)})))
;; just a quick and dirty map to associate atomic words with values
(def atomic-words {
::token/nil nil
::token/true true
::token/false false})
(defn parse-atomic-word [parser token]
(-> parser
(advance)
(assoc ::ast {
::ast/type ::ast/atom
:value (get atomic-words (::token/type token))})))
(declare parse-expr)
(defn- add-member [members member]
(if (nil? member)
members
(conj members member)))
(defn- parse-tuple [parser]
(loop [parser (advance parser)
members []
current_member nil]
(println "***parsing tuple with ")
(print "parser ") (pp/pprint parser)
(print "members ") (pp/pprint members)
(print "current member ")(pp/pprint current_member)
(let [curr (current parser)]
(case (::token/type curr)
::token/rparen (let [ms (add-member members current_member)]
(assoc (advance parser) ::ast
{::ast/type ::ast/tuple
:length (count ms)
:members ms}))
(::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil)
(let [parsed (parse-expr parser)]
(print "Got to expr with")
(pp/pprint (::ast parsed))
(recur parsed members (::ast parsed)))
))))
(defn- parse-list [parser]
(loop [parser (advance parser)
members []
current_member nil]
(println "***parsing list with ")
(print "parser ") (pp/pprint parser)
(print "members ") (pp/pprint members)
(print "current member ")(pp/pprint current_member)
(let [curr (current parser)]
(case (::token/type curr)
::token/rbracket (let [ms (add-member members current_member)]
(assoc (advance parser) ::ast
{::ast/type ::ast/list
:members ms}))
(::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil)
(let [parsed (parse-expr parser)]
(print "Got to expr with ") (pp/pprint (::ast parsed))
(recur parsed members (::ast parsed)))
))))
(defn- parse-set [parser]
(loop [parser (advance parser)
members []
current_member nil]
(println "***parsing set with ")
(print "parser ") (pp/pprint parser)
(print "members ") (pp/pprint members)
(print "current member ")(pp/pprint current_member)
(let [curr (current parser)]
(case (::token/type curr)
::token/rbrace (let [ms (add-member members current_member)]
(assoc (advance parser) ::ast
{::ast/type ::ast/set
:members ms}))
(::token/comma ::token/newline) (recur (advance parser) (add-member members current_member) nil)
(let [parsed (parse-expr parser)]
(print "Got to expr with ") (pp/pprint (::ast parsed))
(recur parsed members (::ast parsed)))
))))
(defn- parse-block [parser]
(println "*** *** parsing block")
(loop [parser (advance parser)
exprs []
current_expr nil]
(print "current ")(pp/pprint (current parser))
(case (::token/type (current parser))
::token/rbrace
(assoc (advance parser) ::ast
{::ast/type ::ast/block :exprs (add-member exprs current_expr)})
(::token/semicolon ::token/newline)
(recur (advance parser) (add-member exprs current_expr) nil)
(let [parsed (parse-expr parser)]
(recur parsed exprs (::ast parsed)))
)))
(defn- parse-script [parser]
(println "*** *** *** parsing script")
(loop [parser parser
exprs []
current_expr nil]
(case (::token/type (current parser))
::token/eof (assoc parser ::ast
{::ast/type ::ast/script :exprs (add-member exprs current_expr)})
(::token/semicolon ::token/newline)
(recur (advance parser) (add-member exprs current_expr) nil)
(let [parsed (parse-expr parser)]
(recur parsed exprs (::ast parsed)))
)))
(defn- parse-expr [parser]
(loop [parser parser]
(let [token (current parser)]
(println "Parsing expr " (::token/type token))
(case (::token/type token)
(::token/number ::token/string ::token/keyword)
(parse-atom parser token)
(::token/nil ::token/true ::token/false)
(parse-atomic-word parser token)
::token/lparen (parse-tuple parser)
::token/lbracket (parse-list parser)
::token/startset (parse-set parser)
::token/lbrace (parse-block parser)
))))
(def source "{1; 2; :foo}\n{1}")
(def tokens (:tokens (scanner/scan source)))
(def p (parser tokens))
(pp/pprint p)
(-> (parse-script p)
(::ast)
(:exprs))
(comment "
Just leaving this note here for myself. I'm too tired to write more code, but I want not to lose momentum. So:
Next, we'll need to look at synthetic expressions. Their first terms are always keywords or words.
Take the case of keywords:
* if the next token is NOT a (, then return the atomic keyword
* if it is, then you're in a synthetic expression
Take the case of words:
* if the next token is NOT : or (, then return the atomic word
* if it is, then you're in a synthetic expression
A synthetic expression is a chain of calls and property accesses.
That means you can parse, starting from either a keyword or a word, any combination of tuples and keywords, in any order.
Until, that is, you reach an expression terminator (of any kind): eof newline ; } )
Tuples of this kind are special in each case:
* Tuples after *initial* keywords may only be of length 1 (b/c that's a called keyword; subsequent keywords in synthetic expressions are property accesses)
* All other tuples may contain exactly one or zero placeholders as members
* We want both of these to be parse errors (should these be at parsing or subsequent correctness checks)
Other quick thoughts:
* Blocks must have at least one expression. This should be a parse error.
* `let`s with literal matching and word matching are easy. Parsing all the patterns comes later.
* The first conditional form to parse is `if` (b/c no patterns) (but it does have funny scoping!)
* Once I get this far, then it's time to wire up the interpreter (with hard-coded functions, and the beginning of static analysis)
* ALSO: time to start working on parsing errors. (poisoned nodes, panic mode, etc.)
For future correctness checks:
* Early (even as part of wiring up the interpreter), begin the static analysis check for unbound names, redeclaration
* Compound `loop` and `gen` forms must have LHS's (tuple patterns) of the same length
")

View File

@ -1,297 +0,0 @@
(ns ludus.scanner
(:require
[ludus.token :as token]
[clojure.pprint :as pp]
[clojure.edn :as edn]
[clojure.string :as s]))
(def reserved-words
"List of Ludus reserved words."
;; see ludus-spec repo for more info
{
"as" ::token/as
"cond" ::token/cond
"else" ::token/else
"false" ::token/false
"fn" ::token/fn
"if" ::token/if
"let" ::token/let
"match" ::token/match
"mut" ::token/mut
"nil" ::token/nil
"panic!" ::token/panic
"then" ::token/then
"true" ::token/true
"var" ::token/var
"with" ::token/with
;; below here, probable
"defer" ::token/defer
"gen" ::token/gen
"loop" ::token/loop
"ns" ::token/ns
"recur" ::token/recur
"repeat" ::token/repeat
"test" ::token/test
"wait" ::token/wait
"yield" ::token/yield
;; below here, possible
"when" ::token/when
})
(defn- new-scanner
"Creates a new scanner."
[source]
{::source source
::length (count source)
::errors []
::start 0
::current 0
::line 1
::tokens []})
(defn- at-end?
"Tests if a scanner is at end of input."
[scanner]
(>= (::current scanner) (::length scanner)))
(defn- current-char
"Gets the current character of the scanner."
[scanner]
(nth (::source scanner) (::current scanner) nil))
(defn- advance
"Advances the scanner by a single character."
[scanner]
(update scanner ::current inc))
(defn- next-char
"Gets the next character from the scanner."
[scanner]
(current-char (advance scanner)))
(defn- current-lexeme
[scanner]
(subs (::source scanner) (::start scanner) (::current scanner)))
(defn- char-in-range? [start end char]
(and char
(>= (int char) (int start))
(<= (int char) (int end))))
(defn- digit? [c]
(char-in-range? \0 \9 c))
(defn- nonzero-digit? [c]
(char-in-range? \1 \9 c))
;; for now, use very basic ASCII charset in words
;; TODO: research the implications of using the whole
;; (defn- alpha? [c] (boolean (re-find #"\p{L}" (str c))))
(defn- alpha? [c]
(or (char-in-range? \a \z c) (char-in-range? \A \Z c)))
;; legal characters in words
(def word-chars #{\_ \? \! \* \/})
(defn- word-char? [c]
(or (alpha? c) (digit? c) (contains? word-chars c)))
(defn- whitespace? [c]
(or (= c \space) (= c \tab)))
(def terminators #{\: \; \newline \{ \} \( \) \[ \] \$ \# \- \< \& \, \| nil \\})
(defn- terminates? [c]
(or (whitespace? c) (contains? terminators c)))
(defn- add-token
([scanner token-type]
(add-token scanner token-type nil))
([scanner token-type literal]
(update scanner ::tokens conj
(token/token
token-type
(current-lexeme scanner)
literal
(::line scanner)
(::start scanner)))))
;; TODO: errors should also be in the vector of tokens
;; The goal is to be able to be able to hand this to an LSP?
;; Do we need a different structure
(defn- add-error [scanner msg]
(update scanner ::errors conj {:msg msg :line (::line scanner) :start (::start scanner)}))
(defn- add-keyword
[scanner]
(loop [scanner scanner
key ""]
(let [char (current-char scanner)]
(cond
(terminates? char) (add-token scanner ::token/keyword (keyword key))
(word-char? char) (recur (advance scanner) (str key char))
:else (add-error scanner "Unexpected " char "after keyword :" key)))))
;; TODO: improve number parsing?
;; This will currently parse 000001 as 1. Is that correct behaviour?
(defn- add-number [char scanner]
(loop [scanner scanner
num (str char)
float? false]
(let [curr (current-char scanner)]
(cond
(= curr \_) (recur (advance scanner) num float?) ;; consume underscores unharmed
(= curr \.) (if float?
(add-error scanner (str "Unexpected second decimal point after " num "."))
(recur (advance scanner) (str num curr) true))
(terminates? curr) (add-token scanner ::token/number (edn/read-string num))
(digit? curr) (recur (advance scanner) (str num curr) float?)
:else (add-error scanner (str "Unexpected " curr " after number " num "."))))))
;; TODO: add string interpolation
;; This still has to be devised
(defn- add-string
[scanner]
(loop [scanner scanner
string ""]
(let [char (current-char scanner)]
(case char
\newline (add-error scanner "Unterminated string.")
\" (add-token (advance scanner) ::token/string string)
\\ (let [next (next-char scanner)
scanner (if (= next \newline)
(update scanner ::line inc)
scanner)]
(recur (advance (advance scanner)) (str string next)))
(if (at-end? scanner)
(add-error scanner "Unterminated string.")
(recur (advance scanner) (str string char)))))))
(defn- add-word
[char scanner]
(loop [scanner scanner
word (str char)]
(let [curr (current-char scanner)]
(cond
(terminates? curr) (add-token scanner (get reserved-words word ::token/word))
(word-char? curr) (recur (advance scanner) (str word curr))
:else (add-error scanner (str "Unexpected " curr " after word " word "."))))))
(defn- add-ignored
[scanner]
(loop [scanner scanner
ignored "_"]
(let [char (current-char scanner)]
(cond
(terminates? char) (add-token scanner ::token/ignored)
(word-char? char) (recur (advance scanner) (str ignored char))
:else (add-error scanner (str "Unexpected " char " after word " ignored "."))))))
(defn- add-comment [char scanner]
(loop [scanner scanner
comm (str char)]
(let [char (current-char scanner)]
(if (= \newline char)
(if (s/starts-with? comm "&&&")
(add-token (update scanner ::line inc) ::token/docstring)
(add-token (update scanner ::line inc) ::token/comment))
(recur (advance scanner) (str comm char))))))
(defn- scan-token [scanner]
(let [char (current-char scanner)
scanner (advance scanner)
next (current-char scanner)]
(case char
;; one-character tokens
\( (add-token scanner ::token/lparen)
\) (add-token scanner ::token/rparen)
\{ (add-token scanner ::token/lbrace)
\} (add-token scanner ::token/rbrace)
\[ (add-token scanner ::token/lbracket)
\] (add-token scanner ::token/rbracket)
\; (add-token scanner ::token/semicolon)
\, (add-token scanner ::token/comma)
\newline (add-token (update scanner ::line inc) ::token/newline)
\\ (add-token scanner ::token/backslash)
;; two-character tokens
;; ->
\- (cond
(= next \>) (add-token (advance scanner) ::token/rarrow)
(digit? next) (add-number char scanner)
:else (add-error scanner (str "Expected -> or negative number. Got " char next)))
;; <-
\< (if (= next \-)
(add-token (advance scanner) ::token/larrow)
(add-error scanner (str "Expected <-. Got " char next)))
;; |>
\| (if (= next \>)
(add-token (advance scanner) ::token/pipeline)
(add-error scanner (str "Expected |>. Got " char next)))
;; possible additional operator: bind/result
;; possible additional operator: bind/some
;; oh god, monads
;; additional arrow possibilities: >> ||> ~> => !>
;; hashmap #{
\# (if (= next \{)
(add-token (advance scanner) ::token/starthash)
(add-error scanner (str "Expected beginning of hash: #{. Got " char next)))
;; set ${
\$ (if (= next \{)
(add-token (advance scanner) ::token/startset)
(add-error scanner (str "Expected beginning of set: ${. Got " char next)))
;; placeholders
;; there's a flat _, and then ignored words
\_ (cond
(terminates? next) (add-token scanner ::token/placeholder)
(alpha? next) (add-ignored scanner)
:else (add-error scanner (str "Expected placeholder: _. Got " char next)))
;; comments
;; & starts an inline comment
;; TODO: include comments in scanned file
;; TODO: add doc comments: &&&
\& (add-comment char scanner)
;; keywords
;; TODO: instead of a separate token, scan a whole type keyword
;; e.g. ::string, ::number
\: (cond
;;(= \: next) (add-token (advance scanner) ::token/doublecolon))
(alpha? next) (add-keyword scanner)
:else (add-error scanner (str "Expected keyword. Got " char next)))
;; splats
\. (let [after_next (current-char (advance scanner))]
(if (= ".." (str next after_next))
(add-token (advance (advance scanner)) ::token/splat)
(add-error scanner (str "Expected splat: ... . Got " (str "." next after_next)))))
;; strings
\" (add-string scanner)
;; word matches
(cond
;; for now, don't add whitespace tokens
(digit? char) (add-number char scanner)
(alpha? char) (add-word char scanner)
:else (add-error scanner (str "Unexpected character: " char))))))
(defn- next-token [scanner]
(assoc scanner ::start (::current scanner)))
(defn scan [source]
(loop [scanner (new-scanner source)]
(if (at-end? scanner)
(let [scanner (add-token scanner ::token/eof)]
{:tokens (::tokens scanner)
:errors (::errors scanner)})
(recur (-> scanner (scan-token) (next-token))))))

View File

@ -1,11 +0,0 @@
(ns ludus.token)
(defn token
([type text]
(token type text nil 1))
([type text literal line start]
{::type type
::lexeme text
::literal literal
::line line
::start start}))

1181
src/parser.janet Normal file

File diff suppressed because it is too large Load Diff

41
src/prelude.janet Normal file
View File

@ -0,0 +1,41 @@
(import /src/base :as b)
(import /src/scanner :as s)
(import /src/parser :as p)
(import /src/validate :as v)
(import /src/interpreter :as i)
(import /src/errors :as e)
(def pkg (do
(def pre-ctx @{:^parent {"base" b/base}})
(def pre-src (slurp "prelude.ld"))
(def pre-scanned (s/scan pre-src :prelude))
(def pre-parsed (p/parse pre-scanned))
(def parse-errors (pre-parsed :errors))
(when (any? parse-errors) (each err parse-errors (e/parse-error err)) (break :error))
(def pre-validated (v/valid pre-parsed pre-ctx))
(def validation-errors (pre-validated :errors))
(when (any? validation-errors) (each err validation-errors (e/validation-error err)) (break :error))
(try
(i/interpret (pre-parsed :ast) pre-ctx)
([err] (e/runtime-error err) :error))))
(def ctx (do
(def ctx @{})
(each [k v] (pairs pkg)
(set (ctx (string k)) v))
(set (ctx "^name") nil)
(set (ctx "^type") nil)
ctx))
(def post/src (slurp "postlude.ld"))
(def post/ast (do
(def post-ctx @{:^parent ctx})
(def post-scanned (s/scan post/src :postlude))
(def post-parsed (p/parse post-scanned))
(def parse-errors (post-parsed :errors))
(when (any? parse-errors) (each err parse-errors (e/parse-error err)) (break :error))
(def post-validated (v/valid post-parsed post-ctx))
(def validation-errors (post-validated :errors))
(when (any? validation-errors) (each err validation-errors (e/validation-error err)) (break :error))
(post-parsed :ast)))

9
src/project.janet Normal file
View File

@ -0,0 +1,9 @@
(declare-project
:dependencies [
{:url "https://github.com/ianthehenry/judge.git"
:tag "v2.8.1"}
{:url "https://github.com/janet-lang/spork"}
])
(declare-source
:source ["ludus.janet"])

356
src/scanner.janet Normal file
View File

@ -0,0 +1,356 @@
(def reserved-words
"List of Ludus reserved words."
## see ludus-spec repo for more info
{
"as" :as ## impl
"box" :box
"do" :do ## impl
"else" :else ## impl
"false" :false ## impl -> literal word
"fn" :fn ## impl
"if" :if ## impl
"import" :import ## impl
"let" :let ## impl
"loop" :loop ## impl
"match" :match ## impl
"nil" :nil ## impl -> literal word
"ns" :ns ## impl
"panic!" :panic ## impl (should _not_ be a function)
"pkg" :pkg
"recur" :recur ## impl
"repeat" :repeat ## impl
"test" :test
"then" :then ## impl
"true" :true ## impl -> literal word
"use" :use ## wip
"when" :when ## impl, replaces cond
"with" :with ## impl
})
(def literal-words {"true" true
"false" false
"nil" nil
})
(defn- new-scanner
"Creates a new scanner."
[source input]
@{:source source
:input input
:length (length source)
:errors @[]
:start 0
:current 0
:line 1
:tokens @[]})
(defn- at-end?
"Tests if a scanner is at end of input."
[scanner]
(>= (get scanner :current) (get scanner :length)))
(defn- current-char
"Gets the current character of the scanner."
[scanner]
(let [source (get scanner :source)
current (get scanner :current)
length (length source)]
(if (>= current length)
nil
(string/from-bytes (get source current)))))
(defn- advance
"Advances the scanner by a single character."
[scanner]
(update scanner :current inc))
(defn- next-char
"Gets the next character from the scanner."
[scanner]
(let [source (get scanner :source)
current (get scanner :current)
next (inc current)
length (length source)]
(if (>= next length)
nil
(string/from-bytes (get source next)))))
(defn- current-lexeme
[scanner]
(slice (get scanner :source) (get scanner :start) (get scanner :current)))
(defn- char-code [char] (get char 0))
(defn- char-in-range? [start end char]
(and char
(>= (char-code char) (char-code start))
(<= (char-code char) (char-code end))))
(defn- digit? [c]
(char-in-range? "0" "9" c))
(defn- nonzero-digit? [c]
(char-in-range? "1" "9" c))
## for now, use very basic ASCII charset in words
## TODO: research the implications of using the whole
## (defn- alpha? [c] (boolean (re-find #"\p{L}" (string c))))
(defn- alpha? [c]
(or (char-in-range? "a" "z" c) (char-in-range? "A" "Z" c)))
(defn- lower? [c] (char-in-range? "a" "z" c))
(defn- upper? [c] (char-in-range? "A" "Z" c))
## legal characters in words
(def word-chars {"_" true "?" true "!" true "*" true "/" true})
(defn- word-char? [c]
(or (alpha? c) (digit? c) (get word-chars c)))
(defn- whitespace? [c]
(or (= c " ") (= c "\t")))
(def terminators {
":" true
";" true
"\n" true
"{" true
"}" true
"(" true
")" true
"[" true
"]" true
"$" true
"#" true
"-" true
"=" true
"&" true
"," true
">" true
"\"" true})
(defn- terminates? [c]
(or (nil? c) (whitespace? c) (get terminators c)))
(defn- add-token
[scanner token-type &opt literal]
(update scanner :tokens array/push
{:type token-type
:lexeme (current-lexeme scanner)
:literal literal
:line (get scanner :line)
:start (get scanner :start)
:source (get scanner :source)
:input (get scanner :input)}))
## TODO: errors should also be in the vector of tokens
## The goal is to be able to be able to hand this to an LSP?
## Do we need a different structure
(defn- add-error [scanner msg]
(let [token {:type :error
:lexeme (current-lexeme scanner)
:literal nil
:line (get scanner :line)
:start (get scanner :start)
:source (get scanner :source)
:input (get scanner :input)
:msg msg}]
(-> scanner
(update :errors array/push token)
(update :tokens array/push token))))
(defn- add-keyword
[scanner]
(defn recur [scanner key]
(let [char (current-char scanner)]
(cond
(terminates? char) (add-token scanner :keyword (keyword key))
(word-char? char) (recur (advance scanner) (string key char))
:else (add-error scanner (string "Unexpected " char "after keyword :" key)))))
(recur scanner ""))
(defn- add-pkg-kw [scanner]
(defn recur [scanner key]
(let [char (current-char scanner)]
(cond
(terminates? char) (add-token scanner :pkg-kw (keyword key))
(word-char? char) (recur (advance scanner) (string key char))
:else (add-error scanner (string "Unexpected " char " after pkg keyword :" key)))))
(recur scanner ""))
(defn- read-literal [lit] (-> lit parse-all first))
### TODO: consider whether Janet's number rules are right for Ludus
(defn- add-number [char scanner]
(defn recur [scanner num float?]
(let [curr (current-char scanner)]
(cond
(= curr "_") (recur (advance scanner) num float?) ## consume underscores unharmed
(= curr ".") (if float?
(add-error scanner (string "Unexpected second decimal point after " num "."))
(recur (advance scanner) (buffer/push num curr) true))
(terminates? curr) (add-token scanner :number (read-literal num))
(digit? curr) (recur (advance scanner) (buffer/push num curr) float?)
:else (add-error scanner (string "Unexpected " curr " after number " num ".")))))
(recur scanner (buffer char) false))
(def escape {
"\"" "\""
"n" "\n"
"{" "{"
"t" "\t"
"r" "\r"
"\\" "\\"
})
(defn- add-string
[scanner]
(defn recur [scanner buff interpolate?]
(let [char (current-char scanner)]
(case char
"{" (recur (advance scanner) (buffer/push buff char) true)
# allow multiline strings
"\n" (recur (update (advance scanner) :line inc) (buffer/push buff char) interpolate?)
"\"" (add-token (advance scanner) (if interpolate? :interpolated :string) (string buff))
"\\" (let [next (next-char scanner)]
(recur
(advance (advance scanner))
(buffer/push buff (get escape next next))
interpolate?))
(if (at-end? scanner)
(add-error scanner "Unterminated string.")
(recur (advance scanner) (buffer/push buff char) interpolate?)))))
(recur scanner @"" false))
(defn- add-word
[char scanner]
(defn recur [scanner word]
(let [curr (current-char scanner)]
(cond
(terminates? curr) (add-token scanner
(get reserved-words (string word) :word)
(get literal-words (string word) :none))
(word-char? curr) (recur (advance scanner) (buffer/push word curr))
:else (add-error scanner (string "Unexpected " curr " after word " word ".")))))
(recur scanner (buffer char)))
(defn- add-pkg
[char scanner]
(defn recur [scanner pkg]
(let [curr (current-char scanner)]
(cond
(terminates? curr) (add-token scanner :pkg-name :none)
(word-char? curr) (recur (advance scanner) (buffer/push pkg curr))
:else (add-error scanner (string "unexpected " curr " after pkg name " pkg)))))
(recur scanner (buffer char)))
(defn- add-ignored
[scanner]
(defn recur [scanner ignored]
(let [char (current-char scanner)]
(cond
(terminates? char) (add-token scanner :ignored)
(word-char? char) (recur (advance scanner) (buffer/push ignored char))
:else (add-error scanner (string "Unexpected " char " after word " ignored ".")))))
(recur scanner @"_"))
(defn- add-comment [char scanner]
(defn recur [scanner comm]
(let [char (current-char scanner)]
(if (or (= "\n" char) (at-end? scanner))
scanner # for now, we don't do anything with comments; can be added later
(recur (advance scanner) (buffer/push comm char)))))
(recur scanner (buffer char)))
(defn- scan-token [scanner]
(let [char (current-char scanner)
scanner (advance scanner)
next (current-char scanner)]
(case char
## one-character tokens
## :break is a special zero-char token before closing braces
## it makes parsing much simpler
"(" (add-token scanner :lparen)
")" (add-token (add-token scanner :break) :rparen)
"{" (add-token scanner :lbrace)
"}" (add-token (add-token scanner :break) :rbrace)
"[" (add-token scanner :lbracket)
"]" (add-token (add-token scanner :break) :rbracket)
";" (add-token scanner :semicolon)
"," (add-token scanner :comma)
"\n" (add-token (update scanner :line inc) :newline)
"\\" (add-token scanner :backslash)
"=" (add-token scanner :equals)
">" (add-token scanner :pipeline)
## two-character tokens
## ->
"-" (cond
(= next ">") (add-token (advance scanner) :arrow)
(digit? next) (add-number char scanner)
:else (add-error scanner (string "Expected > or negative number after `-`. Got `" char next "`")))
## dict #{
"#" (if (= next "{")
(add-token (advance scanner) :startdict)
(add-error scanner (string "Expected beginning of dict: #{. Got " char next)))
## set ${
"$" (if (= next "{")
(add-token (advance scanner) :startset)
(add-error scanner (string "Expected beginning of set: ${. Got " char next)))
## placeholders
## there's a flat _, and then ignored words
"_" (cond
(terminates? next) (add-token scanner :placeholder)
(alpha? next) (add-ignored scanner)
:else (add-error scanner (string "Expected placeholder: _. Got " char next)))
## comments
## & starts an inline comment
"&" (add-comment char scanner)
## keywords
# XXX: make sure we want only lower-only keywords
":" (cond
(lower? next) (add-keyword scanner)
(upper? next) (add-pkg-kw scanner)
:else (add-error scanner (string "Expected keyword or pkg keyword. Got " char next)))
## splats
"." (let [after_next (current-char (advance scanner))]
(if (= ".." (string next after_next))
(add-token (advance scanner) :splat)
(add-error scanner (string "Expected splat: ... . Got " (string "." next after_next)))))
## strings
"\"" (add-string scanner)
## word matches
(cond
(whitespace? char) scanner ## for now just skip whitespace characters
(digit? char) (add-number char scanner)
(upper? char) (add-pkg char scanner)
(lower? char) (add-word char scanner)
:else (add-error scanner (string "Unexpected character: " char))))))
(defn- next-token [scanner]
(put scanner :start (get scanner :current)))
(defn scan [source &opt input]
(default input :input)
(defn recur [scanner]
(if (at-end? scanner)
(let [scanner (add-token (add-token scanner :break) :eof)]
{:tokens (get scanner :tokens)
:errors (get scanner :errors [])})
(recur (-> scanner (scan-token) (next-token)))))
(recur (new-scanner source input)))
(comment
# (do
(def source "add 1 2 () four")
(scan source)
)

793
src/validate.janet Normal file
View File

@ -0,0 +1,793 @@
### A validator for a Ludus AST
(comment
Tracking here, before I start writing this code, the kinds of validation we're hoping to accomplish:
* [x] ensure called keywords are only called w/ one arg
* [x] first-level property access with pkg, e.g. `Foo :bar`--bar must be on Foo
- [x] accept pkg-kws
* [x] validate dict patterns
* [x] compile string-patterns
* [x] `loop` form arity checking
* [x] arity checking of explicit named function calls
* [x] flag tail calls
* [x] no re-bound names
* [x] no unbound names
* [x] no unbound names with `use` forms
* [x] recur in tail position in `loop` forms
* [x] recur not called outside of `loop` forms
* [x] splats come at the end of list, tuple, and dict patterns
Deferred until a later iteration of Ludus:
* [ ] no circular imports DEFERRED
* [ ] correct imports DEFERRED
* [ ] validate `with` forms
)
(def- package-registry @{})
# (try (os/cd "janet") ([_] nil))
(import ./scanner :as s)
(import ./parser :as p)
(defn- new-validator [parser]
(def ast (parser :ast))
@{:ast ast
:errors @[]
:ctx @{}
:status @{}}
)
(var validate nil)
(def terminals [:number :string :bool :nil :placeholder])
(def simple-colls [:list :tuple :set :args])
(defn- simple-coll [validator]
(def ast (validator :ast))
(def data (ast :data))
(each node data
(set (validator :ast) node)
(validate validator))
validator)
(defn- iff [validator]
(def ast (validator :ast))
(def data (ast :data))
(each node data
(set (validator :ast) node)
(validate validator))
validator)
(defn- script [validator]
(def ast (validator :ast))
(def data (ast :data))
(def status (validator :status))
(set (status :toplevel) true)
(each node data
(set (validator :ast) node)
(validate validator))
validator)
(defn- block [validator]
(def ast (validator :ast))
(def data (ast :data))
(when (= 0 (length data))
(array/push (validator :errors)
{:node ast :msg "blocks may not be empty"})
(break validator))
(def status (validator :status))
(set (status :toplevel) nil)
(def tail? (status :tail))
(set (status :tail) false)
(def parent (validator :ctx))
(def ctx @{:^parent parent})
(set (validator :ctx) ctx)
(for i 0 (-> data length dec)
(set (validator :ast) (data i))
(validate validator))
(set (status :tail) tail?)
(set (validator :ast) (last data))
(validate validator)
(set (validator :ctx) parent)
validator)
(defn- resolve-local [ctx name]
(get ctx name))
(defn- resolve-name [ctx name]
(when (nil? ctx) (break nil))
(def node (get ctx name))
(if node node (resolve-name (get ctx :^parent) name)))
(defn- resolve-name-in-script [ctx name]
(when (ctx :^toplevel) (break nil))
(def node (ctx name))
(if node node (resolve-name-in-script (ctx :^parent) name)))
(defn- word [validator]
(def ast (validator :ast))
(def name (ast :data))
(def ctx (validator :ctx))
(def resolved (resolve-name ctx name))
(when (not resolved)
(array/push (validator :errors)
{:node ast :msg "unbound name"}))
validator)
### patterns
(var pattern nil)
(defn- lett [validator]
(def ast (validator :ast))
(def [lhs rhs] (ast :data))
# evaluate the expression first
# otherwise lhs names will appear bound
(set (validator :ast) rhs)
(validate validator)
(set (validator :ast) lhs)
(pattern validator)
validator)
(defn- splattern [validator]
(def ast (validator :ast))
(def status (validator :status))
(when (not (status :last))
(array/push (validator :errors)
{:node ast :msg "splats may only come last in collection patterns"}))
(def data (ast :data))
(when data
(set (validator :ast) data)
(pattern validator))
validator)
(defn- simple-coll-pattern [validator]
(def ast (validator :ast))
(def data (ast :data))
(when (empty? data) (break validator))
(def status (validator :status))
(for i 0 (-> data length dec)
(set (validator :ast) (get data i))
(pattern validator))
(set (status :last) true)
(set (validator :ast) (last data))
(pattern validator)
(set (status :last) nil)
validator)
(defn- word-pattern [validator]
(def ast (validator :ast))
(def name (ast :data))
(def ctx (validator :ctx))
### XXX TODO: this resolution should ONLY be for userspace, NOT prelude
(def resolved (resolve-name-in-script ctx name))
(when resolved
(def {:line line :input input} resolved)
(array/push (validator :errors)
{:node ast :msg (string "name " name " is already bound on line "
line " of " input)}))
(set (ctx name) ast)
# (pp ctx)
validator)
(def types [
:nil
:bool
:number
:keyword
:string
:set
:tuple
:dict
:list
:fn
:box
:pkg
])
(defn typed [validator]
(def ast (validator :ast))
(def [kw-type word] (ast :data))
(def type (kw-type :data))
(when (not (has-value? types type))
(array/push (validator :errors)
{:node kw-type :msg "unknown type"}))
(set (validator :ast) word)
(pattern validator))
(defn- str-pattern [validator]
(def ast (validator :ast))
(def data (ast :data))
(def last-term (-> data array/pop string))
(def grammar @{})
(def bindings @[])
(var current 0)
(each node data
(when (not (buffer? node))
(set (validator :ast) node)
(pattern validator))
(if (buffer? node)
(set (grammar (keyword current)) (string node))
(do
(set (grammar (keyword current))
~(<- (to ,(keyword (inc current)))))
(array/push bindings (node :data))))
(set current (inc current)))
(set (grammar (keyword current)) ~(* ,last-term -1))
(def rules (map keyword (range (length grammar))))
(set (grammar :main) ~(* ,;rules))
(set (ast :grammar) grammar)
(set (ast :compiled) (peg/compile grammar))
(set (ast :bindings) bindings))
(defn- pair [validator]
(def ast (validator :ast))
(def [_ patt] (ast :data))
(set (validator :ast) patt)
(pattern validator))
(defn- pattern* [validator]
# (print "PATTERN*")
(def ast (validator :ast))
(def type (ast :type))
# (print "validating pattern " type)
(cond
(has-value? terminals type) validator
(case type
:word (word-pattern validator)
:placeholder validator
:ignored validator
:word (word-pattern validator)
:list (simple-coll-pattern validator)
:tuple (simple-coll-pattern validator)
:dict (simple-coll-pattern validator)
:splat (splattern validator)
:typed (typed validator)
:interpolated (str-pattern validator)
:pair (pair validator)
)))
(set pattern pattern*)
# XXX: ensure guard includes only allowable names
# XXX: what to include here? (cf Elixir)
(defn- guard [validator])
(defn- match-clauses [validator clauses]
# (print "validating clauses in match-clauses")
(each clause clauses
(def parent (validator :ctx))
(def ctx @{:^parent parent})
(set (validator :ctx) ctx)
(def [lhs guard rhs] clause)
(set (validator :ast) lhs)
(pattern validator)
# (pp (validator :ctx))
# (pp (validator :ctx))
(when guard
(set (validator :ast) guard)
(validate validator))
(set (validator :ast) rhs)
(validate validator)
(set (validator :ctx) parent)))
(defn- matchh [validator]
# (print "validating in matchh")
(def ast (validator :ast))
(def [to-match clauses] (ast :data))
# (print "validating expression:")
# (pp to-match)
(set (validator :ast) to-match)
(validate validator)
# (print "validating clauses")
(match-clauses validator clauses)
validator)
(defn- declare [validator fnn]
(def status (validator :status))
(def declared (get status :declared @{}))
(set (declared fnn) true)
(set (status :declared) declared)
# (print "declared function " (fnn :name))
# (pp declared)
validator)
(defn- define [validator fnn]
(def status (validator :status))
(def declared (get status :declared @{}))
(set (declared fnn) nil)
(set (status :declared) declared)
# (print "defined function " (fnn :name))
# (pp declared)
validator)
(defn- fnn [validator]
(def ast (validator :ast))
(def name (ast :name))
# (print "function name: " name)
(def status (validator :status))
(def tail? (status :tail))
(set (status :tail) true)
(when name
(def ctx (validator :ctx))
(def resolved (ctx name))
(when (and resolved (not= :nothing (resolved :data)))
(def {:line line :input input} (get-in ctx [name :token]))
(array/push (validator :errors)
{:node ast :msg (string "name is already bound on line " line " of " input)}))
(when (and resolved (= :nothing (resolved :data)))
(define validator resolved))
(set (ctx name) ast))
(def data (ast :data))
(when (= data :nothing)
(break (declare validator ast)))
(match-clauses validator data)
(set (status :tail) tail?)
(def rest-arities @{})
(def arities @{:rest rest-arities})
(each clause data
# (print "CLAUSE:")
# (pp clause)
(def patt (first clause))
(def params (patt :data))
(def arity (length params))
# (print "checking clause with arity " arity)
(def rest-param? (and (> arity 0) (= :splat ((last params) :type))))
(if rest-param?
(set (rest-arities arity) true)
(set (arities arity) true)))
# (pp arities)
(set (ast :arities) arities)
validator)
(defn- box [validator]
(def ast (validator :ast))
(def ctx (validator :ctx))
(def expr (ast :data))
(set (validator :ast) expr)
(validate validator)
(def name (ast :name))
(def resolved (ctx name))
(when resolved
(def {:line line :input input} (get-in ctx [name :token]))
(array/push (validator :errors)
{:node ast :msg (string "name is already bound on line " line " of " input)}))
(set (ctx name) ast)
validator)
(defn- interpolated [validator]
(def ast (validator :ast))
(def data (ast :data))
(each node data
(when (not (buffer? node))
(set (validator :ast) node)
(validate validator))))
### TODO:
# * [ ] ensure properties are on pkgs (if *only* pkgs from root)
(defn- pkg-root [validator]
# (print "validating pkg-root access")
(def ast (validator :ast))
(def ctx (validator :ctx))
(def terms (ast :data))
(def pkg-name ((first terms) :data))
(def the-pkg (resolve-name ctx pkg-name))
(when (not the-pkg)
(array/push (validator :errors)
{:node ast :msg "unbound pkg name"})
(break validator))
(def member (get terms 1))
(def accessed (case (member :type)
:keyword (get-in the-pkg [:pkg (member :data)])
:pkg-kw (get-in the-pkg [:pkg (member :data)])
:args (do
(array/push (validator :errors)
{:node member :msg "cannot call a pkg"}
(break validator)))))
(when (not accessed)
# (print "no member " (member :data) " on " pkg-name)
(array/push (validator :errors)
{:node member :msg "invalid pkg access"})
(break validator))
# TODO: validate nested pkg access
)
# (defn- tail-call [validator]
# (def ast (validator :ast))
# (when (ast :partial) (break validator))
# (def status (validator :status))
# (when (not (status :tail)) (break validator))
# (def data (ast :data))
# (def args (last data))
# (set (args :tail-call) true))
(defn- check-arity [validator]
# (print "CHECKING ARITY")
(def ast (validator :ast))
# (when (ast :partial) (break validator))
(def ctx (validator :ctx))
(def data (ast :data))
(def fn-word (first data))
# (pp fn-word)
(def the-fn (resolve-name ctx (fn-word :data)))
# (print "the called function: " the-fn)
# (pp the-fn)
(when (not the-fn) (break validator))
# (print "the function is not nil")
# (print "the function type is " (type the-fn))
(when (= :function (type the-fn)) (break validator))
(when (= :cfunction (type the-fn)) (break validator))
# (print "the function is not a janet fn")
# (print "fn type: " (the-fn :type))
(when (not= :fn (the-fn :type)) (break validator))
# (print "fn name: " (the-fn :name))
(def arities (the-fn :arities))
# when there aren't arities yet, break, since that means we're making a recursive function call
# TODO: enahnce this so that we can determine arities *before* all function bodies; this ensures arity-checking for self-recursive calls
(when (not arities) (break validator))
# (print "arities: ")
# (pp arities)
(def args (get data 1))
(def num-args (length (args :data)))
# (print "called with #args " num-args)
# (pp (get (validator :ctx) "bar"))
(when (has-key? arities num-args) (break validator))
# (print "arities: ")
# (pp arities)
(when (not arities) (break validator))
(def rest-arities (keys (arities :rest)))
(when (empty? rest-arities)
(array/push (validator :errors)
{:node ast :msg "wrong number of arguments"})
(break validator))
(def rest-min (min ;rest-arities))
(when (< num-args rest-min)
(array/push (validator :errors)
{:node ast :msg "wrong number of arguments"}))
validator)
(defn- kw-root [validator]
(def ast (validator :ast))
(def data (ast :data))
(def [_ args] data)
(when (not= :args (args :type))
(break (array/push (validator :errors)
{:node args :msg "called keyword expects an argument"})))
(when (not= 1 (length (args :data)))
(array/push (validator :errors)
{:node args :msg "called keywords take one argument"})))
(defn- synthetic [validator]
(def ast (validator :ast))
(def data (ast :data))
(def status (validator :status))
(def ftype ((first data) :type))
(def stype ((get data 1) :type))
(def ltype ((last data) :type))
(set (status :pkg-access?) nil)
(when (= ftype :pkg-name)
(set (status :pkg-access?) true))
(each node data
(set (validator :ast) node)
(validate validator))
(set (validator :ast) ast)
# (print "ftype " ftype)
# (print "stype " stype)
# (print "ltype " ltype)
(when (= ftype :pkg-name) (pkg-root validator))
(when (= ftype :keyword) (kw-root validator))
# (when (= ltype :args) (tail-call validator))
(when (and (= ftype :word) (= stype :args))
(check-arity validator))
validator)
(defn- pair [validator]
(def ast (validator :ast))
(def [k v] (ast :data))
(set (validator :ast) k)
(validate validator)
(set (validator :ast) v)
(validate validator))
(defn- splat [validator]
(def ast (validator :ast))
(when (get-in validator [:status :pkg])
(array/push (validator :errors)
{:node ast :msg "splats are not allowed in pkgs"})
(break validator))
(def data (ast :data))
(when data
(set (validator :ast) data)
(validate validator))
validator)
(defn- dict [validator]
(def ast (validator :ast))
(def data (ast :data))
(each node data
(set (validator :ast) node)
(validate validator))
validator)
(defn- whenn [validator]
(def ast (validator :ast))
(def data (ast :data))
(each node data
(def [lhs rhs] node)
(set (validator :ast) lhs)
(validate validator)
(set (validator :ast) rhs)
(validate validator))
validator)
# XXX: do this!
(defn- withh [validator])
# XXX: tail calls in last position
(defn- doo [validator]
(def ast (validator :ast))
(def data (ast :data))
(each node data
(set (validator :ast) node)
(validate validator))
validator)
(defn- usee [validator]
(def ast (validator :ast))
(def data (ast :data))
(set (validator :ast) data)
(validate validator)
(def name (data :data))
(def ctx (validator :ctx))
(def pkg (get-in ctx [name :pkg] @{}))
(loop [[k v] :pairs pkg]
(set (ctx (string k)) v))
validator)
(defn- pkg-entry [validator pkg]
(def ast (validator :ast))
(def status (validator :status))
(when (= :pkg-pair (ast :type))
(set (status :pkg-access?) true))
(def data (ast :data))
(def [key value] (ast :data))
# (print "PKG ENTRY***")
# (pp key)
# (pp value)
(set (validator :ast) key)
(validate validator)
(set (validator :ast) value)
(validate validator)
(def entry (if (= :pkg-name (value :type))
(resolve-name (validator :ctx) (string (value :data)))
value))
# (print "entry at " (key :data))
# (pp entry)
(set (status :pkg-access?) nil)
(def kw (key :data))
# (pp kw)
(set (pkg kw) entry)
# (pp pkg)
validator)
(defn- pkg [validator]
(def ast (validator :ast))
(def data (ast :data))
(def name (ast :name))
(def pkg @{})
(each node data
(set (validator :ast) node)
(pkg-entry validator pkg))
(set (ast :pkg) pkg)
# (print "THE PACKAGE")
# (pp pkg)
(def ctx (validator :ctx))
(set (ctx name) ast)
validator)
(defn- ns [validator]
(def ast (validator :ast))
(def data (ast :data))
(def name (ast :name))
(def parent (validator :ctx))
(def ctx @{:^parent parent})
(def block (data :data))
(each node block
(set (validator :ast) node)
(validate validator))
(set (ast :pkg) ctx)
(set (parent name) ast)
validator)
(defn- loopp [validator]
(def ast (validator :ast))
(def status (validator :status))
(def data (ast :data))
(def input (first data))
# (print "LOOP INPUT")
# (pp input)
(def clauses (get data 1))
(def input-arity (length (input :data)))
(set (ast :arity) input-arity)
# (print "input arity to loop " input-arity)
(set (validator :ast) input)
(validate validator)
# harmonize arities
(def rest-arities @{})
(each clause clauses
# (print "CLAUSE:")
# (pp clause)
(def patt (first clause))
(def params (patt :data))
(def clause-arity (length params))
# (print "checking clause with arity " clause-arity)
(def rest-param? (= :splat (get (last params) :type)))
(when (and
(not rest-param?) (not= clause-arity input-arity))
(array/push (validator :errors)
{:node patt :msg "arity mismatch"}))
(when rest-param?
(set (rest-arities clause-arity) patt)))
# (pp rest-arities)
(loop [[arity patt] :pairs rest-arities]
(when (< input-arity arity)
(array/push (validator :errors)
{:node patt :msg "arity mismatch"})))
(def loop? (status :loop))
(set (status :loop) input-arity)
(def tail? (status :tail))
(set (status :tail) true)
(match-clauses validator clauses)
(set (status :loop) loop?)
(set (status :tail) tail?)
validator)
(defn- recur [validator]
(def ast (validator :ast))
(def status (validator :status))
(def loop-arity (status :loop))
(when (not loop-arity)
(array/push (validator :errors)
{:node ast :msg "recur may only be used inside a loop"})
(break validator))
(def called-with (get-in ast [:data :data]))
(def recur-arity (length called-with))
# (print "loop arity " loop-arity)
# (print "recur arity" recur-arity)
(when (not= recur-arity loop-arity)
(array/push (validator :errors)
{:node ast :msg "recur must have the same number of args as its loop"}))
(when (not (status :tail))
(array/push (validator :errors)
{:node ast :msg "recur must be in tail position"}))
(set (validator :ast) (ast :data))
(validate validator))
(defn- repeatt [validator]
(def ast (validator :ast))
(def [times body] (ast :data))
(set (validator :ast) times)
(validate validator)
(set (validator :ast) body)
(validate validator))
(defn- panic [validator]
(def ast (validator :ast))
(def data (ast :data))
(set (validator :ast) data)
(validate validator))
(defn- testt [validator]
(def ast (validator :ast))
(def [_ body] (ast :data))
(set (validator :ast) body)
(validate validator))
(defn- pkg-name [validator]
(def ast (validator :ast))
(def name (ast :data))
(def ctx (validator :ctx))
(def pkg (resolve-name ctx name))
(when (not pkg)
(array/push (validator :errors)
{:node ast :msg "unbound name"}))
validator)
(defn- pkg-kw [validator]
# (print "validating pkg-kw")
(def ast (validator :ast))
(def pkg-access? (get-in validator [:status :pkg-access?]))
# (print "pkg-access? " pkg-access?)
(when (not pkg-access?)
(array/push (validator :errors)
{:node ast :msg "cannot use pkg-kw here"}))
validator)
(defn- pkg-pair [validator]
# (print "validating pkg-pair")
(def ast (validator :ast))
(def status (validator :status))
(def [_ pkg] (ast :data))
(set (status :pkg-access?) true)
(set (validator :ast) pkg)
(validate validator)
(set (status :pkg-access?) nil)
validator)
(defn- kw [validator]
(def status (validator :status))
(set (status :pkg-access?) nil)
validator)
(defn- validate* [validator]
(def ast (validator :ast))
(def type (ast :type))
# (print "validating node " type)
(cond
(has-value? terminals type) validator
(has-value? simple-colls type) (simple-coll validator)
(case type
:keyword (kw validator)
:if (iff validator)
:let (lett validator)
:script (script validator)
:block (block validator)
:word (word validator)
:fn (fnn validator)
:match (matchh validator)
:interpolated (interpolated validator)
:synthetic (synthetic validator)
:do (doo validator)
:dict (dict validator)
:test (testt validator)
:panic (panic validator)
:repeat (repeatt validator)
:when (whenn validator)
:splat (splat validator)
:pair (pair validator)
:pkg-pair (pkg-pair validator)
:ns (ns validator)
:pkg (pkg validator)
:pkg-name (pkg-name validator)
:pkg-kw (pkg-kw validator)
:use (usee validator)
:loop (loopp validator)
:recur (recur validator)
:box (box validator)
(error (string "unknown node type " type)))))
(set validate validate*)
(defn- cleanup [validator]
(def declared (get-in validator [:status :declared] {}))
(when (any? declared)
(each declaration declared
(array/push (validator :errors) {:node declaration :msg "declared fn, but not defined"})))
validator)
(defn valid [ast &opt ctx]
(default ctx @{})
(set (ctx :^toplevel) true)
(def validator (new-validator ast))
(def base-ctx @{:^parent ctx})
(set (validator :ctx) base-ctx)
(validate validator)
(cleanup validator))
(import ./base :as b)
# (do
(comment
(def source `
dec (12)
`)
(def scanned (s/scan source))
(def parsed (p/parse scanned))
(def validated (valid parsed b/ctx))
# (get-in validated [:status :declared])
# (validated :ctx)
)

View File

@ -1,7 +0,0 @@
(ns cludus.core-test
(:require [clojure.test :refer :all]
[cludus.core :refer :all]))
(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))

9
test/judgy.fish Executable file
View File

@ -0,0 +1,9 @@
#!/opt/homebrew/bin/fish
set FILE $argv[1]
set TESTFILE (string join "" $FILE ".tested")
judge $FILE
if test -e $TESTFILE
cp $TESTFILE $FILE
rm $TESTFILE
end

371
test/language.test.janet Normal file
View File

@ -0,0 +1,371 @@
# testing Ludus langauge constructs
(try (os/cd "janet") ([_] nil)) # for REPL
(import /scanner :as s)
(import /parser :as p)
(import /validate :as v)
(import /interpreter :as i)
(import /errors :as e)
(import /base :as b)
(use judge)
(defn run [source]
(def ctx @{})
(def scanned (s/scan source :test))
(when (any? (scanned :errors))
(e/scan-error (scanned :errors)) (error "scanning errors"))
(def parsed (p/parse scanned))
(when (any? (parsed :errors))
(e/parse-error (parsed :errors)) (error "parsing errors"))
(def valid (v/valid parsed ctx))
(when (any? (valid :errors)) (each err (valid :errors)
(e/validation-error err)) (error "validation errors"))
(i/interpret (parsed :ast) ctx))
(deftest "returns bare values from single-line scripts"
(test (run "true") true)
(test (run "false") false)
(test (run "nil") :^nil)
(test (run "12.34") 12.34)
(test (run "-32") -32)
(test (run "0") 0)
(test (run ":foo") :foo)
(test (run ":bar") :bar)
(test (run `"a string, a text, a language"`) "a string, a text, a language"))
(deftest "returns empty collections from single-line scripts"
(test (run "()") [])
(test (run "#{}") @{})
(test (run "${}") @{:^type :set})
(test (run "[]") @[]))
(deftest "returns populated collections from single-line scripts"
(test (run "(1, 2, 3)") [1 2 3])
(test (run "[:a, :b, :c]") @[:a :b :c])
(test (run "${1, 2, 3, 3}") @{1 true 2 true 3 true :^type :set})
(test (run "#{:a 1, :b 2}") @{:a 1 :b 2}))
(deftest "returns nested collections from single-line scripts"
(test (run "((), (1, 2), [:a, (:b)], #{:foo true, :bar false})")
[[]
[1 2]
@[:a [:b]]
@{:bar false :foo true}])
(test (run `#{:foo #{:bar "thing", :baz (1, :foo, nil)}}`) @{:foo @{:bar "thing" :baz [1 :foo :^nil]}}))
(deftest "binds names in let bindings with various patterns"
(test (run `let foo = :bar; foo`) :bar)
(test (run `let 42 = 42`) 42)
(test (run `let foo = :bar; let quux = 42; (foo, quux)`) [:bar 42])
(test (run `let (:ok, value) = (:ok, 42); value`) 42)
(test (run `let #{:a x, ...} = #{:a 1, :b 2}; x`) 1))
(deftest "executes if/then/else properly"
(test (run `if nil then :foo else :bar`) :bar)
(test (run `if false then :foo else :bar`) :bar)
(test (run `if true then :foo else :bar`) :foo)
(test (run `if 42 then :foo else panic! "oops"`) :foo))
(deftest "panics"
(test-error (run `panic! "oops"`)
{:msg "oops"
:node {:data {:data "oops"
:token {:input :test
:lexeme "\"oops\""
:line 1
:literal "oops"
:source "panic! \"oops\""
:start 7
:type :string}
:type :string}
:token {:input :test
:lexeme "panic!"
:line 1
:literal :none
:source "panic! \"oops\""
:start 0
:type :panic}
:type :panic}})
)
(deftest "no match in let panics"
(test-error (run "let :foo = :bar")
{:msg "no match: let binding"
:node {:data @[{:data :foo
:token {:input :test
:lexeme ":foo"
:line 1
:literal :foo
:source "let :foo = :bar"
:start 4
:type :keyword}
:type :keyword}
{:data :bar
:token {:input :test
:lexeme ":bar"
:line 1
:literal :bar
:source "let :foo = :bar"
:start 11
:type :keyword}
:type :keyword}]
:token {:input :test
:lexeme "let"
:line 1
:literal :none
:source "let :foo = :bar"
:start 0
:type :let}
:type :let}
:value :bar})
)
(deftest "blocks execute code and work"
(test (run `
let bar = 12
let foo = {
let bar = 42
let baz = :quux
:foo
}
(foo, bar)
`)
[:foo 12])
(test (run `
let foo = {
let bar = 12
{
let bar = 15
bar
}
}
`)
15))
(deftest "unbound name panics"
(test-error (run `foo`) "validation errors"))
(deftest "rebinding name panics"
(test-error (run `let foo = 42; let foo = 23`) "validation errors"))
(deftest "when forms work as expected"
(test (run `
when {
false -> :nope
nil -> :nope
12 -> :yes
}
`)
:yes)
(test-error (run `
when {
false -> :nope
nil -> :nope
}
`)
{:msg "no match: when form"
:node {:data @[[{:data false
:token {:input :test
:lexeme "false"
:line 2
:literal false
:source " when {\n false -> :nope\n nil -> :nope\n }\n "
:start 12
:type :false}
:type :bool}
{:data :nope
:token {:input :test
:lexeme ":nope"
:line 2
:literal :nope
:source " when {\n false -> :nope\n nil -> :nope\n }\n "
:start 21
:type :keyword}
:type :keyword}]
[{:token {:input :test
:lexeme "nil"
:line 3
:literal :none
:source " when {\n false -> :nope\n nil -> :nope\n }\n "
:start 30
:type :nil}
:type :nil}
{:data :nope
:token {:input :test
:lexeme ":nope"
:line 3
:literal :nope
:source " when {\n false -> :nope\n nil -> :nope\n }\n "
:start 37
:type :keyword}
:type :keyword}]]
:token {:input :test
:lexeme "when"
:line 1
:literal :none
:source " when {\n false -> :nope\n nil -> :nope\n }\n "
:start 2
:type :when}
:type :when}})
)
(deftest "match forms work as expected"
(test (run `
match :foo with {
:bar -> :nope
:baz -> :nope
x -> x
}
`)
:foo)
(test (run `
let foo = 42
match (:ok, foo) with {
(:err, _) -> :nope
(:ok, :foo) -> :nope
(:ok, _) -> :yes
}
`)
:yes)
(test-error (run `
let foo = "foo"
match foo with {
"bar" -> :nope
"baz" -> :nope
12.34 -> :nope
}
`)
{:msg "no match: match form"
:node @{:data [{:data "foo"
:token {:input :test
:lexeme "foo"
:line 2
:literal :none
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 26
:type :word}
:type :word}
@[[{:data "bar"
:token {:input :test
:lexeme "\"bar\""
:line 3
:literal "bar"
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 40
:type :string}
:type :string}
nil
{:data :nope
:token {:input :test
:lexeme ":nope"
:line 3
:literal :nope
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 49
:type :keyword}
:type :keyword}]
[{:data "baz"
:token {:input :test
:lexeme "\"baz\""
:line 4
:literal "baz"
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 58
:type :string}
:type :string}
nil
{:data :nope
:token {:input :test
:lexeme ":nope"
:line 4
:literal :nope
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 67
:type :keyword}
:type :keyword}]
[{:data 12.34
:token {:input :test
:lexeme "12.34"
:line 5
:literal 12.34
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 76
:type :number}
:type :number}
nil
{:data :nope
:token {:input :test
:lexeme ":nope"
:line 5
:literal :nope
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 85
:type :keyword}
:type :keyword}]]]
:match @match-fn
:token {:input :test
:lexeme "match"
:line 2
:literal :none
:source " let foo = \"foo\"\n match foo with {\n \"bar\" -> :nope\n \"baz\" -> :nope\n 12.34 -> :nope \n }\n "
:start 20
:type :match}
:type :match}
:value "foo"})
)
(deftest "string patterns work as expected"
(test (run `let "I {verb} the {noun}" = "I am the walrus"; (verb, noun)`) ["am" "walrus"])
(test (run `let "a {b} c {d}" = "a because I love you c yourself out the door"; (b, d)`)
["because I love you"
"yourself out the door"])
)
(deftest "lambdas may be defined and called"
(test (run `
let foo = fn () -> :foo
foo ()
`)
:foo)
(test (run `
let pair = fn (x, y) -> (x, y)
pair (:foo, :bar)
`)
[:foo :bar])
(test-error (run `
let foo = fn () -> :foo
foo (:bar)
`)
{:called @{:^type :fn
:body [[{:data @[]
:token {:input :test
:lexeme "("
:line 1
:source " let foo = fn () -> :foo\n foo (:bar)\n "
:start 15
:type :lparen}
:type :tuple}
nil
{:data :foo
:token {:input :test
:lexeme ":foo"
:line 1
:literal :foo
:source " let foo = fn () -> :foo\n foo (:bar)\n "
:start 21
:type :keyword}
:type :keyword}]]
:ctx @{}
:match @match-fn}
:msg "no match: function call"
:node {:data "foo"
:token {:input :test
:lexeme "foo"
:line 2
:literal :none
:source " let foo = fn () -> :foo\n foo (:bar)\n "
:start 28
:type :word}
:type :word}
:value [:bar]})
)

34
test/prelude.test.janet Normal file
View File

@ -0,0 +1,34 @@
# testing the prelude
(try (os/cd "janet") ([_] nil))
(import /scanner :as s)
(import /parser :as p)
(import /validate :as v)
(import /interpreter :as i)
(import /errors :as e)
(import /base :as b)
(import /load-prelude :as pre)
(use judge)
(defn run [source]
(when (= :error pre/pkg) (error "could not load prelude"))
(def ctx @{:^parent pre/ctx})
(def scanned (s/scan source :test))
(when (any? (scanned :errors))
(e/scan-error (scanned :errors)) (error "scanning errors"))
(def parsed (p/parse scanned))
(when (any? (parsed :errors))
(e/parse-error (parsed :errors)) (error "parsing errors"))
(def valid (v/valid parsed ctx))
(when (any? (valid :errors)) (each err (valid :errors)
(e/validation-error err)) (error "validation errors"))
(i/interpret (parsed :ast) ctx))
(deftest "debug add_msg"
(test (run `
let msgs = [1, :foo, nil]
let msg = do msgs > map (string, _)
msg
`)
@["1" ":foo" ":^nil"])
# (test (run `print! ("foo", "bar")`) :ok)
)

5
test/watchy.fish Executable file
View File

@ -0,0 +1,5 @@
#!/opt/homebrew/bin/fish
set FILE $argv[1]
fd $FILE | entr ./judgy.fish /_

81
turtle-graphics.md Normal file
View File

@ -0,0 +1,81 @@
# Turtle Graphics protocol
name: "turtle-graphics"
version: 0.1.0
### Description
Turtle graphics describe the movements and drawing behaviours of screen, robot, and print "turtles."
* `proto`: `["turtle-graphics", "{version number}"]`
* `data`: an array of arrays; each array represents a turtle command; the first element of a command array is the verb; any subsequent items are the arguments to the verbs.
* Valid arguments are numbers, strings, and booleans.
* Depending on what we end up doing, we may add arrays of these, representing tuples or lists, and/or objects with string keys whose text are well-formed keywords in Ludus. For now, however, arguments must be atomic values.
* E.g., `["forward", 100]`
* Each turtle has its own stream.
* At current, this protocol describes the behaviour of turtle-like objects, all of which "live" in the same "world"; there is not yet a provision for multiple canvases/worlds. That said, an additional field for "world" in at the top level may well be added in the future to allow for multiple worlds to unfold at the same time.
### Verbs and arguments
* `forward`, steps: number
- Moves the turtle forward by the number of steps/pixels.
* `back`, steps: number
- Moves the turtle backwards by the number of steps/pixels.
* `right`, turns: number
- Turns the turtle right by the number of turns. (1 turn = 360 degrees.)
* `left`, turns: number
- Turns the turtle to the left by the number of turns. (1 turn = 360 degrees.)
* `penup`, no arguments
- "Lifts" the turtle's pen, keeping it from drawing.
* `pendown`, no arguments
- "Lowers" the turtle's pen, starting it drawing a path.
* `pencolor`, red: number, green: number, blue: number, alpha: number, OR: color: string
- Sets the turtle's pen's color to the specified RGBA color.
* `penwidth`, width: number
- Sets the width of the turtle's pen, in pixels (or some other metric).
* `home`, no arguments
- Sends the turtle back to its starting point, with a heading of 0.
* `goto`, x: number, y: number
- Sends the turtle to the specified Cartesian coordinates, where the origin is the turtle's starting position.
* `setheading`, heading: number
- Sets the turtle's heading. 0 is the turtle's starting heading, with increasing numbers turning to the right.
* `show`, no arguments
- Shows the turtle.
* `hide`, no arguments
- Hides the turtle.
* `loadstate`, x: number, y: number, heading: number, pendown: boolean, width: number, color: string OR r: number, g: number, b: number, a: number
- Loads a turtle state.
* `clear`, no arguments
- Erases any paths drawn and sets the background color to the default.
* `background`, red: number, green: number, blue: number, alpha: number
- Sets the background color to the specified RGBA color, OR: color: string
These last two feel a little weird to me, since the background color is more the property of the **world** the turtle is in, not the turtle itself. Worlds with multiple turtles will be set up so that _any_ turtle will be able to change the background, and erase all paths.
That said, since we don't yet have a world abstraction/entity, then there's no other place to put them. This will likely be shifted around in later versions of the protocol.
### Other considerations
**Not all turtles will know how to do all these things.**
The idea is that this single abstraction will talk to all the turtle-like things we eventually use.
That means that some turtles won't be able to do all the things; that's fine!
They just won't do things they can't do; but warnings should go to `stderr`.
**Errors are not passed back to Ludus.**
These are fire-off commands.
Errors should be _reported_ to `stderr` or equivalent.
But Ludus sending things to its output streams should only cause Ludus panics when there's an issue in Ludus.
**Colors aren't always RGBA.**
For pen-and-paper turtles, we don't have RGBA colors.
Colors should also be specifiable with strings corresponding to CSS basic colors: black, silver, gray, white, maroon, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, and aqua.
**Turtles should communicate states.**
Ludus should have access to turtle states.
This is important for push/pop situations that we use for L-systems.
There are two ways to do this: Ludus does its own bookkeeping for turtle states, or it has a way to get the state from a turtle.
The latter has the value of being instantaneous, and gives us an _expected_ state of the turtle after the commands are all processed.
In particular, this will be necessary for the recursive L-systems that require pushing and popping turtle state.
The latter has the drawback of potentially allowing the turtle state and expected turtle state to fall out of synch.
The former has the value of always giving us the correct, actual state of the turtle.
It has the drawback of requiring such state reporting to be asynchronous, and perhaps wildly asynchronous, as things like moving robots and plotters will take quite some time to actually draw what Ludus tells it to.
(Being able to wait until `eq? (expected, actual)` to do anything else may well be extremely useful.)
That suggests, then, that both forms of turtle state are desirable and necessary.
Thus: turtles should communicate states (and thus there ought to be a protocol for communicating state back to Ludus) and Ludus should always do the bookkeeping of calculating the expected state.