-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-small.txt
More file actions
291 lines (269 loc) · 10.4 KB
/
context-small.txt
File metadata and controls
291 lines (269 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# Risor Language Quick Reference
## Core Syntax
### Variable Declaration
```risor
// Use := for variable declaration (not var)
x := 10
y := "hello"
// Multiple variables need container initialization
a, b, c := [1, 2, 3] // Not a = b = c = value
```
### String Interpolation
```risor
name := "world"
// Use single quotes for string interpolation
greeting := 'Hello, {name}!' // "Hello, world!"
```
### Functions
```risor
// Functions with defaults
func greet(name='world') {
return 'Hello, {name}!'
}
// Functions as values
add := func(a, b) {
return a + b
}
```
### Control Flow
```risor
// If-else is an expression
status := if x > 10 {
"greater"
} else {
"lesser"
}
// Switch is also an expression
role := switch name {
case "admin":
"Administrator"
default:
"User"
}
// Ternary expression
allowed := age >= 18 ? true : false
```
### Error Handling
```risor
// Use try function instead of try/catch blocks
result := try(
func() {
// Function that might raise an error
error("failed")
},
"fallback value"
)
```
### Pipe Operator
```risor
// Pipe operator passes left value to first arg of right function
"hello" | strings.to_upper // "HELLO"
```
## Concurrency
```risor
// Channels
c := chan(1) // Buffered channel
c <- "hello" // Send
x := <-c // Receive
// Goroutines
go func() {
// Run in background
print("background")
}()
```
## Common Operations
```risor
// HTTP Request
response := fetch("https://api.example.com")
data := response.json()
// File Operations
content := os.read_file("example.txt")
os.write_file("output.txt", "hello")
// JSON Handling
json_str := json.marshal({name: "Alice", age: 30})
parsed := json.unmarshal('{"name": "Bob"}')
```
# Important rules when writing Risor code
- Use two spaces to indent code
- No `var` keyword for variables (use `:=`)
- No `??` operator for null coalescing (use `coalesce()` function)
- Module imports don't use quotes. Use `import math` and not not `import "math"`
- Use single quotes for string interpolation: `'Hello, {name}'`
- If-else and switch are expressions that return values
- String interpolation with single quotes only supports simple variable substitution (`'{variable}'`). It does not support formatting specifications inside the curly braces like Python's f-strings. To format values, use the `fmt.sprintf()` function separately and concatenate the results
- ^ is not a valid power operator in Risor. Use math.pow() function instead
- Use os.stdin.input() to read input from the user
- Risor doesn't have while to create loops. Always use for and range.
- Risor doesn't have __main__ or __file__ globals
- Risor doesn't support variadic arguments when calling or defining functions
- Lists in Risor behave very similarly to lists in Python. Methods and indexing
on lists are the primary way to interact with these objects. A list can store
objects of any types, including a mix of types in one list.
```go
>>> l := ["a", 1, 2]
["a", 1, 2]
>>> l.append("tail")
["a", 1, 2, "tail"]
```
- Reserved keywords:
- func
- import
- return
- if
- for
- range
- spawn
- from
Always follow the above rules.
# Errors Module Summary
Errors module provides functions for creating error values in Risor.
## Functions
* new(string) - Creates a new error value with the given message
# Exec Module Summary
Exec module is used to run external commands without invoking the system shell.
## Functions
* exec(args, opts) - Runs command and returns result object
* command(args) - Creates a new command
* look_path(name) - Searches for executable in PATH
## Types
* command - Has path, dir, env, stdout, stderr attributes and run(), output(), combined_output(), start(), wait() methods
* result - Has stdout, stderr, pid attributes
# Filepath Module Summary
Filepath module provides utilities to manipulate file paths with OS-specific separators.
## Functions
* abs(path) - Returns absolute representation of path
* base(path) - Returns last element of path
* clean(path) - Returns shortest path name equivalent
* dir(path) - Returns all but last element of path
* ext(path) - Returns file extension
* is_abs(path) - Checks if path is absolute
* join(paths...) - Joins path elements with OS-specific separator
* match(pattern, name) - Checks if filename matches shell pattern
* rel(basepath, targpath) - Returns relative path from base to target
* split_list(path) - Splits path into directory and file components as list
* split(path) - Splits path into directory and file components
* walk_dir(root, fn) - Walks file tree calling function for each entry
# HTTP Module Summary
HTTP module provides functions for making HTTP requests and handling responses.
## Functions
* get(url, headers, params) - Creates a GET request
* delete(url, headers, params) - Creates a DELETE request
* head(url, headers, params) - Creates a HEAD request
* listen_and_serve(addr, handler) - Starts an HTTP server
* listen_and_serve_tls(addr, cert_file, key_file, handler) - Starts an HTTPS server
* patch(url, headers, body) - Creates a PATCH request
* post(url, headers, body) - Creates a POST request
* put(url, headers, body) - Creates a PUT request
* request(url, options) - Creates a custom HTTP request
## Types
* request - Has url, content_length, header attributes and send(), add_header(), add_cookie(), set_body(), set_data() methods
* response - Has status, status_code, proto, content_length, header, cookies, response attributes and json(), text(), close() methods
* response_writer - Has add_header(), del_header(), write(), write_header() methods
# Math Module Summary
Math module provides constants and mathematical functions, wrapping Go's math package with additional utilities.
## Constants
* PI - Mathematical constant pi (3.141592653589793)
* E - Mathematical constant e (2.718281828459045)
## Functions
* abs(x) - Returns absolute value
* sqrt(x) - Returns square root
* min(x, y) - Returns smaller of two numbers
* max(x, y) - Returns larger of two numbers
* floor(x) - Returns largest integer less than or equal to x
* ceil(x) - Returns smallest integer greater than or equal to x
* sin(x) - Returns sine
* cos(x) - Returns cosine
* tan(x) - Returns tangent
* mod(x, y) - Returns remainder of division
* log(x) - Returns natural logarithm
* log10(x) - Returns base-10 logarithm
* log2(x) - Returns base-2 logarithm
* pow(x, y) - Returns x raised to power of y
* pow10(x) - Returns 10 raised to power of x
* is_inf(x) - Checks if value is infinity
* inf() - Returns infinite value
* round(x) - Rounds to nearest integer
* sum(list) - Returns sum of all numbers in list
# OS Module Summary
OS module provides platform-independent access to operating system functionality.
## Attributes
* stdin - Standard input file pointer
* stdout - Standard output file pointer
## Functions
* chdir(dir) - Changes working directory
* create(name) - Creates or truncates a file
* environ() - Returns environment variables as list of strings
* exit(code) - Terminates program with exit code
* getenv(key) - Gets environment variable value
* getpid() - Returns current process ID
* getuid() - Returns current user ID
* getwd() - Gets current working directory
* hostname() - Returns system hostname
* mkdir_all(path, perm) - Creates directory with parents
* mkdir_temp(dir, prefix) - Creates temporary directory
* mkdir(path, perm) - Creates directory
* open(name) - Opens file for reading
* read_dir(name) - Lists directory contents
* read_file(name) - Reads file contents
* remove(name) - Removes file or empty directory
* remove_all(name) - Removes directory and contents
* rename(old, new) - Renames/moves file or directory
* setenv(key, value) - Sets environment variable
* stat(name) - Returns file information
* symlink(old, new) - Creates symbolic link
* temp_dir() - Returns temporary directory path
* unsetenv(key) - Unsets environment variable
* user_cache_dir() - Returns user cache directory
* user_config_dir() - Returns user config directory
* user_home_dir() - Returns user home directory
* write_file(name, data) - Writes data to file
## Types
* File - Has read(), write(), close() methods
* FileInfo - Contains name, mode, size, mod_time attributes
* DirEntry - Contains name, type attributes
# regexp Module Summary
The regexp module provides regular expression matching functionality using Go's regexp syntax.
## Functions
- `compile(expr string)` - Compiles a regular expression string
- `match(expr, s string)` - Tests if a string contains a pattern match
## Types
- `regexp` - Represents a compiled regular expression
- `match(s string)` - Tests if string contains pattern match
- `find(s string)` - Returns leftmost match
- `find_all(s string)` - Returns all matches
- `find_submatch(s string)` - Returns matches and submatches
- `replace_all(s, repl string)` - Replaces all matches with replacement
- `split(s string)` - Splits string by pattern matches
# Strings Module Summary
String manipulation functions from the Go standard library.
## Functions
* compare(s1, s2) - Compares strings lexicographically
* contains(s, substr) - Checks if string contains substring
* count(s, substr) - Counts non-overlapping instances of substring
* fields(s) - Splits string on whitespace
* has_prefix(s, prefix) - Checks if string starts with prefix
* has_suffix(s, suffix) - Checks if string ends with suffix
* index(s, substr) - Returns first index of substring or -1
* join(a, sep) - Concatenates strings with separator
* last_index(s, substr) - Returns last index of substring or -1
* repeat(s, count) - Creates new string with repeated copies
* replace_all(s, old, new) - Replaces all instances of old with new
* split(s, sep) - Splits string on separator
* to_lower(s) - Converts string to lowercase
* to_upper(s) - Converts string to uppercase
* trim_prefix(s, prefix) - Removes leading prefix if present
* trim_space(s) - Removes leading and trailing whitespace
* trim_suffix(s, suffix) - Removes trailing suffix if present
* trim(s, cutset) - Removes leading and trailing characters in cutset
# Time Module Summary
Time module provides functionality for measuring and displaying time.
## Constants
* ANSIC, UnixDate, RubyDate, RFC822, RFC822Z, RFC850, RFC1123, RFC1123Z, RFC3339, RFC3339Nano, Kitchen, Stamp, StampMilli, StampMicro, StampNano - Predefined date/time formats
## Functions
* now() - Returns current time
* unix(sec, nsec) - Returns time from Unix timestamp
* parse(layout, value) - Parses string into time object
* since(t) - Returns seconds elapsed since given time
* sleep(duration) - Pauses execution for specified seconds
## Types
* time - Has before(t), after(t), format(layout), utc(), unix() methods