Codetree Example: nfib
Stephen Leach edited this page Oct 4, 2020
·
3 revisions
This is a variation of a Fibonacci function that has the elegant property that (without optimisation) it returns the number of function calls made.
Nutmeg:
def nfib( n ):
if n <= 1:
1
else:
1 + nfib(n - 1) + nfib(n - 2)
end
end
JSON:
{
"comment": "An example function 'nfib'",
"kind": "binding",
"lhs": {
"kind": "id",
"name": "nfib",
"reftype": "var"
},
"rhs": {
"kind": "lambda",
"parameters": {
"kind": "id",
"name": "n",
"reftype": "var"
},
"body": {
"kind": "if",
"test": {
"kind": "syscall",
"name": "<=",
"arguments": {
"kind": "seq",
"body": [
{
"kind": "id",
"name": "n",
"reftype": "get"
},
{
"kind": "int",
"value": "1"
}
]
}
},
"then": {
"kind": "int",
"value": "1"
},
"else": {
"kind": "syscall",
"name": "+",
"arguments": {
"kind": "seq",
"body": [
{
"kind": "int",
"value": "1"
},
{
"kind": "call",
"function": {
"kind": "id",
"name": "nfib",
"reftype": "get"
},
"arguments": {
"kind": "seq",
"body": [
{
"kind": "syscall",
"name": "-",
"arguments": {
"kind": "seq",
"body": [
{
"kind": "id",
"name": "n",
"reftype": "get"
},
{
"kind": "int",
"value": "1"
}
]
}
}
]
}
},
{
"kind": "call",
"function": {
"kind": "id",
"name": "nfib",
"reftype": "get"
},
"arguments": {
"kind": "syscall",
"name": "-",
"arguments": {
"kind": "seq",
"body": [
{
"kind": "id",
"name": "n",
"reftype": "get"
},
{
"kind": "int",
"value": "2"
}
]
}
}
}
]
}
}
}
}
}
Design Principles
- Nutmeg Design Goals
- The Spirit of Nutmeg
- Syntax Design
- State Encapsulation
- List of Design Decisions
- Influences
Language Features
- $$-Syntax (Echo Expressions)
- Actors
- Allocators
- Annotations
- Assignments
- Bindings
- Blocks and Modifiers
- Brackets
- Capsules (Proposed)
- Clean Procedures
- Comments
- Copy by Snapshot
- Declarations
- Finesses
- For Syntax
- Futures
- Identifiers
- If Syntax
- Messages
- Pass Syntax
- Queries
- References
- Return Statements
- Sealing
- Semicolons
- Strings
- Switch Syntax
Built-in Libraries and Functions
Implementation
- Architecture in one page
- Autoconversion
- Bundle Files
- Code-Trees
- Compiler Pipeline
- Components of the Compiler
- Installation File Structure
- Mishaps
- Peekable Pushable Generators
- Recursive Descent Parser
- Resolver
- Walkthrough of Hello-World
How-Tos
Process