From 436d3ba04853f4bd5a98af68e3355029f0b2b59d Mon Sep 17 00:00:00 2001 From: hanez Date: Thu, 11 Sep 2025 23:24:17 +0200 Subject: [PATCH] Added spec version 0.1. --- README.md | 2 +- draft.fun | 21 ++-- spec/v0.1.md | 272 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+), 12 deletions(-) create mode 100644 spec/v0.1.md diff --git a/README.md b/README.md index ebdcfbf..e1fac65 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Fun Uses Nothing - Life is too short not to code fun... ;) -Influenced by C, Go, Lua, Python and Rust. +Influenced by Bash, C, Go, Lua, Python and Rust. ## Idea diff --git a/draft.fun b/draft.fun index d85596f..ca5478e 100644 --- a/draft.fun +++ b/draft.fun @@ -112,25 +112,23 @@ number foo = 23 // Private variables can only be used here in this file. private number bar = 42 -for i in range(1, 1000) { - echo i - echo add(i, 1) -} +for i in range(1, 1000) + print i + print add(i, 1) -for i in range(1, foo) { - echo "i" -} +for i in range(1, foo) + print "i" // This must not work since range() is an internal function. string range = "f987458n73v03258" if(x != y) - echo x + print x else if(a == b || h != i) - echo [a + b + h + i] + print [a + b + h + i] else if(k < 1 && l > 1) - echo "Buh!" + print "Buh!" fi fi @@ -148,7 +146,8 @@ system(a) // Some kind of cast function for converting data types. cast(a) - return CASTED_string_to_another_existing_type(a) + // Not implemented. + return CASTED_variable_from_one_to_another_existing_type(a) // External functions need f as a prefix to define the function. f get_string(a_string, b_string) diff --git a/spec/v0.1.md b/spec/v0.1.md new file mode 100644 index 0000000..efb6fc3 --- /dev/null +++ b/spec/v0.1.md @@ -0,0 +1,272 @@ +# 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 `a–z`, `A–Z`, `0–9`, 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: +// This is a comment + - Multi-line: +/* + This is + a multi-line comment +*/ + +### Whitespace + + - Indentation is exactly two spaces. + - Tabs are forbidden. + - Line breaks terminate statements (no semicolons). + +## 3. Data Types + +### Number + + - 64-bit signed integer. + +``` +number n = 42 +``` + +### Float + + - 64-bit IEEE floating point. + +``` +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 \'. + +``` +string s1 = "Hello" +string s2 = 'World' +string s3 = "He said: 'Fun!'" +``` + +### Boolean + + - Canonical values: true, false. + - 1 and 0 are accepted in conditionals. + +``` +boolean flag = true +``` + +### Byte + + - Raw byte value. + +``` +byte b1 = 0x23 +byte b2 = "A" +``` + +### Array + + - Declared using [ ... ]. + +``` +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. + +``` +global string message = "Hello" +private number count = 42 +``` + +## 5. Operators + + - Arithmetic: + - * / % + - Comparison: == != > < >= <= + - Boolean: && || ! + - Assignment: = + +## 6. Control Flow + +### If/Else + +``` +if(x != y) + print x +else if(a == b || h != i) + print a + b +else + if(k < 1 && l > 1) + print "Buh!" + fi +fi +``` + +### For Loops + +``` +for i in range(1, 10) + print i +``` + +### While Loops + +``` +while x < 10 + print x + x = x + 1 +``` + +## 7. Functions + +### Internal Functions + +Provided by the runtime (cannot be redefined). Examples: +print, range, system, exec. + +``` +print "Hello, Fun!" +``` + +### User-Defined Functions + +Must be declared with the f prefix. + +``` +f add(a, b) + 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). + +``` +#include crypt/md5 +#include date +#include math +``` + +### Local Includes + +From the current working directory: + +``` +#include utils/file.fun +``` + +### Absolute Includes + +Full path: + +``` +#include /home/user/project/lib.fun +``` + +## 9. System Integration + +### system + +Executes a command, returns exit code. + +``` +system("ls -l") +``` + +### exec + +Executes a command, returns stdout. + +``` +string result = exec("date") +``` + +### spawn + +Executes a command asynchronously, returns process ID. + +``` +number pid = spawn("sleep 10") +``` + +## 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 + +``` +print "Hello, World!" +``` + +### Loop with Range + +``` +for i in range(1, 5) + print i +``` + +### User Function + +``` +f greet(name) + return "Hello " + name + +print greet("Fun") +``` + +### System Call + +``` +string now = exec("date") +print "Current time: " + now +``` + +## 12. License + +This document and the Fun language reference are licensed under the Apache License 2.0 +