Repository navigation
Expand file tree
/
Copy pathusage.py
More file actions
144 lines (112 loc) · 4.13 KB
/
Copy pathusage.py
File metadata and controls
144 lines (112 loc) · 4.13 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
from antlr4 import *
from SimpleLangLexer import SimpleLangLexer
from SimpleLangParser import SimpleLangParser
from interpreter import Interpreter
def run_code(code: str):
input_stream = InputStream(code)
lexer = SimpleLangLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = SimpleLangParser(token_stream)
tree = parser.program()
interpreter = Interpreter()
return interpreter.visit(tree)
test_code = """
// Statistical operations
let data: array<int> = [3, 1, 4, 1, 5, 9];
data.sort(); // Default ascending sort
print("Ascending:", data);
data.sort(desc); // Descending sort
print("Descending:", data);
let data: array<float> = [1.0, 2.0, 3.0, 4.0, 5.0];
data.mean();
print("Mean:", data_mean);
// Mixed Data Type Array Handling
let data1: array<float> = [5, 12.0, "Banana", "monkey", true, false, 19.5, 30.9, -2.1, "zoo"];
data1.sort();
print("Sorted Array: ", data1);
// Test other operations
data.median();
print("Median:", data_median);
data.variance();
print("Variance:", data_variance);
data.stddev();
print("Standard Deviation:", data_stddev);
// Linear regression
let x: array<float> = [1.0, 2.0, 3.0, 4.0, 5.0];
let y: array<float> = [2.1, 4.0, 6.3, 8.0, 9.9];
x.linreg(y);
print("Linear Regression Results:");
print("Slope:", x_slope);
print("Intercept:", x_intercept);
print("R-squared:", x_r_squared);
// Matrix operations
let matrix1: array<array<float>> = [[1.0, 2.0], [3.0, 4.0]];
let matrix2: array<array<float>> = [[5.0, 6.0], [7.0, 8.0]];
matrix1.add(matrix2);
print("Matrix Addition Result:", matrix1_add);
matrix1.multiply(matrix2);
print("Matrix Multiplication Result:", matrix1_multiply);
matrix1.invert();
print("Matrix Inversion Result:", matrix1_invert);
matrix1.transpose();
print("Matrix Transpose Result:", matrix1_transpose);
// Pattern matching example
let value: int = 42;
match value {
case 0 => print("Value is zero");
case 42 => print("Value is forty two");
case _ => print("Value is something else");
}
// Array pattern matching
let arr: array<int> = [1, 2, 3];
match arr {
case [1, 2, 3] => print("Array matches [1, 2, 3]");
case _ => print("Array does not match");
}
let numbers: array<int> = [1,2,3,5,6,8];
numbers.filter(x => x > 2);
print(numbers_filter);
let numbers: array<int> = [1, 2, 3, 4, 5];
numbers.map(x => x + x * 3);
print(numbers_map);
// Array rotation and shifting
let nums: array<int> = [1, 2, 3, 4, 5];
nums.rotate(1);
print("Rotated Array (by 1):", nums_rotate);
nums.rotate(2);
print("Rotated Array (by 2):", nums_rotate);
nums.rotate(3);
print("Rotated Array (by 3):", nums_rotate);
nums.shift(2);
print("Shifted Array (by 2):", nums_shift);
// "Mary Had a Little Lamb" (complete)
let melody1: array<float> = [3,2,1,2,3,3,3, 2,2,2, 3,5,5, 3,2,1,2,3,3,3, 3,2,2,3,2,1];
print("Playing Mary Had a Little Lamb...");
melody1.play();
// "Twinkle Twinkle Little Star" (complete)
let melody2: array<float> = [0,0,4,4,5,5,4, 3,3,2,2,1,1,0, 4,4,3,3,2,2,1, 4,4,3,3,2,2,1, 0,0,4,4,5,5,4, 3,3,2,2,1,1,0];
print("Playing Twinkle Twinkle...");
melody2.play();
// "Row Row Row Your Boat" (complete with rounds)
let melody3: array<float> = [0,0,0,1,2, 2,1,2,3,4, 7,7,7,4,4,4, 2,2,2,1,1,1, 4,4,4,2,2,2, 0,0,0,1,2, 2,1,2,3,4];
print("Playing Row Row Row Your Boat...");
melody3.play();
// "Hot Cross Buns" (complete with repeats)
let melody4: array<float> = [3,2,1, 3,2,1, 1,1,1,1, 2,2,2,2, 3,2,1, 3,2,1, 3,2,1, 1,1,1,1, 2,2,2,2, 3,2,1];
print("Playing Hot Cross Buns...");
melody4.play();
// "Frère Jacques" (complete with repeats)
let melody5: array<float> = [0,1,2,0, 0,1,2,0, 2,3,4, 2,3,4, 4,5,4,3,2,0, 4,5,4,3,2,0, 0,4,0, 0,4,0, 0,1,2,0, 0,1,2,0];
print("Playing Frère Jacques...");
melody5.play();
// "London Bridge" (complete)
let melody6: array<float> = [4,5,4,3,2,3,4, 2,3,4,3,4,5, 4,5,4,3,2,3,4, 2,4,0, 4,5,4,3,2,3,4, 2,3,4,3,4,5, 4,5,4,3,2,3,4, 2,4,0];
print("Playing London Bridge...");
melody6.play();
// "Baa Baa Black Sheep" (complete)
let melody7: array<float> = [0,0,4,4,5,5,4, 3,3,2,2,1,1,0, 4,4,3,3,2, 4,4,3,3,2, 0,0,4,4,5,5,4, 3,3,2,2,1,1,0];
print("Playing Baa Baa Black Sheep...");
melody7.play();
"""
if __name__ == "__main__":
interpreter = run_code(test_code)