-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.php
More file actions
194 lines (163 loc) · 7.43 KB
/
examples.php
File metadata and controls
194 lines (163 loc) · 7.43 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
<?php $title = 'Examples - Bloa Programming Language'; include 'header.php'; ?>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
<div class="mb-12">
<h1 class="text-5xl md:text-6xl font-bold mb-4 bg-gradient-to-r from-cyan-400 to-blue-400 bg-clip-text text-transparent">Code Examples</h1>
<p class="text-xl text-gray-600 dark:text-gray-300">Learn Bloa with practical examples and best practices</p>
</div>
<div class="space-y-12">
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-code text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">Hello World</h2>
</div>
<p class="mb-6 text-gray-700 dark:text-gray-300">The classic first program to get started:</p>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code>say("Hello, World!")</code></pre>
</section>
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-sliders-h text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">Variables and Expressions</h2>
</div>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code>x = 42
y = x * 2 + 10
name = "Bloa"
is_cool = true
say("x = " + str(x))
say("y = " + str(y))
say("Language: " + name)
say("Is cool? " + str(is_cool))</code></pre>
</section>
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-function text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">Functions</h2>
</div>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code>function factorial(n) {
if n <= 1 {
return 1
} else {
return n * factorial(n - 1)
}
}
function greet(name, age) {
say("Hello, " + name + "! You are " + str(age) + " years old.")
}
result = factorial(5)
say("5! = " + str(result))
greet("Alice", 25)</code></pre>
</section>
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-object-group text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">Classes and Inheritance</h2>
</div>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code>class Shape {
function __init__(self, color) {
self.color = color
}
function describe(self) {
say("A " + self.color + " shape")
}
}
class Circle extends Shape {
function __init__(self, color, radius) {
super().__init__(color)
self.radius = radius
}
function area(self) {
return 3.14159 * self.radius * self.radius
}
function describe(self) {
say("A " + self.color + " circle with radius " + str(self.radius))
}
}
circle = Circle("red", 5)
circle.describe()
say("Area: " + str(circle.area()))</code></pre>
</section>
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-list text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">Lists and Loops</h2>
</div>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code># Create a list
numbers = [1, 2, 3, 4, 5]
# Add items
append(numbers, 6)
append(numbers, 7)
# Iterate
for num in numbers {
say("Number: " + str(num))
}
# List comprehension style (manual)
squares = []
for i in range(1, 6) {
append(squares, i * i)
}
say("Squares: " + str(squares))</code></pre>
</section>
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-file-alt text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">File I/O</h2>
</div>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code>import io
# Write to file
io.write_file("example.txt", "Hello from Bloa!")
# Read from file
content = io.read_file("example.txt")
say("File content: " + content)
# Check if file exists
if io.exists("example.txt") {
say("File exists!")
} else {
say("File does not exist.")
}</code></pre>
</section>
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-calculator text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">Math Operations</h2>
</div>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code>import math
# Basic math
a = 16
sqrt_a = math.sqrt(a)
say("Square root of " + str(a) + " is " + str(sqrt_a))
# Trigonometry
angle = 45
sin_val = math.sin(angle * math.pi() / 180)
cos_val = math.cos(angle * math.pi() / 180)
say("sin(45°) = " + str(sin_val))
say("cos(45°) = " + str(cos_val))
# Power
result = math.pow(2, 8)
say("2^8 = " + str(result))</code></pre>
</section>
<section class="card-hover bg-white dark:bg-gray-900 p-8 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800">
<div class="flex items-center mb-6">
<i class="fas fa-exclamation-triangle text-2xl text-cyan-400 dark:text-cyan-300 mr-3"></i>
<h2 class="text-3xl font-bold text-gray-900 dark:text-white">Exception Handling</h2>
</div>
<pre class="code bg-gray-50 dark:bg-black text-gray-900 dark:text-gray-100 p-6 rounded-lg border border-gray-200 dark:border-gray-800 overflow-x-auto"><code>function divide(a, b) {
if b == 0 {
throw "Division by zero!"
}
return a / b
}
try {
result = divide(10, 0)
say("Result: " + str(result))
} except error {
say("Error: " + error)
}
try {
result = divide(10, 2)
say("Result: " + str(result))
} except error {
say("Error: " + error)
}</code></pre>
</section>
</div>
</div>
<?php include 'footer.php'; ?>