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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Fype Programming Language</title>
<link rel="shortcut icon" type="image/gif" href="/favicon.ico" />
<link rel="stylesheet" href="../style.css" />
<link rel="stylesheet" href="style-override.css" />
</head>
<body>
<p class="header">
<a href="https://foo.zone">Home</a> | <a href="https://codeberg.org/snonux/foo.zone/src/branch/content-md/gemfeed/2010-05-09-the-fype-programming-language.md">Markdown</a> | <a href="gemini://foo.zone/gemfeed/2010-05-09-the-fype-programming-language.gmi">Gemini</a>
</p>
<h1 style='display: inline' id='the-fype-programming-language'>The Fype Programming Language</h1><br />
<br />
<span class='quote'>Published at 2010-05-09T12:48:29+01:00; Updated at 2021-05-05</span><br />
<br />
<span>Fype is an interpreted programming language created by me for learning and fun. The interpreter is written in C. It has been tested on FreeBSD and NetBSD and may also work on other Unix like operating systems such as Linux based ones. Besides learning and fun, there is no other use case of why Fype exists as many other programming languages are much faster and more powerful.</span><br />
<br />
<span>The Fype syntax is straightforward and uses a maximum look ahead of 1 and an effortless top-down parsing mechanism. Fype is parsing and interpreting its code simultaneously. This means that syntax errors are only detected during program runtime. </span><br />
<br />
<span>Fype is a recursive acronym and means "Fype is For Your Program Execution" or "Fype is Free Yak Programmed for ELF". You could also say, "It's not a hype - it's Fype!".</span><br />
<br />
<pre>
____ _ __
/ / _|_ _ _ __ ___ _ _ ___ __ _| |__ / _|_ _
/ / |_| | | | '_ \ / _ \ | | | |/ _ \/ _` | '_ \ | |_| | | |
_ / /| _| |_| | |_) | __/ | |_| | __/ (_| | | | |_| _| |_| |
(_)_/ |_| \__, | .__/ \___| \__, |\___|\__,_|_| |_(_)_| \__, |
|___/|_| |___/ |___/
</pre>
<br />
<h2 style='display: inline' id='table-of-contents'>Table of Contents</h2><br />
<br />
<ul>
<li><a href='#the-fype-programming-language'>The Fype Programming Language</a></li>
<li>⇢ <a href='#object-oriented-c-style'>Object-oriented C style</a></li>
<li>⇢ <a href='#data-types'>Data types</a></li>
<li>⇢ <a href='#syntax'>Syntax</a></li>
<li>⇢ ⇢ <a href='#comments'>Comments</a></li>
<li>⇢ ⇢ <a href='#variables'>Variables</a></li>
<li>⇢ ⇢ <a href='#synonyms'>Synonyms</a></li>
<li>⇢ <a href='#statements-and-expressions'>Statements and expressions</a></li>
<li>⇢ ⇢ <a href='#parenthesis'>Parenthesis</a></li>
<li>⇢ ⇢ <a href='#basic-expressions'>Basic expressions</a></li>
<li>⇢ ⇢ <a href='#bitwise-expressions'>Bitwise expressions</a></li>
<li>⇢ ⇢ <a href='#numeric-expressions'>Numeric expressions</a></li>
<li>⇢ <a href='#control-statements'>Control statements</a></li>
<li>⇢ <a href='#scopes'>Scopes</a></li>
<li>⇢ <a href='#definedness-'>Definedness </a></li>
<li>⇢ <a href='#system-'>System </a></li>
<li>⇢ ⇢ <a href='#io-'>I/O </a></li>
<li>⇢ <a href='#procedures-and-functions'>Procedures and functions</a></li>
<li>⇢ ⇢ <a href='#procedures'>Procedures</a></li>
<li>⇢ ⇢ <a href='#nested-procedures'>Nested procedures</a></li>
<li>⇢ ⇢ <a href='#functions'>Functions</a></li>
<li>⇢ ⇢ <a href='#nested-functions'>Nested functions</a></li>
<li>⇢ <a href='#arrays'>Arrays</a></li>
<li>⇢ <a href='#fancy-stuff'>Fancy stuff</a></li>
<li>⇢ <a href='#may-the-source-be-with-you'>May the source be with you</a></li>
</ul><br />
<h2 style='display: inline' id='object-oriented-c-style'>Object-oriented C style</h2><br />
<br />
<span>The Fype interpreter is written in an object-oriented style of C. Each "main component" has its own .h and .c file. There is a struct type for each (most components at least) component, which can be initialized using a "COMPONENT_new" function and destroyed using a "COMPONENT_delete" function. Method calls follow the same schema, e.g. "COMPONENT_METHODNAME". There is no such as class inheritance and polymorphism involved. </span><br />
<br />
<span>To give you an idea of how it works here as an example is a snippet from the main Fype "class header":</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><b><u><font color="#000000">typedef</font></u></b> <b><u><font color="#000000">struct</font></u></b> {
Tupel *p_tupel_argv; <i><font color="silver">// Contains command line options</font></i>
List *p_list_token; <i><font color="silver">// Initial list of token</font></i>
Hash *p_hash_syms; <i><font color="silver">// Symbol table</font></i>
<b><font color="#000000">char</font></b> *c_basename;
} Fype;
</pre>
<br />
<span>And here is a snippet from the primary Fype "class implementation":</span><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre>Fype*
fype_new() {
Fype *p_fype = malloc(<b><u><font color="#000000">sizeof</font></u></b>(Fype));
p_fype->p_hash_syms = hash_new(<font color="#000000">512</font>);
p_fype->p_list_token = list_new();
p_fype->p_tupel_argv = tupel_new();
p_fype->c_basename = NULL;
garbage_init();
<b><u><font color="#000000">return</font></u></b> (p_fype);
}
<b><font color="#000000">void</font></b>
fype_delete(Fype *p_fype) {
argv_tupel_delete(p_fype->p_tupel_argv);
hash_iterate(p_fype->p_hash_syms, symbol_cleanup_hash_syms_cb);
hash_delete(p_fype->p_hash_syms);
list_iterate(p_fype->p_list_token, token_ref_down_cb);
list_delete(p_fype->p_list_token);
<b><u><font color="#000000">if</font></u></b> (p_fype->c_basename)
free(p_fype->c_basename);
garbage_destroy();
}
<b><font color="#000000">int</font></b>
fype_run(<b><font color="#000000">int</font></b> i_argc, <b><font color="#000000">char</font></b> **pc_argv) {
Fype *p_fype = fype_new();
<i><font color="silver">// argv: Maintains command line options</font></i>
argv_run(p_fype, i_argc, pc_argv);
<i><font color="silver">// scanner: Creates a list of token</font></i>
scanner_run(p_fype);
<i><font color="silver">// interpret: Interpret the list of token</font></i>
interpret_run(p_fype);
fype_delete(p_fype);
<b><u><font color="#000000">return</font></u></b> (<font color="#000000">0</font>);
}
</pre>
<br />
<h2 style='display: inline' id='data-types'>Data types</h2><br />
<br />
<span>Fype uses auto type conversion. However, if you want to know what's going on, you may take a look at the following basic data types:</span><br />
<ul>
<li>integer - Specifies a number</li>
<li>double - Specifies a double-precision number</li>
<li>string - Specifies a string</li>
<li>number - May be an integer or a double number</li>
<li>any- May be any type above</li>
<li>void - No type</li>
<li>identifier - It's a variable name or a procedure name, or a function name</li>
</ul><br />
<span>There is no boolean type, but we can use the integer values 0 for false and 1 for true. There is support for explicit type casting too.</span><br />
<br />
<h2 style='display: inline' id='syntax'>Syntax</h2><br />
<br />
<h3 style='display: inline' id='comments'>Comments</h3><br />
<br />
<span>Text from a # character until the end of the current line is considered being a comment. Multi-line comments may start with an #* and with a *# anywhere. Exceptions are if those signs are inside of strings.</span><br />
<br />
<h3 style='display: inline' id='variables'>Variables</h3><br />
<br />
<span>Variables are defined with the "my" keyword (inspired by Perl :-). If you don't assign a value during declaration, it uses the default integer value 0. Variables may be changed during program runtime. Variables may be deleted using the "undef" keyword! Example:</span><br />
<br />
<pre>
my foo = 1 + 2;
say foo;
my bar = 12, baz = foo;
say 1 + bar;
say bar;
my baz;
say baz; # Will print out 0
</pre>
<br />
<span>You may use the "defined" keyword to check if an identifier has been defined or not:</span><br />
<br />
<pre>
ifnot defined foo {
say "No foo yet defined";
}
my foo = 1;
if defined foo {
put "foo is defined and has the value ";
say foo;
}
</pre>
<br />
<h3 style='display: inline' id='synonyms'>Synonyms</h3><br />
<br />
<span>Each variable can have as many synonyms as wished. A synonym is another name to access the content of a specific variable. Here is an example of how to use it:</span><br />
<br />
<pre>
my foo = "foo";
my bar = \foo;
foo = "bar";
# The synonym variable should now also set to "bar"
assert "bar" == bar;
</pre>
<br />
<span>Synonyms can be used for all kind of identifiers. It's not limited to standard variables but can also be used for function and procedure names (more about functions and procedures later).</span><br />
<br />
<pre>
# Create a new procedure baz
proc baz { say "I am baz"; }
# Make a synonym baz, and undefine baz
my bay = \baz;
undef baz;
# bay still has a reference of the original procedure baz
bay; # this prints aut "I am baz"
</pre>
<br />
<span>The "syms" keyword gives you the total number of synonyms pointing to a specific value:</span><br />
<br />
<pre>
my foo = 1;
say syms foo; # Prints 1
my baz = \foo;
say syms foo; # Prints 2
say syms baz; # Prints 2
undef baz;
say syms foo; # Prints 1
</pre>
<br />
<h2 style='display: inline' id='statements-and-expressions'>Statements and expressions</h2><br />
<br />
<span>A Fype program is a list of statements. Each keyword, expression or function call is part of a statement. Each statement is ended with a semicolon. Example:</span><br />
<br />
<pre>
my bar = 3, foo = 1 + 2;
say foo;
exit foo - bar;
</pre>
<br />
<h3 style='display: inline' id='parenthesis'>Parenthesis</h3><br />
<br />
<span>All parenthesis for function arguments is optional. They help to make the code better readable. They also help to force the precedence of expressions.</span><br />
<br />
<h3 style='display: inline' id='basic-expressions'>Basic expressions</h3><br />
<br />
<span>Any "any" value holding a string will be automatically converted to an integer value.</span><br />
<br />
<pre>
(any) <any> + <any>
(any) <any> - <any>
(any) <any> * <any>
(any) <any> / <any>
(integer) <any> == <any>
(integer) <any> != <any>
(integer) <any> <= <any>
(integer) <any> gt <any>
(integer) <any> <> <any>
(integer) <any> gt <any>
(integer) not <any>
</pre>
<br />
<h3 style='display: inline' id='bitwise-expressions'>Bitwise expressions</h3><br />
<br />
<pre>
(integer) <any> :< <any>
(integer) <any> :> <any>
(integer) <any> and <any>
(integer) <any> or <any>
(integer) <any> xor <any>
</pre>
<br />
<h3 style='display: inline' id='numeric-expressions'>Numeric expressions</h3><br />
<br />
<pre>
(number) neg <number>
</pre>
<br />
<span>... returns the negative value of "number":</span><br />
<br />
<pre>
(integer) no <integer>
</pre>
<br />
<span>... returns 1 if the argument is 0; otherwise, it will return 0! If no argument is given, then 0 is returned!</span><br />
<br />
<pre>
(integer) yes <integer>
</pre>
<br />
<span>... always returns 1. The parameter is optional. Example:</span><br />
<br />
<pre>
# Prints out 1, because foo is not defined
if yes { say no defined foo; }
</pre>
<br />
<h2 style='display: inline' id='control-statements'>Control statements</h2><br />
<br />
<span>Control statements available in Fype:</span><br />
<br />
<pre>
if <expression> { <statements> }
</pre>
<br />
<span>... runs the statements if the expression evaluates to a true value.</span><br />
<br />
<pre>
ifnot <expression> { <statements> }
</pre>
<br />
<span>... runs the statements if the expression evaluates to a false value.</span><br />
<br />
<pre>
while <expression> { <statements> }
</pre>
<br />
<span>... runs the statements as long as the expression evaluates to a true value.</span><br />
<br />
<pre>
until <expression> { <statements> }
</pre>
<br />
<span>... runs the statements as long as the expression evaluates to a false value.</span><br />
<br />
<h2 style='display: inline' id='scopes'>Scopes</h2><br />
<br />
<span>A new scope starts with an { and ends with an }. An exception is a procedure, which does not use its own scope (see later in this manual). Control statements and functions support scopes. The "scope" function prints out all available symbols at the current scope. Here is a small example:</span><br />
<br />
<pre>
my foo = 1;
{
# Prints out 1
put defined foo;
{
my bar = 2;
# Prints out 1
put defined bar;
# Prints out all available symbols at this
# point to stdout. Those are: bar and foo
scope;
}
# Prints out 0
put defined bar;
my baz = 3;
}
# Prints out 0
say defined bar;
</pre>
<br />
<span>Another example including an actual output:</span><br />
<br />
<pre>
./fype -e ’my global; func foo { my var4; func bar { my var2, var3; func baz { my var1; scope; } baz; } bar; } foo;’
Scopes:
Scope stack size: 3
Global symbols:
SYM_VARIABLE: global (id=00034, line=-0001, pos=-001, type=TT_INTEGER, dval=0.000000, refs=-1)
SYM_FUNCTION: foo
Local symbols:
SYM_VARIABLE: var1 (id=00038, line=-0001, pos=-001, type=TT_INTEGER, dval=0.000000, refs=-1)
1 level(s) up:
SYM_VARIABLE: var2 (id=00036, line=-0001, pos=-001, type=TT_INTEGER, dval=0.000000, refs=-1)
SYM_VARIABLE: var3 (id=00037, line=-0001, pos=-001, type=TT_INTEGER, dval=0.000000, refs=-1)
SYM_FUNCTION: baz
2 level(s) up:
SYM_VARIABLE: var4 (id=00035, line=-0001, pos=-001, type=TT_INTEGER, dval=0.000000, refs=-1)
SYM_FUNCTION: bar
</pre>
<br />
<h2 style='display: inline' id='definedness-'>Definedness </h2><br />
<br />
<pre>
(integer) defined <identifier>
</pre>
<br />
<span>... returns 1 if "identifier" has been defined. Returns 0 otherwise.</span><br />
<br />
<pre>
(integer) undef <identifier>
</pre>
<br />
<span>... tries to undefine/delete the "identifier". Returns 1 if it succeeded, otherwise 0 is returned.</span><br />
<br />
<h2 style='display: inline' id='system-'>System </h2><br />
<br />
<span>These are some system and interpreter specific built-in functions supported:</span><br />
<br />
<pre>
(void) end
</pre>
<br />
<span>... exits the program with the exit status of 0.</span><br />
<br />
<pre>
(void) exit <integer>
</pre>
<br />
<span>... exits the program with the specified exit status.</span><br />
<br />
<pre>
(integer) fork
</pre>
<br />
<span>... forks a subprocess. It returns 0 for the child process and the PID of the child process otherwise! Example:</span><br />
<br />
<pre>
my pid = fork;
if pid {
put "I am the parent process; child has the pid ";
say pid;
} ifnot pid {
say "I am the child process";
}
</pre>
<br />
<span>To execute the garbage collector do:</span><br />
<br />
<pre>
(integer) GC
</pre>
<br />
<span>It returns the number of items freed! You may wonder why most of the time, it will produce a value of 0! Fype tries to free not needed memory ASAP. This may change in future versions to gain faster execution speed!</span><br />
<br />
<h3 style='display: inline' id='io-'>I/O </h3><br />
<br />
<pre>
(any) put <any>
</pre>
<br />
<span>... prints out the argument</span><br />
<br />
<pre>
(any) say <any>
</pre>
<br />
<span>is the same as put, but also includes an ending newline.</span><br />
<br />
<pre>
(void) ln
</pre>
<br />
<span>... just prints a new line.</span><br />
<br />
<h2 style='display: inline' id='procedures-and-functions'>Procedures and functions</h2><br />
<br />
<h3 style='display: inline' id='procedures'>Procedures</h3><br />
<br />
<span>A procedure can be defined with the "proc" keyword and deleted with the "undef" keyword. A procedure does not return any value and does not support parameter passing. It's using already defined variables (e.g. global variables). A procedure does not have its own namespace. It's using the calling namespace. It is possible to define new variables inside of a procedure in the current namespace.</span><br />
<br />
<pre>
proc foo {
say 1 + a * 3 + b;
my c = 6;
}
my a = 2, b = 4;
foo; # Run the procedure. Print out "11\n"
say c; # Print out "6\n";
</pre>
<br />
<h3 style='display: inline' id='nested-procedures'>Nested procedures</h3><br />
<br />
<span>It's possible to define procedures inside of procedures. Since procedures don't have their own scope, nested procedures will be available to the current scope as soon as the main procedure has run the first time. You may use the "defined" keyword to check if a procedure has been defined or not.</span><br />
<br />
<pre>
proc foo {
say "I am foo";
undef bar;
proc bar {
say "I am bar";
}
}
# Here bar would produce an error because
# the proc is not yet defined!
# bar;
foo; # Here the procedure foo will define the procedure bar!
bar; # Now the procedure bar is defined!
foo; # Here the procedure foo will redefine bar again!
</pre>
<br />
<h3 style='display: inline' id='functions'>Functions</h3><br />
<br />
<span>A function can be defined with the "func" keyword and deleted with the "undef" keyword. Function do not yet return values and do not yet supports parameter passing. It's using local (lexical scoped) variables. If a certain variable does not exist, when It's using already defined variables (e.g. one scope above). </span><br />
<br />
<pre>
func foo {
say 1 + a * 3 + b;
my c = 6;
}
my a = 2, b = 4;
foo; # Run the procedure. Print out "11\n"
say c; # Will produce an error because c is out of scope!
</pre>
<br />
<h3 style='display: inline' id='nested-functions'>Nested functions</h3><br />
<br />
<span>Nested functions work the same way the nested procedures work, except that nested functions will not be available anymore after the function has been left!</span><br />
<br />
<pre>
func foo {
func bar {
say "Hello i am nested";
}
bar; # Calling nested
}
foo;
bar; # Will produce an error because bar is out of scope!
</pre>
<br />
<h2 style='display: inline' id='arrays'>Arrays</h2><br />
<br />
<span>Some progress on arrays has been made too. The following example creates a multidimensional array "foo". Its first element is the return value of the func which is "bar". The fourth value is a string" 3" converted to a double number. The last element is an anonymous array which itself contains another anonymous array as its final element:</span><br />
<br />
<pre>
func bar { say ”bar” }
my foo = [bar, 1, 4/2, double ”3”, [”A”, [”BA”, ”BB”]]];
say foo;
</pre>
<br />
<span>It produces the following output:</span><br />
<br />
<pre>
% ./fype arrays.fy
bar
01
2
3.000000
A
BA
BB
</pre>
<br />
<h2 style='display: inline' id='fancy-stuff'>Fancy stuff</h2><br />
<br />
<span>Fancy stuff like OOP or Unicode or threading is not planed. But fancy stuff like function pointers and closures may be considered.:) </span><br />
<br />
<h2 style='display: inline' id='may-the-source-be-with-you'>May the source be with you</h2><br />
<br />
<span>You can find all of this on the GitHub page. There is also an "examples" folders containing some Fype scripts!</span><br />
<br />
<a class='textlink' href='https://codeberg.org/snonux/fype'>https://codeberg.org/snonux/fype</a><br />
<br />
<span>E-Mail your comments to <span class='inlinecode'>paul@nospam.buetow.org</span> :-)</span><br />
<br />
<a class='textlink' href='../'>Back to the main site</a><br />
<p class="footer">
Generated with <a href="https://codeberg.org/snonux/gemtexter">Gemtexter 3.0.1-develop</a> |
served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/relayd.8">relayd(8)</a>+<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> |
<a href="https://foo.zone/site-mirrors.html">Site Mirrors</a>
<br />
Webring: <a href="https://shring.sh/foo.zone/previous">previous</a> | <a href="https://shring.sh">shring</a> | <a href="https://shring.sh/foo.zone/next">next</a>
</p>
</body>
</html>
|