Funco
This is a small functional programming language I wrote for fun inspired by lispy and Atto.
Still, it has a lexer and a parser, and some optimizations (like recursive tail calls, although they need to be tagged).
The only requirement is Python 3.10 or later.
There are lots of limitations, but check TODO for some ideas to contribute.
The language
An example:
# example of a factorial function
def fact(n acc)
if =(n 1)
acc
else
# tagging a tail call with "@" so it can be optimized
@fact(-(n 1) *(acc n))
end
end
# main is the entry point of a program
def main()
display("Running fact(50)...")
display(fact(50. 1))
end
# output:
# Running fact(50)...
# 3.0414093201713376e+64
A program can have:
- line comments, with
#
- function definitions
- expressions
A main()
function is required and will be executed as entry point for the program.
Function definition:
def ident([param1 [param2] [...]])
[...]
end
Expressions can be:
- function calls
- conditionals with
if [elif [elif] [...]] [else]
- literals
Literals include:
- numbers (int or float):
1
,1.5
- strings:
"this is a string"
- booleans (
true
orfalse
), as result of some logical functions; although 0 is false and non-zero is true - lists
- functions
- none for "no value"
Operators are functions, that includes:
+
,-
,*
,/
,mod
>
,<
,>=
,<=
,=
,!=
and
,or
,not
For example, adding two numbers:
+(1 1) # should be 2
Other built-in functions are:
int(value)
: convert a value into a integerfloat(value)
: convert a value into a floatstring(value)
: convert a value into a stringlist(a b c d)
: convert its arguments into a list, for examplelist(1 2 3 4)
int?(value)
: returns true if a value is a integerfloat?(value)
: returns true if a value is a floatstring?(value)
: returns true if a value is a stringlist?(value)
: returns true if a values is a listnone?(value)
: returns true if a value is noneboolean?(value)
: returns true if a value is a booleanfunction?(value)
: returns true if a value is a functionhead(list)
: returns the first element of a listtail(list)
: returns the end of a list excluding the first elementempty?(list)
: returns true if a list is emptysize(list)
: returns the size of a listmin(list)
: returns the smaller value in a listmax(list)
: returns the larger value in a listcontains(value list)
: returns true if a list contains a valuemap(fn list)
: applies a list to a functionfilter(fn list)
: applies a list to a function, excluding the items for which the function returns truefold(initial fn list)
: folds using a function the receives the accumulator and the value, and returns the updated accumulatortake(n list)
: return the first n elements of a listdrop(n list)
: return the list dropping the n first elementsdisplay(value)
: prints its arguments to the screen, for exampledisplay("hello" "world")
See the examples in ./examples
.
Licence
- This project is licensed GPL 3.0.