flt
A simple functional programming language.
Overview
flt (pronounced "flight") is a lightweight functional language implementation. It provides an expression parser and abstract syntax tree (AST) for a language with literals, identifiers, operators, function calls, and an Elixir-style pipe operator.
Features
- Literals: numbers (arbitrary precision via
BigDecimal), strings, booleans, and symbols (:foo,:"hello") - Operators:
- Unary:
!,+,- - Binary:
+,-,*,/,&,&&,|,||,^,^^,|>(pipe)
- Unary:
- Function calls:
foo(),bar(1),add(1, 2) - Pipe operator:
a |> b |> c— passes the left value as the first argument to the right - Operator precedence (lowest to highest):
||,&&,^^,|,^,&,+/-,*,/
Installation
Add to your Cargo.toml:
[]
= "0.0.1"
Usage
use parse_expr;
Parsing expressions
use parse_expr;
// Numbers
parse_expr; // Literal number
parse_expr; // Decimal
// Strings and symbols
parse_expr; // String literal
parse_expr; // Symbol
// Booleans
parse_expr;
parse_expr;
// Function calls
parse_expr;
parse_expr;
// Pipe operator
parse_expr;
parse_expr;
Unary operators
| Operator | Meaning | Example |
|---|---|---|
! |
Logical not | !true, !x |
+ |
Unary plus | +42 |
- |
Unary minus / negation | -x, -(1 + 2) |
use parse_expr;
parse_expr; // Not
parse_expr; // Negation
parse_expr; // Unary plus
Binary operators
| Operator | Meaning | Example |
|---|---|---|
+, -, *, / |
Arithmetic | 1 + 2, 10 - 3, 4 * 5, 8 / 2 |
&&, ||, ^^ |
Logical and, or, xor | a && b, x || y |
&, |, ^ |
Bitwise and, or, xor | 1 & 2, 1 | 2, 1 ^ 2 |
|> |
Pipe (pass left as first arg to right) | `x |
use parse_expr;
parse_expr; // Arithmetic (precedence: * before +)
parse_expr; // Logical
parse_expr; // Pipe
parse_expr; // Parentheses override precedence
Other Expr forms
| Form | Example |
|---|---|
| Literal | 42, 3.14, "hello", true, false, :foo |
| Identifier | x, myVar |
| Function call | foo(), add(1, 2) |
| Parenthesized | (1 + 2) |
use parse_expr;
parse_expr; // Literal number
parse_expr; // Identifier
parse_expr; // Function call
parse_expr; // Parenthesized expression
Public API
parser:parse_expr,parse_literal,parse_identifier,parse_number,parse_string,parse_symbol,parse_binary_op,parse_unary_opast:Expr,Literal,Identifier,BinaryOp,UnaryOpError: Error types for parsing and runtime
License
MIT