utkrisht 0.1.0

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.
package/readme.md ADDED
@@ -0,0 +1,491 @@
1
+
2
+ <div align="center">
3
+
4
+ # Utkrisht
5
+
6
+ [![Build Status](https://img.shields.io/badge/build-passing-22c55e)]()
7
+ ![Repo Size](https://img.shields.io/github/repo-size/LadyBeGood/utkrisht?color=3b82f6)
8
+ ![Last Commit](https://img.shields.io/github/last-commit/LadyBeGood/utkrisht?color=8b5cf6)
9
+
10
+ *A unified language for the web*
11
+
12
+ </div>
13
+
14
+
15
+
16
+ ## Overview
17
+
18
+ **Utkrisht** (uki) is a **source-to-source compiled programming language** under active development that targets **HTML, CSS, and JavaScript**, with the long-term objective of evolving into a **full-featured, unified web framework**.
19
+
20
+
21
+
22
+ ## Installation
23
+ > [!WARNING]
24
+ > Do not install this now, everything is broken and has errors.
25
+
26
+
27
+ ### Prerequisites
28
+ [Install Nim](https://nim-lang.org/install.html)
29
+
30
+ ### Steps
31
+ Clone this repository
32
+ ```
33
+ git clone https://github.com/LadyBeGood/utkrisht.git
34
+ ```
35
+
36
+ Compile the compiler
37
+
38
+ ```bash
39
+ nim c --d:release --opt:speed --passC:-flto --out:uki --verbosity:0 ./utkrisht/compiler/uki.nim
40
+ ```
41
+
42
+ (Optional) Clean up the repository after successful compilation:
43
+ ```bash
44
+ rm -rf utkrisht
45
+ ```
46
+
47
+ ### Example usage
48
+ Compile a input.uki file to output.js file:
49
+
50
+ ```bash
51
+ ./uki input.uki output.js
52
+ ```
53
+
54
+ ## Tutorial
55
+
56
+ ### Hello, World
57
+ ```
58
+ write "Hello World"
59
+ ```
60
+
61
+ ### Comments
62
+
63
+ #### Regular Comments
64
+ Used for notes. They can be placed anywhere.
65
+ ```
66
+ # This is a regular comment
67
+ when 10 > 5
68
+ # This is also a comment
69
+ write "Condition is correct" # This is also a valid comment
70
+ ```
71
+
72
+ #### Documentation Comments
73
+ You can document a variable by directly placing comments above it. These comments are **directly** copied into the generated JavaScript as JSDoc comments, without any special syntax.
74
+
75
+ ```
76
+ # Divide 2 numbers
77
+ # - `a`: Numerator
78
+ # - `b`: Denominator
79
+ divide a, b: {
80
+ exit a / b
81
+ }
82
+ ```
83
+
84
+
85
+ > [!NOTE]
86
+ > Notation in this tutorial (used wherever necessary):
87
+ > - `#>` is the message logged in the console.
88
+ > - `#->` is the value of the preceding expression.
89
+ > - `#` is a regular comment.
90
+
91
+
92
+ ### Data types
93
+ Utkrisht has 5 data types.
94
+
95
+ | Data Type | Passed by | Clonable | iterable | Mutable | Callable |
96
+ |-----------|-----------|----------|----------|---------|----------|
97
+ | String | Value | Yes | Yes | No | No |
98
+ | Number | Value | Yes | No | No | No |
99
+ | Boolean | Value | Yes | No | No | No |
100
+ | Procedure | Reference | No | No | No | Yes |
101
+ | Structure | Reference | Yes | Yes | Yes | No |
102
+
103
+
104
+ #### String
105
+ A string represents a sequence of UTF-16 code units, identical to JavaScript strings.
106
+
107
+ ```
108
+ write length "😼" #> 2
109
+ write "😼".1 #> \ud83d
110
+
111
+ write length "é" #> 1 (é)
112
+ write length "é" #> 2 (e + "́")
113
+ write "é" = "é" #> wrong (no normalization)
114
+ ```
115
+ #### Number
116
+ Number is represented in the double-precision 64-bit floating point format (IEEE 754), just like JavaScript's number type.
117
+ ```
118
+ 123 # one hundred and twenty three
119
+ 123.0 # same as above
120
+
121
+ 0 # zero
122
+ -0 # same as above
123
+
124
+ infinity
125
+ -infinity
126
+ 1 / 0 #-> infinity
127
+ -1 / 0 #-> -infinity
128
+ 1 / -0 #-> -infinity
129
+ -1 / -0 #-> infinity
130
+
131
+ nan # stands for "not a number", but this is also a number
132
+ -nan # same as above
133
+ 0 / 0 #-> nan
134
+ -0 / 0 #-> nan
135
+ 0 / -0 #-> nan
136
+ -0 / -0 #-> nan
137
+ ```
138
+
139
+ ```
140
+ write 123 = 123.0 #> right
141
+
142
+ write 0 = 0 #> right
143
+ write -0 = -0 #> right
144
+ write 0 = -0 #> right
145
+
146
+ write infinity = infinity #> right
147
+ write infinity = -infinity #> wrong
148
+ write -infinity = -infinity #> right
149
+
150
+ write nan = nan #> wrong (This is not a typo)
151
+ write nan != nan #> right (This is also not a typo)
152
+
153
+ write is-finite 123 #> right
154
+ write is-finite infinity #> wrong
155
+ write is-finite -infinity #> wrong
156
+ write is-nan nan #> right
157
+ ```
158
+
159
+ ```
160
+ write 0.1 + 0.2 = 0.3 #> wrong
161
+ write 0.1 + 0.2 #> 0.30000000000000004
162
+
163
+ write 0.1 * 10 #> 1
164
+ write 0.14 * 100 #> 14.000000000000002
165
+
166
+ write 1.0000000000000001 #> 1
167
+ write 9999999999999999 #> 10000000000000000
168
+ ```
169
+
170
+ #### Boolean
171
+ Boolean literals are represented by the keywords `right` and `wrong`. There are no *truthy* or *falsy* values.
172
+
173
+ #### Procedure
174
+
175
+
176
+
177
+ ### Keywords
178
+
179
+ Keywords are predefined words used by the language to perform internal operations or represent built-in behavior.
180
+
181
+ Utkrisht has **13 keywords**. None of them are reserved and may also be used as [identifiers](#identifiers).
182
+
183
+ | Keywords | Description |
184
+ |-----------------------------------|-----------------------------------------------|
185
+ | `right`, `wrong` | Boolean literals |
186
+ | `when`, `else` | Conditional branching |
187
+ | `loop`, `with`, `stop`, `skip` | Looping and loop control |
188
+ | `try`, `fix` | Error handling |
189
+ | `exit` | Exiting from a procedure |
190
+ | `import`, `export` | Module import and export |
191
+
192
+
193
+
194
+ ### Symbols
195
+
196
+ Symbols are non-alphanumeric tokens that have special meaning in the language syntax.
197
+
198
+ Symbols are context sensitive.
199
+
200
+ In Utkrisht, symbols are grouped by their role into **operators**, **separators**, **delimiters** and **terminators**.
201
+
202
+
203
+
204
+ #### Operators
205
+
206
+ Operators are symbols used to perform operations on values.
207
+
208
+ | Operator | Type | Description |
209
+ |---------------------------------------|-------------------------------------------|---------------------------------------------------------|
210
+ | `:` `=` | Infix | Variable declaration and reassignment |
211
+ | `+` `-` `*` `/` | Infix | Arithmetic operations |
212
+ | `=` `<` `>` `!=` `!<` `!>` | Infix | Comparison operations |
213
+ | `&`, `\|`, `!` | Infix (except `!`, which is prefix) | Logical operations |
214
+ | `_` | Infix | Range construction |
215
+ | `.` | Infix | Access operator |
216
+ | `;` | Postfix | Procedure call |
217
+ | `/` | Prefix | Module access operator |
218
+ | `@` | Prefix | Async operator |
219
+ | `$` | Prefix | Reactivity operator |
220
+ | `~` | Infix | Default values for parameters and named arguments |
221
+ | `\` | Prefix | Escape operator |
222
+
223
+
224
+
225
+ #### Separators
226
+
227
+ Separators are symbols used to divide syntactic elements without performing an operation.
228
+
229
+ | Separator | Separates |
230
+ |-----------|--------------------------------------------------------------|
231
+ | `,` | Arguments, Parameters, Properties |
232
+ | `~,` | Arguments (use default value for the parameter) |
233
+ | Newline | Arguments, Parameters, Properties (non-terminating contexts) |
234
+
235
+
236
+
237
+ #### Delimiters
238
+
239
+ Delimiters mark the beginning and end of syntactic constructs.
240
+
241
+ | Delimiter | Delimits |
242
+ |------------------------------|------------------------------------------------|
243
+ | `(` ... `)` | Expression groups |
244
+ | `{` ... `}` | Procedures |
245
+ | `[` ... `]` | Structures |
246
+ | `"` ... `"` | Strings |
247
+ | `/` ... `/` | Regular expressions |
248
+ | `\(` ... `)` | String Interpolation |
249
+ | `#` ... NewLine or EndOfFile | Comments |
250
+ | Indent ... Dedent | Expressions, Procedures, Arguments, Parameters |
251
+
252
+
253
+ #### Terminators
254
+ Terminators mark the end of a statement or declaration.
255
+
256
+ | Terminator | Terminates |
257
+ |------------|---------------------------------------------|
258
+ | Newline | Statements, Comments (terminating contexts) |
259
+ | EndOfFile | Statements, Comments |
260
+
261
+
262
+
263
+ ### Identifiers
264
+ Identifiers are names given to different entities in Utkrisht to uniquely identify them within the source code. Identifiers can be used in various places:
265
+
266
+ ```
267
+ # variables
268
+ message: "hi"
269
+
270
+ # structure keys
271
+ [name = "Uki"]
272
+
273
+ # modules
274
+ import components/footer
275
+ ```
276
+
277
+ A valid identifier:
278
+
279
+ - contains only lowercase letters (`a–z`), numbers (`0–9`), and `-`
280
+ - starts with a lowercase letter
281
+ - ends with a lowercase letter or number
282
+ - does not contain consecutive hyphens (`--`, `---` etc.)
283
+
284
+
285
+ Valid Identifiers
286
+ ```
287
+ name
288
+ user-1
289
+ user1
290
+ file-path
291
+ data-set-3
292
+ a1-b2
293
+ ```
294
+
295
+ Invalid Identifiers
296
+ ```
297
+ myName # contains uppercase letter
298
+ 1value # starts with a number
299
+ -value # starts with a hyphen, will be interpreted as negation
300
+ value- # ends with a hyphen
301
+ my--var # contains consecutive hyphens
302
+ user_name # contains underscore, will be interpreted as a range
303
+ ```
304
+
305
+ ### Variables
306
+ Variables are identifiers used to store data. All variables are mutable and can be reassigned.
307
+
308
+
309
+
310
+ Declare a variable using `:`
311
+ ```
312
+ message: "Hello World"
313
+ ```
314
+ Reassign a value using `=`
315
+ ```
316
+ quantity: 34
317
+
318
+ quantity = 65
319
+ quantity = "high" # Data type of the value does not matter
320
+ ```
321
+
322
+ ### Control Flow
323
+ Control flow determines how execution proceeds through a program.
324
+
325
+ #### Conditionals
326
+ Conditionals control which procedure will be invoked based on a **condition**. The condition must be a boolean value, Utkrisht does not have truthy or falsy values.
327
+ ```
328
+ # Statement conditionals
329
+ when age > 18
330
+ write "You're an adult"
331
+ else age < 18
332
+ write "You're a child"
333
+ else
334
+ write "You're no longer a child, you became an adult!"
335
+
336
+
337
+ # Expression conditionals
338
+ status: when age < 18 "minor" else "adult"
339
+ ```
340
+ #### Loops
341
+ Loop is a data type sensitive construct. Therefore the behaviour of the loop depends upon the data type of the data following it.
342
+ ```
343
+ # loop keyword followed directly by a procedure
344
+ # loops infinitely
345
+ loop
346
+ write "hello"
347
+
348
+ # loop keyword followed by a boolean
349
+ # loops infinitely if right
350
+ loop right
351
+ write "hello"
352
+
353
+ # doesn't loop if wrong
354
+ loop wrong
355
+ write "hello"
356
+
357
+
358
+ # loop keyword followed by a number
359
+ # loops that many times
360
+ loop 5 # loops 5 times
361
+ write "hello"
362
+
363
+
364
+ # loop keyword followed by a string or structure
365
+ # loops `length iterable` times
366
+ loop "uki" # loops 3 times because there are 2 characters in "uki": "u", "k" and "i"
367
+ write "hello"
368
+
369
+ loop [id = 567, right, "orange"] # loops 3 times
370
+ write "hello"
371
+
372
+
373
+ # with statement
374
+ # declares a iterator/counter
375
+ loop 5 with i
376
+ write i
377
+
378
+ # here
379
+ # `i` is the iterator
380
+ # `i` starts a 1 and ends at 5
381
+
382
+ #> 1
383
+ #> 2
384
+ #> 3
385
+ #> 4
386
+ #> 5
387
+
388
+ fruits: ["apple", "mango", "banana"]
389
+ loop fruits with fruit
390
+ write "I love \(fruit)"
391
+
392
+ #> I love apple
393
+ #> I love mango
394
+ #> I love banana
395
+
396
+ # multiple iterators can be declared
397
+ loop fruits with [i, fruit]
398
+ write "\(i). I love \(fruit)"
399
+
400
+ #> 1. I love apple
401
+ #> 2. I love mango
402
+ #> 3. I love banana
403
+
404
+
405
+ loop "hi" with [index, character]
406
+ write "The character at position \(index) is \(character)"
407
+
408
+ #> The character at position 1 is h
409
+ #> The character at position 2 is i
410
+
411
+
412
+
413
+ ```
414
+
415
+
416
+ > [!NOTE]
417
+ > Some looping constructs are still under consideration and not included here.
418
+
419
+
420
+ ```
421
+ # stop statement, stops the loop
422
+ loop 50 with i
423
+ when i = 4
424
+ stop
425
+ write i
426
+
427
+ #> 1
428
+ #> 2
429
+ #> 3
430
+
431
+ # skip statement, skips the iteration
432
+ loop 4 with i
433
+ when i = 2
434
+ skip
435
+ write i
436
+
437
+ #> 1
438
+ #> 3
439
+ #> 4
440
+
441
+
442
+ # iterators can be used as labels in nested loops for skip and stop statements
443
+ loop 3 with i
444
+ loop 3 with j
445
+ when i = 2
446
+ skip i
447
+ write "\(i) \(j)"
448
+
449
+ #> 1 1
450
+ #> 1 2
451
+ #> 1 3
452
+ #> 3 1
453
+ #> 3 2
454
+ #> 3 3
455
+ ```
456
+
457
+
458
+
459
+ ### Modules
460
+
461
+ A module is a reusable unit of code that organizes logic into separate files and folders.
462
+
463
+ A module can *import* other modules and *export* its variables to share them with other module that import them.
464
+
465
+ > [!NOTE]
466
+ > In an Utkrisht project, in order to be imported and compiled
467
+ > - all file and folder names must be valid identifiers.
468
+ > - a folder must not contain a file and folder of same name.
469
+
470
+ There are two types of modules:
471
+ 1. **File Module**: Any Utkrisht file which is not inside a folder module.
472
+ 2. **Folder Module**: Any folder having a Utkrisht file of same name as a direct child.
473
+
474
+ #### Import
475
+ A module can be imported using the `import` keyword followed by its path:
476
+ ```
477
+ import utilities
478
+ import components/footer
479
+ import ../assets/icons
480
+ import routes/[home, notifications, profile]
481
+ ```
482
+ When you write an import like `import abc`, the compiler looks for:
483
+ - a file named `abc.uki`, or
484
+ - a folder named `abc` that contains a file named `abc.uki`.
485
+
486
+ #### Export
487
+ Use the `export` keyword available to other modules:
488
+ ```
489
+ export message: "hi"
490
+ ```
491
+