-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.du
More file actions
135 lines (108 loc) · 2.54 KB
/
Copy pathexample.du
File metadata and controls
135 lines (108 loc) · 2.54 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
// Example Duso Script demonstrating syntax features
// ========================================
// Variables and basic types
// ========================================
name = "Alice"
age = 30
active = true
data = nil
// Arrays and objects
colors = ["red", "green", "blue"]
config = {timeout = 30, retries = 3, debug = true}
// ========================================
// Strings with templates
// ========================================
greeting = "Hello {{name}}, welcome!"
status = "You are {{age}} years old"
// Multiline strings
documentation = """
This is a multiline string.
It can span multiple lines.
With {{name}}'s information.
"""
// ========================================
// Functions
// ========================================
function greet(person, greeting_msg)
result = greeting_msg + " " + person
return result
end
function process_data(items)
if len(items) > 0 then
first = items[0]
return first
else
return nil
end
end
// Anonymous function assigned to variable
multiply = function(x, y)
return x * y
end
// ========================================
// Control flow
// ========================================
// If statement
if age >= 18 then
print("Adult")
elseif age >= 13 then
print("Teen")
else
print("Child")
end
// While loop
counter = 0
while counter < 5 do
print(counter)
counter = counter + 1
end
// For loop - numeric
for i = 1, 10, 2 do
print("Iteration " + tostring(i))
end
// For loop - iterator
for color in colors do
print("Color: " + color)
end
// ========================================
// Error handling
// ========================================
try
result = risky_operation()
print(result)
catch (err)
print("Error occurred: " + err)
end
// ========================================
// Built-in function examples
// ========================================
// String functions
text = "hello world"
upper_text = upper(text)
lower_text = lower(text)
length = len(text)
// Array functions
sorted = sort([3, 1, 4, 1, 5])
doubled = map(sorted, function(x)
return x * 2
end)
filtered = filter(doubled, function(x)
return x > 5
end)
// Type conversions
num_str = tostring(42)
str_num = tonumber("100")
bool_val = tobool(1)
// ========================================
// Variable scoping
// ========================================
global_var = 100
function test_scope()
var local_var = 50 // Local to this function
global_var = 200 // Modifies outer scope
for i = 1, 3 do
item = i * 2 // In outer scope of loop
end
end
test_scope()
print(global_var) // Prints 200