- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomplex.lua
More file actions
Latest commit
165 lines (130 loc) · 3.88 KB
/
Copy pathcomplex.lua
File metadata and controls
165 lines (130 loc) · 3.88 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
-- Metatable example: Vector operations
Vector= {}
Vector.__index=Vector
functionVector.new(x, y, z)
localvec= {x=xor0, y=yor0, z=zor0}
setmetatable(vec, Vector)
returnvec
end
functionVector:__add(other)
returnVector.new(self.x+other.x, self.y+other.y, self.z+other.z)
end
functionVector:__sub(other)
returnVector.new(self.x-other.x, self.y-other.y, self.z-other.z)
end
functionVector:__tostring()
return"(" ..self.x..", " ..self.y..", " ..self.z..")"
end
functionVector:__call()
print( "Vector called with: ", self )
end
-- Using the vector class
localv1=Vector.new(1, 2, 3)
localv2=Vector.new(4, 5, 6)
localv3=v1+v2
print("Vector Addition: " ..tostring(v3))
localv4=v2-v1
print("Vector Subtraction: " ..tostring(v4))
v1()
-- Closure example: Counter
functioncreate_counter(start)
localcount=startor0
returnfunction(increment)
count=count+ (incrementor1)
returncount
end
end
localcounter1=create_counter(10)
localcounter2=create_counter(100)
print("Counter 1:", counter1(2)) -- 12
print("Counter 1:", counter1(3)) -- 15
print("Counter 2:", counter2(5)) -- 105
print("Counter 2:", counter2(10)) -- 115
-- Table manipulation and recursive function example: Merge Sort
functionmerge_sort(arr)
if#arr<=1then
returnarr
end
localmid=math.floor(#arr/2)
localleft=merge_sort({table.unpack(arr, 1, mid)})
localright=merge_sort({table.unpack(arr, mid+1, #arr)})
returnmerge(left, right)
end
functionmerge(left, right)
localresult= {}
locali, j=1, 1
whilei<=#leftandj<=#rightdo
ifleft[i] <right[j] then
table.insert(result, left[i])
i=i+1
else
table.insert(result, right[j])
j=j+1
end
end
whilei<=#leftdo
table.insert(result, left[i])
i=i+1
end
whilej<=#rightdo
table.insert(result, right[j])
j=j+1
end
returnresult
end
-- Test merge sort
localunsorted= {38, 27, 43, 3, 9, 82, 10}
localsorted=merge_sort(unsorted)
print("Sorted Array:")
print( table.concat(sorted, ", ") )
foridx, vinipairs(sorted) do
print(idx, v)
end
-- Coroutine example: Fibonacci generator
functionfibonacci()
locala, b=0, 1
returncoroutine.create(function()
whiletruedo
ifa>100then
return"Done"
end
coroutine.yield(a)
a, b=b, a+b
end
end)
end
print( 'Fibonacci numbers with coroutine:' )
localfib=fibonacci()
whiletruedo
localsuccess, value=coroutine.resume(fib)
ifsuccess==falsethen
print("Error: " ..value)
break
else
print("Fibonacci number " ..tostring(success) ..", " ..": " ..value)
end
end
print( 'coroutine.wrap test' )
w=coroutine.wrap( function()
fori=1, 10do
coroutine.yield( i )
end
return100
end )
fori=1, 12do
print( w() )
end
--[===[
This code was generated by ChatGPT4o
Explanation of the code components:
Vector Operations with Metatables:
Defines a Vector class using metatables for vector addition, subtraction, and string representation.
The vectors are used and printed to demonstrate their operations.
Closure Example (Counter):
A create_counter function that creates a closure, allowing state to persist across function calls.
Coroutine Example (Fibonacci Generator):
An implementation of merge sort, demonstrating recursion, table manipulation, and table sorting.
This code combines many Lua features and should thoroughly test your Lua parser! Let me know if you'd like any modifications or additional features added.
A Fibonacci number generator using coroutines, yielding the next number in the sequence on each resume.
Recursive Function (Merge Sort):
]===]