-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.du
More file actions
202 lines (167 loc) · 4 KB
/
example.du
File metadata and controls
202 lines (167 loc) · 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
// Duso Script Language Example
// This file demonstrates all syntax highlighting features
// ========================================
// Variables and Types
// ========================================
x = 5
name = "Alice"
flag = true
empty = nil
// ========================================
// Objects and Arrays
// ========================================
colors = ["red", "green", "blue"]
config = {timeout: 30, retries: 3}
// Access elements
first_color = colors[0]
timeout_val = config.timeout
// ========================================
// String Features
// ========================================
// Regular strings with templates
age = 30
msg = "Hello {{name}}, you are {{age}} years old"
sum_msg = "Result={{5 + 10}}"
// Multiline strings (triple quotes)
prompt = """
You are a helpful assistant.
Please respond in JSON format.
Be concise and accurate.
"""
// Multiline with templates
title = "Report"
doc = """
Title={{title}}
This is a multiline document.
It supports {{1 + 1}} expressions.
"""
// ========================================
// Functions and Closures
// ========================================
function add(x, y)
return x + y
end
function greet(name)
msg = "Hello {{name}}!"
return msg
end
// Function with control flow
function process(data)
if data then
result = add(10, 20)
return result
else
return nil
end
end
// ========================================
// Variable Scoping with var Keyword
// ========================================
// var creates a local variable (shadows outer scope)
function testScoping()
var localVar = 100 // var creates a truly local variable
globalVar = 200 // without var, modifies/creates in outer scope
end
globalVar = 10
testScoping()
print(globalVar) // Prints 200 (was modified)
// ========================================
// Closures
// ========================================
// Closures capture and modify variables from outer scope
function makeCounter()
var count = 0
function increment()
count = count + 1 // Modifies captured count
return count
end
return increment
end
counter = makeCounter()
print(counter()) // 1
print(counter()) // 2
print(counter()) // 3
function makeAdder(n)
function add(x)
return x + n
end
return add
end
addFive = makeAdder(5)
print(addFive(10)) // Output: 15
// ========================================
// Control Flow
// ========================================
// If/elseif/else
if x > 10 then
print("big")
elseif x > 5 then
print("medium")
else
print("small")
end
// While loop
counter = 0
while counter < 5 do
print(counter)
counter = counter + 1
end
// Numeric for loop
for i = 1, 5 do
print(i)
end
// Iterator for loop
for item in colors do
print(item)
end
// ========================================
// Operators
// ========================================
// Arithmetic
result = 5 + 3
result = 10 - 2
result = 4 * 5
result = 20 / 4
result = 10 % 3
// Comparison
if x == 5 then print("equal") end
if x != 5 then print("not equal") end
if x < 10 then print("less") end
if x > 3 then print("greater") end
// Logical
if true and false then print("both") end
if true or false then print("one") end
if not false then print("inverted") end
// ========================================
// Exception Handling
// ========================================
try
risky = process(data)
print(risky)
catch (error)
print("Error=" + error)
end
// ========================================
// Built-in Functions
// ========================================
// String functions
upper_text = upper("hello")
lower_text = lower("WORLD")
parts = split("a,b,c", ",")
joined = join(["one", "two"], "-")
// String search and replace
if contains("hello world", "world") then
modified = replace("hello world", "world", "duso")
end
// Type functions
array_len = len(colors)
string_len = len("hello")
t = type(42)
// Array functions
push(colors, "yellow")
sorted = sort([3, 1, 4, 1, 5])
// Math functions
rounded = round(3.7)
absolute = abs(-42)
minimum = min(5, 2, 8)
maximum = max(5, 2, 8)