This repository was archived by the owner on Jun 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
70 lines (59 loc) · 1.92 KB
/
example.lua
File metadata and controls
70 lines (59 loc) · 1.92 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
#!/usr/bin/env lua
-- Example of using Syntrax as a library
local Syntrax = require("syntrax")
-- Example 1: Simple rail sequence
print("Example 1: Simple sequence")
local rails, err = Syntrax.execute("l r s")
if err then
print("Error: " .. err.message)
else
print(string.format("Generated %d rails", #rails))
end
print()
-- Example 2: Circle pattern
print("Example 2: Circle with repetition")
rails, err = Syntrax.execute("[l l s] rep 8")
if err then
print("Error: " .. err.message)
else
assert(rails)
print(string.format("Generated %d rails for a complete circle", #rails))
-- Check if we ended up back at north
if #rails > 0 then
local last_direction = rails[#rails].outgoing_direction
if last_direction == 0 then print("Success: Ended facing north again!") end
end
end
print()
-- Example 3: Fork support with initial rail
print("Example 3: Fork with initial rail")
local Directions = require("syntrax.directions")
rails, err = Syntrax.execute("rpush l r s reset s s", 10, Directions.EAST)
if err then
print("Error: " .. err.message)
else
assert(rails)
print(string.format("Generated %d rails from initial rail 10", #rails))
-- First rail should connect to rail 10
if #rails > 0 then print(string.format("First rail parent: %s", rails[1].parent or "nil")) end
end
print()
-- Example 4: Runtime error handling
print("Example 4: Runtime error handling")
rails, err = Syntrax.execute("s s rpop") -- Error: empty stack
if err then
print("Expected runtime error: " .. err.message)
print("Error code: " .. err.code)
if err.span then
local line, col = err.span:get_printable_range()
print(string.format("At line %d, column %d", line, col))
end
end
print()
-- Example 5: Error handling for parse errors
print("Example 5: Parse error handling")
rails, err = Syntrax.execute("invalid code")
if err then
print("Expected error: " .. err.message)
print("Error code: " .. err.code)
end