-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodules.smash
More file actions
100 lines (77 loc) · 2.45 KB
/
modules.smash
File metadata and controls
100 lines (77 loc) · 2.45 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
// SmashLang Modules Examples
// Importing a module
import "math.smash";
// Using functions from the imported module
let result = math.add(5, 3);
print("5 + 3 = " + result); // Output: 5 + 3 = 8
let squareRoot = math.sqrt(16);
print("Square root of 16: " + squareRoot); // Output: Square root of 16: 4
// Importing specific functions from a module
import { subtract, multiply } from "math.smash";
print("5 - 3 = " + subtract(5, 3)); // Output: 5 - 3 = 2
print("5 * 3 = " + multiply(5, 3)); // Output: 5 * 3 = 15
// Importing with alias
import { divide as div } from "math.smash";
print("10 / 2 = " + div(10, 2)); // Output: 10 / 2 = 5
// Importing everything with an alias
import * as mathUtils from "math.smash";
print("PI: " + mathUtils.PI); // Output: PI: 3.14159
print("10^2: " + mathUtils.power(10, 2)); // Output: 10^2: 100
// Creating a module
// In file: logger.smash
export fn log(message) {
print("[LOG] " + message);
}
export fn error(message) {
print("[ERROR] " + message);
}
export const LOG_LEVELS = {
INFO: "info",
WARNING: "warning",
ERROR: "error"
};
// Default export
export default fn(message) {
print("[DEFAULT] " + message);
}
// Using the created module
// In another file
import defaultLogger, { log, error, LOG_LEVELS } from "logger.smash";
log("This is an info message"); // Output: [LOG] This is an info message
error("Something went wrong"); // Output: [ERROR] Something went wrong
defaultLogger("Default log message"); // Output: [DEFAULT] Default log message
print("Log level: " + LOG_LEVELS.INFO); // Output: Log level: info
// Dynamic imports
async fn loadModule() {
const utils = await import("utils.smash");
utils.doSomething();
}
loadModule();
// Module with initialization
// In file: database.smash
let connection = null;
export fn connect(url) {
print("Connecting to database at " + url);
connection = { url: url, status: "connected" };
return connection;
}
export fn query(sql) {
if (!connection) {
throw "Not connected to database";
}
print("Executing query: " + sql);
return ["result1", "result2"];
}
export fn disconnect() {
if (connection) {
print("Disconnecting from database");
connection = null;
}
}
// Using the database module
// In another file
import { connect, query, disconnect } from "database.smash";
connect("mongodb://localhost:27017");
let results = query("SELECT * FROM users");
print("Query results: " + results);
disconnect();