-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDOCUMENTATION.html
More file actions
9376 lines (7449 loc) · 341 KB
/
DOCUMENTATION.html
File metadata and controls
9376 lines (7449 loc) · 341 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub-Style Markdown with Ace Editor</title>
<!-- Link to markdown-it library -->
<script src="https://cdn.jsdelivr.net/npm/markdown-it@12.0.4/dist/markdown-it.min.js"></script>
<!-- Ace Editor -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/theme-monokai.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/mode-javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/mode-python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/mode-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/mode-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/mode-json.min.js"></script>
<!-- Font Awesome for icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<style>
/* Dark Mode Styles */
body {
background-color: #121212;
color: #e0e0e0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
padding-left: 30px;
padding-right: 30px;
margin: 0;
word-wrap: break-word;
}
h1, h2, h3, h4, h5, h6 {
color: #ffffff;
position: relative;
}
/* Container for the markdown content */
#markdown-output {
background-color: #1e1e1e;
border-radius: 8px;
padding: 30px;
margin-top: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* Header Styling */
h1 {
font-size: 2.5em;
border-bottom: 2px solid #444;
padding-bottom: 10px;
}
h2 {
color: #ffffff;
border-bottom: 1px solid #444;
padding-bottom: 5px;
margin-top: 30px;
}
h3 {
color: #81a1c1;
margin-top: 20px;
}
/* Links */
a {
color: #1e90ff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* External link icon */
a.external-link:after {
content: " \2197";
display: inline-block;
margin-left: 4px;
font-size: 0.8em;
}
/* Header anchor links that show on hover */
.header-anchor {
opacity: 0;
font-size: 0.8em;
margin-left: 8px;
color: #666;
transition: opacity 0.2s ease-in-out;
}
h1:hover .header-anchor,
h2:hover .header-anchor,
h3:hover .header-anchor,
h4:hover .header-anchor,
h5:hover .header-anchor,
h6:hover .header-anchor {
opacity: 1;
color: #1e90ff;
}
/* Ace Editor Styling */
.ace-editor-container {
margin: 20px 0;
border-radius: 8px;
overflow: hidden;
}
.ace-editor {
width: 100%;
border-radius: 8px;
/* Height will be set dynamically based on content */
}
.ace-editor-header {
background-color: #333;
color: #fff;
padding: 8px 15px;
font-family: monospace;
font-size: 0.9em;
display: flex;
justify-content: space-between;
align-items: center;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
.ace-editor-language {
font-weight: bold;
color: #82aaff;
}
/* Copy Button Styling */
.copy-button {
background-color: #2a2a2a;
border: none;
color: #ccc;
border-radius: 4px;
padding: 3px 8px;
font-size: 12px;
cursor: pointer;
margin-right: 10px;
transition: all 0.2s ease;
display: flex;
align-items: center;
}
.copy-button:hover {
background-color: #3a3a3a;
color: #fff;
}
.copy-button i {
margin-right: 4px;
}
.copy-success {
background-color: #28a745 !important;
color: white !important;
}
/* Inline code */
p code, li code {
background-color: #2b2b2b;
padding: 2px 5px;
border-radius: 3px;
font-family: Consolas, 'Courier New', monospace;
}
/* Table Styling */
table {
width: 100%;
margin: 20px 0;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
border: 1px solid #444;
}
th {
background-color: #333;
}
tr:nth-child(even) {
background-color: #2a2a2a;
}
tr:hover {
background-color: #444;
}
/* Fix for internal links */
:target {
scroll-margin-top: 20px;
animation: highlight 2s ease;
}
@keyframes highlight {
from { background-color: #333333; }
to { background-color: transparent; }
}
</style>
</head>
<body>
<!-- Markdown content written in normal syntax -->
<div id="markdown-content" style="display:none;">
# HTVM Documentation
1. [Introduction](#introduction-to-htvm)
Understand why HTVM is the future of programming.
2. [OSP (Ordinal Struct Programming)](#osp-ordinal-struct-programming)
Learn about OSP (Ordinal Struct Programming) a simple way to use Structs.
3. [How Your Language Looks Like](#how-your-language-looks-like)
Get to know how HTVM code is structured and its syntax style.
4. [Programming Blocks](#programming-blocks)
HTVM introduces **programming blocks** that allow you to code in the target language seamlessly in your code. This feature is designed to handle cases where HTVM itself can't achieve something directly or when you'd prefer to rely on another language for specific tasks.
5. [Variables](#variables)
Learn how to declare and use variables in HTVM for storing and manipulating data.
6. [Functions](#functions)
Explore how to define and use functions for modular and reusable code.
7. [Pattern Matching](#pattern-matching)
HTVM provides a flexible and minimalist `when` block for pattern matching. It is designed to be highly intuitive, allowing you to write complex conditional logic with the least possible syntax and elegantly eliminating the verbosity of traditional `if-else` chains.
8. [Arrays](#arrays)
Dive into working with arrays, one of the most essential data structures in HTVM.
9. [Loops](#loops)
Master the looping structures available in HTVM, such as the standard loop and infinite loops.
10. [Comments](#comments)
Add clarity to your code by using comments to document and explain your code.
11. [GUI (Graphical User Interface with HTVM)](#gui-graphical-user-interface-with-htvm)
Learn how to the GUI feature in HTVM that allows for the creation of graphical user interfaces specifically for **web browsers (JavaScript)**.
12. [Backend in HTVM](#backend-in-htvm)
Learn how to use HTVM's Backend functionality that generates **Python** code using **Flask**.
13. [Error Handling](#error-handling)
Understand how to manage errors and handle exceptions in HTVM to make your programs more robust.
14. [Include](#include)
Discover how to include external files and resources in your HTVM project for enhanced functionality.
15. [Commands](#commands)
Commands in HTVM are a simplified way to perform actions, similar to functions, but with a more compact, direct, and concise syntax for better efficiency.
16. [All Syntax](#all-syntax)
This is all of your syntax.
17. [Built-in Functions](#built-in-functions)
Explore the wide range of built-in functions categorized for ease of use.
---
## Introduction to HTVM
[Go back](#htvm-documentation)
HTVM is a revolutionary tool designed to replace traditional programming. With HTVM, you can create your own programming language directly [here](https://themaster1127.github.io/HTVM/).
HTVM offers **extensive syntax customization**, giving you the freedom to tailor the language structure to your own preferences. Almost all of these customizations can be converted into 3 languages: JavaScript, Python, and C++. Providing flexibility while keeping things simple.
## Customization Features
### Code Block Delimiters
You can **customize the block delimiters**, which define the start and end of code blocks. You can use:
- Curly braces `{}` (default).
- **Other characters**, words, or even single letters, as long as the opening and closing delimiters are different from each other.
### Indentation Styles
You have the ability to choose between multiple **indentation styles**:
- **Lua indentation style**: Uses the customizable `end` keyword to close code blocks.
The `end` keyword is fully customizable. You can use `end` or any other word.`, or any other word).
### Toggleable Semicolons
Semicolons are **toggleable** in HTVM. By default, semicolons are not required to terminate statements, but you can enable the use of semicolons if you prefer them in your syntax. It's completely up to you whether or not to use them in your code.
### Parentheses Customization
For **conditional statements** like `if`, `else if`, and `while` loops, you can choose to **use or omit parentheses** around conditions. This customization allows you to streamline your code and make it more intuitive according to your preferences.
### Customizable Operators
HTVM allows you to **customize operators** to make your code more readable or suit your style. For example, instead of using `==` for equality, you can change it to a keyword like `is`. This means you can write more natural, human-readable code, such as:
- `if var1 is 5` (instead of `if var1 == 5`).
This flexibility extends across various operators, giving you full control over how your conditions and operations are structured.
## More Customization Options in HTVM
HTVM offers even **more customization** for your syntax and style. Here's a breakdown of additional features:
### Type Definition Styles
You can choose between **PostFix** or **Prefix** style for type definitions, similar to TypeScript:
- **PostFix**: Define the variable first, then the type after the colon (`var1: int`).
- **Prefix**: Define the type first, then the variable (`int var1`).
You can toggle between these styles depending on which one fits your preferred syntax better.
### Array Syntax Customization
HTVM lets you customize the array syntax by choosing between two options:
- **Regular braces** (`[]`): The default style for arrays.
- **Curly braces** (`{}`): If you prefer this style, you can toggle it on.
It's fully flexible, so you can use whichever syntax feels right for your code.
### Customizable Operators
HTVM extends **operator customizability** even further, allowing you to adjust a wide range of operators:
- **All operators**: You can customize **all operators**, including logical, comparison, and even **bitwise operators**.
- **Assignment operators**: You can customize assignment operators, such as `=`, to whatever you prefer (e.g., `is`, `set`, or any other single word).
However, remember that you are only allowed to use **one word** for customization—no spaces in operator names.
### Customizable Comments
Comments are also fully customizable:
- **Single-line comments**: Change the single-line comment syntax to whatever you like (e.g., `//`, `#`, or any word).
- **Multi-line comments**: Customize the syntax for multi-line comments, allowing for multiple words or characters if necessary.
### Custom Escape Characters
Escape characters can now be **customized**:
- **Escape character**: You are no longer limited to using just the backslash (`\`). You can choose any single character you want as your escape character—such as the backtick (`` ` ``) or something entirely different.
### Loop Index Customization
You can **change the index for regular loops**. By default, HTVM uses **zero-based indexing** for loops. However, you can change this for certain languages:
- **AutoHotKey and Lua**: By default, both of these languages use **one-based indexing** for arrays and loops.
- **Other languages**: It's important to note that **changing loop indices may cause issues** with some languages, as not all languages support customizable loop indexes. **Use caution** when making changes to this setting, especially when working with languages like Python, JavaScript, or C++.
HTVM allows for this change, but it’s recommended to keep the default zero-based indexing unless you’re specifically creating **meme languages** where the index may be something unconventional (like `-69` or `420`).
### Key Considerations for Indexing
- For **AutoHotKey** and **Lua**, the index starts at **one** by default.
- For **other languages**, **zero-based indexing** is the norm, so be mindful of this when converting to other languages.
If you are creating custom languages or experimenting with unconventional indexing, keep these limitations in mind to avoid potential issues when converting to other languages.
---
## Further Customization in HTVM
HTVM offers **even more customization options**, allowing you to tailor the language to your preferences, especially when it comes to types and arrays.
### Custom Type Names
HTVM allows you to **customize type names** to anything you like. For example:
- **Basic Types**: Instead of using the traditional `int`, you can use `number`, `i`, or any other word you prefer. The same goes for types like `double`, `char`, `boolean`, `float`, and more.
- **Integer Sizes**: You can change types like `int16` to `i16` or any naming convention you prefer.
**Important:** While you can use **multiple words** for type names, be cautious about potential conflicts in the parser. For instance, using common words that might be parsed as something else could lead to issues. Always ensure that the type names are unique and won't interfere with other syntax elements.
The available types you can customize include:
- **Void**: You can rename `void` to whatever you want.
- **Integers**: `int`, `int8`, `int16`, `int32`, `int64`, etc.
- **Unsigned Integers**: `uint8`, `uint16`, `uint32`, `uint64` (Note: be cautious when converting unsigned integers to Java, as Java doesn’t support them).
- **Floating Point**: `double`, `float`.
- **Character**: `char`.
- **String**: `string`.
- **Boolean**: `boolean`.
---
### Array Types Customization
In HTVM, you can customize the **array types** as well:
- **Four main array types**:
- `arr str`: For arrays of strings.
- `arr int`: For arrays of integers.
- `arr float`: For arrays of floating-point numbers.
- `arr bool`: For arrays of booleans.
- **Dynamic Arrays**: For dynamic-type languages, you can use the generic `array` keyword, but it defaults to a string array when converted to static type languages. This provides a flexible way to define arrays in your code.
**Note on Static vs. Dynamic Types**: In **static-type languages**, arrays are strictly defined, so if you use the `array` keyword, it will default to a **string array**. For **dynamic languages**, the array type is more flexible.
---
### Array Methods Customization
HTVM allows you to customize array methods with ease. These methods work by appending a dot (`.`) after the array, followed by the method name:
- **append**: Adds an element to the end of the array.
- **pop**: Removes the last element from the array.
- **size**: Retrieves the size (length) of the array.
- **insert**: Inserts an element at a specific index (not by value).
- **remove**: Removes an element at a specific index (not by value).
- **indexOf**: Finds the index of a value in the array.
These methods can be customized with different names or functionality if needed, making it easier to manipulate arrays according to your needs.
---
### Important Considerations
- **Java and Unsigned Integers**: Be careful when converting unsigned integers (`uint8`, `uint16`, etc.) to **Java**, as Java does not support unsigned integers. HTVM will **NOT** warn you when attempting this conversion.
- **Array Methods**: Ensure that you define methods like `insert` and `remove` clearly to avoid confusion with other types of array manipulations. Using the dot (`.`) ensures consistency and clarity in how methods are applied to arrays.
---
## Extensive Customization in HTVM
HTVM offers **wide-ranging customization options** to ensure the language suits your preferences, especially when it comes to keywords and functionality. You can customize several aspects of the language, ranging from basic variable declarations to control structures and even language-specific keywords.
### Customizing Keywords
HTVM allows you to customize many fundamental keywords that are used across different programming languages:
- **Variable Declarations**: Customize `var`, `let`, and `const` keywords. These are primarily used in **JavaScript** and **TypeScript**.
- The `const` keyword is supported in most languages, but in languages that don't have constants (e.g., **Python**, **AutoHotKey**, **Lua** and some more), it will be ignored, and the variable will be created as usual.
- **Boolean Keywords**: Change the `true` and `false` keywords to something else like `yeah` and `nah`. This customization provides flexibility in how logical values are represented.
- **Null Keyword**: You can also customize the `null` keyword, though be cautious—some languages do not support `null` in the same way (e.g., **Python**, **C++**, **AutoHotKey** and some more). For languages that don't support `null`, the value will simply be ignored or treated as an empty value.
- **End Keyword**: You can change the `end` keyword to something else. This is typically used for denoting the end of code blocks in various languages and is customizable in HTVM.
- **Global Keyword**:
- The `global` keyword works only in **Python** and **AutoHotKey**. It allows passing a global variable into a function and modifying it. In other languages, the `global` keyword will be ignored, but the variable will still be created.
- You can also customize the `global` keyword to something else, keeping the functionality intact within supported languages.
---
### Main Label and Main Function Customization
HTVM allows you to **customize the main function label**. The main label refers to the starting point of your program, especially for languages like **C++** and many more but in **JavaScript** and **TypeScript** it's a bit different:
- **Main Label**:
- This is a customizable label where your main function is typically located. You can define a single word or even multiple words as the main label. The parser will ensure that the code below the label is treated as the main function's content.
- The main label is optional, and it’s simply a label—there’s no need to wrap the code in a block unless the language requires it (e.g., **Python** doesn’t need a `main` function, so the code will be placed directly beneath it).
- **Main Function**:
- In **JavaScript** and **TypeScript**, you can toggle between an **async main function** and a regular one.
- The language will default to creating asynchronous functions for all function definitions, but you can customize this behavior with the **async** keyword (which is also customizable to other terms if you prefer). Also, if you don’t use the main label keyword, the HTVM parser will assume that you haven't created any functions, and all the code will be placed in the main function if the target language has a main function.
---
### Function Customization
HTVM allows you to **customize various function-related keywords** to suit your needs:
- **Async Functions**:
- By default, all functions in HTVM are asynchronous. You can toggle this behavior or use the **asynchronous** keyword, which is customizable.
- The **await** keyword can also be modified to something else (e.g., `wait`) to fit your naming preferences.
- **Loop Customization**:
- The loop syntax can be customized with terms like `A_Index` and `A_LoopField` to resemble **AutoHotKey**-style loops. These keywords provide a cleaner, more intuitive loop structure for those familiar with AutoHotKey.
---
### Important Considerations
- **Customizing Keywords**: When you customize keywords (e.g., `const`, `true`, `false`, `null`), ensure that they don’t conflict with existing language constructs. The parser will handle many of these customizations, but be aware of limitations in certain languages.
- **Global Variables**: The `global` keyword is supported in **Python** and **AutoHotKey**, but it will be ignored in other languages like **JavaScript** or **C++**. Ensure you understand how this will behave across different languages.
- **Async Functions in JavaScript/TypeScript**: This is especially useful for **JavaScript** and **TypeScript** but can be customized to suit your project’s needs.
---
HTVM provides a **high level of flexibility** for customizing how the language operates, allowing you to adapt it to the specific needs of your project. Whether you're working with variable declarations, loop structures, or customizing language-specific keywords, HTVM makes it easier to code in a way that feels natural to you.
---
## Advanced Customizations in HTVM
HTVM offers **advanced customizations** that allow you to control everything from ternary operators to error handling and even modularity with `include`. This flexibility makes it possible to tailor the language to your coding style and needs.
### Ternary Operator Customization
HTVM lets you customize the **ternary operator**:
- By default, HTVM uses the regular **C-style ternary operator** (`condition ? true : false`). This works in most languages, but certain languages (like **Go**) do not support this syntax.
- For languages that use a different syntax for ternary operators, HTVM will handle the conversion automatically, converting it to the appropriate format.
- For example, **Python** uses the `condition if true else false` syntax.
- HTVM will ensure that the ternary operator is properly converted, but if the target language does not support ternary operators, HTVM will generate a message and prevent execution in languages like **Go** where it won’t run. This ensures that you avoid runtime errors from unsupported syntax.
### Error Handling Customization
HTVM allows you to **customize error handling** as well:
- The `try`, `catch`, `finally`, and `throw` keywords are customizable, enabling you to tailor them to your preference.
- When using **throw**, you can customize the function name for the error message. This is how it works:
- Type `throw`, followed by the function name (which is customizable), open parentheses, and inside the parentheses, you can pass a string that represents the error message.
- Example:
```htvm
throw ErrorMsg("Something went wrong!")
```
- **Conversion to Other Languages**:
- HTVM converts error handling to most languages, but **Lua** and **Go** are exceptions. In those languages, the error handling structure is not supported, and HTVM will generate a string that alerts you about the lack of support.
- HTVM does not provide error handling within itself. Instead, it relies on the target language to handle errors since HTVM assumes your code is flawless, and any issues should be identified by the language you're transpiling to.
### Include Keyword Customization
HTVM also supports **modularity** through the `include` keyword:
- The `include` keyword allows you to include other HTVM files at the top of your code.
- This is useful for organizing your code across multiple files or for reusing common functionality across different projects.
- Example:
```htvm
include "utility_functions.htvm"
```
- The `include` keyword is fully customizable, so you can change it to something else if preferred.
---
### Key Takeaways:
- **Ternary Operator**: Customizable to fit different syntaxes across languages, with automatic conversion. However, unsupported in **Go** and it will hardcode a message instead of running.
- **Error Handling**: Customizable for the `try`, `catch`, `finally`, and `throw` keywords, and the function name for error messages can also be customized. HTVM does not handle errors itself but relies on the target language’s error handling. However, unsupported languages like **Go** and **Lua** will hardcode a message instead of running.
- **Include**: Supports modularity by including external HTVM files, and the keyword itself is customizable.
These advanced customizations further enhance HTVM's flexibility, allowing you to adjust error handling, ternary operators, and file management to fit your needs. With these tools, HTVM can be tailored to suit your specific coding style and make your development process more efficient.
---
## Programming Blocks in HTVM
HTVM introduces **programming blocks** that allow you to **mix languages** seamlessly in your code. This feature is designed to handle cases where HTVM itself can't achieve something directly or when you'd prefer to rely on another language for specific tasks.
### How Programming Blocks Work
1. **Opening a Programming Block**: You can open a programming block for a specific target language with the following syntax:
- One line to open the programming block.
- Inside the block, you can write in the target language.
- You close the block on a new line after your code.
Example:
```htvm
// This is a JavaScript programming block. It will stay intact and included in the JS output.
___js start
// this is now JavaScript code
console.log("==========================")
___js end
```
2. **Incorporating Blocks into HTVM Code**:
- When you write in HTVM, you can seamlessly insert target-language code using programming blocks.
- For example, if you're converting to **JavaScript**, you can open a **JavaScript programming block**, and the code inside will be converted directly to JavaScript and placed in the converted code without alteration.
- If the HTVM code contains loops or other structures, the programming block code stays intact in the appropriate place during conversion.
3. **Multiple Programming Blocks**: You can use **multiple programming blocks** for different target languages, but they will be context-sensitive:
- **For JavaScript**: The **Python programming block** will be ignored during conversion.
- **For Python**: The **JavaScript programming block** will be ignored.
- In other words, only the relevant programming block for the target language will be included in the final code.
Example:
- **JavaScript block** in HTVM:
```htvm
// Open JavaScript block
___js start
console.log("Hello, world!");
___js end
// End JavaScript block
```
- **Python block** in HTVM:
```htvm
// Open Python block
___py start
print("Hello, world!")
___py end
// End Python block
```
When converting to **JavaScript**, the Python block will disappear, and only the JavaScript block will be included.
4. **Reverse Programming Blocks**:
- You can also open an **HTVM programming block** within other target language code (e.g., C++). The block will then be converted into HTVM-compatible code.
- For example, if you're writing in C++ and need to use HTVM-specific functionality, you can insert an HTVM block, and it will be transpiled into C++ when you convert the code.
- The process works by specifying the **target file** (e.g., `main.cpp`), and the HTVM block inside it will be converted based on the file's extension.
5. **Universal Programming Block**:
- If two languages share the same syntax for a particular construct, HTVM allows you to use a **universal programming block**. This block can handle situations where HTVM cannot achieve the desired behavior but the syntax matches in multiple languages.
- This is useful when the target languages have compatible syntax for specific code constructs but HTVM doesn't support them directly.
6. **Conversion and Functionality**:
- During conversion, HTVM ensures that only the relevant programming block is preserved based on the target language. Unused blocks are omitted.
- When converting code, HTVM will automatically include any necessary built-in functions or libraries for the target language, displaying them in the console.
### Key Advantages of Programming Blocks:
- **Flexibility**: Allows mixing HTVM with other languages for complex or specialized tasks.
- **Modularity**: Enables using language-specific libraries or features when HTVM cannot do the job.
- **Customizability**: Supports a wide range of languages, with options to adjust and adapt as needed.
- **Seamless Conversion**: Code stays in place, and HTVM ensures proper language-specific syntax is followed.
---
### Key Takeaways:
- **Programming blocks** allow writing code in another language directly within HTVM, which will be preserved during conversion to that language.
- Multiple programming blocks can be used, but only the relevant block will be included based on the target language.
- **Reverse programming blocks** allow you to insert HTVM code into other target language code.
- The **universal programming block** enables handling languages with similar syntax for specific constructs.
#### **And More...**
- Go to [HTVM's website](https://themaster1127.github.io/HTVM) to explore all the syntax customizability and create your own language.
### **The Power of HTVM – Beyond Anything Else**
This isn’t just some hobby project. **HTVM Version 2 took many months just for the parser.** And that was with prior experience from HTVM Version 1 and all the work put into previous languages like HT++, HTH, and HTpy. Every single language had to be carefully mapped out—loops, if-statements, try/catch blocks, functions, variables, types—**every detail had to be considered, tested, and tweaked.**
And here’s the thing: **HTVM doesn’t just output to one language at a time—it can compile to multiple languages simultaneously.** That means you can write a single HTVM file and have it generate both JavaScript and Python code at the same time. That alone is something **almost no other tool in the world can do.**
---
### **Programming Blocks – Total Control Over Your Code**
And then we have **Programming Blocks.** These allow you to **write native code for the target language directly inside HTVM files**. So if you’re converting to JavaScript and need something that HTVM doesn’t handle natively, you can just open a **JavaScript Programming Block**, write JavaScript inside it, and it will only be included when compiling to JavaScript.
But it gets even crazier—**you can have multiple programming blocks for different languages in the same file.** That means you can mix HTVM, JavaScript, Python, and C++ **all in one place** and still have everything work correctly.
And if that wasn’t enough, there are **Reverse Programming Blocks**—which allow for even more insane levels of customization. Even if nobody uses them, **HTVM is so complete that the feature is still there.**
---
### **The Future of Programming**
HTVM **isn’t just a transpiler—it’s the future of programming.**
This is a system that allows **anyone** to create their own syntax, define how they want their language to behave, and then generate fully functional code in fifteen different programming languages. No more being stuck with a single syntax or workflow—**HTVM lets you build programming your way.**
Think about it. If you had to manually convert HTVM to fifteen different languages, it would take months just for a single feature. But **HTVM automates that entire process**—**once your syntax is defined, your language works across all targets instantly.**
### **HTVM – The Future of Programming**
This level of customization **doesn’t exist anywhere else.** No other system gives you this much control over how your language looks and feels. You’re not just writing code—you’re **designing your own programming experience.**
HTVM isn’t just powerful—it’s **insane.** This is how programming should be.
**HTVM doesn’t force you into a fixed way of coding—it lets you create your own language and then outputs it into real programming languages.**
HTVM **isn’t just another programming tool. It’s a revolution.**
---
# OSP (Ordinal Struct Programming)
[Go back](#htvm-documentation)
OSP is HTVM's built-in system for organizing data using **structs** and **instances**. It provides a clean, developer-friendly way to define data structures and create multiple independent copies of them with minimal syntax.
---
## Table of Contents
1. [Structs: The Basics](#structs-the-basics)
2. [Type Inference](#type-inference)
3. [Instances: Creating Data](#instances-creating-data)
4. [Arrays in Structs](#arrays-in-structs)
5. [Organization with `crew` and `alliance`](#organization-with-crew-and-alliance)
6. [Syntax Styles: New vs. Old](#syntax-styles-new-vs-old)
7. [The Formatter (Optional Tool)](#the-formatter-optional-tool)
8. [Complete Examples](#complete-examples)
9. [Common Patterns](#common-patterns)
---
## Structs: The Basics
A `struct` is a container for related data. You define it once, then create multiple independent **instances** of it.
**Basic struct definition:**
```htvm
struct Player
health: 100
mana: 50
subout
```
This defines a struct named `Player` with two properties: `health` and `mana`, both initialized to their respective values.
**Key rule:** Always close a struct with `subout` (curly braces are `NOT` allowed for the new syntax with `subout`).
---
## Type Inference
When you use the new syntax (colon-style with `subout`), HTVM automatically infers types from the initial values:
- **`int`** ← inferred from numeric values (e.g., `100`, `50`)
- **`str`** ← inferred from string values (e.g., `"hello"`)
- **`float`** ← inferred from floating-point values (e.g., `3.14`, `2.5`)
**Examples of inference:**
```htvm
struct Character
name: "Warrior" // Inferred as str
level: 5 // Inferred as int
experience: 1250.75 // Inferred as float
subout
```
### What About Booleans and Arrays?
**Booleans and arrays are NOT inferred.** You must use the `prop` keyword explicitly for these types:
```htvm
struct Player
health: 100 // Inferred as int
prop bool isAlive // Must use prop for bool
prop arr str inventory // Must use prop for arrays
subout
```
---
## Instances: Creating Data
⚠️ **CRITICAL BEFORE YOU START:** If you plan to use `instance` anywhere in your code, **NEVER use `crew` or `alliance` in that project.** Mixing them causes bugs. You must choose one approach: either instances with standalone structs, OR crews/alliances for organization (no instances at all).
An **instance** is an independent copy of a struct with its own data. You create instances using the `instance` keyword:
**Basic instance creation:**
```htvm
struct Player
health: 100
mana: 50
subout
main
instance Player as hero
instance Player as goblin
hero.health = 200
goblin.health = 30
print("Hero health: " . STR(hero.health))
print("Goblin health: " . STR(goblin.health))
```
**How it works:**
- `instance Player as hero` creates a new variable `hero` with all of `Player`'s properties
- Each instance is **completely independent** — modifying `hero.health` does NOT affect `goblin.health`
- All instances are **globally accessible** by their name (below the point of creation)
- Instance names become global variables that hold the struct's data
---
## Arrays in Structs
Arrays must be declared using the `prop` keyword. You cannot use type inference for arrays.
**Declaring array properties:**
```htvm
struct Inventory
prop arr str items
prop arr int quantities
subout
main
instance Inventory as backpack
backpack.items.push("sword")
backpack.items.push("shield")
backpack.quantities.push(1)
backpack.quantities.push(1)
```
**Array methods:**
- **`.push(value)`** or **`.push(value)`** — Add an item to the array (both work the same)
- **`.size()`** — Get the array length
- **`[index]`** — Access element by index (zero-based)
**Complete array example:**
```htvm
struct Player
health: 100
prop arr str spells
subout
main
instance Player as wizard
wizard.spells.push("Fireball")
wizard.spells.push("Ice Storm")
wizard.spells.push("Lightning")
Loop, % wizard.spells.size() {
print(wizard.spells[A_Index])
}
```
---
## Organization with `crew` and `alliance`
For larger projects, you can organize structs using `crew` and `alliance` containers:
- **`alliance`** — Top-level organizational container
- **`crew`** — Sub-container within an alliance (or standalone)
**Purpose:** Group related structs logically and avoid naming conflicts.
**Important:** Crew and alliance use `{}` syntax, not `subout`.
**Using `crew`:**
```htvm
crew Audio {
struct Settings
volume: 80
pitch: 1.0
subout
}
main
// Access the struct's default properties directly
Audio.Settings.volume = 100
print("Audio volume: " . STR(Audio.Settings.volume))
```
**Using `alliance`:**
```htvm
alliance Game {
crew Entities {
struct Player
health: 100
mana: 50
subout
}
crew UI {
struct Button
x: 0
y: 0
label: "Click me"
subout
}
}
main
// Access struct properties using their full path
Game.Entities.Player.health = 200
Game.UI.Button.label = "Start Game"
print("Player health: " . STR(Game.Entities.Player.health))
```
### ⚠️ CRITICAL: Never Mix Crew/Alliance with Instances
**If you use `instance` in your code, you CANNOT use `crew` or `alliance` anywhere in that same project.** This applies even to standalone structs. Mixing them causes bugs.
You must choose ONE approach:
- **Option A:** Use standalone structs + instances (NO crews/alliances anywhere)
- **Option B:** Use crews/alliances for organization (NO instances anywhere)
**This causes bugs:**
```htvm
crew Audio {
struct Settings
volume: 80
subout
}
struct Player
health: 100
subout
main
// DON'T mix them like this
instance Player as hero // This will cause bugs if you have crew/alliance
Audio.Settings.volume = 100
```
**Option A (instances only):**
```htvm
struct Player
health: 100
subout
struct Settings
volume: 80
subout
main
instance Player as hero
instance Settings as config
```
**Option B (crew/alliance only):**
```htvm
crew Audio {
struct Settings
volume: 80
subout
}
crew Game {
struct Player
health: 100
subout
}
main
// Access properties directly, no instances
Game.Player.health = 200
Audio.Settings.volume = 100
```
---
## Syntax Styles: New vs. Old
HTVM supports two syntax styles. You can use them separately or mix them in the same struct.
### New Style (Inference with `subout`):
```htvm
struct Player
health: 100
mana: 50
name: "Hero"
subout
```
Clean, minimal, types inferred automatically.
### Old Style (Explicit with `prop` and `{}`):
```htvm
struct Player {
prop int health = 100
prop int mana = 50
prop str name = "Hero"
}
```
Verbose, but explicit types.
### Mixed Style:
```htvm
struct Player
health: 100
name: "Hero"
prop bool isAlive
prop arr str inventory
subout
```
Use inference for simple types, `prop` for complex ones.
---
## The Formatter (Optional Tool)
The formatter is a **code beautification tool**. It is **completely optional** and does NOT affect compilation.
**What the formatter does:**
- Converts new syntax (`health: 100` with `subout`) to old syntax (`prop int health = 100` with `{}`)
- Adds explicit types to all properties
- Removes indentation from `main` blocks (standard formatting)
- Keeps instance declarations clean and unchanged