An attempt to build a scripting language from scratch in C
- C 97.6%
- Makefile 2.2%
- Fancy 0.2%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| docs | ||
| examples | ||
| src | ||
| CLAUDE.md | ||
| fype | ||
| INSTALL | ||
| LICENSE | ||
| Makefile | ||
| NEW | ||
| README.md | ||
| README.txt | ||
| tags | ||
| test.fy | ||
| test.out | ||
| TODO | ||
Fype
For Your Program Execution — a lightweight scripting language.
Synopsis
fype script.fy # Run a .fy file
fype -e "say 1 + 2;" # Run inline code
fype -h # Show help
fype -v # Show version
About
Fype is a single-pass, simultaneous-parse-and-interpret scripting language. There is no AST; the scanner produces a flat token list which the interpreter consumes directly.
Fype is developed under the BSD license.
Building
make # Build the fype binary
make install # Install to /usr/local/bin
make clean # Remove build artifacts
Data Types
Fype uses automatic type conversion between these types:
- integer — whole numbers
- double — floating-point numbers
- string — text strings
Explicit conversion: integer, double, string.
Syntax
Comments
# Single-line comment
#* Block comment *#
#* Multi-line
block comment *#
say 1 #* inline *# + 1;
Variables
my foo = 1 + 2; # declare and assign
my bar = 4, baz = 5; # multiple declarations
my bay; # defaults to 0
Arithmetic
say 10 + 3; # 13
say 10 - 3; # 7
say 10 * 3; # 30
say 10 / 3; # 3 (integer division)
say neg 5; # -5
incr x; # x = x + 1
decr x; # x = x - 1
Comparison
5 == 5 # equal
5 != 4 # not equal
5 < 10 # less than
5 <= 5 # less or equal
5 > 3 # greater than
5 >= 5 # greater or equal
not 0 # logical not (returns 1)
Bitwise
5 and 3 # bitwise AND
5 or 2 # bitwise OR
5 xor 3 # bitwise XOR
2 :< 2 # left shift (2 << 2 = 8)
8 :> 2 # right shift (8 >> 2 = 2)
Conditionals
if 1 { say "true"; }
ifnot 0 { say "false"; }
Loops
while x < 10 { incr x; }
until x == 10 { incr x; }
loop {
if x == 5 { break; }
incr x;
}
do { incr x; } while x < 10;
do { incr x; } until x == 10;
break exits the loop; next skips to the next iteration.
Procedures
Procedures share the caller's scope:
proc greet {
say "Hello";
my local_var = 42; # visible to caller
}
greet;
Functions
Functions have local scope and support parameters and return values:
# Zero-arg function
fun answer() { ret 42; }
say answer();
# With parameters
fun add(a, b) { ret a + b; }
say add(3, 5);
# Multiple return values
fun minmax(a, b) {
if a < b { ret a, b; }
ret b, a;
}
say minmax(3, 7); # prints 3 then 7
Arrays
my nums = [10, 20, 30, 40, 50];
say len nums; # 5
say nums[0]; # 10
say nums[1 + 1]; # 30 (expression index)
nums[2] = 99; # element assignment
# Slices (half-open ranges)
my sub = nums[1:4]; # [20, 30, 40]
my head = nums[:2]; # [10, 20]
my tail = nums[3:]; # [40, 50]
my copy = nums[:]; # full shallow copy
Scoping
my outer = 1;
{
my inner = 2;
say defined inner; # 1
}
say defined inner; # 0
Synonyms (Aliases)
my foo = 42;
my bar = \foo; # bar is an alias for foo
foo = 99;
say bar; # 99 (reflects change)
say syms foo; # 2 (two symbols point to same value)
undef foo;
say defined bar; # 1 (alias keeps value alive)
Built-in Functions
| Function | Description |
|---|---|
put x |
Print without newline |
say x |
Print with newline |
ln |
Print newline |
len arr |
Array length |
defined id |
1 if defined, else 0 |
undef id |
Undefine a symbol |
syms id |
Count synonyms |
assert cond |
Assert condition is true |
exit n |
Exit with code n |
fork |
Fork a subprocess |
gc |
Run garbage collector |
scope |
Print visible symbols |
Examples
See the examples/ directory for complete demonstrations:
variables.fy— variable declarationsexpressions.fy— arithmetic expressionsconditionals.fy— if/ifnotcontrol.fy— while/until loopsloop_do.fy— loop, break, next, do-while, do-untilfunctions.fy— functions and nestingfunc_args_ret.fy— function arguments and return valuesprocedures.fy— proceduresslices.fy— array slicingsynonyms.fy— aliasesscopeing.fy— lexical scopingbitwise.fy— bitwise operatorstypes.fy— type conversionio.fy— I/O operationsfork.fy— process forkingcomments.fy— comment syntaxbreak_next.fy— loop controluber.fy— comprehensive example exercising all features
Author
Paul C. Buetow (https://buetow.org)