-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkey.html
More file actions
1008 lines (830 loc) · 36.1 KB
/
Copy pathkey.html
File metadata and controls
1008 lines (830 loc) · 36.1 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>
<head>
<meta charset="UTF-8">
<title>KeyLang: A Scripting Language in the Key of C</title>
<style type="text/css">
code { background: #def; font-family: inherit; }
var { font-style: italic; }
h2 { margin-top: 2em; }
h3 { margin-top: 1.5em; }
h3 code { font-family: inherit; }
div.example { margin: 0 2em; }
pre {
margin: .5em 0;
padding: .2em .5em;
background: #efd;
border: 1px solid #8f8;
}
dl { margin: 0 2em; padding: 0; }
dt {
margin: 0;
padding: .2em .5em;
background: #def;
border: 1px solid #bdf;
border-bottom: 0;
}
dt code { font-weight: bold; }
dd {
margin: 0 0 .5em 0;
padding: 0 2em;
border: 1px solid #bdf;
}
p { margin: 1em; }
dl p { margin: 1em 0; }
li { margin: 1em; }
.enum {
border-spacing: 1em 0;
margin: 1em;
}
.enum th { text-align: left; }
.oper {
border-spacing: 0 0;
margin: 0 2em;
border: 1px solid #bdf;
}
.oper th {
padding: .2em .5em;
text-align: left;
background: #def;
border-top: 1px solid #bdf;
}
.oper td {
padding: .2em .5em;
}
</style>
</head>
<body>
<h1>KeyLang: A Scripting Language in the Key of C</h1>
<h2>Overview</h2>
<p>KeyLang was designed to be small, simple, and easy to integrate and use. No
attempts have been made to support any of the latest whiz-bang paradigms or
theoretical stuff, just the basics. The primary design goal was to avoid malloc
or GC during script execution.</p>
<p>Key features of KeyLang:</p>
<ul>
<li>Simple syntax, similar to C, C++, Java, Javascript, etc., etc. (Well,
not similar to C++. Nobody wants to be similar to C++.)</li>
<li>No run-time malloc or GC. The only allocations are during compilation
or by user-defined functions.</li>
<li>No objects. No closures. No co-routines. No clever, mathy, or weird
stuff. Just good old-fashioned programming.</li>
<li>No arrays, hash-maps, collections, lists, or sequences. No file I/O.
You can add your own.</li>
<li>No threading support, but scripts can be paused and resumed, which is
close enough.</li>
<li>Dynamic yet strict typing. Only integers, strings, and undefined are
built-in.</li>
<li>Strings are atomic, immutable, interned, and mostly encoding-agnostic.
(Anything consistent with ASCII, including UTF-8.)</li>
<li>Anonymous function expressions, but no closures.</li>
<li>No FFI, but easy-enough integration with native code and data.</li>
<li>User-defined functions and types. Syntactic support for accessing members
and methods of compound types.</li>
<li>Easy to get up and running; the API isn't full of constructors and options
for stuff that will end up being the same in every program anyway.</li>
<li>No actual boolean type, but undefined, zero, the empty string
(<code>""</code>), and <code>NULL</code> custom types are false.</li>
<li>Great for video games. (Hopefully. I haven't actually used this for
anything yet.)</li>
</ul>
<h2>The Key Language</h2>
<h3>Tokens</h3>
<p><b>Comments:</b> Block <code>/*...*/</code> and line
<code>//...\n</code>.</p>
<p><b>Integers:</b> Consistent with <code>strtol</code>: decimal, hexadecimal,
and octal. Most likely 32-bit and signed.</p>
<p><b>Strings:</b> Double-quoted or single-quoted. The only escapes recognized
are newlines (<code>\r</code>, <code>\n</code>), tab (<code>\t</code>),
literals (<code>\\</code>), and embedded newlines (<code>\<newline></code>
is dropped from the string). No numeric escapes are recognized; are they even
needed?</p>
<p><b>Identifiers:</b> Letters, digits, underscore (<code>_</code>), and any
extended characters (Unicode or whatever). Identifiers cannot start
with a digit.</p>
<p><b>Keywords:</b> <code>break</code>, <code>continue</code>, <code>do</code>,
<code>else</code>, <code>fn</code>, <code>for</code>, <code>if</code>,
<code>is</code>, <code>isnot</code>, <code>return</code>, <code>typeof</code>,
<code>undef</code>, <code>var</code>, <code>while</code>.</p>
<p><b>Type names:</b> <code>undef</code>, <code>int</code>,
<code>string</code>, <code>native</code> (C function),
<code>function</code> (script function).</p>
<p><b>Operators:</b> All of them, and then some. Have a precedence table:</p>
<table class="oper">
<tr><th>Operator</th><th>Associativity</th><th>Description</th><th>Types</th></tr>
<tr><th colspan="4">Primary</th></tr>
<tr><td><code>( )</code></td><td>l-to-r</td><td>sub-expression</td><td>any</td></tr>
<tr><td><code>{ }</code></td><td>l-to-r</td><td>object literal</td><td>any</td></tr>
<tr><td><code>[ ]</code></td><td>l-to-r</td><td>array literal</td><td>any</td></tr>
<tr><th colspan="4">Postfix Member</th></tr>
<tr><td><code>[ ]</code></td><td>l-to-r</td><td>subscript</td><td>any</td></tr>
<tr><td><code>.</code></td><td>l-to-r</td><td>member</td><td>any</td></tr>
<tr><th colspan="4">Postfix</th></tr>
<tr><td><code>( )</code></td><td>l-to-r</td><td>function call</td><td>native, function</td></tr>
<tr><td><code>++</code></td><td>l-to-r</td><td>post-increment</td><td>int</td></tr>
<tr><td><code>--</code></td><td>l-to-r</td><td>post-decrement</td><td>int</td></tr>
<tr><th colspan="4">Prefix</th></tr>
<tr><td><code>~</code></td><td>r-to-l</td><td>bitwise not</td><td>int</td></tr>
<tr><td><code>!</code></td><td>r-to-l</td><td>logical not</td><td>any</td></tr>
<tr><td><code>+</code></td><td>r-to-l</td><td>no operation</td><td>any</td></tr>
<tr><td><code>-</code></td><td>r-to-l</td><td>negation</td><td>int</td></tr>
<tr><td><code>?</code></td><td>r-to-l</td><td>is defined</td><td>any</td></tr>
<tr><td><code>typeof</code></td><td>r-to-l</td><td>type of value as a string</td><td>any</td></tr>
<tr><td><code>++</code></td><td>r-to-l</td><td>pre-increment</td><td>int</td></tr>
<tr><td><code>--</code></td><td>r-to-l</td><td>pre-decrement</td><td>int</td></tr>
<tr><th colspan="4">Infix Multiplicative</th></tr>
<tr><td><code>*</code></td><td>l-to-r</td><td>multiplication</td><td>int</td></tr>
<tr><td><code>/</code></td><td>l-to-r</td><td>division</td><td>int</td></tr>
<tr><td><code>%</code></td><td>l-to-r</td><td>modulus</td><td>int</td></tr>
<tr><th colspan="4">Infix Additive</th></tr>
<tr><td><code>+</code></td><td>l-to-r</td><td>addition</td><td>int</td></tr>
<tr><td><code>-</code></td><td>l-to-r</td><td>subtraction</td><td>int</td></tr>
<tr><th colspan="4">Infix Bitwise</th></tr>
<tr><td><code>^</code></td><td>l-to-r</td><td>bitwise exclusive-or</td><td>int</td></tr>
<tr><td><code>&</code></td><td>l-to-r</td><td>bitwise and</td><td>int</td></tr>
<tr><td><code>|</code></td><td>l-to-r</td><td>bitwise inclusive-or</td><td>int</td></tr>
<tr><td><code><<</code></td><td>l-to-r</td><td>shift left</td><td>int</td></tr>
<tr><td><code>>></code></td><td>l-to-r</td><td>signed shift right</td><td>int</td></tr>
<tr><td><code>>>></code></td><td>l-to-r</td><td>unsigned shift right</td><td>int</td></tr>
<tr><th colspan="4">Infix Comparison</th></tr>
<tr><td><code>==</code></td><td>l-to-r</td><td>equal</td><td>any</td></tr>
<tr><td><code>!=</code></td><td>l-to-r</td><td>not equal</td><td>any</td></tr>
<tr><td><code><</code></td><td>l-to-r</td><td>less than</td><td>int, string</td></tr>
<tr><td><code><=</code></td><td>l-to-r</td><td>less than or equal</td><td>int, string</td></tr>
<tr><td><code>></code></td><td>l-to-r</td><td>greater than</td><td>int, string</td></tr>
<tr><td><code>>=</code></td><td>l-to-r</td><td>greater than or equal</td><td>int, string</td></tr>
<tr><td><code>is</code></td><td>l-to-r</td><td>is of a type</td><td>any</td></tr>
<tr><td><code>isnot</code></td><td>l-to-r</td><td>is not of a type</td><td>any</td></tr>
<tr><th colspan="4">Infix Logical (Short-Circuiting)</th></tr>
<tr><td><code>&&</code></td><td>l-to-r</td><td>logical and</td><td>any</td></tr>
<tr><td><code>||</code></td><td>l-to-r</td><td>logical inclusive-or</td><td>any</td></tr>
<tr><td><code>??</code></td><td>l-to-r</td><td>coalesce (evaluate right-hand side if left is undefined)</td><td>any</td></tr>
<tr><th colspan="4">Ternary</th></tr>
<tr><td><code>? :</code></td><td>l-to-r</td><td>conditional</td><td>any</td></tr>
<tr><th colspan="4">Assignment</th></tr>
<tr><td><code>=</code></td><td>r-to-l</td><td>assignment</td><td>any</td></tr>
<tr><td><code>+=</code></td><td>r-to-l</td><td>addition assignment</td><td>int</td></tr>
<tr><td><code>-=</code></td><td>r-to-l</td><td>subtraction assignment</td><td>int</td></tr>
<tr><td><code>*=</code></td><td>r-to-l</td><td>multiplication assignment</td><td>int</td></tr>
<tr><td><code>/=</code></td><td>r-to-l</td><td>division assignment</td><td>int</td></tr>
<tr><td><code>%=</code></td><td>r-to-l</td><td>modulus assignment</td><td>int</td></tr>
<tr><td><code>^=</code></td><td>r-to-l</td><td>bitwise exclusive-or assignment</td><td>int</td></tr>
<tr><td><code>&=</code></td><td>r-to-l</td><td>bitwise and assignment</td><td>int</td></tr>
<tr><td><code>&&=</code></td><td>r-to-l</td><td>logical and assignment (replace value if current value is truthy)</td><td>any</td></tr>
<tr><td><code>|=</code></td><td>r-to-l</td><td>bitwise inclusive-or assignment</td><td>int</td></tr>
<tr><td><code>||=</code></td><td>r-to-l</td><td>logical inclusive-or assignment (replace value if current value is falsey)</td><td>any</td></tr>
<tr><td><code><<=</code></td><td>r-to-l</td><td>shift left assignment</td><td>int</td></tr>
<tr><td><code>>>=</code></td><td>r-to-l</td><td>signed shift right assignment</td><td>int</td></tr>
<tr><td><code>>>>=</code></td><td>r-to-l</td><td>unsigned shift right assignment</td><td>int</td></tr>
<tr><td><code>??=</code></td><td>r-to-l</td><td>coalesce assignment (replace value if current value is undefined)</td><td>any</td></tr>
<tr><th colspan="4">Comma</th></tr>
<tr><td><code>,</code></td><td>l-to-r</td><td>expression separator</td><td>any</td></tr>
</table>
<p>There's also the rest-argument operator (<code>...</code>) which doesn't
have precedence and is only valid following the last identifier in a function
definition's parameter list.</p>
<p>(Note: You can use <code>?</code> with <code>!</code> for an “is
undefined” operator like so: <code>!?<var>expr</var></code>; I call
this the “wtf” operator because “<em>why the — is it
undefined!?”)</em></p>
<p><b>Custom operators:</b> Several syntactic constructs are supported via
special functions, either native or script. These functions are listed in
their own section.</p>
<h3>Grammar</h3>
<p>Examples:</p>
<div class="example">
<pre>
var foo = 5;
var bar = "happy times!";
fn doTheThing(a,b,c) {
var d = a + b;
return c * d;
}
fn stuff(a) return a + 5;
var baz = 3 + 5;
</pre>
</div>
<p>Statement types include:</p>
<dl>
<dt><code>if</code> (<var>expr</var>) <var>statement</var><br>
<code>if</code> (<var>expr</var>) <var>statement</var> <code>else</code>
<var>statement</var></dt>
<dd>
<p>If the <var>expr</var> is not false, the first <var>statement</var> will
be evaluated; otherwise, the second <var>statement</var> will be evaluated.</p>
</dd>
<dt><code>break</code>;<br>
<code>continue</code>;</dt>
<dd>
<p>Only allowed inside a <code>while</code> loop. <code>break</code> exits
the loop; <code>continue</code> jumps back to the top, re-evaluating the
loop's condition.</p>
</dd>
<dt>{ }<br>
{ <var>statements</var> }</dt>
<dd>
<p>Curly braces are used to group a set of statements together as a single
statement. Useful for <code>if</code> and <code>while</code>, and pretty
much required for functions. (Single-statement functions don't need the
curly braces.)</p>
</dd>
<dt><code>return</code>;<br>
<code>return</code> <var>expr</var>;</dt>
<dd>
<p>Return a value from a function. The <var>expr</var> is optional; if
a value is not given, the function returns <code>undef</code>.</p>
</dd>
<dt><code>var</code> <var>ident</var>;<br>
<code>var</code> <var>ident</var> = <var>expr</var>;<br>
<code>var</code> <var>ident</var>, <var>ident</var> = <var>expr</var>;</dt>
<dd>
<p>Declare locally-scoped variables inside a function; or, if at top-level,
file-scoped variables.</p>
<p>The <code>var</code> statement may contain one or more identifiers,
with optional values, separated by commas. Variables must be declared
before use, and cannot be declared multiple times.</p>
</dd>
<dt>
<code>for</code> ( ; ; ) <var>statement</var><br>
<code>for</code> (<var>expr</var>; <var>expr</var>; <var>expr</var>)
<var>statement</var><br>
<code>do</code> <var>statement</var> <code>while</code> (<var>expr</var>);<br>
<code>while</code> (<var>expr</var>) <var>statement</var></dt>
<dd>
<p>Loops. As long as the condition is true, evaluate <var>statement</var>.</p>
</p>
</dd>
<dt>;<br>
<var>expr</var>;</dt>
<dd>
<p>Evaluate the expression, or do nothing.</p>
</dd>
<dt><code>fn</code> <var>ident</var>() <var>statement</var><br>
<code>fn</code> <var>ident</var>(<var>arg1</var>,<var>arg2</var>)
<var>statement</var><br>
<code>fn</code> <var>ident</var>(<var>arg1</var>,<var>arg2</var>...)
<var>statement</var></dt>
<dd>
<p>Declare a function with name <var>ident</var>. Functions can have any
number of parameters. Functions can be declared inside of other functions
and can also be declared with or without a name inside an expression (similar
to Javascript) but do not have access to the enclosing lexical scope
(i.e., no closures).</p>
<p>A function may be called with any number of arguments. Missing parameters
will be set to <code>undef</code>. If the last parameter is specified as a
rest-argument (<code>...</code>), its value depends on the return value of
the special function <code>__array__</code>, which will be called every time
the function is called, regardless of the number of arguments passed to it.
An example of rest-arguments:</p>
<div class="example">
<pre>
fn one(a...) { return a; }
fn rest(a,b,c...) { return c; }
one(); // []
one(1); // [1]
one(1,2); // [1,2]
rest(1,2); // []
rest(1,2,3); // [3]
rest(1,2,3,4); // [3,4]
</pre>
</div>
<p>If the special function <code>__array__</code> is not defined, then calling
a function taking rest-arguments will result in a runtime error (specifically,
an error about <code>__array__</code> being undefined).</p>
</dd>
</dl>
<h3>Variable Scope</h3>
<p>Values and functions defined through the <code>key_global_*</code> functions
are available to all script environments. Values declared at top-level in a
file are file-scoped. Values declared inside a function are locally-scoped to
that function. There is no block-level scope.</p>
<p>A function defined inside another function does not have access to
variables in the containing function (i.e., no closures).</p>
<p>Names are not allowed to shadow names in outer scopes. A locally-scoped
variable cannot have the same name as a file-scoped variable, and a file-scoped
variable cannot have the same name as a globally-scoped variable. Nested
functions may shadow names defined in the enclosing function because the
enclosing function's scope isn't available to the nested function.</p>
<div class="example">
<pre>
// 'global_foo' is defined globally
var foo = 5;
fn bar(foo) { // error: 'foo' redeclared
var a = "local";
fn stuff() {
var a = 5; // okay
var foo = 4; // error
var global_foo; // error
}
}
</pre>
</div>
<h3>Default Functions</h3>
<p>The default functions are only provided if <code>key_global_defaults</code>
is called. The supporting native functions are available for defining directly
under other names.</p>
<dl>
<dt>fn <code>fail</code>(...);</dt>
<dd>
<p>Prints all arguments to <code>key_errmsg</code> then stops the script's
execution, returning the error to the script's caller.</p>
</dd>
<dt>fn <code>debug</code>(...);</dt>
<dd>
<p>Prints all arguments to <code>stderr</code>, followed by a newline.</p>
</dd>
<dt>fn <code>trace</code>();</dt>
<dd>
<p>Prints the current stack trace to <code>stderr</code>.</p>
</dd>
<dt>fn <code>pause</code>();</dt>
<dd>
<p>Suspend execution of the script.</p>
</dd>
</dl>
<h3>Special Functions</h3>
<p>Special functions provide syntactic support for custom compound types,
and may be implemented as script functions or as native functions. These
are not defined by <code>key_global_defaults</code>.</p>
<dl>
<dt>fn <code>__member__</code>(<var>object</var>, <var>member</var>,
<var>value</var>);<br>
keyret <code>__member__</code>(keyenv * <var>e</var>, keyval * <var>rv</var>,
int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Retrieve or assign a member value in an object. If the function receives
three arguments, then the assignment form is being invoked. Either way, the
value of the member must be returned. This function handles subscript
(<code>[ ]</code>, <code>[ ] =</code>) and member
(<code>.</code>, <code>. =</code>) operators; there is no way
to tell the difference between the two syntaxes.</p>
<p>Compund assignments (<code>+=</code>, etc.) and the increment operators
are handled by the scripting language, with the final simple assignment
performed by the <code>__member__</code> function.</p>
</dd>
<dt>fn <code>__array__</code>(<var>value</var>, ...);<br>
keyret <code>__array__</code>(keyenv * <var>e</var>, keyval * <var>rv</var>,
int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Create an array from an array literal expression
(<code>[ ]</code>) or from rest-arguments.</p>
<p>When called to handle rest-arguments, <code>__array__</code> only receives
the values that should be collected into the rest-argument. If not enough
arguments are passed to the rest-argument function, <code>__array__</code>
will receive no arguments (<code><var>argc</var></code> will be 0). There
is no way for the <code>__array__</code> function to tell if it's being
called to handle rest-arguments or an array literal.</p>
</dd>
<dt>fn <code>__object__</code>(<var>key</var>, <var>value</var>, ...);<br>
keyret <code>__object__</code>(keyenv * <var>e</var>, keyval * <var>rv</var>,
int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Create an object from an object literal expression (<code>{ }</code>).
The keys may be of any type, including <code>undef</code>. There is no way to
tell the difference between a string key and an identifier key. There is
always an even number of arguments.</p>
</dd>
</dl>
<h3>Notes</h3>
<p>Conversion to boolean type: Undefined, zero, the empty string
(<code>""</code>), and custom types of value <code>NULL</code> are false.
Anything else is true.</p>
<p>Boolean results are represented by the integers 0 (false) or 1 (true).</p>
<h2>The C API</h2>
<p>The <code>key.h</code> header includes <code>stddef.h</code> because I like
<code>size_t</code>. There's also a certain joy in knowing <code>NULL</code>
is defined. (Don't worry, dearest macro; my love is as constant as thou.)</p>
<h3>Types</h3>
<dl>
<dt>typedef enum keyret <code>keyret</code>;</dt>
<dd>
<p>An enumeration returned by various functions, indicating error and
continuation status.</p>
<table class="enum">
<tr><th>KEY_OK</th>
<td>The operation completed successfully.</td>
</tr>
<tr><th>KEY_PAUSE</th>
<td>The script is done for now but can be continued later. (Only returned from
<code>keyenv_run</code> and native functions.)</td>
</tr>
<tr><th>KEY_ERROR</th>
<td>There was an error. A descriptive message will be in
<code>key_errmsg</code>.</td>
</tr>
</table>
</dd>
<dt>typedef enum keytype <code>keytype</code>;</dt>
<dd>
<p>An enumeration containing the type of the value stored in the
<code>type</code> member of a <code>keyval</code>. New types can
be added by calling <code>keyval_custom_type</code>.</p>
<table class="enum">
<tr><th>KEY_UNDEF</th>
<td>The value is undefined.</td>
</tr>
<tr><th>KEY_INT</th>
<td>The value is an integer, which can be found in the <code>ival</code>
member of the <code>keyval</code>.</td>
</tr>
<tr><th>KEY_STR</th>
<td>The value is an interned string, which can be found in the <code>sval</code>
member of the <code>keyval</code>.</td>
</tr>
</table>
</dd>
<dt>typedef struct keyval <code>keyval</code>;</dt>
<dd>
<p>A tagged union treated as a value type, containing the various types and
their values. Members include:</p>
<table class="enum">
<tr><th>keytype type;</th>
<td>The type of the value.</td>
</tr>
<tr><th>int ival;</th>
<td>An integer value, if <code>type</code> is <code>KEY_INT</code>.</td>
</tr>
<tr><th>const char * sval;</th>
<td>A string, most likely interned, if <code>type</code> is
<code>KEY_STR</code>.</td>
</tr>
<tr><th>void * pval;</th>
<td>A pointer to a value of custom type, if <code>type</code> is a custom
type handle.</td>
</tr>
</table>
</dd>
<dt>typedef struct keyenv <code>keyenv</code>;</dt>
<dd>
<p>A script environment, containing the symbols defined in a script file
and the execution context information. Only one thread of execution is
allowed per file, but interrupt (signal-like) functions may be invoked.</p>
</dd>
<dt>typedef keyret (*<code>key_native_fn</code>)(keyenv * <var>e</var>,
keyval * <var>rv</var>, int <var>argc</var>, const keyval *
<var>argv</var>);</dt>
<dd>
<p>The signature of native callback functions. The environment of the
running script is in <code>e</code>. The return value of the function
or operator is stored in the <code>keyval</code> pointed to by
<code><var>rv</var></code>. The arguments to the function or operator are in
<code><var>argv</var></code> which is <code><var>argc</var></code> members
in length. The first argument is in <code><var>argv</var>[0]</code>.</p>
<p>If a function is assigned to a member of a custom object and is called
as a method, the custom object will be passed as the first argument, which
is consistent with Uniform Function Call Syntax.</p>
<p>The function may return <code>KEY_PAUSE</code> to pause execution
of the script or <code>KEY_ERROR</code> to stop. If everything
finished successfully, <code>KEY_OK</code> should be returned.</p>
</dd>
</dl>
<h3>Global Variables</h3>
<dl>
<dt>char <code>key_errmsg</code>[];</dt>
<dd>
<p>A short message describing the most recent error. At least it's not
<em>exactly</em> like <code>errno</code>.</p>
</dd>
</dl>
<h3>Macros</h3>
<dl>
<dt>#define <code>keyval_undef</code>()</dt>
<dd>
<p>Returns a <code>keyval</code> of type <code>KEY_UNDEF</code>.</p>
</dd>
<dt>#define <code>keyval_int</code>(<var>V</var>)</dt>
<dd>
<p>Returns a <code>keyval</code> of type <code>KEY_INT</code>
with <code>ival</code> value <code><var>V</var></code>.</p>
</dd>
<dt>#define <code>keyval_str</code>(<var>V</var>)</dt>
<dd>
<p>Returns a <code>keyval</code> of type <code>KEY_STR</code>
with <code>sval</code> value <code><var>V</var></code>. Interning the
string before passing it to this macro is encouraged but not required.</p>
</dd>
<dt>#define <code>keyval_custom</code>(<var>T</var>,<var>V</var>)</dt>
<dd>
<p>Returns a <code>keyval</code> of type <code><var>T</var></code> with
<code>pval</code> value <code><var>V</var></code>.</p>
</dd>
</dl>
<h3>Error Handling Functions</h3>
<dl>
<dt>keyret <code>key_error</code>(const char * <var>fmt</var>, ...);</dt>
<dd>
<p>Record an error in <code>key_errmsg</code> and returns
<code>KEY_ERROR</code>.</p>
</dd>
<dt>keyret <code>key_type_error</code>(keyval <var>a</var>);</dt>
<dd>
<p>Record an error for an object of the incorrect type. Always returns
<code>KEY_ERROR</code>.</p>
</dd>
<dt>keyret <code>key_type_require</code>(keyval <var>a</var>, keytype
<var>type</var>);</dt>
<dd>
<p>Record an error if the object's type is not <code><var>type</var></code>.
Returns <code>KEY_OK</code> if the types match, <code>KEY_ERROR</code> if
not.</p>
</dd>
<dt>keyret <code>key_type_require_fn</code>(keyval <var>a</var>);</dt>
<dd>
<p>Record an error if the object's type is not a script function or
native function. Returns <code>KEY_OK</code> if it is a function,
<code>KEY_ERROR</code> if not.</p>
</dd>
<dt>size_t <code>keyenv_stack_trace</code>(const keyenv * <var>e</var>,
char * <var>buf</var>, size_t <var>buflen</var>);</dt>
<dd>
<p>Write a stack trace for the environment into the buffer. It's just a list
of function names formatted as “<code>in name:\n</code>”.
The message in <code>key_errmsg</code> is not appended nor modified.
<code><var>buf</var></code> may be <code>NULL</code> if
<code><var>buflen</var></code> is 0. The return value is
the number of bytes needed for the resulting string.</p>
</dd>
<dt>void <code>key_print_stats</code>(void);</dt>
<dd>
<p>Print a list of usage statistics to <code>stdout</code>. If the
compile-time option <code>TRACK_USAGES</code> was disabled, this
function will only print the size of the <code>keyenv</code> struct.
If enabled, it will print the amount of string internment room used,
the number of global variables defined, and the maximum stack lengths
for all environments (the environments must have been destroyed with
<code>keyenv_delete</code> for their maximum lengths to be recorded).</p>
</dd>
</dl>
<h3>Type Functions</h3>
<dl>
<dt>keytype <code>keyval_custom_type</code>(const char * <var>name</var>);</dt>
<dd>
<p>Register a new <code>keytype</code> value for use as the <code>type</code>
of a <code>keyval</code>. Returns 0 if the type could not be registered. The
name must be unique among the type names and will be interned.</p>
</dd>
<dt>int <code>keyval_tobool</code>(keyval <var>a</var>);</dt>
<dd>
<p>Converts a <code>keyval</code> to a boolean. Undefined, integer zero,
the empty string, and custom types with value <code>NULL</code> are
false. Everything else is true.</p>
</dd>
<dt>int <code>keyval_equal</code>(keyval <var>a</var>, keyval
<var>b</var>);</dt>
<dd>
<p>Compares two <code>keyval</code> values for equality. No type conversion
is performed. Two arguments of different type never compare equal.
Custom types are compared for reference equality only.</p>
</dd>
</dl>
<h3>String Functions</h3>
<dl>
<dt>keyret <code>key_intern</code>(const char ** <var>dst</var>, const char *
<var>src</var>);</dt>
<dd>
<p>Inserts the string <code><var>src</var></code> into the intern table.
A pointer to the interned string's location will be returned in
<code><var>dst</var></code>. Strings passed to Key functions do
not have to be interned.</p>
</dd>
<dt>size_t <code>key_escape</code>(char * <var>dst</var>, size_t
<var>dstlen</var>, const char * <var>src</var>);</dt>
<dd>
<p>Converts the string <code><var>src</var></code> to have back-slashed escapes
consistent with KeyLang string literals. If <code><var>dstlen</var></code>
is 0, <code><var>dst</var></code> may be <code>NULL</code>. The return value
is the size needed to store the resulting string (i.e., the index of the
terminating nil byte).</p>
</dd>
</dl>
<h3>Environment Functions</h3>
<dl>
<dt>keyret <code>keyenv_new</code>(keyenv ** <var>ep</var>, const char *
<var>src</var>, const char * <var>fname</var>, int <var>line</var>);</dt>
<dd>
<p>Allocate and initialize a new environment with the KeyLang source code
<code><var>src</var></code>. The <code><var>fname</var></code> and
<code><var>line</var></code> arguments are used for error messages during
compilation. <code><var>line</var></code> should be 1 for the first line
of a file.</p>
<p>The contents of the file will not be executed. The top-level of code
(mostly variable and function definitions) will be queued for execution.
<code>keyenv_run</code> should be called until <code>KEY_PAUSE</code> is no
longer returned before performing any other operation on the environment.</p>
<div class="example">
<pre>
keyenv * e;
/* load file */
keyenv_new(&e, "fn main() return 5;", "main.key", 1);
/* define 'main' */
while (keyenv_run(e) == KEY_PAUSE) /**/;
/* call 'main' */
keyenv_push(e, "main", 0, NULL);
while (keyenv_run(e) == KEY_PAUSE) /**/;
</pre>
</div>
</dd>
<dt>void <code>keyenv_delete</code>(keyenv * <var>e</var>);</dt>
<dd>
<p>Deallocate the resources for an environement.</p>
</dd>
<dt>keyret <code>keyenv_run</code>(keyenv * <var>e</var>);</dt>
<dd>
<p>Execute a script until it completes (<code>KEY_OK</code>), encounters
an error (<code>KEY_ERROR</code>), or pauses (<code>KEY_PAUSE</code>).</p>
<p>This function may be used to resume a paused environment. The return
value of the pausing function will be <code>undef</code>.</p>
</dd>
<dt>int <code>keyenv_running</code>(const keyenv * <var>e</var>);</dt>
<dd>
<p>Returns true if the environment has not completed executing. This is true
for environments that have been paused or have encountered an error.</p>
</dd>
<dt>void <code>keyenv_reset</code>(keyenv * <var>e</var>);</dt>
<dd>
<p>Reset the execution context so that it's not in the middle of executing
anything.</p>
</dd>
<dt>keyret <code>keyenv_resume</code>(keyenv * <var>e</var>, keyval
<var>rv</var>);</dt>
<dd>
<p>Return a value to a paused environment. This should only be called on
an evironment that has been paused by returning <code>KEY_PAUSE</code>
from a native function.</p>
</dd>
<dt>keyret <code>keyenv_push</code>(keyenv * <var>e</var>, const char *
<var>name</var>, int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Start a call to a script function in an environment. The environment
must not be running. The function call won't be performed until
<code>keyenv_run</code> is called. This should only be used on
environments that are not currently running.</p>
</dd>
<dt>keyret <code>keyenv_interrupt</code>(keyenv * <var>e</var>, const char *
<var>name</var>, int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Start a call to a script function in an environment. The environment
may be running. The return value will not be available. The function call
won't be performed until <code>keyenv_run</code> is called. Resumed execution
will continue past the interrupt's completion. This is useful for inserting
events into a script environment.</p>
</dd>
<dt>keyret <code>keyenv_callback</code>(keyenv * <var>e</var>, keyval *
<var>rv</var>, keyval <var>func</var>, int <var>argc</var>, const keyval *
<var>argv</var>);</dt>
<dd>
<p>Run a script function or native function to completion. The function cannot
pause. The function is called immediately and its return value is placed into
<code><var>rv</var></code>. This is useful for implementing native functions
that take functions as arguments, such as sorting functions.</p>
</dd>
<dt>keyret <code>keyenv_pop</code>(keyenv * <var>e</var>, keyval *
<var>rv</var>);</dt>
<dd>
<p>Pop a return value from an environment. This should only be called once,
on an environement that has finished executing a function called by
<code>keyenv_push</code>.</p>
</dd>
<dt>int <code>keyenv_has</code>(const keyenv * <var>e</var>, const char *
<var>name</var>);</dt>
<dd>
<p>Returns true if <code><var>name</var></code> is found in the environment.
If the environment is running, the currently active local scope will be checked
before the file scope.</p>
</dd>
<dt>keyret <code>keyenv_get</code>(const keyenv * <var>e</var>, const char *
<var>name</var>, keyval * <var>valp</var>);</dt>
<dd>
<p>Retrieves the value of <code><var>name</var></code>. If the environment
is running, the currently active local scope will be checked before the
file scope.</p>
</dd>
<dt>keyret <code>keyenv_set</code>(keyenv * <var>e</var>, const char *
<var>name</var>, keyval <var>val</var>);</dt>
<dd>
<p>Sets the value of <code><var>name</var></code> if found. If the
environment is running, the currently active local scope will be checked
before the file scope.</p>
</dd>
<dt>keyret <code>keyenv_def</code>(keyenv * <var>e</var>, const char *
<var>name</var>, keyval <var>val</var>);</dt>
<dd>
<p>Defines <code><var>name</var></code> and its value if not already defined.
If the environment is running, <code><var>name</var></code> will be in the
currently active local scope, otherwise <code><var>name</var></code> is
file scope.</p>
</dd>
</dl>
<h3>Global Environment Functions</h3>
<dl>
<dt>int <code>key_global_has</code>(const char * <var>name</var>);</dt>
<dd>
<p>Returns true if <code><var>name</var></code> is defined as a global
value.</p>
</dd>
<dt>keyret <code>key_global_get</code>(const char * <var>name</var>, keyval *
<var>valp</var>);</dt>
<dd>
<p>Retrieves the value of <code><var>name</var></code> if it is defined
as a global value, or an error if it is not found.</p>
</dd>
<dt>keyret <code>key_global_set</code>(const char * <var>name</var>, keyval
<var>val</var>);</dt>
<dd>
<p>Sets the value of <code><var>name</var></code> to
<code><var>val</var></code> if it is defined as a global value,
or returns an error if it is not defined.</p>
</dd>
<dt>keyret <code>key_global_def</code>(const char * <var>name</var>, keyval
<var>val</var>);</dt>
<dd>
<p>Defines the variable <code><var>name</var></code> with the value of
<code><var>val</var></code> if not defined as a global value,
or returns an error if it is already defined.</p>
</dd>
<dt>keyret <code>key_global_fndef</code>(const char * <var>name</var>,
key_native_fn <var>eval</var>);</dt>
<dd>
<p>Define the variable <code><var>name</var></code> to the native function
<code><var>eval</var></code> if not defined as a global value,
or returns an error if it is already defined.</p>
</dd>
<dt>keyret <code>key_global_defaults</code>(void);</dt>
<dd>
<p>Called to define the default function set.</p>
</dd>
</dl>
<h3>Native Functions</h3>
<p>The default native function set. These are all defined by
<code>key_global_defaults</code> but may be defined using
any name with <code>key_global_fndef</code>.</p>
<dl>
<dt>keyret <code>key_default_fail</code>(keyenv * <var>e</var>, keyval *
<var>rv</var>, int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Defined as <code>fail</code> by <code>key_global_defaults</code>.</p>
</dd>
<dt>keyret <code>key_default_debug</code>(keyenv * <var>e</var>, keyval *
<var>rv</var>, int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Defined as <code>debug</code> by <code>key_global_defaults</code>.</p>
</dd>
<dt>keyret <code>key_default_trace</code>(keyenv * <var>e</var>, keyval *
<var>rv</var>, int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Defined as <code>trace</code> by <code>key_global_defaults</code>.</p>
</dd>
<dt>keyret <code>key_default_pause</code>(keyenv * <var>e</var>, keyval *
<var>rv</var>, int <var>argc</var>, const keyval * <var>argv</var>);</dt>
<dd>
<p>Defined as <code>pause</code> by <code>key_global_defaults</code>.</p>
</dd>
</dl>
<h2>Compile-time Options</h2>
<p>These macros are defined and can be redefined in the <code>key.c</code>
file. They are not part of the run-time API.</p>
<dl>
<dt>#define <code>ERROR_BUFLEN</code> (128)</dt>
<dd>
<p>The length of the error message buffer <code>key_errmsg</code>,
in bytes.</p>
</dd>
<dt>#define <code>TOKEN_BUFLEN</code> (1024)</dt>
<dd>
<p>The maximum length of a script token, in bytes. This includes string
literals.</p>
</dd>
<dt>#define <code>INTERN_BUFLEN</code> (16 * 1024)</dt>
<dd>
<p>Storage space for interned string, in bytes.</p>
</dd>
<dt>#define <code>GLOBAL_MAX</code> (128)</dt>
<dd>
<p>Maximum number of global variables and functions that can be defined.</p>
</dd>
<dt>#define <code>OPER_DEPTH</code> (128)</dt>
<dd>
<p>Size of a script environment's operand stack. Bigger numbers allow more
complex expressions.</p>
</dd>
<dt>#define <code>SCOPE_DEPTH</code> (256)</dt>
<dd>
<p>Size of a script environment's file and local scope stack. Bigger
numbers allow deeper nesting of function calls with lots of local
variables.</p>
</dd>
<dt>#define <code>EXEC_DEPTH</code> (128)</dt>
<dd>
<p>Size of the execution stack. Bigger numbers allow deeper nesting of
script function calls and loops.</p>
</dd>
<dt>#define <code>TRACK_USAGES</code> (0)</dt>
<dd>
<p>Enable or disable recording usage statistics for many of the
compile-time options. Call <code>key_print_stats</code> to print