52 Commits

Author SHA1 Message Date
Bartłomiej Pluta
d720266b87 Add *.mus files 2019-09-17 23:08:37 +02:00
Bartłomiej Pluta
1d01d1c373 Add stdlib documentation 2019-09-17 23:08:37 +02:00
Bartłomiej Pluta
231ce4c4c5 Add README.md 2019-09-17 23:08:35 +02:00
Bartłomiej Pluta
aa38e6b7b6 Merge branch 'add-fft' 2019-09-09 15:32:09 +02:00
Bartłomiej Pluta
f5c72dd8a5 Add fft() function 2019-09-09 15:31:43 +02:00
Bartłomiej Pluta
55adf616c5 Merge branch 'add-possibility-to-tune' 2019-09-09 15:31:32 +02:00
Bartłomiej Pluta
06579e8e78 Enable tuning 2019-09-09 15:29:12 +02:00
Bartłomiej Pluta
2c1f5f7942 Merge branch 'add-cli' 2019-09-09 15:28:56 +02:00
Bartłomiej Pluta
83ef3f6888 Create CLI scaffolding 2019-09-06 18:26:02 +02:00
Bartłomiej Pluta
3061dae723 Merge branch 'create-virtualenv' 2019-09-06 14:46:51 +02:00
Bartłomiej Pluta
aca227ac5e Create virtual environment with pipenv 2019-09-06 13:48:28 +02:00
Bartłomiej Pluta
7ec967a014 Merge branch 'add-polyphony' 2019-09-06 13:46:02 +02:00
Bartłomiej Pluta
aca6e6bb55 Create tools for compiling waves 2019-07-30 16:51:35 +02:00
Bartłomiej Pluta
a7de7f0279 Optimise time of generating overtones 2019-07-30 15:12:32 +02:00
Bartłomiej Pluta
7e55fe4c1a Add 'plot' function and remove deprecated 'percent' type 2019-07-30 13:59:18 +02:00
Bartłomiej Pluta
8abae7c2ff Add decay to synthetiser 2019-07-29 22:11:30 +02:00
Bartłomiej Pluta
07f08b0557 Add support for integers in passing overtones to synth 2019-07-29 17:59:38 +02:00
Bartłomiej Pluta
13b069dc7d Add support for non-quoted (identifier) map keys being used as string 2019-07-29 17:53:41 +02:00
Bartłomiej Pluta
73ea88d8d9 Overload synth function to accept notes without config object 2019-07-29 17:19:41 +02:00
Bartłomiej Pluta
a5875425fc Enable passing config map to synth function 2019-07-28 21:20:11 +02:00
Bartłomiej Pluta
0dcf5287e1 Add polyphony AND add overtones do synthesed tones 2019-07-28 19:48:39 +02:00
Bartłomiej Pluta
75dcacce67 Merge branch 'add-float-type' 2019-07-28 09:32:31 +02:00
Bartłomiej Pluta
70687ddc02 Fix relation operators between floats and integers 2019-07-27 12:52:30 +02:00
Bartłomiej Pluta
d802c58eee Add Float function to convert strings and integers to floats 2019-07-27 12:48:17 +02:00
Bartłomiej Pluta
c9a3fc070b Add Integer function to convert strings and floats to integers 2019-07-26 20:59:41 +02:00
Bartłomiej Pluta
b126f83824 Enable basic support for evaluating float types 2019-07-25 13:37:31 +02:00
Bartłomiej Pluta
6dc503ba86 Enable parsing float types 2019-07-25 13:07:53 +02:00
Bartłomiej Pluta
6222dccaac Improve float type tokenizer 2019-07-25 13:02:33 +02:00
Bartłomiej Pluta
0657214aa3 Create tokenizer for float type 2019-07-25 12:51:48 +02:00
Bartłomiej Pluta
3feec0839b Fix scope leakage after exit function 2019-07-16 23:59:34 +02:00
Bartłomiej Pluta
56ca69246d Merge branch 'add-filtering-clause-to-loop-operator' 2019-07-16 13:25:22 +02:00
Bartłomiej Pluta
5a2508e804 Move parenthesed expression to atom 2019-07-16 10:23:30 +02:00
Bartłomiej Pluta
ea28ab6235 Fix leaking scope of function to outer scope 2019-07-16 10:18:00 +02:00
Bartłomiej Pluta
6e9e252b86 Create 'read' function 2019-07-15 23:54:21 +02:00
Bartłomiej Pluta
17ef5be057 Remove unnecessary functions from module 2019-07-15 21:10:05 +02:00
Bartłomiej Pluta
44e63ed18d Add optional filtering expression to loop operator 2019-07-15 20:43:33 +02:00
Bartłomiej Pluta
83c7b92741 Merge branch 'optional-function-args' 2019-07-15 20:26:48 +02:00
Bartłomiej Pluta
79a7b8bb1d Add optional semicolon at the end of statements and move loop parser from factor to expression (change precedence) 2019-07-15 20:17:40 +02:00
Bartłomiej Pluta
2737139962 Clear main 2019-07-14 00:13:05 +02:00
Bartłomiej Pluta
c5435e66ff Enable checking matching optional arguments with declared types 2019-07-14 00:12:14 +02:00
Bartłomiej Pluta
460deb4981 Create evaluators for optional arguments in function and method definitions 2019-07-13 23:52:15 +02:00
Bartłomiej Pluta
69bac69946 Fix checking signature 2019-07-13 23:49:33 +02:00
Bartłomiej Pluta
6bd8046346 Enable parser to handle optional arguments 2019-07-13 23:08:35 +02:00
Bartłomiej Pluta
e70b5fa71a Add 'optional' matcher 2019-07-13 23:08:17 +02:00
Bartłomiej Pluta
44d234d36a Move semitones, transpose and interval functions to standard library 2019-07-13 22:07:49 +02:00
Bartłomiej Pluta
78ea26ea08 Add evaluators for logic operators 'and' and 'or' 2019-07-13 21:35:00 +02:00
Bartłomiej Pluta
b6983df2d3 Add 'source' to RuntimeException 2019-07-13 15:06:53 +02:00
Bartłomiej Pluta
86cf5d01f3 Add 'throw' statement 2019-07-13 14:48:58 +02:00
Bartłomiej Pluta
a07b226edb Remove 'synth' method and fix mapping string to note pitch 2019-07-13 13:17:59 +02:00
Bartłomiej Pluta
9ae9da089b Fix return statement 2019-07-13 10:32:16 +02:00
Bartłomiej Pluta
4f2058eaac Move some functions to standard library 2019-07-13 10:21:08 +02:00
Bartłomiej Pluta
a68f870037 Merge branch 'left-associative-ops' 2019-07-12 23:27:32 +02:00
123 changed files with 5536 additions and 606 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
__pycache__/ __pycache__/
*.mus *.mus
.idea/* .idea/*
venv/

16
Pipfile Normal file
View File

@@ -0,0 +1,16 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
sounddevice = "*"
soundfile = "*"
numpy = "*"
matplotlib = "*"
tkinter = "*"
[requires]
python_version = "3.7"

180
Pipfile.lock generated Normal file
View File

@@ -0,0 +1,180 @@
{
"_meta": {
"hash": {
"sha256": "92b0c636f6ac9be9b32aa25b1cdc96dcbe85602d5c68a2084dc39d25392540b6"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.7"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"cffi": {
"hashes": [
"sha256:041c81822e9f84b1d9c401182e174996f0bae9991f33725d059b771744290774",
"sha256:046ef9a22f5d3eed06334d01b1e836977eeef500d9b78e9ef693f9380ad0b83d",
"sha256:066bc4c7895c91812eff46f4b1c285220947d4aa46fa0a2651ff85f2afae9c90",
"sha256:066c7ff148ae33040c01058662d6752fd73fbc8e64787229ea8498c7d7f4041b",
"sha256:2444d0c61f03dcd26dbf7600cf64354376ee579acad77aef459e34efcb438c63",
"sha256:300832850b8f7967e278870c5d51e3819b9aad8f0a2c8dbe39ab11f119237f45",
"sha256:34c77afe85b6b9e967bd8154e3855e847b70ca42043db6ad17f26899a3df1b25",
"sha256:46de5fa00f7ac09f020729148ff632819649b3e05a007d286242c4882f7b1dc3",
"sha256:4aa8ee7ba27c472d429b980c51e714a24f47ca296d53f4d7868075b175866f4b",
"sha256:4d0004eb4351e35ed950c14c11e734182591465a33e960a4ab5e8d4f04d72647",
"sha256:4e3d3f31a1e202b0f5a35ba3bc4eb41e2fc2b11c1eff38b362de710bcffb5016",
"sha256:50bec6d35e6b1aaeb17f7c4e2b9374ebf95a8975d57863546fa83e8d31bdb8c4",
"sha256:55cad9a6df1e2a1d62063f79d0881a414a906a6962bc160ac968cc03ed3efcfb",
"sha256:5662ad4e4e84f1eaa8efce5da695c5d2e229c563f9d5ce5b0113f71321bcf753",
"sha256:59b4dc008f98fc6ee2bb4fd7fc786a8d70000d058c2bbe2698275bc53a8d3fa7",
"sha256:73e1ffefe05e4ccd7bcea61af76f36077b914f92b76f95ccf00b0c1b9186f3f9",
"sha256:a1f0fd46eba2d71ce1589f7e50a9e2ffaeb739fb2c11e8192aa2b45d5f6cc41f",
"sha256:a2e85dc204556657661051ff4bab75a84e968669765c8a2cd425918699c3d0e8",
"sha256:a5457d47dfff24882a21492e5815f891c0ca35fefae8aa742c6c263dac16ef1f",
"sha256:a8dccd61d52a8dae4a825cdbb7735da530179fea472903eb871a5513b5abbfdc",
"sha256:ae61af521ed676cf16ae94f30fe202781a38d7178b6b4ab622e4eec8cefaff42",
"sha256:b012a5edb48288f77a63dba0840c92d0504aa215612da4541b7b42d849bc83a3",
"sha256:d2c5cfa536227f57f97c92ac30c8109688ace8fa4ac086d19d0af47d134e2909",
"sha256:d42b5796e20aacc9d15e66befb7a345454eef794fdb0737d1af593447c6c8f45",
"sha256:dee54f5d30d775f525894d67b1495625dd9322945e7fee00731952e0368ff42d",
"sha256:e070535507bd6aa07124258171be2ee8dfc19119c28ca94c9dfb7efd23564512",
"sha256:e1ff2748c84d97b065cc95429814cdba39bcbd77c9c85c89344b317dc0d9cbff",
"sha256:ed851c75d1e0e043cbf5ca9a8e1b13c4c90f3fbd863dacb01c0808e2b5204201"
],
"version": "==1.12.3"
},
"cycler": {
"hashes": [
"sha256:1d8a5ae1ff6c5cf9b93e8811e581232ad8920aeec647c37316ceac982b08cb2d",
"sha256:cd7b2d1018258d7247a71425e9f26463dfb444d411c39569972f4ce586b0c9d8"
],
"version": "==0.10.0"
},
"kiwisolver": {
"hashes": [
"sha256:05b5b061e09f60f56244adc885c4a7867da25ca387376b02c1efc29cc16bcd0f",
"sha256:26f4fbd6f5e1dabff70a9ba0d2c4bd30761086454aa30dddc5b52764ee4852b7",
"sha256:3b2378ad387f49cbb328205bda569b9f87288d6bc1bf4cd683c34523a2341efe",
"sha256:400599c0fe58d21522cae0e8b22318e09d9729451b17ee61ba8e1e7c0346565c",
"sha256:47b8cb81a7d18dbaf4fed6a61c3cecdb5adec7b4ac292bddb0d016d57e8507d5",
"sha256:53eaed412477c836e1b9522c19858a8557d6e595077830146182225613b11a75",
"sha256:58e626e1f7dfbb620d08d457325a4cdac65d1809680009f46bf41eaf74ad0187",
"sha256:5a52e1b006bfa5be04fe4debbcdd2688432a9af4b207a3f429c74ad625022641",
"sha256:5c7ca4e449ac9f99b3b9d4693debb1d6d237d1542dd6a56b3305fe8a9620f883",
"sha256:682e54f0ce8f45981878756d7203fd01e188cc6c8b2c5e2cf03675390b4534d5",
"sha256:79bfb2f0bd7cbf9ea256612c9523367e5ec51d7cd616ae20ca2c90f575d839a2",
"sha256:7f4dd50874177d2bb060d74769210f3bce1af87a8c7cf5b37d032ebf94f0aca3",
"sha256:8944a16020c07b682df861207b7e0efcd2f46c7488619cb55f65882279119389",
"sha256:8aa7009437640beb2768bfd06da049bad0df85f47ff18426261acecd1cf00897",
"sha256:939f36f21a8c571686eb491acfffa9c7f1ac345087281b412d63ea39ca14ec4a",
"sha256:9733b7f64bd9f807832d673355f79703f81f0b3e52bfce420fc00d8cb28c6a6c",
"sha256:a02f6c3e229d0b7220bd74600e9351e18bc0c361b05f29adae0d10599ae0e326",
"sha256:a0c0a9f06872330d0dd31b45607197caab3c22777600e88031bfe66799e70bb0",
"sha256:acc4df99308111585121db217681f1ce0eecb48d3a828a2f9bbf9773f4937e9e",
"sha256:b64916959e4ae0ac78af7c3e8cef4becee0c0e9694ad477b4c6b3a536de6a544",
"sha256:d3fcf0819dc3fea58be1fd1ca390851bdb719a549850e708ed858503ff25d995",
"sha256:d52e3b1868a4e8fd18b5cb15055c76820df514e26aa84cc02f593d99fef6707f",
"sha256:db1a5d3cc4ae943d674718d6c47d2d82488ddd94b93b9e12d24aabdbfe48caee",
"sha256:e3a21a720791712ed721c7b95d433e036134de6f18c77dbe96119eaf7aa08004",
"sha256:e8bf074363ce2babeb4764d94f8e65efd22e6a7c74860a4f05a6947afc020ff2",
"sha256:f16814a4a96dc04bf1da7d53ee8d5b1d6decfc1a92a63349bb15d37b6a263dd9",
"sha256:f2b22153870ca5cf2ab9c940d7bc38e8e9089fa0f7e5856ea195e1cf4ff43d5a",
"sha256:f790f8b3dff3d53453de6a7b7ddd173d2e020fb160baff578d578065b108a05f"
],
"version": "==1.1.0"
},
"matplotlib": {
"hashes": [
"sha256:1febd22afe1489b13c6749ea059d392c03261b2950d1d45c17e3aed812080c93",
"sha256:31a30d03f39528c79f3a592857be62a08595dec4ac034978ecd0f814fa0eec2d",
"sha256:4442ce720907f67a79d45de9ada47be81ce17e6c2f448b3c64765af93f6829c9",
"sha256:796edbd1182cbffa7e1e7a97f1e141f875a8501ba8dd834269ae3cd45a8c976f",
"sha256:934e6243df7165aad097572abf5b6003c77c9b6c480c3c4de6f2ef1b5fdd4ec0",
"sha256:bab9d848dbf1517bc58d1f486772e99919b19efef5dd8596d4b26f9f5ee08b6b",
"sha256:c1fe1e6cdaa53f11f088b7470c2056c0df7d80ee4858dadf6cbe433fcba4323b",
"sha256:e5b8aeca9276a3a988caebe9f08366ed519fff98f77c6df5b64d7603d0e42e36",
"sha256:ec6bd0a6a58df3628ff269978f4a4b924a0d371ad8ce1f8e2b635b99e482877a"
],
"index": "pypi",
"version": "==3.1.1"
},
"numpy": {
"hashes": [
"sha256:03f2ebcbffcce2dec8860633b89a93e80c6a239d21a77ae8b241450dc21e8c35",
"sha256:078c8025da5ab9e8657edc9c2a1e9642e06e953bc7baa2e65c1aa9d9dfb7e98b",
"sha256:0fbfa98c5d5c3c6489cc1e852ec94395d51f35d9ebe70c6850e47f465038cdf4",
"sha256:1c841033f4fe6801648180c3033c45b3235a8bbd09bc7249010f99ea27bb6790",
"sha256:2c0984a01ddd0aeec89f0ce46ef21d64761048cd76c0074d0658c91f9131f154",
"sha256:4c166dcb0fff7cb3c0bbc682dfb5061852a2547efb6222e043a7932828c08fb5",
"sha256:8c2d98d0623bd63fb883b65256c00454d5f53127a5a7bcdaa8bdc582814e8cb4",
"sha256:8cb4b6ae45aad6d26712a1ce0a3f2556c5e1484867f9649e03496e45d6a5eba4",
"sha256:93050e73c446c82065b7410221b07682e475ac51887cd9368227a5d944afae80",
"sha256:a3f6b3024f8826d8b1490e6e2a9b99e841cd2c375791b1df62991bd8f4c00b89",
"sha256:bede70fd8699695363f39e86c1e869b2c8b74fb5ef135a67b9e1eeebff50322a",
"sha256:c304b2221f33489cd15a915237a84cdfe9420d7e4d4828c78a0820f9d990395c",
"sha256:f11331530f0eff69a758d62c2461cd98cdc2eae0147279d8fc86e0464eb7e8ca",
"sha256:fa5f2a8ef1e07ba258dc07d4dd246de23ef4ab920ae0f3fa2a1cc5e90f0f1888",
"sha256:fb6178b0488b0ce6a54bc4accbdf5225e937383586555604155d64773f6beb2b",
"sha256:fd5e830d4dc31658d61a6452cd3e842213594d8c15578cdae6829e36ad9c0930"
],
"index": "pypi",
"version": "==1.17.1"
},
"pycparser": {
"hashes": [
"sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3"
],
"version": "==2.19"
},
"pyparsing": {
"hashes": [
"sha256:6f98a7b9397e206d78cc01df10131398f1c8b8510a2f4d97d9abd82e1aacdd80",
"sha256:d9338df12903bbf5d65a0e4e87c2161968b10d2e489652bb47001d82a9b028b4"
],
"version": "==2.4.2"
},
"python-dateutil": {
"hashes": [
"sha256:7e6584c74aeed623791615e26efd690f29817a27c73085b78e4bad02493df2fb",
"sha256:c89805f6f4d64db21ed966fda138f8a5ed7a4fdbc1a8ee329ce1b74e3c74da9e"
],
"version": "==2.8.0"
},
"six": {
"hashes": [
"sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c",
"sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73"
],
"version": "==1.12.0"
},
"sounddevice": {
"hashes": [
"sha256:8d0571349f9a438a97f2c69da760f195cf5ddf2351072199cc1dfede4785a207",
"sha256:a0dda9dea9a5038ec0dafee4cdebd6d9a09086405d4833a7e3f6b18c109f89f3",
"sha256:a71cd3daabc8cb13e0ec9a29e7d4ba9b707386f3c81b3501b20f64209bcaece7",
"sha256:b038315e3497e01b1cb22630bc9b5025fcfad40e5ef7c125a9d565befa7220e0",
"sha256:c340d0a68945439571a13b287bd11b2d2884b61cd47cc5de8356d88221edf580"
],
"index": "pypi",
"version": "==0.3.13"
},
"soundfile": {
"hashes": [
"sha256:181393f8daac38bda997c4284b760bcd726dc0e847f620020d7bf2d3d538a985",
"sha256:3f90971bb1884575ada9a0731e878c91cc1ffde14a778f0857deaa2fd04849ac",
"sha256:637f6218c867b8cae80f6989634a0813b416b3e6132480d056e6e5a89a921571",
"sha256:7fe04272cbfb94ab648acb0db31ace0800f5a4cf46e9d45b4d71ddb15abce1a0",
"sha256:80a3d2ed37199ffa7066c097d7b82b8413922e22870b62b54dc264bf0169f4c2"
],
"index": "pypi",
"version": "==0.10.2"
}
},
"develop": {}
}

2366
README.md Normal file

File diff suppressed because it is too large Load Diff

1084
STDLIB.md Normal file

File diff suppressed because it is too large Load Diff

36
examples/adeste.mus Normal file
View File

@@ -0,0 +1,36 @@
println("Adeste Fideles");
println("John Francis Wade");
s1 = [@g, @g:2, @d, @g, @a:2, @d:2, @h, @a, @h, @c5, @h:2, @a, @g];
s2 = [@g:2, @f#, @e, @f#, @g, @a, @h, @f#:2, @e:4d, @d:8, @d:2d, 4];
s3 = [@d5:2, @c5, @h, @c5:2, @h:2, @a, @h, @g, @a, @f#:4d, @e:8, @d];
s4 = [@g, @g, @f#, @g, @a, @g:2, @d, @h, @h, @a, @h, @c5, @h:2, @a];
s5 = [@h, @c5, @h, @a, @g, @f#:2, @g, @c5, @h:2, @a:4d, @g:8, @g:1];
S = s1 + s2 + s3 + s4 + s5;
a1 = [@d, @d:2, @d, @d, @e:2, @d:2, @d, @d, @d, @e, @d:2, @d, @h3];
a2 = [@h3, @c#, @d, @c#, @d, @d, @d, @d, @d:2, @c#:4d, @d:8, @d:2d, 4];
a3 = [@d:2, @e:8, @f#:8, @g, @g, @f#, @g:2, @d, @d, @e, @e, @d:2, @d];
a4 = [@d, @d:1, @d:2d, @d, @d:1, @d:2d];
a5 = [@g, @f#, @g, @d, @d:8, @c#:8, @d:2, @d, @e, @d:2, @d:4d, @h3:8, @h3:1];
A = a1 + a2 + a3 + a4 + a5;
t1 = [@h3, @h3:2, @h3, @h3, @c:2, @a3:2, @g3, @a3, @g3, @g3, @g3:2, @f#3, @g3];
t2 = [@g3:2, @a3, @a3, @a3, @g3, @f#3, @d3, @a3:2, @g3:4d, @f#3:8, @f#3:2d, 4];
t3 = [@h3:2, @c, @d, @c:2, @d:2, @d, @g3, @h3, @c, @a3:2, @f#3];
t4 = [@h3, @h3, @a3, @h3, @c, @h3:2d, @g3, @g3, @f#3, @g3, @a3, @g3:2, @f#3];
t5 = [@d, @d, @d, @a3, @a3, @a3:2, @g3:2, @g3:2, @f#3:4d, @g3:8, @g3:1];
T = t1 + t2 + t3 + t4 + t5;
b1 = [@g3, @g3:2, @h3, @g3, @g3:2, @f#3:2, @g3, @f#3, @g3, @c3, @d3:2, @d3, @e3];
b2 = [@e3:2, @d3, @a2, @d3, @h2, @f#2, @g2, @a2:2, @a2:4d, @d3:8, @d2:2d, 4];
b3 = [@h3:2, @a3, @g3, @a3:2, @g3:2, @f#3, @g3, @e3, @c3, @d3:2, @d3];
b4 = [4, 1, 1, 1, 2, 4];
b5 = [@g3, @a3, @g3, @f#3, @e3, @d3, @c3, @h2, @c3, @d3:2, @d3:4d, @g2:8, @g2:1];
B = b1 + b2 + b3 + b4 + b5;
synth({ tuning -> 431, attack -> 100, decay -> 1.2, overtones -> [0.7, 0, 0.2, 0.1] }, S, A, T, B);

42
examples/bohemian.mus Normal file
View File

@@ -0,0 +1,42 @@
println("Bohemian Rhapsody :: part");
println("by Queen");
m1 = [@d:8, @d:8, @d:2, 8, @b3:8, @c:8, @d:16, @d:16, @d, 4, 8, @d:16, @d:16];
m2 = [@eb:8, @f:8, @eb:8, @d:8, @c, @c:8, @d:8, @eb:16, @f:8, @eb:8d, @d:8, @c, 4, @d:8, @d:8, @d:2, @d:8, @f:8];
m3 = [@a:8d, @g:16, @g:2, 8, @g:8, @b:8, @b:8, @b:8, @b:8, @b:8d, @g:16, @d:8d, @c:16, @c:2, 2];
M1 = [1, 1] + m1 + m2 + m3;
p_B = [@d:8, @f3:8, @b3:8, @d:8, @g:8, @f3:8, @f:8, @f:8];
p_g = [@d:8, @g3:8, @b3:8, @d:8, @a:8, @g3:8, @g:8, @b3:8];
p_c = [@g:8, @c:8, @eb:8, @g:8, @d5:8, @c:8, @c5:8, @c:8];
p_cF = [@b:8, @c:8, @eb:8, @g:8, @a:8, @eb:8, @f:8, @c:8];
p1 = [@eb:8, @c:8, @eb:8, @g:8, @eb:8, @g:8, @eb:8, @g:8, @eb:8, @a3:8, @eb:8, @g:8, @eb:8, @g:8, @eb:8, @g:8];
P1 = p_B + p_B + p_B + p_g + p_c + p_cF + p_B + p_g + p1;
b1 = [@b3:1, @g3:1, @c3:1, @c3:2, @f3:2];
b2 = [@b3:2d, @a3, @g3:1, @c3:2, @h2, @b2, @a2:2, @ab2, @g2];
B1 = [@b3:1, @b3:1] + b1 + b2;
# Eb
m1 = [@g:16, @g:16, @g:2, 8, @f:8, @g:16, @ab:16, @g:2, 4, 8, @g:16, @g:16, @ab:8d, @g:16, @g:8, @f:16, @f:2, @b3:16];
m2 = [@b3:8, @f:8, @f:8, @g:16, @g:8d, @ab:8, @ab:8, @b:16, @ab:16, @ab:8, @g:8, 8, @f:16, @g:16, @b:4d, @f:16, @g:16];
m3 = [@eb:4d, @b3:16, @b3:16, @h3:8, @db:8, @h3:16, @db:16, @h3:8, @b3:2, 2];
M2 = m1 + m2 + m3;
p_Eb = [@b:8, @eb:8, @eb5:8, @eb:8, @b:8, @eb:8, @f:8, @b:8];
p_f = [@ab:8, @f:8, @ab:8, @ab:8, @ab:8, @eb:8, @ab:8, @d:8];
p_B = [@d, @f, @g, @ab];
p_EbB = [@b:8, @eb:8, @eb5:8, @eb:8, @b:8, @d:8, @b:8, @d:8];
p_cf = [@g:8, @c:8, @g:8, @c:8, @h3:2];
P2 = p_Eb + p_c + p_f + p_B + p_EbB + p_cf;
b1 = [@eb3:2d, @d3, @c3:1, @f3:4d, @e3:8, @eb3, @d3, @b2, @b2, @b2, @b2, @eb3:2, @d3:2];
b2 = [@c2:2, @f2:2, @eb2:2, 2];
B2 = b1 + b2;
M = M1 + M2;
P = P1 + P2;
B = B1 + B2;
synth({ tuning -> 432, bpm -> 72, overtones -> [0.5, 0.3, 0.15, 0.05], decay -> 0.8 }, transpose(12, M), P, B);

120
examples/cantina.mus Normal file
View File

@@ -0,0 +1,120 @@
println("Star Wars :: Cantina Band");
println("by John Williams");
cb1 = [@a, @d5, @a, @d5] + [@a:8, @d5, @a:8, 8, @g#:8, @a];
cb2 = [@a:8, @g#:8, @a:8, @g:8, 8, @f#:8, @g:8, @gb:8];
cb3 = [@f:4d, @d:2, 8];
cb4 = [@g:8, 8, @g:4d, @f#:8, @g];
cb5 = [@c5:8, @b, @a, @g:4d];
cb6 = [@c5:8, 8, @c5:4d, @a:8, @g];
cb7 = [@f:4d, @d:2, 8];
cb8 = [@d:2, @f:2, @a:2, @c5:2];
cb9 = [@eb5, @d5, @g#:8, @a, @f:8];
CB1 = cb1 + cb2 + cb3 + cb1 + cb4 + cb5 + cb1 + cb6 + cb7 + cb8 + cb9 + [1];
cb11 = 2 ^ [8, @a5, @f5:8, @a5:8, 8, 4];
cb12 = [8, @a5, @f5:8, @g#5:8, @a5, @f5:8];
cb13 = [@f5:4d, @d5:2, 8];
cb14 = [8, @a5, @f5:8, @g#5:8, @a5, @g5:8];
cb15 = [@g5:2, @c5:2];
cb16 = [@b:8, @d5:8, @f5, @h:8, @d5:8, @f5];
cb17 = [@g#:8, @a5, @d5:2, 8];
cb18 = [@d:8, @f:8, @h:8, @d5:8, @g#:8, @a, @f:8];
cb19 = [@f:2d, 4];
CB2 = cb11 + cb12 + cb13 + cb11 + cb14 + cb15 + cb11 + cb12 + cb13 + cb16 + cb17 + cb18 + cb19;
cb20 = [@f, 8, @ab, @f:8, @g];
cb21 = [8, @f:8, @ab:8, @f:8, @g:8, @f:8, @ab:8, @d:8];
cb22 = [@f, 8, @ab, @f:8, @g];
cb23 = [@f:8, @f:8, @ab:8, @f:8, @g:8, @f:8, @ab:8, @f:8];
cb24 = [@g:8, @f:8, @ab:8, @f:8, @g:8, @f:8, @ab:8, @d:8];
cb25 = [@f:8, @f:8, @ab:8, @f:8, @ab:8, @f, @f:8, @f:2d, 4];
CB3 = cb20 + cb21 + cb22 + cb21 + cb22 + cb21 + cb23 + cb24 + cb22 + cb21 + cb22 + cb21 + cb22 + cb21 + cb25;
cb26 = [@c5, 8, @e5:8, 4, @g5];
cb27 = [@g5:8, @g5, @g5:8, @g5, @e5:8, @c5:8];
cb28 = [@f5, @f5:8, @f5:8, @f5:8, @g5, @a5:8, @a5:1];
cb29 = [@e5, @e5, @g5:8, @g5, @c5:8];
cb30 = [@c5:8, @e5, @c5:8, @e5, @g5];
cb31 = [@f5, @f5:8, @f5, @g5, @a5:8, @a5:1];
cb32 = [@b5, @b5, @d6, @b5:8, @db6:8];
cb33 = [@db6:8, @db6:8, @b5, @db6, @b5];
cb34 = [@f5, @f5, @f5:8, @ab5, @d6:8, @d6:1];
cb35 = [8, @b, @d5:8, @g5, @g5];
cb36 = [8, @e5, 8, @e5, @e5];
cb37 = [8, @f5, @f5:8, @f5, @f5:8, @f5:8];
cb38 = [@f5:8, @f5, @f5:8, @f5:8, @f5:8, @f5];
CB4 = cb26 + cb27 + cb28 + cb29 + cb30 + cb31 + cb32 + cb33 + cb34 + cb35 + cb36 + cb37 + cb38;
cb39 = [@ab:8, @gb:8, @ab:8, @gb:8, @ab:8, @gb:8, @ab:8, @gb:8];
cb40 = [@ab:8, @db5, @ab:8, @db5, @ab:8, @db5:8];
cb41 = [8, @ab:8, @b:8, @h:8, @c5:8, @h:8, @b:8, 8];
cb42 = [@ab:8, @db5, @ab:8, @db5, @ab:8, @a:8];
cb43 = [@b:8, @a:8, @b:4d, @a:8, @b:8, @a:8];
cb44 = [@b:8, @a:8, @b:8, @h:8, @c5:8, 8, @gb:8, @g:8];
cb45 = [@b:8, @a:8, @b:4d, @a:8, @b:8, @h:8];
cb46 = [@c5:8, @g:8, @e:8, @c:8, 8, @c:8, @db:8, @c:8];
cb47 = [@db:8, @b3, @db:8, @fb:8, @db, @fb:8];
cb48 = [@f:8, @e:8, @f:8, @gb:8, @g:8, @c, @g:8];
cb49 = [@ab:8, @g:8, @ab:8, @a:8, @b:8, @a:8, @b:8, @h:8];
cb50 = [@c5:8, @c5:8, 4, @c5:8, @c5:8, 4];
cb51 = [@c5, @e5:2, @f5:8, @db5:8];
cb52 = [@ab:8, @f:8, @fb:8, @eb:8, @d:8, 4, @gb:8];
cb53 = [@c5:8, @c5, @gb:8, @c5:8, @c5, @h:8];
cb54 = [@c5:8, @c5, @h:8, @c5:8, @c5, @c5:8];
cb55 = [@db5:8, @fb:8, @db5:4d, @fb:8, @db5:8, @fb:8];
cb56 = [@db5:8, @fb:8, @db5:8, @fb:8, @eb:8, @c5, @c5:8];
cb57 = [@db5:8, @fb:8, @db5:8, @fb:8, @eb:8, @c5, @h3:8];
cb58 = [@b3:8, @db:8, @gb:8, @b:8, @c:8, @e:8, @g:8, @c5:8];
cb59 = [@db5:8, @db, @c:8, @db, 4];
cb60 = [4, @a3, @b3, @a3, @b3, @a3, @gb3:8, @f3:8, @d3:8, 8];
cb61 = [8, @a3, @a3, @a3, @a3, @a3, @a3:8, @a3:8, @a3];
CB5 = cb39 + cb40 + cb41 + cb39 + cb42 + cb43 + cb44 + cb39 + cb42 + cb45 + cb46 + cb47 + cb48 + cb49 + cb50 + cb50 + cb51 + cb52 + cb53 + cb54 + cb55 + cb56 + cb55 + cb56 + cb55 + cb57 + cb58 + cb59 + cb60 + cb60 + cb60;
CB = CB1 + CB2 + CB3 + (2 ^ CB4) + CB5;
b1 = (3 ^ [@d3, 4, @a2, 4]) + [@f3, 4, @c3, 4, @d3, 4, @a2, 4, @d3, 4, @a2, 4, @g2, 4, @g2, 4];
b2 = [@c3, @d3, @d#3, @e3, @d3, 4, @a2, 4, @d3, 4, @a2, 4, @c3, 4, @c3, 4];
b3 = [@f3, 4, @c3, 4, @b2, 4, @b2, 4, @f3, 4, @d3, 4, @g3, 4, @c3, 4, @f3, @e3, @d3, @c3];
B1 = b1 + b2 + b3;
b4 = (3 ^ [@d3, 4, @a2, 4]) + [@d3:4d, @g#2:4d, @a2];
b5 = (3 ^ [@d3, 4, @a2, 4]) + [@eb3:2, @c3, @c3];
b6 = (4 ^ [@d3, 4, @a2, 4]);
b7 = [@b2:8, @b2:8, 4, @h2:8, @h2:8, 4, @c3:4d, @d3:8, 8, @d3:8, @d3, @g2, 4, @c3:4d, @f3:4d, @e3, @d3, @c3];
B2 = b4 + b5 + b6 + b7;
b8 = 3 ^ [@d3, @c3, @b2, @a2, @g2, @f2, @e2, @d2];
b9 = [@d3, 4, 2, 2, 4, @a2];
b10 = [@d3, @b2, @a2, @a2, @d3:8, 8, 4, 2];
B3 = b8 + b9 + b8 + b10;
b11 = [@c3, 4, @g2, 4, @c3, 8, @b2:4d, @g2, @f2, 4, @c3, 4, @f3, 8, @e3:4d, @d3];
b12 = [@c3, 4, @g3, 4, @c3, 8, @b2:4d, @g2, @f2, 4, @c3, 4, @d3, 8, @f#2:4d, @a2];
b13 = [@b2, 4, @b2, 4, @h2, 4, @h2, 4, @c3, 4, @c3, 4, @d3, 8, @d3:4d, @f#2];
b14 = [@g2, 4, @d3, 4, @c3, 8, @c3:4d, @g3, @f3, 4, @db3, 4, @f3, @f3, @e3, @d3];
B4 = b11 + b12 + b13 + b14;
b15 = [@db3, @c3, @b2, @ab2, @gb2, @f2, @eb2, @db2, @db3, @b2, @a2, @ab2];
b16 = [@db3, @c3, @b2, @ab2, @gb2, @f2, @eb2, @db2, @eb2, @e2, @f2, @gb2];
b17 = [@e2, @d2, @h2, @ab2, @h2, @ab2, @f2, @eb2, @db2, @c2, @b1, @ab1];
# ^ or B?
b18 = [@gb2, @gb2, @gb2, @db3, @c3, @c3, @e3, @g3, @gb3, @gb3, @g3, @g3];
b19 = [@ab3, @db3, @c3, @c3, @f3, @c3, @gb3, @gb3, @c3, 4, @c3, 4];
b20 = [@f3, 4, @f3, 4, @c3, @c3, 4, @db3:8, @f3:8, @ab3:8, @a3:8, @b3:8, @h3:8, @c:8, 8, 8, 16, @g3:16];
b21 = [@c3, 4, @c3, 4, @f3, 4, @f3, 4, @gb2, 4, @ab2, 4, @a2, 4, @db3, 4, @gb2, 4, @ab2, 4, @a2, 4, @db3, 4, @d, 4, @eb, 4];
b22 = [@e3, 4, @f3, 4, @gb3, 4, @g3, 4, @db3, 4, @db3, 4];
B5 = b15 + b16 + b17 + b18 + b19 + b20 + b21 + b22;
B = B1 + B2 + B3 + (2 ^ B4) + B5;
synth({ tuning -> 432, bpm -> 270 }, flat(CB), flat(B));

31
examples/les_anges.mus Normal file
View File

@@ -0,0 +1,31 @@
println("Les Anges dans nos campagnes");
s = [@a, @a, @a, @c5, @c5:4d, @b:8, @a:2, @a, @g, @a, @c5, @a:4d, @g:8, @f:2];
sc = [@c5:2, @d5:8, @c5:8, @b:8, @a:8, @b:2, @c5:8, @b:8, @a:8, @g:8, @a:2, @b:8, @a:8, @g:8, @f:8, @g:4d, @c:8, @c:2, @f, @g, @a, @b];
sca = [@a:2, @g, 4];
scb = [@a:2, @g:2, @f:2d, 4];
S = flat(2^s, sc, sca, sc, scb);
a = [@f, @f, @e, @e, @g, @e, @f:2, @f, @e, @f, @f, @f, @e, @f:2];
ac = [@f, @a:8, @g:8, @f:2d, @g:8, @f:8, @e:2d, @f:8, @e:8, @d:2, @c:4d, @c:8, @c:2, @c, @e, @f, @g];
aca = [@f:2, @e, 4];
acb = [@f:2, @e:2, @c:2d, 4];
A = flat(2^a, ac, aca, ac, acb);
t = [@c, @c, @c, @c, @d, @c, @c:2, @c, @c, @c, @c, @c:4d, @b3:8, @a3:2];
tc = [@a3:2, @d:1, @c:1, @b3:2, @g3, @f3, @e3:2, @f3, @c, @c, @d];
tca = [@c:2, @c, 4];
tcb = [@c:2d, @b3, @a3:2d, 4];
T = flat(2^t, tc, tca, tc, tcb);
b = [@f3, @f3, @a3, @a3, @g3, @c3, @f3:2, @f3, @c3, @f3, @a3, @c, @c3, @f3:2];
bc = [@f3:2, @d3, @f3, @g3:2, @c3, @e3, @f3:2, @b2, @d3, @e3, @d3, @c3, @b2, @a2, @c3, @f3, @b2];
bca = [@c3:2, @c3, 4];
bcb = [@c3:1, @f3:2d, 4];
B = flat(2^b, bc, bca, bc, bcb);
synth({ tuning -> 432, overtones -> [0.6, 0.3, 0.07, 0.03], attack -> 200, decay -> 0.6 }, S, A, T, B);

18
examples/per_crucem.mus Normal file
View File

@@ -0,0 +1,18 @@
println("Per Crucem - canon");
d1 = [@d:2, @d:2, @c:2, @f:4d, @f:8, @f, @f, @e, @d, @e:2, @d:2];
d2 = [@d:8, @f:8, @a:8, @d5:8, @b:8, @a:8, @g, @c:8, @e:8, @g:8, @c5:8, @a:8, @g:8, @f, @b3:8, @d:8, @f:8, @b:8, @g:8, @f:8, @e, @a, @e, @f:2];
d3 = [@a:2, @b:2, @c5:2, @a:4d, @a:8, @b, @f, @g, @g, @a, @e, @f:2];
d4 = [@d5:8, @c5:8, @b:8, @a:8, @g:8, @a:8, @b, @c5:8, @b:8, @a:8, @g:8, @f:8, @g:8, @a, @b:8, @a:8, @g:8, @f:8, @e:8, @f:8, @g, @a, @e, @f:2];
d5 = [@d5:2, @d5:2, @c5:2, @c5:2, @d5:8, @c5:8, @b:8, @a:8, @b, @b, @a:2, @a:2];
d6 = [@d:8, @d:8, @d:8, @d:8, @g:8, @g:8, @g, @c:8, @c:8, @c:8, @c:8, @f:8, @f:8, @f, @b3:8, @b3:8, @b3:8, @b3:8, @e:8, @e:8, @e, @a3, @a3, @d:2];
p = [1, 1, 1, 1];
S = d1 + d2 + d3 + d4 + d5 + d6 + d1 + d2 + d3;
A = p + d1 + d2 + d3 + d4 + d5 + d6 + d1 + d2;
T = p + p + d1 + d2 + d3 + d4 + d5 + d6 + d1;
B = p + p + p + d1 + d2 + d3 + d4 + d5 + d6;
wv = wave(S, A, transpose(-12, T), transpose(-12, B));
synth(wv);

View File

@@ -0,0 +1,31 @@
println("Narodził się nam Zbawiciel / Quem pastores laudavere");
println("mel.: XIV w.");
println("sł.: XV - XVI w.");
println("harm.: Bartłomiej Pluta");
s1 = [@f:2, @a, @c5:2, @a, @c5:2, @d5, @c5, @g:2];
s2 = [@a:2, @c5, @b:2, @g, @f:2, @d, @e, @c:2];
s3 = [@a:2, @b, @c5:2, @d5, @c5:2, @g, @a, @f:2];
s4 = [@b:2, @b, @a, @g, @a, @f, @d, @e, @f:2d];
a1 = [@c:2, @f, @g:2, @f, @f, @g, @f, @f, @f, @e];
a2 = [@f, @e, @eb, @d:2, @c, @d:2, @d, @c, @c, @b3];
a3 = [@c:2, @c, @f, @d, @g, @g:2, @e, @e, @d:2];
a4 = [@f:2, @f, @f, @e, @f, @c:2, @c, @c:2d];
t1 = [@a, @b, @c5, @c5, @b, @c5, @a:2, @b, @a, @c5:2];
t2 = [@c5:2, @a, @b:2, @c5, @d5, @c, @b, @g, @g:2];
t3 = [@f, @a, @g, @a:2, @b, @b:2, @g, @g, @a:2];
t4 = [@d5, @db5, @db5, @c5, @b, @a, @g:2, @b, @a:2d];
b1 = [@f:2, @f, @e:2, @f, @f:2, @f, @c, @c:2];
b2 = [@f:2, @f#, @g, @f, @e, @d, @f, @g, @c, @e:2];
b3 = [@f:2, @e, @f, @f#, @g, @e, @d, @c, @c#, @d, @c];
b4 = [@b3:2, @b, @c:2, @c, @c:2, @c, @f:2d];
S = s1 + s2 + s3 + s4;
A = a1 + a2 + a3 + a4;
T = transpose(-12, t1 + t2 + t3 + t4);
B = transpose(-12, b1 + b2 + b3 + b4);
synth({ overtones -> [0.6, 0.25, 0.1] + (2 ^ 0.0) + [0.05], attack -> 50, decay -> 1 }, S, A, T, B);

BIN
img/notes/cd4fga8c.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
img/notes/cdef.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

BIN
img/notes/poly1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

BIN
img/notes/poly2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

BIN
img/notes/starwars.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
img/notes/twinkle1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
img/notes/twinkle2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

BIN
img/notes/twinkle3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
img/plots/a_127.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
img/plots/a_1overtone.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
img/plots/a_2overtones.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
img/plots/a_3overtones.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
img/plots/a_440.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
img/plots/attack0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

BIN
img/plots/attack1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

BIN
img/plots/attack10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

BIN
img/plots/attack100.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

BIN
img/plots/attack5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

BIN
img/plots/c.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
img/plots/decay0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

BIN
img/plots/decay05.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

BIN
img/plots/decay1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

BIN
img/plots/decay10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
img/plots/decay5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

BIN
img/plots/default.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

BIN
img/plots/spectrum.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
img/schemas/wait_fsm.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

2
os_requirements.txt Normal file
View File

@@ -0,0 +1,2 @@
You have to have installed `PortAudio` in order to use app.

View File

@@ -27,6 +27,10 @@ class IntegerLiteral(Atom):
pass pass
class FloatLiteral(Atom):
pass
class StringLiteral(Atom): class StringLiteral(Atom):
pass pass
@@ -47,6 +51,10 @@ def IntegerParser(input):
return Parser.terminal(TokenType.INTEGER, createNode=IntegerLiteral.withValue)(input) return Parser.terminal(TokenType.INTEGER, createNode=IntegerLiteral.withValue)(input)
def FloatParser(input):
return Parser.terminal(TokenType.FLOAT, createNode=FloatLiteral.withValue)(input)
def StringParser(input): def StringParser(input):
return Parser.terminal(TokenType.STRING, createNode=StringLiteral.withValue)(input) return Parser.terminal(TokenType.STRING, createNode=StringLiteral.withValue)(input)
@@ -66,6 +74,7 @@ def TypeLiteralParser(input):
def LiteralParser(input): def LiteralParser(input):
return Parser.oneOf( return Parser.oneOf(
IntegerParser, IntegerParser,
FloatParser,
StringParser, StringParser,
NoteParser, NoteParser,
BoolParser, BoolParser,
@@ -78,8 +87,18 @@ def AtomParser(input):
from smnp.ast.node.identifier import IdentifierParser from smnp.ast.node.identifier import IdentifierParser
from smnp.ast.node.list import ListParser from smnp.ast.node.list import ListParser
from smnp.ast.node.map import MapParser from smnp.ast.node.map import MapParser
from smnp.ast.node.expression import ExpressionParser
parentheses = Parser.allOf(
Parser.terminal(TokenType.OPEN_PAREN),
Parser.doAssert(ExpressionParser, "expression"),
Parser.terminal(TokenType.CLOSE_PAREN),
createNode=lambda open, expr, close: expr,
name="grouping parentheses"
)
return Parser.oneOf( return Parser.oneOf(
parentheses,
LiteralParser, LiteralParser,
IdentifierParser, IdentifierParser,
ListParser, ListParser,

View File

@@ -1,4 +1,6 @@
from smnp.ast.node.operator import BinaryOperator from smnp.ast.node.model import Node
from smnp.ast.node.none import NoneNode
from smnp.ast.node.operator import BinaryOperator, Operator
from smnp.ast.node.term import TermParser from smnp.ast.node.term import TermParser
from smnp.ast.parser import Parser from smnp.ast.parser import Parser
from smnp.token.type import TokenType from smnp.token.type import TokenType
@@ -20,7 +22,43 @@ class Or(BinaryOperator):
pass pass
def ExpressionParser(input): class Loop(BinaryOperator):
def __init__(self, pos):
super().__init__(pos)
self.children.extend([NoneNode(), NoneNode()])
@property
def parameters(self):
return self[3]
@parameters.setter
def parameters(self, value):
self[3] = value
@property
def filter(self):
return self[4]
@filter.setter
def filter(self, value):
self[4] = value
@classmethod
def loop(cls, left, parameters, operator, right, filter):
node = cls(left.pos)
node.left = left
node.parameters = parameters
node.operator = operator
node.right = right
node.filter = filter
return node
class LoopParameters(Node):
pass
def ExpressionWithoutLoopParser(input):
expr1 = Parser.leftAssociativeOperatorParser( expr1 = Parser.leftAssociativeOperatorParser(
TermParser, TermParser,
[TokenType.PLUS, TokenType.MINUS], [TokenType.PLUS, TokenType.MINUS],
@@ -49,3 +87,42 @@ def ExpressionParser(input):
lambda left, op, right: Or.withValues(left, op, right) lambda left, op, right: Or.withValues(left, op, right)
)(input) )(input)
def LoopParser(input):
from smnp.ast.node.identifier import IdentifierLiteralParser
from smnp.ast.node.iterable import abstractIterableParser
from smnp.ast.node.statement import StatementParser
loopParameters = Parser.allOf(
Parser.terminal(TokenType.AS),
Parser.oneOf(
Parser.wrap(IdentifierLiteralParser, lambda id: LoopParameters.withChildren([id], id.pos)),
abstractIterableParser(LoopParameters, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, IdentifierLiteralParser)
),
createNode=lambda asKeyword, parameters: parameters,
name="loop parameters"
)
loopFilter = Parser.allOf(
Parser.terminal(TokenType.PERCENT),
Parser.doAssert(ExpressionWithoutLoopParser, "filter as bool expression"),
createNode=lambda percent, expr: expr,
name="loop filter"
)
return Parser.allOf(
ExpressionWithoutLoopParser,
Parser.optional(loopParameters),
Parser.terminal(TokenType.CARET, createNode=Operator.withValue),
StatementParser,
Parser.optional(loopFilter),
createNode=Loop.loop,
name="caret-loop"
)(input)
def ExpressionParser(input):
return Parser.oneOf(
LoopParser,
ExpressionWithoutLoopParser
)(input)

View File

@@ -1,6 +1,3 @@
from smnp.ast.node.iterable import abstractIterableParser
from smnp.ast.node.model import Node
from smnp.ast.node.none import NoneNode
from smnp.ast.node.operator import BinaryOperator, Operator, UnaryOperator from smnp.ast.node.operator import BinaryOperator, Operator, UnaryOperator
from smnp.ast.node.unit import UnitParser from smnp.ast.node.unit import UnitParser
from smnp.ast.parser import Parser from smnp.ast.parser import Parser
@@ -13,57 +10,12 @@ class NotOperator(UnaryOperator):
class Power(BinaryOperator): class Power(BinaryOperator):
pass pass
class Loop(BinaryOperator):
def __init__(self, pos):
super().__init__(pos)
self.children.append(NoneNode())
@property
def parameters(self):
return self[3]
@parameters.setter
def parameters(self, value):
self[3] = value
@classmethod
def loop(cls, left, parameters, operator, right):
node = cls(left.pos)
node.left = left
node.parameters = parameters
node.operator = operator
node.right = right
return node
class LoopParameters(Node):
pass
def FactorParser(input): def FactorParser(input):
from smnp.ast.node.expression import ExpressionParser
from smnp.ast.node.statement import StatementParser
from smnp.ast.node.identifier import IdentifierLiteralParser
parentheses = Parser.allOf(
Parser.terminal(TokenType.OPEN_PAREN),
Parser.doAssert(ExpressionParser, "expression"),
Parser.terminal(TokenType.CLOSE_PAREN),
createNode=lambda open, expr, close: expr,
name="grouping parentheses"
)
factorOperands = Parser.oneOf(
parentheses,
UnitParser,
name="factor operands"
)
powerFactor = Parser.leftAssociativeOperatorParser( powerFactor = Parser.leftAssociativeOperatorParser(
factorOperands, UnitParser,
[TokenType.DOUBLE_ASTERISK], [TokenType.DOUBLE_ASTERISK],
factorOperands, UnitParser,
lambda left, op, right: Power.withValues(left, op, right), lambda left, op, right: Power.withValues(left, op, right),
name="power operator" name="power operator"
) )
@@ -75,27 +27,7 @@ def FactorParser(input):
name="not" name="not"
) )
loopParameters = Parser.allOf(
Parser.terminal(TokenType.AS),
Parser.oneOf(
Parser.wrap(IdentifierLiteralParser, lambda id: LoopParameters.withChildren([id], id.pos)),
abstractIterableParser(LoopParameters, TokenType.OPEN_PAREN, TokenType.CLOSE_PAREN, IdentifierLiteralParser)
),
createNode=lambda asKeyword, parameters: parameters,
name="loop parameters"
)
loopFactor = Parser.allOf(
powerFactor,
Parser.optional(loopParameters),
Parser.terminal(TokenType.DASH, createNode=Operator.withValue),
StatementParser,
createNode=Loop.loop,
name="dash-loop"
)
return Parser.oneOf( return Parser.oneOf(
loopFactor,
notOperator, notOperator,
powerFactor, powerFactor,
name="factor" name="factor"

View File

@@ -1,4 +1,5 @@
from smnp.ast.node.block import BlockParser from smnp.ast.node.block import BlockParser
from smnp.ast.node.expression import ExpressionParser
from smnp.ast.node.identifier import IdentifierLiteralParser from smnp.ast.node.identifier import IdentifierLiteralParser
from smnp.ast.node.iterable import abstractIterableParser from smnp.ast.node.iterable import abstractIterableParser
from smnp.ast.node.model import Node from smnp.ast.node.model import Node
@@ -16,7 +17,7 @@ class Argument(Node):
def __init__(self, pos): def __init__(self, pos):
super().__init__(pos) super().__init__(pos)
self.children = [NoneNode(), NoneNode(), False] self.children = [NoneNode(), NoneNode(), False, NoneNode()]
@property @property
def type(self): def type(self):
@@ -48,6 +49,14 @@ class Argument(Node):
self[2] = value self[2] = value
@property
def optionalValue(self):
return self[3]
@optionalValue.setter
def optionalValue(self, value):
self[3] = value
class VarargNode(Node): class VarargNode(Node):
pass pass
@@ -90,7 +99,7 @@ class FunctionDefinition(Node):
return node return node
def ArgumentParser(input): def RegularArgumentParser(input):
def createNode(type, variable, vararg): def createNode(type, variable, vararg):
pos = type.pos if isinstance(type, Type) else variable.pos pos = type.pos if isinstance(type, Type) else variable.pos
node = Argument(pos) node = Argument(pos)
@@ -104,6 +113,33 @@ def ArgumentParser(input):
Parser.doAssert(IdentifierLiteralParser, "argument name"), Parser.doAssert(IdentifierLiteralParser, "argument name"),
Parser.optional(Parser.terminal(TokenType.DOTS, lambda val, pos: True)), Parser.optional(Parser.terminal(TokenType.DOTS, lambda val, pos: True)),
createNode=createNode, createNode=createNode,
name="regular function argument"
)(input)
def OptionalArgumentParser(input):
def createNode(type, variable, _, optional):
pos = type.pos if isinstance(type, Type) else variable.pos
node = Argument(pos)
node.type = type
node.variable = variable
node.optionalValue = optional
return node
return Parser.allOf(
Parser.optional(TypeParser),
Parser.doAssert(IdentifierLiteralParser, "argument name"),
Parser.terminal(TokenType.ASSIGN),
Parser.doAssert(ExpressionParser, "expression"),
createNode=createNode,
name="optional function argument"
)(input)
def ArgumentParser(input):
return Parser.oneOf(
OptionalArgumentParser,
RegularArgumentParser,
name="function argument" name="function argument"
)(input) )(input)

View File

@@ -1,4 +1,5 @@
from smnp.ast.node.atom import LiteralParser from smnp.ast.node.atom import LiteralParser
from smnp.ast.node.identifier import IdentifierLiteralParser
from smnp.ast.node.iterable import abstractIterableParser from smnp.ast.node.iterable import abstractIterableParser
from smnp.ast.node.model import Node from smnp.ast.node.model import Node
from smnp.ast.node.operator import BinaryOperator, Operator from smnp.ast.node.operator import BinaryOperator, Operator
@@ -31,12 +32,15 @@ class Map(Node):
def MapParser(input): def MapParser(input):
from smnp.ast.node.expression import ExpressionParser from smnp.ast.node.expression import ExpressionParser
keyParser = LiteralParser keyParser = Parser.oneOf(
LiteralParser,
IdentifierLiteralParser
)
valueParser = ExpressionParser valueParser = ExpressionParser
mapEntryParser = Parser.allOf( mapEntryParser = Parser.allOf(
keyParser, keyParser,
Parser.terminal(TokenType.ARROW, createNode=Operator.withValue, doAssert=True), Parser.terminal(TokenType.ARROW, createNode=Operator.withValue),
Parser.doAssert(valueParser, "expression"), Parser.doAssert(valueParser, "expression"),
createNode=MapEntry.withValues createNode=MapEntry.withValues
) )

View File

@@ -1,5 +1,6 @@
from smnp.ast.node.model import Node from smnp.ast.node.model import Node
from smnp.ast.parser import Parser from smnp.ast.parser import Parser
from smnp.token.type import TokenType
class Statement(Node): class Statement(Node):
@@ -10,12 +11,27 @@ def StatementParser(input):
from smnp.ast.node.block import BlockParser from smnp.ast.node.block import BlockParser
from smnp.ast.node.condition import IfElseStatementParser from smnp.ast.node.condition import IfElseStatementParser
from smnp.ast.node.expression import ExpressionParser from smnp.ast.node.expression import ExpressionParser
from smnp.ast.node.ret import ReturnParser from smnp.ast.node.ret import ReturnParser
return Parser.oneOf( from smnp.ast.node.throw import ThrowParser
IfElseStatementParser,
ExpressionParser, return withSemicolon(
BlockParser, Parser.oneOf(
ReturnParser, IfElseStatementParser,
name="statement" ExpressionParser, # Must be above BlockParser because of Map's syntax with curly braces
)(input) BlockParser,
ReturnParser,
ThrowParser,
name="statement"
), optional=True)(input)
def withSemicolon(parser, optional=False, doAssert=False):
semicolonParser = Parser.optional(Parser.terminal(TokenType.SEMICOLON)) if optional else Parser.terminal(
TokenType.SEMICOLON, doAssert=doAssert)
return Parser.allOf(
parser,
semicolonParser,
createNode=lambda stmt, semicolon: stmt,
name="semicolon" + "?" if optional else ""
)

17
smnp/ast/node/throw.py Normal file
View File

@@ -0,0 +1,17 @@
from smnp.ast.node.expression import ExpressionParser
from smnp.ast.node.valuable import Valuable
from smnp.ast.parser import Parser
from smnp.token.type import TokenType
class Throw(Valuable):
pass
def ThrowParser(input):
return Parser.allOf(
Parser.terminal(TokenType.THROW),
Parser.doAssert(ExpressionParser, "error message as string"),
createNode=lambda throw, message: Throw.withValue(message, throw.pos),
name="throw"
)(input)

0
smnp/cli/__init__.py Normal file
View File

23
smnp/cli/parser.py Normal file
View File

@@ -0,0 +1,23 @@
import argparse
VERSION = "0.1"
DESCRIPTION = """
Simple Music Notation Processor is a command line tool enabling you to do some music stuff using custom domain-specific language.
"""
class CliParser(object):
def __init__(self):
self.parser = argparse.ArgumentParser(description=DESCRIPTION)
self.parser.add_argument('file', nargs='*', help='a file containing SMNP code')
self.parser.add_argument('-c', '--code', action='append', default=[], type=str, help='a string with SMNP code')
self.parser.add_argument('-m', '--mic', action='store_true', help='test microphone level')
self.parser.add_argument('-C', '--config', type=argparse.FileType('-r'), help='a file containing settings (not implemented yet)')
self.parser.add_argument('-p', '--params', action='append', help='pass arguments to program (not implemented yet)')
self.parser.add_argument('-v', '--version', action='version')
self.parser.add_argument('--tokens', action='store_true', help='print tokens of parsed code')
self.parser.add_argument('--ast', action='store_true', help='print abstract syntax tree of parsed code')
self.parser.add_argument('--dry-run', action='store_true', help='don\'t execute passed code')
self.parser.version = VERSION
def parse(self):
return self.parser.parse_args()

View File

@@ -1,17 +1,19 @@
from smnp.error.function import FunctionNotFoundException, MethodNotFoundException, IllegalFunctionInvocationException from smnp.error.function import FunctionNotFoundException, MethodNotFoundException, IllegalFunctionInvocationException
from smnp.error.runtime import RuntimeException from smnp.error.runtime import RuntimeException
from smnp.function.tools import argsTypesToString from smnp.function.tools import argsTypesToString
from smnp.runtime.evaluators.function import BodyEvaluator from smnp.runtime.evaluators.function import BodyEvaluator, Return
from smnp.type.model import Type
class Environment(): class Environment():
def __init__(self, scopes, functions, methods): def __init__(self, scopes, functions, methods, source):
self.scopes = scopes self.scopes = scopes
self.functions = functions self.functions = functions
self.methods = methods self.methods = methods
self.customFunctions = [] self.customFunctions = []
self.customMethods = [] self.customMethods = []
self.callStack = [] self.callStack = []
self.source = source
def invokeMethod(self, object, name, args): def invokeMethod(self, object, name, args):
builtinMethodResult = self._invokeBuiltinMethod(object, name, args) builtinMethodResult = self._invokeBuiltinMethod(object, name, args)
@@ -38,10 +40,15 @@ class Environment():
if method.typeSignature.check([object])[0] and method.name == name: #Todo sprawdzic sygnature typu if method.typeSignature.check([object])[0] and method.name == name: #Todo sprawdzic sygnature typu
signatureCheckresult = method.signature.check(args) signatureCheckresult = method.signature.check(args)
if signatureCheckresult[0]: if signatureCheckresult[0]:
self.scopes.append({argName: argValue for argName, argValue in zip(method.arguments, list(signatureCheckresult[1:]))}) self.scopes.append(method.defaultArgs)
self.scopes[-1].update({argName: argValue for argName, argValue in zip(method.arguments, list(signatureCheckresult[1:]))})
self.scopes[-1][method.alias] = object self.scopes[-1][method.alias] = object
self.callStack.append(CallStackItem(name)) self.callStack.append(CallStackItem(name))
result = BodyEvaluator.evaluate(method.body, self).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult result = Type.void()
try:
BodyEvaluator.evaluate(method.body, self).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
except Return as r:
result = r.value
self.callStack.pop(-1) self.callStack.pop(-1)
self.scopes.pop(-1) self.scopes.pop(-1)
return (True, result) return (True, result)
@@ -74,20 +81,27 @@ class Environment():
if function.name == name: if function.name == name:
signatureCheckresult = function.signature.check(args) signatureCheckresult = function.signature.check(args)
if signatureCheckresult[0]: if signatureCheckresult[0]:
self.scopes.append({ argName: argValue for argName, argValue in zip(function.arguments, list(signatureCheckresult[1:])) }) self.appendScope(function.defaultArgs)
appendedScopeIndex = len(self.scopes)-1
self.scopes[-1].update({ argName: argValue for argName, argValue in zip(function.arguments, list(signatureCheckresult[1:])) })
self.callStack.append(CallStackItem(name)) self.callStack.append(CallStackItem(name))
result = BodyEvaluator.evaluate(function.body, self).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult result = Type.void()
try:
BodyEvaluator.evaluate(function.body, self).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
except Return as r:
result = r.value
self.callStack.pop(-1) self.callStack.pop(-1)
self.scopes.pop(-1) self.popScope(mergeVariables=False)
self.removeScopesAfter(appendedScopeIndex)
return (True, result) return (True, result)
raise IllegalFunctionInvocationException(f"{function.name}{function.signature.string}", f"{name}{argsTypesToString(args)}") raise IllegalFunctionInvocationException(f"{function.name}{function.signature.string}", f"{name}{argsTypesToString(args)}")
return (False, None) return (False, None)
def addCustomFunction(self, name, signature, arguments, body): def addCustomFunction(self, name, signature, arguments, body, defaultArguments):
if len([fun for fun in self.functions + self.customFunctions if fun.name == name]) > 0: if len([fun for fun in self.functions + self.customFunctions if fun.name == name]) > 0:
raise RuntimeException(f"Cannot redeclare function '{name}'", None) raise RuntimeException(f"Cannot redeclare function '{name}'", None)
self.customFunctions.append(CustomFunction(name, signature, arguments, body)) self.customFunctions.append(CustomFunction(name, signature, arguments, body, defaultArguments))
# TODO: # TODO:
# There is still problem with checking existing of generic types, like lists: # There is still problem with checking existing of generic types, like lists:
@@ -98,14 +112,14 @@ class Environment():
# function foo() { return 2 } # function foo() { return 2 }
# } # }
# Then calling [1, 2, 3, 4].foo() will produce 1, when the second method is more suitable # Then calling [1, 2, 3, 4].foo() will produce 1, when the second method is more suitable
def addCustomMethod(self, typeSignature, alias, name, signature, arguments, body): def addCustomMethod(self, typeSignature, alias, name, signature, arguments, body, defaultArguments):
if len([m for m in self.methods if m.name == name and m.signature.matchers[0] == typeSignature.matchers[0]]) > 0: if len([m for m in self.methods if m.name == name and m.signature.matchers[0] == typeSignature.matchers[0]]) > 0:
raise RuntimeException(f"Cannot redeclare method '{name}' for type '{typeSignature.matchers[0]}'", None) raise RuntimeException(f"Cannot redeclare method '{name}' for type '{typeSignature.matchers[0]}'", None)
if len([m for m in self.customMethods if m.name == name and m.typeSignature.matchers[0] == typeSignature.matchers[0]]) > 0: if len([m for m in self.customMethods if m.name == name and m.typeSignature.matchers[0] == typeSignature.matchers[0]]) > 0:
raise RuntimeException(f"Cannot redeclare method '{name}' for type '{typeSignature.matchers[0]}'", None) raise RuntimeException(f"Cannot redeclare method '{name}' for type '{typeSignature.matchers[0]}'", None)
self.customMethods.append(CustomMethod(typeSignature, alias, name, signature, arguments, body)) self.customMethods.append(CustomMethod(typeSignature, alias, name, signature, arguments, body, defaultArguments))
def findVariable(self, name, type=None, pos=None): def findVariable(self, name, type=None, pos=None):
for scope in reversed(self.scopes): for scope in reversed(self.scopes):
@@ -128,6 +142,20 @@ class Environment():
else: else:
return scope return scope
def appendScope(self, variables=None):
if variables is None:
variables = {}
self.scopes.append(variables)
def popScope(self, mergeVariables=True):
lastScope = self.scopes.pop(-1)
if mergeVariables:
self.scopes[-1].update(lastScope)
def removeScopesAfter(self, index):
del self.scopes[index:]
def scopesToString(self): def scopesToString(self):
return "Scopes:\n" + ("\n".join([ f" [{i}]: {scope}" for i, scope in enumerate(self.scopes) ])) return "Scopes:\n" + ("\n".join([ f" [{i}]: {scope}" for i, scope in enumerate(self.scopes) ]))
@@ -162,22 +190,23 @@ class Environment():
class CallStackItem: class CallStackItem:
def __init__(self, function): def __init__(self, function):
self.function = function self.function = function
self.value = None
class CustomFunction: class CustomFunction:
def __init__(self, name, signature, arguments, body): def __init__(self, name, signature, arguments, body, defaultArgs):
self.name = name self.name = name
self.signature = signature self.signature = signature
self.arguments = arguments self.arguments = arguments
self.body = body self.body = body
self.defaultArgs = defaultArgs
class CustomMethod: class CustomMethod:
def __init__(self, typeSignature, alias, name, signature, arguments, body): def __init__(self, typeSignature, alias, name, signature, arguments, body, defaultArgs):
self.typeSignature = typeSignature self.typeSignature = typeSignature
self.alias = alias self.alias = alias
self.name = name self.name = name
self.signature = signature self.signature = signature
self.arguments = arguments self.arguments = arguments
self.body = body self.body = body
self.defaultArgs = defaultArgs

View File

@@ -1,7 +1,3 @@
from smnp.environment.environment import Environment
from smnp.module import functions, methods
def createEnvironment(): def createEnvironment():
return Environment([{}], functions, methods) return

View File

@@ -2,6 +2,7 @@ class SmnpException(Exception):
def __init__(self, msg, pos): def __init__(self, msg, pos):
self.msg = msg self.msg = msg
self.pos = pos self.pos = pos
self.file = None
def _title(self): def _title(self):
pass pass
@@ -10,7 +11,10 @@ class SmnpException(Exception):
return "" return ""
def _position(self): def _position(self):
return "" if self.pos is None else f" [line {self.pos[0]+1}, col {self.pos[1]+1}]" return "" if self.pos is None else f"[line {self.pos[0]+1}, col {self.pos[1]+1}]"
def _file(self):
return "" if self.file is None else f"File: {self.file}"
def message(self): def message(self):
return f"{self._title()}{self._position()}:\n{self.msg}\n{self._postMessage()}" return f"{self._title()}\n{self._file()} {self._position()}\n\n{self.msg}\n{self._postMessage()}"

12
smnp/error/custom.py Normal file
View File

@@ -0,0 +1,12 @@
from smnp.error.runtime import RuntimeException
class CustomException(RuntimeException):
def __init__(self, message, pos):
super().__init__(message, pos)
def _title(self):
return "Execution Error"
def _postMessage(self):
return "\n" + self.environment.callStackToString() if len(self.environment.callStack) > 0 else ""

View File

@@ -12,6 +12,9 @@ class Signature:
def varargSignature(varargMatcher, *basicSignature, wrapVarargInValue=False): def varargSignature(varargMatcher, *basicSignature, wrapVarargInValue=False):
def check(args): def check(args):
if any([ matcher.optional for matcher in [ varargMatcher, *basicSignature ]]):
raise RuntimeError("Vararg signature can't have optional arguments")
if len(basicSignature) > len(args): if len(basicSignature) > len(args):
return doesNotMatchVararg(basicSignature) return doesNotMatchVararg(basicSignature)
@@ -38,7 +41,7 @@ def doesNotMatchVararg(basicSignature):
def signature(*signature): def signature(*signature):
def check(args): def check(args):
if len(signature) != len(args): if len(args) > len(signature) or len(args) < len([ matcher for matcher in signature if not matcher.optional ]):
return doesNotMatch(signature) return doesNotMatch(signature)
for s, a in zip(signature, args): for s, a in zip(signature, args):
@@ -52,6 +55,12 @@ def signature(*signature):
return Signature(check, string, signature) return Signature(check, string, signature)
def optional(matcher):
matcher.optional = True
matcher.string += "?"
return matcher
def doesNotMatch(sign): def doesNotMatch(sign):
return (False, *[None for n in sign]) return (False, *[None for n in sign])

492
smnp/library/code/main.mus Normal file
View File

@@ -0,0 +1,492 @@
function flat(list lists...) {
return _flat(lists as l ^ _flat(l, []), []);
}
function _flat(list l, list output) {
l as elem ^ {
if (typeOf(elem) == list) {
output = _flat(elem, output);
} else {
output = output + [elem];
}
}
return output;
}
extend note as n {
function withOctave(integer octave) {
return Note(n.pitch, octave, n.duration, n.dot);
}
function withDuration(integer duration) {
return Note(n.pitch, n.octave, duration, n.dot);
}
function withDot(bool dot) {
return Note(n.pitch, n.octave, n.duration, dot);
}
function toIntRepr() {
return n.octave * 12 + _pitchToNumber(n.pitch);
}
function transpose(integer value) {
return noteFromIntRepr(n.toIntRepr() + value, n.duration, n.dot);
}
}
function noteFromIntRepr(integer intRepr, integer duration, bool dot) {
pitch = _numberToPitch(mod(intRepr, 12));
octave = Integer(intRepr / 12);
return Note(pitch, octave, duration, dot);
}
function mod(integer a, integer b) {
return a - b * Integer(a/b);
}
function _pitchToNumber(string pitch) {
return _keysToIntMapper(
"C",
"CIS",
"D",
"DIS",
"E",
"F",
"FIS",
"G",
"GIS",
"A",
"AIS",
"H"
).get(pitch);
}
function _keysToIntMapper(keys...) {
return Map(keys as (i, key) ^ [key, i]);
}
function _numberToPitch(integer number) {
return ["C", "CIS", "D", "DIS", "E", "F", "FIS", "G", "GIS", "A", "AIS", "H"].get(number);
}
function transpose(integer value, <note, integer, list<note, integer>> notes...) {
if (notes.size == 1) {
first = notes.get(0);
if (typeOf(first) == integer) {
return first;
} else if (typeOf(first) == note) {
return first.transpose(value);
} else if (typeOf(first) == list) {
return _transpose(value, first);
}
}
noteOrInteger = false;
lists = false;
notes as n ^ {
if (typeOf(n) == note or typeOf(n) == integer) {
noteOrInteger = true;
if (lists) {
throw "Mixing notes and integers with lists of them is not supported";
}
} else if (typeOf(n) == list) {
lists = true;
if (noteOrInteger) {
throw "Mixing notes and integers with lists of them is not supported";
}
}
}
output = [];
notes as n ^ {
if (typeOf(n) == integer) {
output = output + [n];
} else if (typeOf(n) == note) {
output = output + [n.transpose(value)];
} else if (typeOf(n) == list) {
output = output + [_transpose(value, n)];
}
}
return output;
}
function _transpose(integer value, list<note, integer> notes) {
output = [];
notes as n ^ {
if (typeOf(n) == integer) {
output = output + [n];
} else if (typeOf(n) == note) {
output = output + [n.transpose(value)];
}
}
return output;
}
function transposeTo(note target, <note, integer, list<note, integer>> notes...) {
if (notes.size == 1) {
first = notes.get(0);
if (typeOf(first) == integer) {
return first;
} else if (typeOf(first) == note) {
return _transposeTo(target, notes).get(0);
} else if (typeOf(first) == list) {
return _transposeTo(target, first);
}
}
noteOrInteger = false;
lists = false;
notes as n ^ {
if (typeOf(n) == note or typeOf(n) == integer) {
noteOrInteger = true;
if (lists) {
throw "Mixing notes and integers with lists of them is not supported";
}
} else if (typeOf(n) == list) {
lists = true;
if (noteOrInteger) {
throw "Mixing notes and integers with lists of them is not supported";
}
}
}
if (noteOrInteger) {
return _transposeTo(target, notes);
}
if (lists) {
return notes as n ^ _transposeTo(target, n);
}
}
function _transposeTo(note target, list<note, integer> notes) {
if (notes.size == 0) {
throw "Provide list with one note at least";
}
firstNote = notes.get(0);
semitones = semitones(firstNote, target);
return transpose(semitones, notes);
}
function tuplet(integer n, integer m, note notes...) {
if (n != notes.size) {
throw "Expected " + n.toString() + " notes exactly, whereas " + notes.size.toString() + " was passed";
}
return notes as x ^ x.withDuration(x.duration * n / m);
}
extend list as l with function contains(expectedValue) {
return (l as value ^ value % value == expectedValue).size > 0;
}
extend map as m {
function containsKey(expectedKey) {
return m.keys.contains(expectedKey);
}
function containsValue(expectedValue) {
return m.values.contains(expectedValue);
}
function contains(key, value) {
if (m.keys.contains(key)) {
return m.get(key) == value;
}
return false;
}
}
function sample(items...) {
if (items.size == 0) {
throw "Provide one item at least";
}
if (items.size == 1 and typeOf(items) == list) {
return items.get(0).get(rand(0, items.get(0).size-1));
}
return items.get(rand(0, items.size-1));
}
extend string as s with function join(list<string> l) {
output = "";
l as (index, item) ^ {
output = output + item;
if (index < l.size - 1) {
output = output + s;
}
}
return output;
}
function random(map<string><> items...) {
accumulator = 0;
items as (index, item) ^ {
if (item.size != 2) {
throw "Expected lists with two items: percent and value";
}
if (not item.containsKey("percent")) {
throw "Item " + (index+1).toString() + " does not have 'percent' key";
}
if (not item.containsKey("value")) {
throw "Item " + (index+1).toString() + " does not have 'value' key";
}
accumulator = accumulator + item.get("percent");
}
if (accumulator != 100) {
throw "Sum of first element of each item must be equal to 100";
}
accumulator = 0;
random = rand(0, 99);
items as item ^ {
accumulator = accumulator + item.get("percent");
if (random < accumulator) {
return item.get("value");
}
}
}
function semitones(<note, integer, list<note, integer>> notes...) {
noteOrInteger = false;
lists = false;
notes as n ^ {
if (typeOf(n) == note or typeOf(n) == integer) {
noteOrInteger = true;
if (lists) {
throw "Mixing notes and integers with lists of them is not supported";
}
} else if (typeOf(n) == list) {
lists = true;
if (noteOrInteger) {
throw "Mixing notes and integers with lists of them is not supported";
}
}
}
if (noteOrInteger) {
return _semitones(notes);
}
if (lists) {
output = [];
notes as n ^ {
output = output + [_semitones(n)];
}
#if (output.size == 1) {
# return output.get(0)
#}
return output;
}
return [];
}
function _semitones(list<note, integer> notes) {
onlyNotes = notes as n ^ n % typeOf(n) == note;
if (onlyNotes.size == 2 and typeOf(onlyNotes.get(0)) == note and typeOf(onlyNotes.get(1)) == note) {
first = onlyNotes.get(0);
second = onlyNotes.get(1);
return second.toIntRepr() - first.toIntRepr();
}
if (onlyNotes.size < 2) {
throw "Provide 2 notes at least to evaluate semitones between them";
}
output = [];
range(1, onlyNotes.size-1) as i ^ {
output = output + [onlyNotes.get(i).toIntRepr() - onlyNotes.get(i-1).toIntRepr()];
}
return output;
}
function stringInterval(integer semitones) {
return [
"1",
"2m",
"2M",
"3m",
"3M",
"4",
"5d/4A",
"5",
"6m",
"6M",
"7m",
"7M"
].get(semitones);
}
function interval(<note, integer, list<note, integer>> notes...) {
noteOrInteger = false;
lists = false;
notes as n ^ {
if (typeOf(n) == note or typeOf(n) == integer) {
noteOrInteger = true;
if (lists) {
throw "Mixing notes and integers with lists of them is not supported";
}
} else if (typeOf(n) == list) {
lists = true;
if (noteOrInteger) {
throw "Mixing notes and integers with lists of them is not supported";
}
}
}
if (noteOrInteger) {
semitones = _semitones(notes);
if (typeOf(semitones) == list) {
return semitones as n ^ stringInterval(n);
} else {
return stringInterval(semitones);
}
}
if (lists) {
output = [];
notes as n ^ {
semitones = _semitones(n);
if (typeOf(semitones) == list) {
output = output + [_semitones(n) as semitone ^ stringInterval(semitone)];
} else {
output = output + [stringInterval(semitones)];
}
}
#if (output.size == 1) {
# return output.get(0);
#}
return output;
}
return [];
}
function noteRange(note a, note b, string filter = "all") {
filters = {
"all" -> [ "C", "CIS", "D", "DIS", "E", "F", "FIS", "G", "GIS", "A", "AIS", "H" ],
"diatonic" -> [ "C", "D", "E", "F", "G", "A", "H" ],
"chromatic" -> [ "CIS", "DIS", "FIS", "GIS", "AIS" ]
};
if (not filters.containsKey(filter)) {
throw "Unknown filter: '" + filter + "'";
}
notes = range(a.toIntRepr(), b.toIntRepr()) as intRepr ^ noteFromIntRepr(intRepr, a.duration, a.dot);
return notes as n ^ n % filters.get(filter).contains(n.pitch);
}
function range(<integer, float> a, <integer, float> b, <integer, float> step = 1) {
if (not (step > 0)) {
throw "Step should be greater than 0";
}
if (a > b) {
throw "Upper range value should be greater than lower or equal to";
}
output = [];
i = a;
i <= b ^ {
output = output + [i];
i = i + step;
}
return output;
}
function alert(<integer, bool> cycles = true, string melody = "beep", list<float, integer> overtones = [0.5, 0.0, 0.0, 0.5]) {
if (not [integer, bool].contains(typeOf(cycles))) {
throw "Provide 'true' or number of cycles as first argument";
}
if (typeOf(cycles) == integer) {
if (cycles < 1) {
throw "Number of cycles cannot be less than 1";
}
}
if (typeOf(cycles) == bool) {
if (not cycles) {
throw "Provide 'true' or number of cycles as first argument";
}
}
notes = {
"beep" -> [@c5:16, 32, @c5:16, 3],
"s1" -> noteRange(@c5:32, @g5:32),
"s2" -> _upDown(noteRange(@c5:32, @g5:32)),
"s3" -> [@a5:16, @d5:16],
"semitone" -> [@c5:16, @db5:16]
};
if (not notes.containsKey(melody)) {
throw "Unknown melody '" + melody + "'. Available: 'beep', 's1', 's2', 's3' and 'semitone'";
}
config = {
bpm -> 120,
decay -> 0.5,
attack -> 200,
overtones -> overtones
};
wave = wave(config, notes.get(melody));
cycles ^ synth(wave);
}
function _upDown(list l) {
return l + -l;
}
function metronome(integer bpm = 120, integer beats = 4, countMeasures = false) {
accent = wave({
overtones -> flat([0.5, 0.1, 10^0, 0.1, 10^0, 0.1, 20^0, 0.1, 25^0, 0.05, 25^0, 0.05]),
attack -> 0,
decay -> 5,
bpm -> bpm
}, @c);
beat = wave({
overtones -> flat([0.5, 10^0, 0.3, 10^0, 0.2]),
attack -> 0,
decay -> 100,
bpm -> bpm
}, @c);
measure = 1;
true ^ {
if (countMeasures) {
println(measure);
measure = measure + 1;
}
synth(accent);
beats - 1 ^ synth(beat);
}
}

View File

@@ -5,5 +5,5 @@ from smnp.program.interpreter import Interpreter
def loadStandardLibrary(): def loadStandardLibrary():
mainSource = resource_string('smnp.library.code', 'main.mus').decode("utf-8") mainSource = resource_string('smnp.library.code', 'main.mus').decode("utf-8")
env = Interpreter.interpretString(mainSource) env = Interpreter.interpretString(mainSource, "<stdlib>")
return env return env

View File

@@ -1,20 +1,38 @@
import sys from smnp.cli.parser import CliParser
from smnp.error.base import SmnpException from smnp.error.base import SmnpException
from smnp.library.loader import loadStandardLibrary from smnp.library.loader import loadStandardLibrary
from smnp.module.mic.lib.detector.noise import NoiseDetector
from smnp.program.interpreter import Interpreter from smnp.program.interpreter import Interpreter
def interpretFile(args, file):
stdLibraryEnv = loadStandardLibrary() if not args.dry_run else None
Interpreter.interpretFile(file, printTokens=args.tokens, printAst=args.ast, execute=not args.dry_run, baseEnvironment=stdLibraryEnv)
def interpretString(args, string):
stdLibraryEnv = loadStandardLibrary() if not args.dry_run else None
Interpreter.interpretString(string, printTokens=args.tokens, printAst=args.ast, execute=not args.dry_run, baseEnvironment=stdLibraryEnv, source='<cli>')
def main(): def main():
try: try:
stdLibraryEnv = loadStandardLibrary() parser = CliParser()
Interpreter.interpretFile(sys.argv[1], printTokens=False, printAst=False, execute=True, baseEnvironment=stdLibraryEnv) args = parser.parse()
#draft()
#tokens = tokenize(['function a(b...) { x+y}']) if args.mic:
#FunctionDefinitionParser(tokens).node.print() nd = NoiseDetector()
nd.test()
for code in args.code:
interpretString(args, code)
for file in args.file:
interpretFile(args, file)
except SmnpException as e: except SmnpException as e:
print(e.message()) print(e.message())
except KeyboardInterrupt: except KeyboardInterrupt:
print("Program interrupted") print("Program interrupted")

View File

@@ -1,4 +1,4 @@
from smnp.module import system, mic, note, iterable, sound, synth, string, util from smnp.module import system, mic, note, iterable, sound, synth, string, util, integer, float
functions = [ *system.functions, *mic.functions, *note.functions, *iterable.functions, *sound.functions, *synth.functions, *string.functions, *util.functions ] functions = [ *system.functions, *mic.functions, *note.functions, *iterable.functions, *sound.functions, *synth.functions, *string.functions, *util.functions, *integer.functions, *float.functions ]
methods = [ *system.methods, *mic.methods, *note.methods, *iterable.methods, *sound.methods, *synth.methods, *string.functions, *util.methods ] methods = [ *system.methods, *mic.methods, *note.methods, *iterable.methods, *sound.methods, *synth.methods, *string.methods, *util.methods, *integer.methods, *float.methods ]

View File

@@ -0,0 +1,4 @@
from smnp.module.float.function import float
functions = [ float.function ]
methods = []

View File

View File

@@ -0,0 +1,26 @@
from smnp.function.model import CombinedFunction, Function
from smnp.function.signature import signature
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature1 = signature(ofType(Type.INTEGER))
def _function1(env, value):
return Type.float(float(value.value))
_signature2 = signature(ofType(Type.STRING))
def _function2(env, value):
return Type.float(float(value.value))
_signature3 = signature(ofType(Type.FLOAT))
def _function3(env, value):
return value
function = CombinedFunction(
'Float',
Function(_signature1, _function1),
Function(_signature2, _function2),
Function(_signature3, _function3),
)

View File

@@ -0,0 +1,4 @@
from smnp.module.integer.function import integer
functions = [ integer.function ]
methods = []

View File

View File

@@ -0,0 +1,25 @@
from smnp.function.model import CombinedFunction, Function
from smnp.function.signature import signature
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature1 = signature(ofType(Type.FLOAT))
def _function1(env, value):
return Type.integer(int(value.value))
_signature2 = signature(ofType(Type.STRING))
def _function2(env, value):
return Type.integer(int(value.value))
_signature3 = signature(ofType(Type.INTEGER))
def _function3(env, value):
return value
function = CombinedFunction(
'Integer',
Function(_signature1, _function1),
Function(_signature2, _function2),
Function(_signature3, _function3),
)

View File

@@ -1,4 +1,4 @@
from smnp.module.iterable.function import combine, map, range, get from smnp.module.iterable.function import map, get
functions = [ combine.function, map.function, range.function ] functions = [ map.function ]
methods = [ get.function ] methods = [ get.function ]

View File

@@ -1,18 +0,0 @@
from functools import reduce
from smnp.function.model import Function
from smnp.function.signature import varargSignature
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofTypes
_signature = varargSignature(ofTypes(Type.LIST))
def _function(env, vararg):
if len(vararg) == 1:
return vararg[0]
combined = reduce(lambda x, y: x.value + y.value, vararg)
return Type.list(combined)
function = Function(_signature, _function, 'combine')

View File

@@ -1,36 +0,0 @@
from smnp.function.model import CombinedFunction, Function
from smnp.function.signature import signature
from smnp.note.model import Note
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature1 = signature(ofType(Type.INTEGER))
def _function1(env, upper):
return Type.list([ Type.integer(i) for i in range(upper.value + 1)])
_signature2 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER))
def _function2(env, lower, upper):
return Type.list([ Type.integer(i) for i in range(lower.value, upper.value + 1)])
_signature3 = signature(ofType(Type.INTEGER), ofType(Type.INTEGER), ofType(Type.INTEGER))
def _function3(env, lower, upper, step):
return Type.list([ Type.integer(i) for i in range(lower.value, upper.value + 1, step.value)])
_signature4 = signature(ofType(Type.NOTE), ofType(Type.NOTE))
def _function4(env, lower, upper):
return Type.list([Type.note(n) for n in Note.range(lower.value, upper.value)])
# TODO
# signature5 = range(note lower, note upper, integer step) OR step = "diatonic" | "chromatic" | "augmented" | "diminish"
function = CombinedFunction(
'range',
Function(_signature1, _function1),
Function(_signature2, _function2),
Function(_signature3, _function3),
Function(_signature4, _function4),
)

View File

@@ -1,4 +1,4 @@
from smnp.module.note.function import tuplet, transpose, semitones, octave, duration, interval from smnp.module.note.function import note
functions = [ semitones.function, interval.function, transpose.function, tuplet.function ] functions = [ note.function ]
methods = [ duration.function, octave.function ] methods = []

View File

@@ -1,11 +0,0 @@
from smnp.function.model import Function
from smnp.function.signature import signature
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
def _function(env, note, duration):
return Type.note(note.value.withDuration(duration.value))
function = Function(_signature, _function, 'withDuration')

View File

@@ -1,27 +0,0 @@
from smnp.function.model import Function, CombinedFunction
from smnp.function.signature import varargSignature
from smnp.note.interval import intervalToString
from smnp.note.model import Note
from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf
from smnp.type.signature.matcher.type import ofTypes
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function1(env, vararg):
withoutPauses = [note.value for note in vararg if note.type == Type.NOTE]
if len(withoutPauses) < 2:
return Type.list([])
semitones = [Note.checkInterval(withoutPauses[i-1], withoutPauses[i]) for i in range(1, len(withoutPauses))]
return Type.list([Type.string(intervalToString(s)) for s in semitones]).decompose()
_signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function2(env, vararg):
return Type.list([_function1(env, arg.value) for arg in vararg]).decompose()
function = CombinedFunction(
'interval',
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -0,0 +1,11 @@
from smnp.function.model import Function
from smnp.function.signature import signature
from smnp.note.model import Note
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature = signature(ofType(Type.STRING), ofType(Type.INTEGER), ofType(Type.INTEGER), ofType(Type.BOOL))
def _function(env, note, octave, duration, dot):
return Type.note(Note(note.value, octave.value, duration.value, dot.value))
function = Function(_signature, _function, 'Note')

View File

@@ -1,11 +0,0 @@
from smnp.function.model import Function
from smnp.function.signature import signature
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature = signature(ofType(Type.NOTE), ofType(Type.INTEGER))
def _function(env, note, octave):
return Type.note(note.value.withOctave(octave.value))
function = Function(_signature, _function, 'withOctave')

View File

@@ -1,25 +0,0 @@
from smnp.function.model import Function, CombinedFunction
from smnp.function.signature import varargSignature
from smnp.note.model import Note
from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf
from smnp.type.signature.matcher.type import ofTypes
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function1(env, vararg):
withoutPauses = [note.value for note in vararg if note.type == Type.NOTE]
if len(withoutPauses) < 2:
return Type.list([])
return Type.list([Type.integer(Note.checkInterval(withoutPauses[i-1], withoutPauses[i])) for i in range(1, len(withoutPauses))]).decompose()
_signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function2(env, vararg):
return Type.list([_function1(env, arg.value) for arg in vararg]).decompose()
function = CombinedFunction(
"semitones",
Function(_signature1, _function1),
Function(_signature2, _function2),
)

View File

@@ -1,22 +0,0 @@
from smnp.function.model import CombinedFunction, Function
from smnp.function.signature import varargSignature
from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf
from smnp.type.signature.matcher.type import ofTypes
_signature1 = varargSignature(ofTypes(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
def _function1(env, value, vararg):
transposed = [Type.note(arg.value.transpose(value.value)) if arg.type == Type.NOTE else arg for arg in vararg]
return Type.list(transposed).decompose()
_signature2 = varargSignature(listOf(Type.INTEGER, Type.NOTE), ofTypes(Type.INTEGER))
def _function2(env, value, vararg):
return Type.list([_function1(env, value, arg.value) for arg in vararg]).decompose()
function = CombinedFunction(
'transpose',
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -1,23 +0,0 @@
from smnp.function.model import CombinedFunction, Function
from smnp.function.signature import signature, varargSignature
from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf
from smnp.type.signature.matcher.type import ofTypes
_signature1 = varargSignature(ofTypes(Type.NOTE), ofTypes(Type.INTEGER), ofTypes(Type.INTEGER))
def _function1(env, n, m, vararg):
t = [Type.note(arg.value.withDuration(int(arg.value.duration * n.value / m.value))) for arg in vararg]
return Type.list(t).decompose()
_signature2 = signature(ofTypes(Type.INTEGER), ofTypes(Type.INTEGER), listOf(Type.NOTE))
def _function2(env, n, m, notes):
return _function1(env, n, m, notes.value)
function = CombinedFunction(
'tuplet',
Function(_signature1, _function1),
Function(_signature2, _function2)
)

View File

@@ -1,4 +1,4 @@
from smnp.module.string.function import concat from smnp.module.string.function import stringify
functions = [ concat.function ] functions = []
methods = [] methods = [ stringify.function ]

View File

@@ -1,11 +0,0 @@
from smnp.function.model import Function
from smnp.function.signature import varargSignature
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature = varargSignature(ofType(Type.STRING))
def _function(env, vararg):
return Type.string("".join([ arg.value for arg in vararg ]))
function = Function(_signature, _function, 'concat')

View File

@@ -0,0 +1,10 @@
from smnp.function.model import Function
from smnp.function.signature import signature
from smnp.type.model import Type
from smnp.type.signature.matcher.type import allTypes
_signature = signature(allTypes())
def _function(env, object):
return Type.string(object.stringify())
function = Function(_signature, _function, 'toString')

View File

@@ -1,4 +1,4 @@
from smnp.module.synth.function import synth, pause from smnp.module.synth.function import synth, pause, plot, compile, fft
functions = [ synth.function, pause.function ] functions = [ synth.function, pause.function, plot.function, compile.function, fft.function ]
methods = [] methods = []

View File

@@ -0,0 +1,157 @@
from smnp.error.runtime import RuntimeException
from smnp.function.model import Function, CombinedFunction
from smnp.function.signature import varargSignature
from smnp.module.synth.lib.wave import compilePolyphony
from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf
from smnp.type.signature.matcher.type import ofTypes, ofType
DEFAULT_BPM = 120
DEFAULT_OVERTONES = [0.4, 0.3, 0.1, 0.1, 0.1]
DEFAULT_DECAY = 4
DEFAULT_ATTACK = 100
DEFAULT_TUNING = 440
def getBpm(config):
key = Type.string("bpm")
if key in config.value:
bpm = config.value[key]
if bpm.type != Type.INTEGER or bpm.value <= 0:
raise RuntimeException("The 'bpm' property must be positive integer", None)
return bpm.value
return DEFAULT_BPM
def getOvertones(config):
key = Type.string("overtones")
if key in config.value:
overtones = config.value[key]
rawOvertones = [overtone.value for overtone in overtones.value]
if overtones.type != Type.LIST or not all(overtone.type in [Type.FLOAT, Type.INTEGER] for overtone in overtones.value):
raise RuntimeException("The 'overtones' property must be list of floats", None)
if len(rawOvertones) < 1:
raise RuntimeException("The 'overtones' property must contain one overtone at least", None)
if any(overtone < 0 for overtone in rawOvertones):
raise RuntimeException("The 'overtones' property mustn't contain negative values", None)
if sum(rawOvertones) > 1.0:
raise RuntimeException("The 'overtones' property must contain overtones which sum is not greater than 1.0", None)
return rawOvertones
return DEFAULT_OVERTONES
def getDecay(config):
key = Type.string("decay")
if key in config.value:
decay = config.value[key]
if not decay.type in [Type.INTEGER, Type.FLOAT] or decay.value < 0:
raise RuntimeException("The 'decay' property must be non-negative integer or float", None)
return decay.value
return DEFAULT_DECAY
def getAttack(config):
key = Type.string("attack")
if key in config.value:
attack = config.value[key]
if not attack.type in [Type.INTEGER, Type.FLOAT] or attack.value < 0:
raise RuntimeException("The 'attack' property must be non-negative integer or float", None)
return attack.value
return DEFAULT_ATTACK
def getTuning(config):
key = Type.string("tuning")
if key in config.value:
tuning = config.value[key]
if not tuning.type in [Type.INTEGER, Type.FLOAT] or tuning.value < 0:
raise RuntimeException("The 'tuning' property must be non-negative integer or float", None)
return tuning.value
return DEFAULT_TUNING
class Config:
def __init__(self, bpm, overtones, decay, attack, tuning):
self.bpm = bpm
self.overtones = overtones
self.decay = decay
self.attack = attack
self.tuning = tuning
@staticmethod
def default():
return Config(DEFAULT_BPM, DEFAULT_OVERTONES, DEFAULT_DECAY, DEFAULT_ATTACK, DEFAULT_TUNING)
_signature1 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function1(env, notes):
return Type.list([Type.float(float(m)) for m in __function1(notes)])
def __function1(notes):
return compilePolyphony([note.value for note in notes], Config.default())
_signature2 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function2(env, notes):
return Type.list([Type.float(float(m)) for m in __function2(notes)])
def __function2(notes):
return compilePolyphony([ notes ], Config.default())
_signature3 = varargSignature(listOf(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
def _function3(env, config, notes):
return Type.list([ Type.float(float(m)) for m in __function3(config, notes) ])
def __function3(config, notes):
rawNotes = [note.value for note in notes]
bpm = getBpm(config)
overtones = getOvertones(config)
decay = getDecay(config)
attack = getAttack(config)
tuning = getTuning(config)
return compilePolyphony(rawNotes, Config(bpm, overtones, decay, attack, tuning))
_signature4 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
def _function4(env, config, notes):
return Type.list([ Type.float(float(m)) for m in __function4(config, notes) ])
def __function4(config, notes):
bpm = getBpm(config)
overtones = getOvertones(config)
decay = getDecay(config)
attack = getAttack(config)
tuning = getTuning(config)
return compilePolyphony([ notes ], Config(bpm, overtones, decay, attack, tuning))
function = CombinedFunction(
'wave',
Function(_signature1, _function1),
Function(_signature2, _function2),
Function(_signature3, _function3),
Function(_signature4, _function4),
)

View File

@@ -0,0 +1,17 @@
import numpy as np
from smnp.function.model import Function
from smnp.function.signature import signature
from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf
_signature = signature(listOf(Type.FLOAT))
def _function(env, signal):
raw = [ x.value for x in signal.value ]
N = len(raw)
fft = np.fft.fft(raw)/N
fft = fft[range(int(N/2))]
return Type.list([ Type.float(float(abs(x))) for x in fft ])
function = Function(_signature, _function, 'fft')

View File

@@ -1,13 +1,13 @@
from smnp.function.model import Function from smnp.function.model import Function
from smnp.function.signature import signature from smnp.function.signature import signature
from smnp.module.synth.lib import player from smnp.module.synth.lib.wave import pause
from smnp.type.model import Type from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofTypes from smnp.type.signature.matcher.type import ofTypes
_signature = signature(ofTypes(Type.INTEGER)) _signature = signature(ofTypes(Type.INTEGER))
def _function(env, value): def _function(env, value):
bpm = env.findVariable('bpm') bpm = env.findVariable('bpm')
player.pause(value.value, bpm.value) pause(value.value, bpm.value)
function = Function(_signature, _function, 'pause') function = Function(_signature, _function, 'pause')

View File

@@ -0,0 +1,13 @@
from smnp.function.model import Function
from smnp.function.signature import signature
from smnp.module.synth.lib.wave import plot
from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf
_signature = signature(listOf(Type.FLOAT))
def _function(env, wave):
rawWave = [ m.value for m in wave.value ]
plot(rawWave)
function = Function(_signature, _function, 'plot')

View File

@@ -1,25 +1,47 @@
from smnp.function.model import CombinedFunction, Function from smnp.function.model import Function, CombinedFunction
from smnp.function.signature import varargSignature from smnp.function.signature import varargSignature
from smnp.module.synth.lib.player import playNotes from smnp.module.synth.function import compile
from smnp.module.synth.lib.wave import play
from smnp.type.model import Type from smnp.type.model import Type
from smnp.type.signature.matcher.list import listOf from smnp.type.signature.matcher.list import listOf
from smnp.type.signature.matcher.type import ofTypes from smnp.type.signature.matcher.type import ofTypes, ofType
_signature1 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER)) _signature1 = varargSignature(listOf(Type.NOTE, Type.INTEGER))
def _function1(env, vararg): def _function1(env, notes):
notes = [arg.value for arg in vararg] wave = compile.__function1(notes)
bpm = env.findVariable('bpm') play(wave)
playNotes(notes, bpm.value)
_signature2 = varargSignature(listOf(Type.NOTE, Type.INTEGER)) _signature2 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER))
def _function2(env, vararg): def _function2(env, notes):
for arg in vararg: wave = compile.__function2(notes)
_function1(env, arg.value) play(wave)
_signature3 = varargSignature(listOf(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
def _function3(env, config, notes):
wave = compile.__function3(config, notes)
play(wave)
_signature4 = varargSignature(ofTypes(Type.NOTE, Type.INTEGER), ofType(Type.MAP))
def _function4(env, config, notes):
wave = compile.__function4(config, notes)
play(wave)
_signature5 = varargSignature(listOf(Type.FLOAT))
def _function5(env, waves):
for wave in waves:
rawWave = [m.value for m in wave.value]
play(rawWave)
function = CombinedFunction( function = CombinedFunction(
'synth', 'synth',
Function(_signature1, _function1), Function(_signature1, _function1),
Function(_signature2, _function2) Function(_signature2, _function2),
) Function(_signature3, _function3),
Function(_signature4, _function4),
Function(_signature5, _function5)
)

View File

@@ -1,24 +0,0 @@
import time
from smnp.module.synth.lib.wave import sine
from smnp.note.model import Note
def playNotes(notes, bpm):
for note in notes:
{
Note: play,
int: pause
}[type(note)](note, bpm)
def play(note, bpm):
frequency = note.toFrequency()
duration = 60 * 4 / note.duration / bpm
duration *= 1.5 if note.dot else 1
sine(frequency, duration)
def pause(value, bpm):
time.sleep(60 * 4 / value / bpm)

View File

@@ -1,12 +1,80 @@
import time import time
import matplotlib.pyplot as plt
import numpy as np import numpy as np
import sounddevice as sd import sounddevice as sd
from smnp.note.pitch import Tuning
from smnp.type.model import Type
FS = 44100 FS = 44100
def pause(value, bpm):
time.sleep(60 * 4 / value / bpm)
def plot(wave):
X = np.arange(len(wave))
plt.plot(X, wave)
plt.show()
def play(wave):
sd.play(wave)
time.sleep(len(wave) / FS)
def compilePolyphony(notes, config):
tuning = Tuning(config.tuning)
compiledLines = [1 / len(notes) * compileNotes(line, config, tuning) for line in notes]
return sum(adjustSize(compiledLines))
def adjustSize(compiledLines):
maxSize = max(len(line) for line in compiledLines)
return [np.concatenate([line, np.zeros(maxSize - len(line))]) for line in compiledLines]
def compileNotes(notes, config, tuning):
dispatcher = {
Type.NOTE: lambda note, overtones: sineForNote(note.value, config, tuning),
Type.INTEGER: lambda note, overtones: silenceForPause(note.value, config)
}
return np.concatenate([dispatcher[note.type](note, config) for note in notes])
def sineForNote(note, config, tuning):
frequency = note.toFrequency(tuning)
duration = 60 * 4 / note.duration / config.bpm
duration *= 1.5 if note.dot else 1
return sound(frequency, duration, config)
def sound(frequency, duration, config):
return attack(decay(sum(overtone * sine((i+1) * frequency, duration) for i, overtone in enumerate(config.overtones) if overtone > 0), config), config)
def decay(wave, config):
magnitude = np.exp(-config.decay/len(wave) * np.arange(len(wave)))
return magnitude * wave
def attack(wave, config):
magnitude = -np.exp(-config.attack / len(wave) * np.arange(len(wave)))+1 \
if config.attack > 0 \
else np.ones(len(wave))
return magnitude * wave
def sine(frequency, duration): def sine(frequency, duration):
samples = (np.sin(2*np.pi*np.arange(FS*duration)*frequency/FS)).astype(np.float32) return (np.sin(2 * np.pi * np.arange(FS * duration) * frequency / FS)).astype(np.float32)
sd.play(samples, FS)
time.sleep(duration)
def silenceForPause(value, config):
duration = 60 * 4 / value / config.bpm
return np.zeros(int(FS * duration))

View File

@@ -1,4 +1,4 @@
from smnp.module.system.function import sleep, display, debug, exit, type from smnp.module.system.function import sleep, display, displayln, debug, exit, type, read
functions = [ debug.function, display.function, exit.function, sleep.function, type.function ] functions = [ debug.function, display.function, displayln.function, exit.function, sleep.function, type.function, read.function ]
methods = [] methods = []

View File

@@ -4,7 +4,7 @@ from smnp.type.signature.matcher.type import allTypes
_signature = varargSignature(allTypes()) _signature = varargSignature(allTypes())
def _function(env, vararg): def _function(env, vararg):
print("".join([arg.stringify() for arg in vararg])) print("".join([arg.stringify() for arg in vararg]), end="")
function = Function(_signature, _function, 'print') function = Function(_signature, _function, 'print')

View File

@@ -0,0 +1,10 @@
from smnp.function.model import Function
from smnp.function.signature import varargSignature
from smnp.type.signature.matcher.type import allTypes
_signature = varargSignature(allTypes())
def _function(env, vararg):
print("".join([arg.stringify() for arg in vararg]))
function = Function(_signature, _function, 'println')

View File

@@ -1,3 +1,72 @@
from smnp.error.runtime import RuntimeException
from smnp.function.model import CombinedFunction, Function
from smnp.function.signature import signature
from smnp.token.tokenizers.bool import boolTokenizer
from smnp.token.tokenizers.note import noteTokenizer
from smnp.type.model import Type
from smnp.type.signature.matcher.type import ofType
_signature1 = signature()
def _function1(env):
value = input()
return Type.string(value)
_signature2 = signature(ofType(Type.STRING))
def _function2(env, prompt):
print(prompt.value, end="")
value = input()
return Type.string(value)
_signature3 = signature(ofType(Type.TYPE))
def _function3(env, type):
value = input()
return getValueAccordingToType(value, type)
def getValueAccordingToType(value, type):
try:
if type.value == Type.STRING:
return Type.string(value)
if type.value == Type.INTEGER:
return Type.integer(int(value))
if type.value == Type.BOOL:
consumedChars, token = boolTokenizer(value, 0, 0)
if consumedChars > 0:
return Type.bool(token.value)
return ValueError()
if type.value == Type.NOTE:
consumedChars, token = noteTokenizer(value, 0, 0)
if consumedChars > 0:
return Type.note(token.value)
raise ValueError()
raise RuntimeException(f"Type {type.value.name.lower()} is not supported", None)
except ValueError:
raise RuntimeException(f"Invalid value '{value}' for type {type.value.name.lower()}", None)
_signature4 = signature(ofType(Type.STRING), ofType(Type.TYPE))
def _function4(env, prompt, type):
print(prompt.value, end="")
value = input()
return getValueAccordingToType(value, type)
function = CombinedFunction(
'read',
Function(_signature1, _function1),
Function(_signature2, _function2),
Function(_signature3, _function3),
Function(_signature4, _function4)
)
# TODO read function # TODO read function
# def read(args, env): # def read(args, env):

View File

@@ -1,29 +1,12 @@
import random as r import random
from smnp.error.function import IllegalArgumentException from smnp.function.model import Function
from smnp.function.model import Function, CombinedFunction from smnp.function.signature import signature
from smnp.function.signature import varargSignature
from smnp.type.model import Type from smnp.type.model import Type
from smnp.type.signature.matcher.list import listMatches from smnp.type.signature.matcher.type import ofType
from smnp.type.signature.matcher.type import ofTypes
_signature = signature(ofType(Type.INTEGER), ofType(Type.INTEGER))
def _function(env, min, max):
return Type.integer(random.randint(min.value, max.value))
def forType(t): function = Function(_signature, _function, 'rand')
_signature = varargSignature(listMatches(ofTypes(Type.PERCENT), ofTypes(t)))
def _function(env, vararg):
choice = r.random()
acc = 0
if sum(arg.value[0].value for arg in vararg) != 1.0:
raise IllegalArgumentException("Sum of all percentage values must be equal 100%")
for arg in vararg:
percent, item = arg.value
acc += percent.value
if choice <= acc:
return item
return Function(_signature, _function)
function = CombinedFunction('random', *[ forType(t) for t in Type if t != Type.VOID ])
#TODO: sample

View File

@@ -16,8 +16,8 @@ class Note:
self.duration = duration self.duration = duration
self.dot = dot self.dot = dot
def toFrequency(self): def toFrequency(self, tuning):
return self.note.toFrequency() * 2 ** self.octave return tuning[self.note] * 2 ** self.octave
def transpose(self, interval): def transpose(self, interval):
origIntRepr = self._intRepr() origIntRepr = self._intRepr()

View File

@@ -2,6 +2,7 @@ from enum import Enum
from smnp.error.note import NoteException from smnp.error.note import NoteException
_semitone = 2**(1/12)
class NotePitch(Enum): class NotePitch(Enum):
C = 0 C = 0
@@ -16,35 +17,47 @@ class NotePitch(Enum):
A = 9 A = 9
AIS = 10 AIS = 10
H = 11 H = 11
def toFrequency(self): # def toFrequency(self):
return { # return {
NotePitch.C: 16.35, # NotePitch.C: 16.35,
NotePitch.CIS: 17.32, # NotePitch.CIS: 17.32,
NotePitch.D: 18.35, # NotePitch.D: 18.35,
NotePitch.DIS: 19.45, # NotePitch.DIS: 19.45,
NotePitch.E: 20.60, # NotePitch.E: 20.60,
NotePitch.F: 21.83, # NotePitch.F: 21.83,
NotePitch.FIS: 23.12, # NotePitch.FIS: 23.12,
NotePitch.G: 24.50, # NotePitch.G: 24.50,
NotePitch.GIS: 25.96, # NotePitch.GIS: 25.96,
NotePitch.A: 27.50, # NotePitch.A: 27.50,
NotePitch.AIS: 29.17, # NotePitch.AIS: 29.17,
NotePitch.H: 30.87 # NotePitch.H: 30.87
}[self] # }[self]
def __str__(self): def __str__(self):
return self.name return self.name
def __repr__(self): def __repr__(self):
return self.__str__() return self.__str__()
@staticmethod @staticmethod
def toPitch(string): def toPitch(string):
try: if string.lower() in stringToPitch:
return stringToPitch[string.lower()] return stringToPitch[string.lower()]
except KeyError as e:
raise NoteException(f"Note '{string}' does not exist") if string.upper() in NotePitch.__members__:
return NotePitch[string.upper()]
raise NoteException(f"Note '{string}' does not exist")
class Tuning(object):
def __init__(self, a4=440):
self.tuning = { value: a4/_semitone**(57-i) for i, value in enumerate(NotePitch.__members__) }
def __getitem__(self, item):
return self.tuning[str(item)]
stringToPitch = { stringToPitch = {
'cb': NotePitch.H, 'cb': NotePitch.H,
@@ -67,4 +80,4 @@ stringToPitch = {
'a#': NotePitch.AIS, 'a#': NotePitch.AIS,
'b': NotePitch.AIS, 'b': NotePitch.AIS,
'h': NotePitch.H 'h': NotePitch.H
} }

View File

@@ -1,6 +1,7 @@
from smnp.ast.parser import parse from smnp.ast.parser import parse
from smnp.environment.factory import createEnvironment from smnp.environment.environment import Environment
from smnp.error.runtime import RuntimeException from smnp.error.runtime import RuntimeException
from smnp.module import functions, methods
from smnp.program.FileReader import readLines from smnp.program.FileReader import readLines
from smnp.runtime.evaluator import evaluate from smnp.runtime.evaluator import evaluate
from smnp.token.tokenizer import tokenize from smnp.token.tokenizer import tokenize
@@ -9,16 +10,31 @@ from smnp.token.tokenizer import tokenize
class Interpreter: class Interpreter:
@staticmethod @staticmethod
def interpretString(string, printTokens=False, printAst=False, execute=True, baseEnvironment=None): def interpretString(string, source, printTokens=False, printAst=False, execute=True, baseEnvironment=None):
return Interpreter._interpret(string.splitlines(), printTokens, printAst, execute, baseEnvironment) return Interpreter._interpret(
string.splitlines(),
source,
printTokens,
printAst,
execute,
baseEnvironment,
)
@staticmethod @staticmethod
def interpretFile(file, printTokens=False, printAst=False, execute=True, baseEnvironment=None): def interpretFile(file, printTokens=False, printAst=False, execute=True, baseEnvironment=None, source=None):
return Interpreter._interpret(readLines(file), printTokens, printAst, execute, baseEnvironment) return Interpreter._interpret(
readLines(file),
source if source is not None else file,
printTokens,
printAst,
execute,
baseEnvironment,
)
@staticmethod @staticmethod
def _interpret(lines, printTokens=False, printAst=False, execute=True, baseEnvironment=None): def _interpret(lines, source, printTokens=False, printAst=False, execute=True, baseEnvironment=None):
environment = createEnvironment() environment = Environment([{}], functions, methods, source=source)
if baseEnvironment is not None: if baseEnvironment is not None:
environment.extend(baseEnvironment) environment.extend(baseEnvironment)
@@ -37,4 +53,5 @@ class Interpreter:
return environment return environment
except RuntimeException as e: except RuntimeException as e:
e.environment = environment e.environment = environment
e.file = environment.source
raise e raise e

View File

@@ -5,6 +5,7 @@ from smnp.ast.node.function import FunctionDefinition
from smnp.ast.node.imports import Import from smnp.ast.node.imports import Import
from smnp.ast.node.program import Program from smnp.ast.node.program import Program
from smnp.ast.node.ret import Return from smnp.ast.node.ret import Return
from smnp.ast.node.throw import Throw
from smnp.error.runtime import RuntimeException from smnp.error.runtime import RuntimeException
from smnp.type.model import Type from smnp.type.model import Type
@@ -69,7 +70,6 @@ class EvaluationResult():
def evaluate(node, environment): def evaluate(node, environment):
from smnp.runtime.evaluators.program import ProgramEvaluator from smnp.runtime.evaluators.program import ProgramEvaluator
from smnp.runtime.evaluators.expression import expressionEvaluator from smnp.runtime.evaluators.expression import expressionEvaluator
from smnp.runtime.evaluators.condition import IfElseStatementEvaluator from smnp.runtime.evaluators.condition import IfElseStatementEvaluator
from smnp.runtime.evaluators.block import BlockEvaluator from smnp.runtime.evaluators.block import BlockEvaluator
@@ -77,6 +77,8 @@ def evaluate(node, environment):
from smnp.runtime.evaluators.function import FunctionDefinitionEvaluator from smnp.runtime.evaluators.function import FunctionDefinitionEvaluator
from smnp.runtime.evaluators.function import ReturnEvaluator from smnp.runtime.evaluators.function import ReturnEvaluator
from smnp.runtime.evaluators.extend import ExtendEvaluator from smnp.runtime.evaluators.extend import ExtendEvaluator
from smnp.runtime.evaluators.throw import ThrowEvaluator
result = Evaluator.oneOf( result = Evaluator.oneOf(
Evaluator.forNodes(ProgramEvaluator.evaluate, Program), Evaluator.forNodes(ProgramEvaluator.evaluate, Program),
Evaluator.forNodes(IfElseStatementEvaluator.evaluate, IfElse), Evaluator.forNodes(IfElseStatementEvaluator.evaluate, IfElse),
@@ -85,6 +87,7 @@ def evaluate(node, environment):
Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinition), Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinition),
Evaluator.forNodes(ReturnEvaluator.evaluate, Return), Evaluator.forNodes(ReturnEvaluator.evaluate, Return),
Evaluator.forNodes(ExtendEvaluator.evaluate, Extend), Evaluator.forNodes(ExtendEvaluator.evaluate, Extend),
Evaluator.forNodes(ThrowEvaluator.evaluate, Throw),
#Evaluator.forNodes(ImportEvaluator.evaluate, ImportNode), #Evaluator.forNodes(ImportEvaluator.evaluate, ImportNode),
#Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinitionNode), #Evaluator.forNodes(FunctionDefinitionEvaluator.evaluate, FunctionDefinitionNode),
#Evaluator.forNodes(ExtendEvaluator.evaluate, ExtendNode), #Evaluator.forNodes(ExtendEvaluator.evaluate, ExtendNode),

View File

@@ -8,11 +8,7 @@ class AssignmentEvaluator(Evaluator):
def evaluator(cls, node, environment): def evaluator(cls, node, environment):
target = node.left.value target = node.left.value
value = expressionEvaluator(doAssert=True)(node.right, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult value = expressionEvaluator(doAssert=True)(node.right, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
scopeOfExistingVariable = environment.findVariableScope(target) environment.scopes[-1][target] = value
if scopeOfExistingVariable is None:
environment.scopes[-1][target] = value
else:
scopeOfExistingVariable[target] = value
return value return value

View File

@@ -1,104 +0,0 @@
from smnp.ast.node.identifier import Identifier
from smnp.runtime.evaluator import evaluate, Evaluator, EvaluationResult
from smnp.runtime.evaluators.expression import expressionEvaluator
from smnp.type.model import Type
class AsteriskEvaluator(Evaluator):
@classmethod
def evaluator(cls, node, environment):
iterator = expressionEvaluator(doAssert=True)(node.iterator, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
return Evaluator.oneOf(
cls._numberIteratorAsteriskEvaluator(iterator),
cls._listIteratorAsteriskEvaluator(iterator),
cls._mapIteratorAsteriskEvaluator(iterator)
)(node, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
@classmethod
def _numberIteratorAsteriskEvaluator(cls, evaluatedIterator):
def evaluator(node, environment):
if evaluatedIterator.type == Type.INTEGER:
results = []
automaticVariable = cls._automaticNamedVariable(node.iterator, environment, "_")
for i in range(evaluatedIterator.value):
environment.scopes[-1][automaticVariable] = Type.integer(i + 1)
result = evaluate(node.statement, environment).value #TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
if result is None or result.type == Type.VOID:
results = None
if results is not None:
results.append(result)
del environment.scopes[-1][automaticVariable]
return EvaluationResult.OK(Type.list(results).decompose() if results is not None else Type.void())
return EvaluationResult.FAIL()
return evaluator
@classmethod
def _automaticNamedVariable(cls, iteratorNode, environment, prefix=''):
if type(iteratorNode) == Identifier:
return cls._automaticVariableName(environment, prefix, iteratorNode.value, False)
else:
return cls._automaticVariableName(environment, prefix, '', True)
@classmethod
def _automaticVariableName(cls, environment, prefix='', suffix='', startWithNumber=False):
number = 1 if startWithNumber else ''
variableName = lambda x: f"{prefix}{x}{suffix}"
while environment.findVariableScope(variableName(number)) is not None:
if number == '':
number = 1
else:
number += 1
return variableName(number)
@classmethod
def _listIteratorAsteriskEvaluator(cls, evaluatedIterator):
def evaluator(node, environment):
if evaluatedIterator.type == Type.LIST:
results = []
automaticVariableKey = cls._automaticNamedVariable(node.iterator, environment, "_")
automaticVariableValue = cls._automaticNamedVariable(node.iterator, environment, "__")
for i, v in enumerate(evaluatedIterator.value):
environment.scopes[-1][automaticVariableKey] = Type.integer(i + 1)
environment.scopes[-1][automaticVariableValue] = v
result = evaluate(node.statement, environment).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
if result is not None and result.type != Type.VOID:
results.append(result)
del environment.scopes[-1][automaticVariableKey]
del environment.scopes[-1][automaticVariableValue]
return EvaluationResult.OK(Type.list(results).decompose())
return EvaluationResult.FAIL()
return evaluator
@classmethod
def _mapIteratorAsteriskEvaluator(cls, evaluatedIterator):
def evaluator(node, environment):
if evaluatedIterator.type == Type.MAP:
results = []
automaticVariableKey = cls._automaticNamedVariable(node.iterator, environment, "_")
automaticVariableValue = cls._automaticNamedVariable(node.iterator, environment, "__")
for k, v in evaluatedIterator.value.items():
environment.scopes[-1][automaticVariableKey] = k
environment.scopes[-1][automaticVariableValue] = v
result = evaluate(node.statement, environment).value # TODO check if it isn't necessary to verify 'result' attr of EvaluatioNResult
if result is not None and result.type != Type.VOID:
results.append(result)
del environment.scopes[-1][automaticVariableKey]
del environment.scopes[-1][automaticVariableValue]
return EvaluationResult.OK(Type.list(results).decompose())
return EvaluationResult.FAIL()
return evaluator

Some files were not shown because too many files have changed in this diff Show More