maryChain - lightweight programming functional language for LLM
Project description
Sure, here is an example of what your README.md could look like:
MaryChain Parser
MaryChain Parser is a basic lexer and parser written in Python using the Python Lex-Yacc (PLY) library. It uses LR-parsing which is suitable for larger grammars.
Features
- Mathematical operations: addition, subtraction, multiplication, and division
- Parentheses to dictate the order of operations
- Unary minus operation
- Conditional statements: if-then-else
- Loop construct: while-do
- Function calls with arguments
- Lazy evaluation: expressions are only evaluated when needed
- Pipeline operation: pipes the result of one expression into another
- Placeholder: represents a wildcard or unnamed entity for pattern matching
Usage
To parse and evaluate a string s
, use the eval
function:
result = eval(s)
If there is a syntax error in the string, eval
will print an error message and return None
. If the string can be parsed successfully, eval
will return the result of evaluating the parsed Abstract Syntax Tree (AST).
Technical Details
Tokens
The lexer recognizes a variety of tokens, such as NUMBER
, STRING
, IDENTIFIER
, and various keywords and symbols.
Grammar
The parser uses the following grammar:
<program> ::= <definition>* <import>* <expression>?
<definition> ::= <identifier> <identifier>* '=' <expression>
<import> ::= 'import' <identifier> 'as' <identifier>
<expression> ::= <boolean>
| <number>
| <string>
| <identifier>
| <unary>
| <binary>
| <function_call>
| <let_in>
| <if_then_else>
| <if_then>
| <while>
| <pipe>
| <lambda_function>
<boolean> ::= 'True' | 'False'
<number> ::= [0-9]+('.'[0-9]+)?
<string> ::= '"' <char>* '"'
<identifier> ::= [a-zA-Z_][a-zA-Z0-9_]*
<unary> ::= ('-' | 'not') <expression>
<binary> ::= <expression> ('+' | '-' | '*' | '/' | '&&' | '||' | '->' | '>' | '<' | '>=' | '<=' | '==') <expression>
<function_call> ::= <identifier> '(' <expression>* ')'
<let_in> ::= 'let' <identifier> '=' <expression> 'in' <expression>
<if_then_else> ::= 'if' <expression> 'then' <expression> 'else' <expression>
<if_then> ::= 'if' <expression> 'then' <expression>
<while> ::= 'while' <expression> <expression>
<pipe> ::= <expression> '|' <expression>
<lambda_function> ::= 'lambda' <identifier>* ':' <expression>
Operator precedence and associativity are defined separately in the parser.
Abstract Syntax Tree (AST)
The parser generates an AST from the input string. Each node in the tree is an instance of a class defined in maryChainAST.py
, such as BinOp
, UnaryOperation
, Number
, String
, Placeholder
, Identifier
, FunctionCall
, While
, IfThenElse
, IfThen
, Lazy
, and Pipe
.
Evaluation
The eval
function evaluates the AST using a visitor pattern: each node class has an evaluate
method that computes the value of the node based on its child nodes.
Certainly, here is a documentation of MaryChain's semantics:
MaryChain Language Semantics
The MaryChain language provides a set of constructs and features that allow users to write functional code.
Language Constructs
-
Definitions: The program can include definitions which bind an identifier to a function or value. This function or value can then be used later in the code by referencing the identifier.
Example:
let sum x y = x + y in sum 5 3
-
Import: The
import
statement allows users to import modules. Imported modules are given an alias which is used to call the module's functions. The alias is used along with the function name separated by a::
symbol.Example:
import numpy as np
and use it asnp::sum(x,y)
-
Expressions: Expressions can be any type of values, identifiers, unary and binary operations, function calls, conditionals (if-then-else or if-then), and control flow structures (such as while). Expressions can be evaluated to give a result.
-
Function Calls: Functions can be called with zero or more arguments. User-defined functions, built-in functions, and functions from imported modules can all be called.
Example:
sum(5, 3)
-
Unary Operations: The unary operations available are negation (
-
) and logical not (not
). -
Binary Operations: MaryChain supports common arithmetic and logical binary operations.
-
Let-in Expression: This construct allows local bindings. It binds an expression to an identifier in the scope of another expression.
-
If-Then-Else and If-Then: These are conditional structures.
If-Then-Else
evaluates the first expression if the condition is true, and the second expression if it is false.If-Then
only evaluates the first expression if the condition is true. -
While: This is a control flow structure that evaluates an expression while a condition is true.
-
Pipe Operator: The pipe operator (
->
) allows users to chain function calls in a clear and readable way. The output of the first function call is passed as the input to the second function call. -
Lambda Functions: Users can create anonymous functions (also known as lambda functions) with the
lambda
keyword.
Evaluation Semantics
Evaluation semantics in MaryChain are mostly similar to those in traditional functional programming languages. Here are some specifics:
-
Lazy Evaluation: Expressions are evaluated in a lazy manner. This means that an expression is not evaluated until its value is needed. This is particularly important when dealing with infinite data structures or long-running computations.
-
Environment: The environment is a mapping of identifiers to their current values. When a new value is bound to an identifier using a
let-in
expression, a new environment is created where the identifier maps to the new value. The old environment is restored after thelet-in
expression is evaluated. -
Function Application: When a function is called, the arguments are evaluated from left to right. The function body is then evaluated in a new environment where the function parameters are bound to the evaluated arguments.
-
Control Flow:
if-then-else
andwhile
structures are evaluated in the usual way. The condition is evaluated first, and depending on its value, the corresponding expression(s) is/are evaluated. In awhile
structure, the body is evaluated repeatedly as long as the condition is true. -
Pipe Operator: When using the pipe operator (
|
), the expression on the left is evaluated first. Its value is then passed as an argument to the function call on the right.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for maryChain-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d5d272079e37bf422b5d4ebb4cd7a37286f81396dc8ed02443c2af4b4c108cf |
|
MD5 | 72f3032e728792137a85b5449872203e |
|
BLAKE2b-256 | c297e6cf08be86729849afb3bdf4dea5729277fde12edc3ebd9158a2db74c32c |