Welcome to PawScript ā a ācute yet practicalā staticallyātyped, functional scripting language. This README covers all v0.1 syntax, including Record (struct), Optional types, async/await, error handling, module import, and more.
- Installation & Running
- CLI StackāSize Options
- Core Structure
- Data Types
- Optional Types & Null Value
- Variable Declaration
- Expressions
- Statements
- Control Flow
- Functions
- Asynchronous Programming
- Arrays
- Record (struct)
- Type Casting
- Comments
- Error Handling
- Module Import
- Full Example
-
Clone and build:
git clone https://github.com/KinLeoapple/pawc.git cd pawc cargo build --release -
Run a script:
target/release/pawc hello.paw
PawScript Interpreter supports adjusting the backup stack size for the main thread via a CLI flag (in MiB) to accommodate deepārecursion scenarios.
# Default: maināthread backup stack 1 MiB
target/release/pawc script.paw
# Custom: maināthread backup stack 4 MiB
target/release/pawc --stack-size 4 script.pawA PawScript program consists of statements and function declarations executed in order.
- Primitive types:
Int,Long,Float,Double,Bool,Char,String - Generics:
Array<T> - Special types:
Any(dynamic),Optional<T>(nullable, can also be writtenT?)
PawScript supports optional types to represent possibly missing values.
-
Declare an optional type by appending
?, e.g.Int?isOptional<Int>. -
The null literal is
nopaw. -
Assigning
nopawto a nonāoptional type is a compileātime error. -
Example:
let maybeNum: Int? = nopaw if maybeNum == nopaw { say "No number provided" }
let x: Int = 10
let y: Int? = nopaw # optional type
x = x + 1 # reassignment
- Arithmetic:
+ - * / % - Comparison:
== != < <= > >= - Logic:
&& || ! - String concatenation:
"Hi " + name + "!" - Await:
await <asyncCall> - Grouping:
(a + b) * c
- Declaration/assignment:
let/= - Output:
say <expr> - Input:
ask "prompt"orlet x: String <- ask "?" - Return:
return <expr>orreturn
if cond {
ā¦
} else if cond2 {
ā¦
} else {
ā¦
}
loop forever { ⦠}
loop cond { ⦠}
loop i in start..end { ⦠}
loop item in array { ⦠}
breakexits the nearest loop.continueskips to the next iteration.
fun add(a: Int, b: Int): Int {
return a + b
}
let result: Int = add(1, 2)
PawScript supports defining asynchronous functions and awaiting their results, enabling nonāblocking I/O and concurrent tasks.
To define an asynchronous function, prefix the signature with the async keyword:
async fun fetchData(url: String): String {
// ... perform asynchronous operations ...
bark "Fetched data from " + url
return "data"
}
asyncmust appear before thefunkeyword.- Async functions return a
Future<T>internally. - You can store or pass async functions as values.
Use await to suspend until a Future completes:
let content: String = await fetchData("http://example.com/data")
say "Received: " + content
awaitmay be used at topālevel or within async functions.- Awaiting a nonāFuture returns the value unchanged.
let a: Array<Int> = [1, 2, 3]
say a[0] # index access
say a.length() # length property
PawScript supports userādefined composite types called Record (struct).
record Point {
x: Int
y: Int
}
- Fields must all be provided at initialization.
let p: Point = Point { y: 4, x: 3 }
- Field order is arbitrary.
say p.x # 3
say p.y # 4
Use as for explicit casts:
let i: Int = 3
let f: Float = i as Float
say f + 1.5
- Supports
Int ā Long ā Float ā Double. - Invalid casts are compileātime errors.
# singleāline comment
let x: Int = 5 # endāofāline comment
| Keyword | Purpose |
|---|---|
bark |
throw error |
sniff |
try block |
snatch |
catch block |
lastly |
finally block |
sniff {
ā¦
} snatch (e) {
say "Caught: " + e
} lastly {
say "Cleanup"
}
import utils.math # binds module to `math`
import utils.math as m # binds module to alias `m`
- Access functions/constants via module name or alias.
import utils.math as m
import string
say "=== Record & Async Example ==="
# Record
record Point { x: Int, y: Int }
let p: Point = Point { y: 4, x: 3 }
say "p.x + p.y = " + (p.x + p.y)
# Async
async fun fetchData(url: String): String {
bark "network not implemented"
return "data"
}
let result: String = await fetchData("http://example.com")
# Loop with break/continue
let sum: Int = 0
loop i in 1..10 {
if i == 5 {
continue
}
if i == 8 {
break
}
sum = sum + i
}
say "sum = " + sum
Enjoy coding in PawScript ā cute yet powerful!