1
0
Fork 0
forked from fun/fun
fun/spec/v0.1.md

309 lines
4.6 KiB
Markdown
Raw Normal View History

2025-09-11 23:24:17 +02:00
# Fun Language Specification v0.1
**Fun (Fun Uses Nothing)** is a lightweight, multi-purpose programming language designed for clarity, safety, and—above all—fun.
This document defines the language syntax and semantics as of version 0.1.
## 1. Introduction
Fun is a scripting and systems programming language inspired by the best ideas from C, Lua, and Python—yet deliberately strict in its rules to ensure readability and consistency.
- **Readable**: fixed indentation rules, no ambiguous syntax.
- **Strict**: type safety, no implicit coercion, no silent shadowing.
- **Hackable**: strong system integration with safe defaults.
- **Fun**: simple enough to learn in a day, expressive enough for real work.
## 2. Lexical Structure
### Case Sensitivity
Fun is **case-sensitive**. `Fun` and `fun` are different identifiers.
### Identifiers
- May contain `az`, `AZ`, `09`, and `_`.
- Must not start with a number.
### Keywords
Reserved words cannot be redefined:
`if`, `else`, `for`, `while`, `fi`, `f`, `global`, `private`, `return`, `true`, `false`.
### Comments
- Single-line:
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
// This is a comment
2025-09-11 23:31:44 +02:00
```
2025-09-11 23:24:17 +02:00
- Multi-line:
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
/*
2025-09-11 23:31:44 +02:00
This is a
multi-line comment
2025-09-11 23:24:17 +02:00
*/
2025-09-11 23:31:44 +02:00
```
2025-09-11 23:24:17 +02:00
### Whitespace
- Indentation is exactly two spaces.
- Tabs are forbidden.
- Line breaks terminate statements (no semicolons).
## 3. Data Types
### Number
- 64-bit signed integer.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
number n = 42
```
### Float
- 64-bit IEEE floating point.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
float pi = 3.14159
```
### String
- Double or single quotes may be used.
- ' has higher priority, so " can appear unescaped inside '...'.
- To include ' inside '...', escape with \'.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
string s1 = "Hello"
string s2 = 'World'
2025-09-11 23:31:44 +02:00
string s3 = 'He said: "Fun!"'
2025-09-11 23:24:17 +02:00
```
### Boolean
- Canonical values: true, false.
- 1 and 0 are accepted in conditionals.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
boolean flag = true
```
### Byte
- Raw byte value.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
byte b1 = 0x23
2025-09-12 00:01:25 +02:00
byte b2 = 'A'
2025-09-11 23:24:17 +02:00
```
### Array
- Declared using [ ... ].
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
array nums = [1, 2, 3]
```
## 4. Variables and Scope
- Global variables: accessible everywhere.
- Private variables: file-local, cannot be used outside the file.
- Redefining globals or shadowing names is forbidden.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
global string message = "Hello"
private number count = 42
```
## 5. Operators
2025-09-12 00:01:25 +02:00
- Arithmetic: `+`, `-`, `*`, `/`, `%`
- Comparison: `==`, `!=`, `>`, `<`, `>=`, `<=`
- Boolean: `&&`, `||`, `!`
- Assignment: `=`
2025-09-11 23:24:17 +02:00
## 6. Control Flow
### If/Else
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
if(x != y)
print x
else if(a == b || h != i)
print a + b
else
if(k < 1 && l > 1)
print "Buh!"
```
### For Loops
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
for i in range(1, 10)
print i
```
### While Loops
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
while x < 10
print x
x = x + 1
```
## 7. Functions
### Internal Functions
Provided by the runtime (cannot be redefined). Examples:
print, range, system, exec.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
print "Hello, Fun!"
```
### User-Defined Functions
2025-09-12 00:01:25 +02:00
Must be declared with the fun prefix.
2025-09-11 23:24:17 +02:00
2025-09-11 23:31:44 +02:00
```fun
2025-09-12 00:01:25 +02:00
fun add(a, b)
2025-09-11 23:24:17 +02:00
return a + b
```
### Return Values
- Scalars: return a + b
- Arrays: return [a, b, c]
## 8. Modules & Includes
### System Includes
Looked up in /var/lib/fun/, supporting .so (native) and .fun (Fun code).
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:32:34 +02:00
#include crypt/md5
#include date
#include math
2025-09-11 23:24:17 +02:00
```
### Local Includes
From the current working directory:
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:32:34 +02:00
#include utils/file.fun
2025-09-11 23:24:17 +02:00
```
### Absolute Includes
Full path:
2025-09-11 23:31:44 +02:00
```fun
2025-09-12 00:01:25 +02:00
#include /home/user/project/lib.fun
2025-09-11 23:24:17 +02:00
```
## 9. System Integration
2025-09-12 00:01:25 +02:00
### Blocking
#### exec(cmd)
2025-09-11 23:24:17 +02:00
Executes a command, returns stdout.
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
string result = exec("date")
```
2025-09-12 00:17:19 +02:00
#### system(cmd)
2025-09-12 00:01:25 +02:00
Executes a command, returns exit code.
```fun
system("ls -l")
```
2025-09-12 00:17:19 +02:00
### Non-Blocking
2025-09-12 00:01:25 +02:00
#### nexec(cmd)
Executes a command, returns stdout.
```fun
string result = exec("date")
```
#### nspawn(cmd)
2025-09-11 23:24:17 +02:00
Executes a command asynchronously, returns process ID.
2025-09-11 23:31:44 +02:00
```fun
2025-09-12 00:01:25 +02:00
number pid = nspawn("sleep 10")
2025-09-11 23:24:17 +02:00
```
2025-09-12 00:17:19 +02:00
#### nsystem(cmd)
Executes a command, returns exit code.
```fun
system("ls -l")
```
2025-09-12 00:01:25 +02:00
### Helpers
#### wait(pid)
Blocks until process finishes, returns exit code.
#### read(pid)
Returns captured stdout (for processes started with nexec).
#### kill(pid)
Terminates process.
2025-09-11 23:24:17 +02:00
## 10. Error Handling
- No implicit type coercion.
- Redefinition of globals is a compile-time error.
- Using undefined variables or functions is an error.
- Internal/runtime functions cannot be shadowed.
## 11. Examples
### Hello World
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
print "Hello, World!"
```
### Loop with Range
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
for i in range(1, 5)
print i
```
### User Function
2025-09-11 23:31:44 +02:00
```fun
2025-09-12 02:27:41 +02:00
fun greet(name)
2025-09-11 23:24:17 +02:00
return "Hello " + name
print greet("Fun")
```
### System Call
2025-09-11 23:31:44 +02:00
```fun
2025-09-11 23:24:17 +02:00
string now = exec("date")
print "Current time: " + now
```
## 12. License
This document and the Fun language reference are licensed under the Apache-2.0 License.