curl-programming-lang 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- curl_programming_lang-1.0.0/LICENSE +201 -0
- curl_programming_lang-1.0.0/PKG-INFO +7 -0
- curl_programming_lang-1.0.0/README.md +212 -0
- curl_programming_lang-1.0.0/curl_programming_lang.egg-info/PKG-INFO +7 -0
- curl_programming_lang-1.0.0/curl_programming_lang.egg-info/SOURCES.txt +13 -0
- curl_programming_lang-1.0.0/curl_programming_lang.egg-info/dependency_links.txt +1 -0
- curl_programming_lang-1.0.0/curl_programming_lang.egg-info/entry_points.txt +2 -0
- curl_programming_lang-1.0.0/curl_programming_lang.egg-info/top_level.txt +5 -0
- curl_programming_lang-1.0.0/errors.py +26 -0
- curl_programming_lang-1.0.0/interpreter.py +168 -0
- curl_programming_lang-1.0.0/lexer.py +122 -0
- curl_programming_lang-1.0.0/main.py +47 -0
- curl_programming_lang-1.0.0/parser.py +310 -0
- curl_programming_lang-1.0.0/pyproject.toml +15 -0
- curl_programming_lang-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Ritvik Gautam
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# Curl Programming Language
|
|
2
|
+
|
|
3
|
+
Curl is an open-source programming language built on Python technology, designed to be simple and expressive. Every statement uses a `keyword{...}\` style, and blocks are opened with `-` and closed with `--\`.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Python 3.7+
|
|
8
|
+
- Node.js *(optional — only needed for `otherCoding{"JavaScript", ...}` blocks)*
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
pip install -e .
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
This registers the `curlang` command globally.
|
|
17
|
+
|
|
18
|
+
## Running a Curl program
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
curlang [YOUR-FILE].curl
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Syntax Reference
|
|
27
|
+
|
|
28
|
+
### Print — `pcType`
|
|
29
|
+
|
|
30
|
+
Outputs text to the console. Supports string concatenation with `+`.
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
pcType{"Hello, World!"}\
|
|
34
|
+
pcType{"Hello, " + var{name} + "!"}\
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
### Input — `pcAsk`
|
|
40
|
+
|
|
41
|
+
Prompts the user for input. The result is accessed anywhere with `input{ans}`.
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
pcAsk{"What is your name?">>}\
|
|
45
|
+
pcType{"You said: " + input{ans}}\
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
### Variables — `var`
|
|
51
|
+
|
|
52
|
+
Assign a value with `var{name, value}\`. Reference it later with `var{name}`.
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
var{name, "Ritvik"}\
|
|
56
|
+
pcType{var{name}}\
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Variables can hold strings, numbers, or lists.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
### Lists — `list`
|
|
64
|
+
|
|
65
|
+
Create a list with items separated by `;`. Typically assigned to a variable.
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
var{colors, list{"red"; "green"; "blue"}}\
|
|
69
|
+
pcType{var{colors}}\
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
### Functions — `createFunc` / `func`
|
|
75
|
+
|
|
76
|
+
Define a function with `createFunc{name}-` and close it with `--\`. Call it with `func{name}\`.
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
createFunc{greet}-
|
|
80
|
+
pcType{"Hello from greet!"}\
|
|
81
|
+
--\
|
|
82
|
+
|
|
83
|
+
func{greet}\
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
### Conditionals — `if` / `elif` / `else`
|
|
89
|
+
|
|
90
|
+
**Simple if:**
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
if{var{score} >= 90, then}-
|
|
94
|
+
pcType{"Grade: A"}\
|
|
95
|
+
--\
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**If / else:**
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
if{var{score} >= 90, then}-
|
|
102
|
+
pcType{"Grade: A"}\
|
|
103
|
+
else:
|
|
104
|
+
pcType{"Grade: B or below"}\
|
|
105
|
+
--\
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**If / elif / else:**
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
if{var{score} >= 90, then}-
|
|
112
|
+
pcType{"Grade: A"}\
|
|
113
|
+
elif{var{score} >= 80, then}-
|
|
114
|
+
pcType{"Grade: B"}\
|
|
115
|
+
else:
|
|
116
|
+
pcType{"Grade: C or below"}\
|
|
117
|
+
--\
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
> For `if`+`elif` chains without an `else`, close with `--\--\`.
|
|
121
|
+
|
|
122
|
+
**Supported comparison operators:** `==` `!=` `<` `>` `<=` `>=`
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
### Embedded code blocks — `otherCoding`
|
|
127
|
+
|
|
128
|
+
Run a block of code written in another language. The closing `}\` must be on its own line.
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
otherCoding{"Python",
|
|
132
|
+
|
|
133
|
+
x = 10
|
|
134
|
+
print("x =", x)
|
|
135
|
+
|
|
136
|
+
}\
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**Supported languages:**
|
|
140
|
+
|
|
141
|
+
| Language | Status |
|
|
142
|
+
|---|---|
|
|
143
|
+
| Python | Fully supported |
|
|
144
|
+
| JavaScript / Node.js | Supported (requires Node.js) |
|
|
145
|
+
| Java, C, C++ | Not supported at runtime |
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
### Import — `import`
|
|
150
|
+
|
|
151
|
+
Import a Python package and give it a nickname.
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
import{"math", m}\
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
### AI — `pcAI`
|
|
160
|
+
|
|
161
|
+
*(Stub — reserved for future AI integration.)*
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
pcAI{".on", "You are a helpful assistant", "profanityControl"}\
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Symbols
|
|
170
|
+
|
|
171
|
+
| Symbol | Meaning |
|
|
172
|
+
|---|---|
|
|
173
|
+
| `\` | End of a statement |
|
|
174
|
+
| `-` | Start of a block |
|
|
175
|
+
| `--\` | End of a block |
|
|
176
|
+
| `{}` | Argument container |
|
|
177
|
+
| `""` | String / text data |
|
|
178
|
+
| `;` | Separator inside lists |
|
|
179
|
+
| `,` | Separator between parameters |
|
|
180
|
+
| `==` | Equals |
|
|
181
|
+
| `!=` | Not equals |
|
|
182
|
+
| `=` | Assignment |
|
|
183
|
+
| `+` | Concatenation / addition |
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Example program
|
|
188
|
+
|
|
189
|
+
```
|
|
190
|
+
var{name, "Ritvik"}\
|
|
191
|
+
pcType{"Hello, " + var{name} + "!"}\
|
|
192
|
+
|
|
193
|
+
var{score, 95}\
|
|
194
|
+
if{var{score} >= 90, then}-
|
|
195
|
+
pcType{"You got an A!"}\
|
|
196
|
+
else:
|
|
197
|
+
pcType{"Keep trying!"}\
|
|
198
|
+
--\
|
|
199
|
+
|
|
200
|
+
createFunc{sayBye}-
|
|
201
|
+
pcType{"Goodbye, " + var{name} + "!"}\
|
|
202
|
+
--\
|
|
203
|
+
|
|
204
|
+
func{sayBye}\
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## License
|
|
210
|
+
|
|
211
|
+
This project is licensed under the Apache License 2.0 (OSI-approved).
|
|
212
|
+
See the full license at: https://github.com/gautamritvik/Curl-Programming/blob/main/LICENSE
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
errors.py
|
|
4
|
+
interpreter.py
|
|
5
|
+
lexer.py
|
|
6
|
+
main.py
|
|
7
|
+
parser.py
|
|
8
|
+
pyproject.toml
|
|
9
|
+
curl_programming_lang.egg-info/PKG-INFO
|
|
10
|
+
curl_programming_lang.egg-info/SOURCES.txt
|
|
11
|
+
curl_programming_lang.egg-info/dependency_links.txt
|
|
12
|
+
curl_programming_lang.egg-info/entry_points.txt
|
|
13
|
+
curl_programming_lang.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class CurlError(Exception):
|
|
2
|
+
pass
|
|
3
|
+
|
|
4
|
+
class CurlSyntaxError(CurlError):
|
|
5
|
+
def __init__(self, message, line=None, char=None):
|
|
6
|
+
self.message = message
|
|
7
|
+
self.line = line
|
|
8
|
+
self.char = char
|
|
9
|
+
|
|
10
|
+
def __str__(self):
|
|
11
|
+
location = f" at line {self.line}, char {self.char}" if self.line and self.char else ""
|
|
12
|
+
return f"SyntaxError{location}: {self.message}"
|
|
13
|
+
|
|
14
|
+
class CurlNameError(CurlError):
|
|
15
|
+
def __init__(self, message):
|
|
16
|
+
self.message = message
|
|
17
|
+
|
|
18
|
+
def __str__(self):
|
|
19
|
+
return f"NameError: {self.message}"
|
|
20
|
+
|
|
21
|
+
class CurlTypeError(CurlError):
|
|
22
|
+
def __init__(self, message):
|
|
23
|
+
self.message = message
|
|
24
|
+
|
|
25
|
+
def __str__(self):
|
|
26
|
+
return f"TypeError: {self.message}"
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def execute(ast, env=None):
|
|
5
|
+
if env is None:
|
|
6
|
+
env = {
|
|
7
|
+
"variables": {},
|
|
8
|
+
"functions": {},
|
|
9
|
+
"imports": {},
|
|
10
|
+
"last_input": None,
|
|
11
|
+
}
|
|
12
|
+
for stmt in ast:
|
|
13
|
+
_exec_stmt(stmt, env)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _exec_stmt(stmt, env):
|
|
17
|
+
t = stmt["type"]
|
|
18
|
+
|
|
19
|
+
if t == "print":
|
|
20
|
+
print(_eval(stmt["value"], env))
|
|
21
|
+
|
|
22
|
+
elif t == "input":
|
|
23
|
+
env["last_input"] = input(stmt["prompt"] + " ")
|
|
24
|
+
|
|
25
|
+
elif t == "assign":
|
|
26
|
+
env["variables"][stmt["name"]] = _eval(stmt["value"], env)
|
|
27
|
+
|
|
28
|
+
elif t == "var_ref":
|
|
29
|
+
pass # standalone var{name}\ — no-op
|
|
30
|
+
|
|
31
|
+
elif t == "list_stmt":
|
|
32
|
+
pass # standalone list{...}\ — no-op
|
|
33
|
+
|
|
34
|
+
elif t == "func_def":
|
|
35
|
+
env["functions"][stmt["name"]] = stmt["body"]
|
|
36
|
+
|
|
37
|
+
elif t == "func_call":
|
|
38
|
+
_call_func(stmt["name"], env)
|
|
39
|
+
|
|
40
|
+
elif t == "if":
|
|
41
|
+
if _eval_condition(stmt["condition"], env):
|
|
42
|
+
execute(stmt["then_body"], _child_env(env))
|
|
43
|
+
else:
|
|
44
|
+
ran = False
|
|
45
|
+
for clause in stmt["elif_clauses"]:
|
|
46
|
+
if _eval_condition(clause["condition"], env):
|
|
47
|
+
execute(clause["body"], _child_env(env))
|
|
48
|
+
ran = True
|
|
49
|
+
break
|
|
50
|
+
if not ran and stmt["else_body"] is not None:
|
|
51
|
+
execute(stmt["else_body"], _child_env(env))
|
|
52
|
+
|
|
53
|
+
elif t == "other_code":
|
|
54
|
+
_exec_other(stmt["language"], stmt["code"], env)
|
|
55
|
+
|
|
56
|
+
elif t == "import":
|
|
57
|
+
try:
|
|
58
|
+
mod = importlib.import_module(stmt["package"])
|
|
59
|
+
env["imports"][stmt["nickname"]] = mod
|
|
60
|
+
except ImportError as e:
|
|
61
|
+
raise ImportError(f"Could not import '{stmt['package']}': {e}")
|
|
62
|
+
|
|
63
|
+
elif t == "ai":
|
|
64
|
+
print(f"[pcAI — mode: {stmt['mode']} | {stmt['directions']}]")
|
|
65
|
+
|
|
66
|
+
else:
|
|
67
|
+
raise RuntimeError(f"Unknown statement type: {t!r}")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _call_func(name, env):
|
|
71
|
+
if name not in env["functions"]:
|
|
72
|
+
raise NameError(f"Function '{name}' is not defined")
|
|
73
|
+
execute(env["functions"][name], _child_env(env))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _child_env(env):
|
|
77
|
+
return {
|
|
78
|
+
"variables": dict(env["variables"]),
|
|
79
|
+
"functions": env["functions"],
|
|
80
|
+
"imports": env["imports"],
|
|
81
|
+
"last_input": env["last_input"],
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _eval(expr, env):
|
|
86
|
+
t = expr["type"]
|
|
87
|
+
|
|
88
|
+
if t == "string":
|
|
89
|
+
return expr["value"]
|
|
90
|
+
|
|
91
|
+
if t == "number":
|
|
92
|
+
return expr["value"]
|
|
93
|
+
|
|
94
|
+
if t == "var_ref":
|
|
95
|
+
name = expr["name"]
|
|
96
|
+
if name not in env["variables"]:
|
|
97
|
+
raise NameError(f"Variable '{name}' is not defined")
|
|
98
|
+
return env["variables"][name]
|
|
99
|
+
|
|
100
|
+
if t == "input_ref":
|
|
101
|
+
if env["last_input"] is None:
|
|
102
|
+
raise RuntimeError("No input has been received yet (use pcAsk first)")
|
|
103
|
+
return env["last_input"]
|
|
104
|
+
|
|
105
|
+
if t == "list":
|
|
106
|
+
return [_eval(item, env) for item in expr["items"]]
|
|
107
|
+
|
|
108
|
+
if t == "concat":
|
|
109
|
+
return "".join(str(_eval(p, env)) for p in expr["parts"])
|
|
110
|
+
|
|
111
|
+
if t == "binop":
|
|
112
|
+
left = _eval(expr["left"], env)
|
|
113
|
+
right = _eval(expr["right"], env)
|
|
114
|
+
op = expr["op"]
|
|
115
|
+
if op == "-":
|
|
116
|
+
return left - right
|
|
117
|
+
if op == "*":
|
|
118
|
+
return left * right
|
|
119
|
+
if op == "/":
|
|
120
|
+
return left / right
|
|
121
|
+
raise RuntimeError(f"Unknown binary operator: {op!r}")
|
|
122
|
+
|
|
123
|
+
if t == "func_call_expr":
|
|
124
|
+
_call_func(expr["name"], env)
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
raise RuntimeError(f"Unknown expression type: {t!r}")
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _eval_condition(cond, env):
|
|
131
|
+
if cond["type"] != "condition":
|
|
132
|
+
return bool(_eval(cond, env))
|
|
133
|
+
left = _eval(cond["left"], env)
|
|
134
|
+
right = _eval(cond["right"], env)
|
|
135
|
+
op = cond["op"]
|
|
136
|
+
if op == "==": return left == right
|
|
137
|
+
if op == "!=": return left != right
|
|
138
|
+
if op == "<": return left < right
|
|
139
|
+
if op == ">": return left > right
|
|
140
|
+
if op == "<=": return left <= right
|
|
141
|
+
if op == ">=": return left >= right
|
|
142
|
+
raise RuntimeError(f"Unknown comparison operator: {op!r}")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _exec_other(language, code, env):
|
|
146
|
+
lang = language.lower()
|
|
147
|
+
if lang == "python":
|
|
148
|
+
globs = {"__builtins__": __builtins__}
|
|
149
|
+
globs.update(env["variables"])
|
|
150
|
+
exec(code, globs)
|
|
151
|
+
for key, val in globs.items():
|
|
152
|
+
if not key.startswith("__"):
|
|
153
|
+
env["variables"][key] = val
|
|
154
|
+
elif lang in ("javascript", "node.js"):
|
|
155
|
+
import subprocess, shutil
|
|
156
|
+
node = shutil.which("node")
|
|
157
|
+
if not node:
|
|
158
|
+
print("[otherCoding JavaScript] Node.js not found — skipping")
|
|
159
|
+
return
|
|
160
|
+
result = subprocess.run([node, "-e", code], capture_output=True, text=True)
|
|
161
|
+
if result.stdout:
|
|
162
|
+
print(result.stdout, end="")
|
|
163
|
+
if result.returncode != 0:
|
|
164
|
+
raise RuntimeError(f"JavaScript error: {result.stderr.strip()}")
|
|
165
|
+
elif lang in ("java", "c", "c++"):
|
|
166
|
+
print(f"[otherCoding {language}] Compilation-based languages are not supported at runtime")
|
|
167
|
+
else:
|
|
168
|
+
raise RuntimeError(f"Unsupported otherCoding language: {language!r}")
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
KEYWORD = "KEYWORD"
|
|
4
|
+
STRING = "STRING"
|
|
5
|
+
NUMBER = "NUMBER"
|
|
6
|
+
IDENTIFIER = "IDENTIFIER"
|
|
7
|
+
PLUS = "PLUS"
|
|
8
|
+
MINUS = "MINUS"
|
|
9
|
+
TIMES = "TIMES"
|
|
10
|
+
DIVIDE = "DIVIDE"
|
|
11
|
+
EQ = "EQ"
|
|
12
|
+
NEQ = "NEQ"
|
|
13
|
+
LT = "LT"
|
|
14
|
+
GT = "GT"
|
|
15
|
+
LTE = "LTE"
|
|
16
|
+
GTE = "GTE"
|
|
17
|
+
ASSIGN = "ASSIGN"
|
|
18
|
+
LBRACE = "LBRACE"
|
|
19
|
+
RBRACE = "RBRACE"
|
|
20
|
+
SEMICOLON = "SEMICOLON"
|
|
21
|
+
COMMA = "COMMA"
|
|
22
|
+
COLON = "COLON"
|
|
23
|
+
ARROW = "ARROW"
|
|
24
|
+
LINE_END = "LINE_END"
|
|
25
|
+
BLOCK_END = "BLOCK_END"
|
|
26
|
+
RAW_CODE = "RAW_CODE"
|
|
27
|
+
|
|
28
|
+
KEYWORDS = {
|
|
29
|
+
"pcType", "pcAsk", "var", "list", "createFunc", "func",
|
|
30
|
+
"if", "elif", "else", "then", "pcAI", "otherCoding", "import", "input"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
TOKEN_PATTERNS = [
|
|
34
|
+
(BLOCK_END, r'--\\'),
|
|
35
|
+
(LINE_END, r'\\'),
|
|
36
|
+
(EQ, r'=='),
|
|
37
|
+
(NEQ, r'!='),
|
|
38
|
+
(LTE, r'<='),
|
|
39
|
+
(GTE, r'>='),
|
|
40
|
+
(ARROW, r'>>'),
|
|
41
|
+
(LT, r'<'),
|
|
42
|
+
(GT, r'>'),
|
|
43
|
+
(PLUS, r'\+'),
|
|
44
|
+
(MINUS, r'-'),
|
|
45
|
+
(TIMES, r'\*'),
|
|
46
|
+
(DIVIDE, r'/'),
|
|
47
|
+
(ASSIGN, r'='),
|
|
48
|
+
(LBRACE, r'\{'),
|
|
49
|
+
(RBRACE, r'\}'),
|
|
50
|
+
(SEMICOLON, r';'),
|
|
51
|
+
(COMMA, r','),
|
|
52
|
+
(COLON, r':'),
|
|
53
|
+
(STRING, r'"[^"]*"'),
|
|
54
|
+
(NUMBER, r'\d+(?:\.\d+)?'),
|
|
55
|
+
(IDENTIFIER, r'[a-zA-Z_][a-zA-Z0-9_]*'),
|
|
56
|
+
('WHITESPACE', r'\s+'),
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def curl_tokenize(code):
|
|
61
|
+
tokens = []
|
|
62
|
+
pos = 0
|
|
63
|
+
|
|
64
|
+
while pos < len(code):
|
|
65
|
+
# Special handling: otherCoding blocks contain raw code that shouldn't be tokenized
|
|
66
|
+
match = re.match(r'otherCoding\b', code[pos:])
|
|
67
|
+
if match:
|
|
68
|
+
tokens.append((KEYWORD, 'otherCoding'))
|
|
69
|
+
pos += match.end()
|
|
70
|
+
|
|
71
|
+
# Skip whitespace, consume '{'
|
|
72
|
+
ws = re.match(r'\s*', code[pos:])
|
|
73
|
+
pos += ws.end()
|
|
74
|
+
if pos >= len(code) or code[pos] != '{':
|
|
75
|
+
raise SyntaxError("Expected '{' after otherCoding")
|
|
76
|
+
tokens.append((LBRACE, '{'))
|
|
77
|
+
pos += 1
|
|
78
|
+
|
|
79
|
+
# Skip whitespace, consume language string
|
|
80
|
+
ws = re.match(r'\s*', code[pos:])
|
|
81
|
+
pos += ws.end()
|
|
82
|
+
lang_match = re.match(r'"([^"]*)"', code[pos:])
|
|
83
|
+
if not lang_match:
|
|
84
|
+
raise SyntaxError("Expected language name string in otherCoding")
|
|
85
|
+
tokens.append((STRING, lang_match.group(0)))
|
|
86
|
+
pos += lang_match.end()
|
|
87
|
+
|
|
88
|
+
# Skip whitespace, consume ','
|
|
89
|
+
ws = re.match(r'\s*', code[pos:])
|
|
90
|
+
pos += ws.end()
|
|
91
|
+
if pos >= len(code) or code[pos] != ',':
|
|
92
|
+
raise SyntaxError("Expected ',' after language name in otherCoding")
|
|
93
|
+
tokens.append((COMMA, ','))
|
|
94
|
+
pos += 1
|
|
95
|
+
|
|
96
|
+
# Capture everything until }\ on its own line
|
|
97
|
+
close_match = re.search(r'\n[ \t]*\}\\', code[pos:])
|
|
98
|
+
if not close_match:
|
|
99
|
+
raise SyntaxError("Unclosed otherCoding block — expected }\\ on its own line")
|
|
100
|
+
raw_code = code[pos:pos + close_match.start()].strip('\n')
|
|
101
|
+
tokens.append((RAW_CODE, raw_code))
|
|
102
|
+
pos += close_match.end()
|
|
103
|
+
tokens.append((LINE_END, '\\'))
|
|
104
|
+
continue
|
|
105
|
+
|
|
106
|
+
matched = False
|
|
107
|
+
for token_type, pattern in TOKEN_PATTERNS:
|
|
108
|
+
m = re.match(pattern, code[pos:])
|
|
109
|
+
if m:
|
|
110
|
+
if token_type != 'WHITESPACE':
|
|
111
|
+
value = m.group()
|
|
112
|
+
if token_type == IDENTIFIER and value in KEYWORDS:
|
|
113
|
+
token_type = KEYWORD
|
|
114
|
+
tokens.append((token_type, value))
|
|
115
|
+
pos += m.end()
|
|
116
|
+
matched = True
|
|
117
|
+
break
|
|
118
|
+
|
|
119
|
+
if not matched:
|
|
120
|
+
raise SyntaxError(f"Unknown character: {code[pos]!r} at position {pos}")
|
|
121
|
+
|
|
122
|
+
return tokens
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from lexer import curl_tokenize
|
|
3
|
+
from parser import Parser
|
|
4
|
+
from interpreter import execute
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def main():
|
|
8
|
+
if len(sys.argv) < 2:
|
|
9
|
+
print("Usage: curlang <file.curl>")
|
|
10
|
+
sys.exit(1)
|
|
11
|
+
|
|
12
|
+
file_path = sys.argv[1]
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
with open(file_path, "r") as f:
|
|
16
|
+
code = f.read()
|
|
17
|
+
except FileNotFoundError:
|
|
18
|
+
print(f"Error: File not found — '{file_path}'")
|
|
19
|
+
sys.exit(1)
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
tokens = curl_tokenize(code)
|
|
23
|
+
parser = Parser(tokens)
|
|
24
|
+
ast = parser.parse()
|
|
25
|
+
execute(ast)
|
|
26
|
+
except KeyboardInterrupt:
|
|
27
|
+
print("\nOperation interrupted by user.")
|
|
28
|
+
print("{You cut off Curl 1.0.0 while it was trying to work! \U0001f61e}")
|
|
29
|
+
sys.exit(1)
|
|
30
|
+
except SyntaxError as e:
|
|
31
|
+
print(f"Syntax Error: {e}")
|
|
32
|
+
sys.exit(1)
|
|
33
|
+
except NameError as e:
|
|
34
|
+
print(f"Name Error: {e}")
|
|
35
|
+
sys.exit(1)
|
|
36
|
+
except RuntimeError as e:
|
|
37
|
+
print(f"Runtime Error: {e}")
|
|
38
|
+
sys.exit(1)
|
|
39
|
+
except Exception as e:
|
|
40
|
+
print(f"Error: {e}")
|
|
41
|
+
sys.exit(1)
|
|
42
|
+
|
|
43
|
+
print("\n{You were using Curl 1.0.0 \U0001f609}")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
main()
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
from lexer import (
|
|
2
|
+
KEYWORD, STRING, NUMBER, IDENTIFIER,
|
|
3
|
+
PLUS, MINUS, TIMES, DIVIDE,
|
|
4
|
+
EQ, NEQ, LT, GT, LTE, GTE,
|
|
5
|
+
LBRACE, RBRACE, SEMICOLON, COMMA, COLON, ARROW,
|
|
6
|
+
LINE_END, BLOCK_END, RAW_CODE
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
COMPARISON_OPS = {EQ, NEQ, LT, GT, LTE, GTE}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Parser:
|
|
13
|
+
def __init__(self, tokens):
|
|
14
|
+
self.tokens = tokens
|
|
15
|
+
self.pos = 0
|
|
16
|
+
|
|
17
|
+
def current(self):
|
|
18
|
+
if self.pos < len(self.tokens):
|
|
19
|
+
return self.tokens[self.pos]
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
def consume(self, token_type, value=None):
|
|
23
|
+
token = self.current()
|
|
24
|
+
if token is None:
|
|
25
|
+
expected = f"{token_type}" + (f" '{value}'" if value else "")
|
|
26
|
+
raise SyntaxError(f"Unexpected end of input — expected {expected}")
|
|
27
|
+
if token[0] != token_type:
|
|
28
|
+
raise SyntaxError(f"Expected {token_type}{' ' + repr(value) if value else ''}, got {token[0]} {repr(token[1])}")
|
|
29
|
+
if value is not None and token[1] != value:
|
|
30
|
+
raise SyntaxError(f"Expected keyword '{value}', got '{token[1]}'")
|
|
31
|
+
self.pos += 1
|
|
32
|
+
return token
|
|
33
|
+
|
|
34
|
+
def check(self, token_type, value=None):
|
|
35
|
+
token = self.current()
|
|
36
|
+
if not token:
|
|
37
|
+
return False
|
|
38
|
+
if token[0] != token_type:
|
|
39
|
+
return False
|
|
40
|
+
if value is not None and token[1] != value:
|
|
41
|
+
return False
|
|
42
|
+
return True
|
|
43
|
+
|
|
44
|
+
# ── top-level ──────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
def parse(self):
|
|
47
|
+
statements = []
|
|
48
|
+
while self.current() is not None:
|
|
49
|
+
stmt = self.parse_statement()
|
|
50
|
+
if stmt is not None:
|
|
51
|
+
statements.append(stmt)
|
|
52
|
+
return statements
|
|
53
|
+
|
|
54
|
+
def parse_block(self):
|
|
55
|
+
"""Parse statements until BLOCK_END or a block-level keyword (elif/else)."""
|
|
56
|
+
statements = []
|
|
57
|
+
while self.current() is not None:
|
|
58
|
+
if self.check(BLOCK_END):
|
|
59
|
+
self.consume(BLOCK_END)
|
|
60
|
+
break
|
|
61
|
+
if self.check(KEYWORD, "elif") or self.check(KEYWORD, "else"):
|
|
62
|
+
break
|
|
63
|
+
stmt = self.parse_statement()
|
|
64
|
+
if stmt is not None:
|
|
65
|
+
statements.append(stmt)
|
|
66
|
+
return statements
|
|
67
|
+
|
|
68
|
+
# ── statements ─────────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
def parse_statement(self):
|
|
71
|
+
token = self.current()
|
|
72
|
+
if token is None:
|
|
73
|
+
return None
|
|
74
|
+
|
|
75
|
+
if token[0] != KEYWORD:
|
|
76
|
+
raise SyntaxError(f"Expected a Curl keyword, got {token[0]} {repr(token[1])}")
|
|
77
|
+
|
|
78
|
+
dispatch = {
|
|
79
|
+
"pcType": self.parse_print,
|
|
80
|
+
"pcAsk": self.parse_input,
|
|
81
|
+
"var": self.parse_var_statement,
|
|
82
|
+
"list": self.parse_list_statement,
|
|
83
|
+
"createFunc": self.parse_func_def,
|
|
84
|
+
"func": self.parse_func_call,
|
|
85
|
+
"if": self.parse_if,
|
|
86
|
+
"otherCoding": self.parse_other_coding,
|
|
87
|
+
"import": self.parse_import,
|
|
88
|
+
"pcAI": self.parse_ai,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
handler = dispatch.get(token[1])
|
|
92
|
+
if handler is None:
|
|
93
|
+
raise SyntaxError(f"Unexpected keyword at statement level: '{token[1]}'")
|
|
94
|
+
return handler()
|
|
95
|
+
|
|
96
|
+
def parse_print(self):
|
|
97
|
+
self.consume(KEYWORD, "pcType")
|
|
98
|
+
self.consume(LBRACE)
|
|
99
|
+
expr = self.parse_concat_expr()
|
|
100
|
+
self.consume(RBRACE)
|
|
101
|
+
self.consume(LINE_END)
|
|
102
|
+
return {"type": "print", "value": expr}
|
|
103
|
+
|
|
104
|
+
def parse_input(self):
|
|
105
|
+
self.consume(KEYWORD, "pcAsk")
|
|
106
|
+
self.consume(LBRACE)
|
|
107
|
+
prompt = self.consume(STRING)[1].strip('"')
|
|
108
|
+
self.consume(ARROW)
|
|
109
|
+
self.consume(RBRACE)
|
|
110
|
+
self.consume(LINE_END)
|
|
111
|
+
return {"type": "input", "prompt": prompt}
|
|
112
|
+
|
|
113
|
+
def parse_var_statement(self):
|
|
114
|
+
self.consume(KEYWORD, "var")
|
|
115
|
+
self.consume(LBRACE)
|
|
116
|
+
name = self.consume(IDENTIFIER)[1]
|
|
117
|
+
|
|
118
|
+
if self.check(COMMA):
|
|
119
|
+
# var{name, value}\ → assignment
|
|
120
|
+
self.consume(COMMA)
|
|
121
|
+
value = self.parse_expr()
|
|
122
|
+
self.consume(RBRACE)
|
|
123
|
+
self.consume(LINE_END)
|
|
124
|
+
return {"type": "assign", "name": name, "value": value}
|
|
125
|
+
else:
|
|
126
|
+
# var{name}\ → standalone reference (no-op)
|
|
127
|
+
self.consume(RBRACE)
|
|
128
|
+
self.consume(LINE_END)
|
|
129
|
+
return {"type": "var_ref", "name": name}
|
|
130
|
+
|
|
131
|
+
def parse_list_statement(self):
|
|
132
|
+
# Standalone list{...}\ — unusual but spec mentions it
|
|
133
|
+
items = self._parse_list_body()
|
|
134
|
+
self.consume(LINE_END)
|
|
135
|
+
return {"type": "list_stmt", "items": items}
|
|
136
|
+
|
|
137
|
+
def parse_func_def(self):
|
|
138
|
+
self.consume(KEYWORD, "createFunc")
|
|
139
|
+
self.consume(LBRACE)
|
|
140
|
+
name = self.consume(IDENTIFIER)[1]
|
|
141
|
+
self.consume(RBRACE)
|
|
142
|
+
self.consume(MINUS)
|
|
143
|
+
body = self.parse_block()
|
|
144
|
+
return {"type": "func_def", "name": name, "body": body}
|
|
145
|
+
|
|
146
|
+
def parse_func_call(self):
|
|
147
|
+
self.consume(KEYWORD, "func")
|
|
148
|
+
self.consume(LBRACE)
|
|
149
|
+
name = self.consume(IDENTIFIER)[1]
|
|
150
|
+
self.consume(RBRACE)
|
|
151
|
+
self.consume(LINE_END)
|
|
152
|
+
return {"type": "func_call", "name": name}
|
|
153
|
+
|
|
154
|
+
def parse_if(self):
|
|
155
|
+
self.consume(KEYWORD, "if")
|
|
156
|
+
self.consume(LBRACE)
|
|
157
|
+
condition = self.parse_condition()
|
|
158
|
+
self.consume(COMMA)
|
|
159
|
+
self.consume(KEYWORD, "then")
|
|
160
|
+
self.consume(RBRACE)
|
|
161
|
+
self.consume(MINUS)
|
|
162
|
+
then_body = self.parse_block()
|
|
163
|
+
|
|
164
|
+
elif_clauses = []
|
|
165
|
+
else_body = None
|
|
166
|
+
|
|
167
|
+
while self.check(KEYWORD, "elif"):
|
|
168
|
+
self.consume(KEYWORD, "elif")
|
|
169
|
+
self.consume(LBRACE)
|
|
170
|
+
elif_cond = self.parse_condition()
|
|
171
|
+
self.consume(COMMA)
|
|
172
|
+
self.consume(KEYWORD, "then")
|
|
173
|
+
self.consume(RBRACE)
|
|
174
|
+
self.consume(MINUS)
|
|
175
|
+
elif_body = self.parse_block()
|
|
176
|
+
elif_clauses.append({"condition": elif_cond, "body": elif_body})
|
|
177
|
+
|
|
178
|
+
if self.check(KEYWORD, "else"):
|
|
179
|
+
self.consume(KEYWORD, "else")
|
|
180
|
+
self.consume(COLON)
|
|
181
|
+
else_body = self.parse_block()
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
"type": "if",
|
|
185
|
+
"condition": condition,
|
|
186
|
+
"then_body": then_body,
|
|
187
|
+
"elif_clauses": elif_clauses,
|
|
188
|
+
"else_body": else_body,
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
def parse_other_coding(self):
|
|
192
|
+
self.consume(KEYWORD, "otherCoding")
|
|
193
|
+
self.consume(LBRACE)
|
|
194
|
+
lang = self.consume(STRING)[1].strip('"')
|
|
195
|
+
self.consume(COMMA)
|
|
196
|
+
code = self.consume(RAW_CODE)[1]
|
|
197
|
+
self.consume(LINE_END)
|
|
198
|
+
return {"type": "other_code", "language": lang, "code": code}
|
|
199
|
+
|
|
200
|
+
def parse_import(self):
|
|
201
|
+
self.consume(KEYWORD, "import")
|
|
202
|
+
self.consume(LBRACE)
|
|
203
|
+
package = self.consume(STRING)[1].strip('"')
|
|
204
|
+
self.consume(COMMA)
|
|
205
|
+
nickname = self.consume(IDENTIFIER)[1]
|
|
206
|
+
self.consume(RBRACE)
|
|
207
|
+
self.consume(LINE_END)
|
|
208
|
+
return {"type": "import", "package": package, "nickname": nickname}
|
|
209
|
+
|
|
210
|
+
def parse_ai(self):
|
|
211
|
+
self.consume(KEYWORD, "pcAI")
|
|
212
|
+
self.consume(LBRACE)
|
|
213
|
+
mode = self.consume(STRING)[1].strip('"')
|
|
214
|
+
self.consume(COMMA)
|
|
215
|
+
directions = self.consume(STRING)[1].strip('"')
|
|
216
|
+
options = []
|
|
217
|
+
while self.check(COMMA):
|
|
218
|
+
self.consume(COMMA)
|
|
219
|
+
opt = self.consume(STRING)[1].strip('"')
|
|
220
|
+
options.append(opt)
|
|
221
|
+
self.consume(RBRACE)
|
|
222
|
+
self.consume(LINE_END)
|
|
223
|
+
return {"type": "ai", "mode": mode, "directions": directions, "options": options}
|
|
224
|
+
|
|
225
|
+
# ── expressions ────────────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
def parse_concat_expr(self):
|
|
228
|
+
"""expr ('+' expr)* — used inside pcType{}"""
|
|
229
|
+
parts = [self.parse_math_expr()]
|
|
230
|
+
while self.check(PLUS):
|
|
231
|
+
self.consume(PLUS)
|
|
232
|
+
parts.append(self.parse_math_expr())
|
|
233
|
+
if len(parts) == 1:
|
|
234
|
+
return parts[0]
|
|
235
|
+
return {"type": "concat", "parts": parts}
|
|
236
|
+
|
|
237
|
+
def parse_math_expr(self):
|
|
238
|
+
"""expr (('-' | '*' | '/') expr)*"""
|
|
239
|
+
left = self.parse_expr()
|
|
240
|
+
while self.current() and self.current()[0] in (MINUS, TIMES, DIVIDE):
|
|
241
|
+
op = self.consume(self.current()[0])[1]
|
|
242
|
+
right = self.parse_expr()
|
|
243
|
+
left = {"type": "binop", "op": op, "left": left, "right": right}
|
|
244
|
+
return left
|
|
245
|
+
|
|
246
|
+
def parse_expr(self):
|
|
247
|
+
"""Single atom: string, number, var ref, input ref, list literal, func call."""
|
|
248
|
+
token = self.current()
|
|
249
|
+
if token is None:
|
|
250
|
+
raise SyntaxError("Unexpected end of expression")
|
|
251
|
+
|
|
252
|
+
if token[0] == STRING:
|
|
253
|
+
self.pos += 1
|
|
254
|
+
return {"type": "string", "value": token[1].strip('"')}
|
|
255
|
+
|
|
256
|
+
if token[0] == NUMBER:
|
|
257
|
+
self.pos += 1
|
|
258
|
+
raw = token[1]
|
|
259
|
+
return {"type": "number", "value": float(raw) if '.' in raw else int(raw)}
|
|
260
|
+
|
|
261
|
+
if token[0] == KEYWORD:
|
|
262
|
+
kw = token[1]
|
|
263
|
+
|
|
264
|
+
if kw == "var":
|
|
265
|
+
self.pos += 1
|
|
266
|
+
self.consume(LBRACE)
|
|
267
|
+
name = self.consume(IDENTIFIER)[1]
|
|
268
|
+
self.consume(RBRACE)
|
|
269
|
+
return {"type": "var_ref", "name": name}
|
|
270
|
+
|
|
271
|
+
if kw == "input":
|
|
272
|
+
self.pos += 1
|
|
273
|
+
self.consume(LBRACE)
|
|
274
|
+
self.consume(IDENTIFIER) # 'ans'
|
|
275
|
+
self.consume(RBRACE)
|
|
276
|
+
return {"type": "input_ref"}
|
|
277
|
+
|
|
278
|
+
if kw == "list":
|
|
279
|
+
items = self._parse_list_body()
|
|
280
|
+
return {"type": "list", "items": items}
|
|
281
|
+
|
|
282
|
+
if kw == "func":
|
|
283
|
+
self.pos += 1
|
|
284
|
+
self.consume(LBRACE)
|
|
285
|
+
name = self.consume(IDENTIFIER)[1]
|
|
286
|
+
self.consume(RBRACE)
|
|
287
|
+
return {"type": "func_call_expr", "name": name}
|
|
288
|
+
|
|
289
|
+
raise SyntaxError(f"Unexpected token in expression: {token[0]} {repr(token[1])}")
|
|
290
|
+
|
|
291
|
+
def _parse_list_body(self):
|
|
292
|
+
"""list{item; item; ...} — shared by list expr and list statement."""
|
|
293
|
+
self.consume(KEYWORD, "list")
|
|
294
|
+
self.consume(LBRACE)
|
|
295
|
+
items = [self.parse_expr()]
|
|
296
|
+
while self.check(SEMICOLON):
|
|
297
|
+
self.consume(SEMICOLON)
|
|
298
|
+
items.append(self.parse_expr())
|
|
299
|
+
self.consume(RBRACE)
|
|
300
|
+
return items
|
|
301
|
+
|
|
302
|
+
def parse_condition(self):
|
|
303
|
+
"""left op right, where op is ==, !=, <, >, <=, >=."""
|
|
304
|
+
left = self.parse_expr()
|
|
305
|
+
token = self.current()
|
|
306
|
+
if token and token[0] in COMPARISON_OPS:
|
|
307
|
+
op = self.consume(token[0])[1]
|
|
308
|
+
right = self.parse_expr()
|
|
309
|
+
return {"type": "condition", "left": left, "op": op, "right": right}
|
|
310
|
+
return left
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "curl-programming-lang"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Curl programming language interpreter"
|
|
9
|
+
requires-python = ">=3.7"
|
|
10
|
+
|
|
11
|
+
[project.scripts]
|
|
12
|
+
curlang = "main:main"
|
|
13
|
+
|
|
14
|
+
[tool.setuptools]
|
|
15
|
+
py-modules = ["main", "lexer", "parser", "interpreter", "errors"]
|