Skip to content

Commit

Permalink
refactor external
Browse files Browse the repository at this point in the history
  • Loading branch information
glyh committed Nov 8, 2024
1 parent 7071ab2 commit 497eecd
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/bin/main.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ fn main {
if closureps_interpreter.val {
let clops = status.clops.unwrap()
let interpreter = @closureps_eval.CloPSInterpreter::new(clops)
add_clops_interp_fns(interpreter)
try {
interpreter.eval!(clops.root)
} catch {
Expand Down
6 changes: 3 additions & 3 deletions src/bin/externals.mbt → src/closureps_eval/externals.mbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn add_clops_interp_fns(interpreter : @closureps_eval.CloPSInterpreter) -> Unit {
fn CloPSInterpreter::init_extern_fns(interpreter : CloPSInterpreter) -> Unit {
interpreter.add_extern_fn(
"print_char",
fn(args) {
Expand Down Expand Up @@ -29,9 +29,9 @@ fn add_clops_interp_fns(interpreter : @closureps_eval.CloPSInterpreter) -> Unit
},
0,
)
let create_array = fn(args : Array[@closureps_eval.Value]) {
let create_array = fn(args : Array[Value]) {
match args[0] {
Int(n) => @closureps_eval.Value::Array(Array::make(n, args[1]))
Int(n) => Value::Array(Array::make(n, args[1]))
_ => @util.die("create_array expects Int")
}
}
Expand Down
1 change: 1 addition & 0 deletions src/closureps_eval/interpreter.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn CloPSInterpreter::reset_env(self : CloPSInterpreter) -> Unit {
pub fn CloPSInterpreter::new(clops : @closureps.ClosurePS) -> CloPSInterpreter {
let ret = { extern_fns: Map::new(), clops, cur_env: @hashmap.new() }
ret.reset_env()
ret.init_extern_fns()
ret
}

Expand Down
3 changes: 2 additions & 1 deletion src/closureps_eval/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"path": "moonbitlang/minimbt",
"alias": "types"
},
"moonbitlang/minimbt/util"
"moonbitlang/minimbt/util",
"lijunchen/unstable_io/io"
],
"test-import": [
"moonbitlang/minimbt/parser",
Expand Down
File renamed without changes.
30 changes: 19 additions & 11 deletions test/source_generator/matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[l, n, m] = [10, 10, 10]

def generate_mat(m: int, n: int, min_value: float = -10.0, max_value: float = 10.0) -> List[List[float]]:
def generate_mat(m: int, n: int, min_value: float = 0.0, max_value: float = 10.0) -> List[List[float]]:
"""
Generates an m x n matrix filled with random floating-point numbers.
Expand Down Expand Up @@ -48,8 +48,12 @@ def format_mat(symbol: str, m: int, n: int, mat: List[List[float]]) -> str:
operate_mat += "\n" + indent_str + f"let _ = matmul({l},{n},{m},a,b,c);"


operate_mat += "\n" + indent_str + f"matshow({l}, {m}, c)"

operate_mat += "\n" + indent_str + f"let _ = matshow({l}, {n}, a);"
operate_mat += "\n" + indent_str + f"let _ = print_endline();"
operate_mat += "\n" + indent_str + f"let _ = matshow({n}, {m}, b);"
operate_mat += "\n" + indent_str + f"let _ = print_endline();"
operate_mat += "\n" + indent_str + f"let _ = matshow({l}, {m}, c);"
operate_mat += "\n" + indent_str + f"()"
operate_mat += "\n"

generated = """
Expand Down Expand Up @@ -150,17 +154,21 @@ def matrix_multiply(matrix_a, matrix_b):

return result

def float_to_ieee754(float_value):
import struct
# Pack the float as a binary string (double precision)
packed = struct.pack('>d', float_value) # '>d' means big-endian double
# Convert the packed binary string to an integer representation
integer_representation = int.from_bytes(packed, byteorder='big')
return integer_representation

mat_c = matrix_multiply(mat_a, mat_b)

with open(ans_path, 'w') as ans_file:
for row in mat_a:
for ele in row:
ans_file.write(f'{int(ele)} ')
ans_file.write('\n')
ans_file.write('\n')

for row in mat_b:
for ele in row:
ans_file.write(f'{int(ele)} ')
ans_file.write('\n')
ans_file.write('\n')

for row in mat_c:
for ele in row:
ans_file.write(f'{int(ele)} ')
Expand Down
177 changes: 177 additions & 0 deletions test/source_generator/matmul_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/usr/bin/env python
# Multiply a n by m matrix and a m by k matrix
import random
from typing import List

[l, n, m] = [18, 15, 16]

def generate_mat(m: int, n: int, min_value: int = -8, max_value: int = 8) -> List[List[int]]:
"""
Generates an m x n matrix filled with random floating-point numbers.
:param m: Number of rows
:param n: Number of columns
:param min_value: Minimum value for random floats (inclusive)
:param max_value: Maximum value for random floats (inclusive)
:return: m x n matrix with random floating-point numbers
"""
return [[random.randint(min_value, max_value) for _ in range(n)] for _ in range(m)]


indent = 2
indent_str = " " * indent

def format_mat(symbol: str, m: int, n: int, mat: List[List[int]]) -> str:
ret = ""
for row in range(m):
for col in range(n):
ret += indent_str if col == 0 else "; "
ret += f"{symbol}[{row}][{col}] = {mat[row][col]}"
ret += ";\n"

return ret


operate_mat = \
f"""
let a = gen_arr({l}, {n});
let b = gen_arr({n}, {m});
let c = gen_arr({l}, {m});
"""

mat_a = generate_mat(l, n)
operate_mat += "\n" + format_mat("a", l, n, mat_a)

mat_b = generate_mat(n, m)
operate_mat += "\n" + format_mat("b", n, m, mat_b)

operate_mat += "\n" + indent_str + f"let _ = matmul({l},{n},{m},a,b,c);"


operate_mat += "\n" + indent_str + f"let _ = matshow({l}, {n}, a);"
operate_mat += "\n" + indent_str + f"let _ = print_endline();"
operate_mat += "\n" + indent_str + f"let _ = matshow({n}, {m}, b);"
operate_mat += "\n" + indent_str + f"let _ = print_endline();"
operate_mat += "\n" + indent_str + f"let _ = matshow({l}, {m}, c);"
operate_mat += "\n" + indent_str + f"()"
operate_mat += "\n"

generated = """
fn matshow(m: Int, n: Int, mat: Array[Array[Int]]) -> Unit {
fn loop1(i: Int) -> Unit {
if i <= m - 1 {
fn loop2(j: Int) -> Unit {
if j <= n - 1 {
let _ = print_int(mat[i][j]);
let _ = print_char(32);
loop2(j+1)
} else {
print_endline()
}
};
let _ = loop2(0);
loop1(i+1)
} else {
()
}
};
loop1(0)
};
fn matmul(l: Int, m: Int, n: Int, a: Array[Array[Int]], b: Array[Array[Int]], c: Array[Array[Int]]) -> Unit {
fn loop1(i: Int) -> Unit {
if 0 <= i {
fn loop2(j: Int) -> Unit {
if 0 <= j {
fn loop3(k: Int) -> Unit {
if 0 <= k {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
loop3(k - 1)
} else {
()
}
};
let _ = loop3(m - 1);
loop2(j - 1)
} else {
()
}
};
let _ = loop2(n - 1);
loop1(i - 1)
} else {
()
}
};
loop1(l - 1)
};
fn main {
let dummy = Array::make(0, 0);
fn gen_arr(m: Int, n: Int) -> Array[Array[Int]] {
let mat = Array::make(m, dummy);
fn init_arr(i: Int) -> Unit {
if 0 <= i {
mat[i] = Array::make(n, 0);
init_arr(i - 1)
} else {
()
}
};
let _ = init_arr(m - 1);
mat
};
""" \
+ operate_mat + \
"""
};"""

source_path = "../test_src/matmul_gen_int.mbt"
ans_path = "../test_src/matmul_gen_int.ans"

with open(source_path, 'w') as source_file:
source_file.write(generated)

def matrix_multiply(matrix_a, matrix_b):
# Get the dimensions of the matrices
rows_a = len(matrix_a)
cols_a = len(matrix_a[0])
rows_b = len(matrix_b)
cols_b = len(matrix_b[0])

# Check if multiplication is possible
if cols_a != rows_b:
raise ValueError("Cannot multiply: The number of columns in matrix A must equal the number of rows in matrix B.")

# Initialize the result matrix with zeros
result = [[0 for _ in range(cols_b)] for _ in range(rows_a)]

# Perform multiplication
for i in range(rows_a):
for j in range(cols_b):
for k in range(cols_a):
result[i][j] += matrix_a[i][k] * matrix_b[k][j]

return result

mat_c = matrix_multiply(mat_a, mat_b)

with open(ans_path, 'w') as ans_file:
for row in mat_a:
for ele in row:
ans_file.write(f'{int(ele)} ')
ans_file.write('\n')
ans_file.write('\n')

for row in mat_b:
for ele in row:
ans_file.write(f'{int(ele)} ')
ans_file.write('\n')
ans_file.write('\n')

for row in mat_c:
for ele in row:
ans_file.write(f'{int(ele)} ')
ans_file.write('\n')


42 changes: 32 additions & 10 deletions test/test_src/matmul_gen.ans
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
-67 24 124 106 -53 -82 -114 82 -165 15
-30 -93 -94 -36 -44 23 -56 58 -15 29
-85 -91 171 67 132 5 22 116 -112 12
152 4 -76 29 -39 -40 -139 -28 -75 25
-49 137 208 21 72 46 28 -1 -39 -2
104 -9 -213 41 -71 -39 -22 -181 67 34
45 122 -27 -54 -18 -15 102 101 -86 -141
-145 8 -90 -46 -180 -57 190 48 71 -47
117 -184 -129 -50 179 83 47 -42 40 42
-38 -99 57 140 124 88 -91 35 57 153
6 7 7 6 5 6 8 8 2 4
8 2 5 2 9 6 0 2 5 9
2 7 8 8 3 9 7 2 3 2
1 1 9 6 3 4 4 7 6 9
8 3 2 9 6 9 2 2 7 6
8 6 5 6 1 2 9 0 2 9
3 1 4 7 1 2 1 7 9 0
9 9 2 2 2 1 9 0 3 5
7 9 2 4 5 1 7 10 7 7
1 8 4 9 7 6 5 7 2 3

2 7 1 7 3 5 9 8 7 8
8 4 4 1 3 9 6 5 9 2
5 1 5 5 8 4 0 3 3 2
6 6 5 8 8 8 9 0 0 3
2 1 6 8 2 4 7 2 8 0
2 4 5 0 4 8 9 1 2 2
8 3 0 9 4 1 2 1 2 5
9 5 8 1 7 3 4 7 7 3
9 0 1 1 1 6 8 1 1 6
4 5 7 9 2 2 5 9 8 8

391 277 307 340 327 348 380 281 342 255
264 222 274 311 217 296 370 262 319 250
335 227 250 286 288 343 350 188 239 200
354 210 291 302 282 277 311 226 254 233
312 258 264 323 254 366 451 235 280 269
317 247 207 351 241 281 329 253 277 277
289 156 180 173 207 243 281 144 160 177
295 220 168 284 193 267 306 240 283 248
430 279 288 332 281 338 399 319 379 300
365 247 301 300 297 340 371 225 294 197
47 changes: 26 additions & 21 deletions test/test_src/matmul_gen.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,34 @@ fn main {
let b = gen_arr(10, 10);
let c = gen_arr(10, 10);

a[0][0] = 5.71; a[0][1] = 2.99; a[0][2] = 1.27; a[0][3] = -1.2; a[0][4] = 3.85; a[0][5] = -2.38; a[0][6] = 7.07; a[0][7] = -3.66; a[0][8] = -0.77; a[0][9] = 7.66;
a[1][0] = -7.72; a[1][1] = -3.64; a[1][2] = -4.7; a[1][3] = 0.96; a[1][4] = -3.05; a[1][5] = -5.44; a[1][6] = 5.15; a[1][7] = -9.01; a[1][8] = -0.81; a[1][9] = 1.0;
a[2][0] = 6.42; a[2][1] = 3.88; a[2][2] = 1.39; a[2][3] = -6.05; a[2][4] = -0.51; a[2][5] = 5.65; a[2][6] = 7.8; a[2][7] = 5.27; a[2][8] = -0.85; a[2][9] = 2.36;
a[3][0] = -3.69; a[3][1] = -6.34; a[3][2] = -1.18; a[3][3] = 0.18; a[3][4] = 6.54; a[3][5] = 3.33; a[3][6] = -3.95; a[3][7] = -5.8; a[3][8] = 2.27; a[3][9] = 1.84;
a[4][0] = 6.37; a[4][1] = 5.16; a[4][2] = 9.95; a[4][3] = 4.57; a[4][4] = 3.51; a[4][5] = 6.27; a[4][6] = 3.89; a[4][7] = 9.66; a[4][8] = 0.43; a[4][9] = -2.1;
a[5][0] = -2.21; a[5][1] = -8.26; a[5][2] = -7.76; a[5][3] = -3.47; a[5][4] = 9.23; a[5][5] = -3.59; a[5][6] = -4.49; a[5][7] = -1.02; a[5][8] = -0.53; a[5][9] = -6.34;
a[6][0] = 8.49; a[6][1] = 8.29; a[6][2] = 3.39; a[6][3] = -6.45; a[6][4] = -4.2; a[6][5] = 9.37; a[6][6] = -7.88; a[6][7] = -3.89; a[6][8] = 3.7; a[6][9] = -8.51;
a[7][0] = -1.3; a[7][1] = 9.36; a[7][2] = -7.58; a[7][3] = -2.48; a[7][4] = -5.52; a[7][5] = -6.25; a[7][6] = 0.95; a[7][7] = -5.38; a[7][8] = -6.98; a[7][9] = -1.37;
a[8][0] = -6.29; a[8][1] = -8.16; a[8][2] = -6.31; a[8][3] = -5.41; a[8][4] = 0.72; a[8][5] = 8.89; a[8][6] = -1.03; a[8][7] = 3.63; a[8][8] = 2.56; a[8][9] = -8.58;
a[9][0] = -9.7; a[9][1] = -6.76; a[9][2] = 8.55; a[9][3] = -6.56; a[9][4] = 1.11; a[9][5] = -6.31; a[9][6] = 7.5; a[9][7] = 2.46; a[9][8] = 1.38; a[9][9] = -4.59;
a[0][0] = 6.31; a[0][1] = 7.15; a[0][2] = 7.98; a[0][3] = 6.0; a[0][4] = 5.97; a[0][5] = 6.64; a[0][6] = 8.35; a[0][7] = 8.73; a[0][8] = 2.27; a[0][9] = 4.85;
a[1][0] = 8.03; a[1][1] = 2.96; a[1][2] = 5.47; a[1][3] = 2.95; a[1][4] = 9.4; a[1][5] = 6.64; a[1][6] = 0.7; a[1][7] = 2.87; a[1][8] = 5.56; a[1][9] = 9.94;
a[2][0] = 2.94; a[2][1] = 7.27; a[2][2] = 8.14; a[2][3] = 8.42; a[2][4] = 3.82; a[2][5] = 9.53; a[2][6] = 7.19; a[2][7] = 2.6; a[2][8] = 3.39; a[2][9] = 2.6;
a[3][0] = 1.04; a[3][1] = 1.34; a[3][2] = 9.74; a[3][3] = 6.77; a[3][4] = 3.73; a[3][5] = 4.77; a[3][6] = 4.71; a[3][7] = 7.22; a[3][8] = 6.39; a[3][9] = 9.43;
a[4][0] = 8.74; a[4][1] = 3.57; a[4][2] = 2.89; a[4][3] = 9.43; a[4][4] = 6.62; a[4][5] = 9.37; a[4][6] = 2.04; a[4][7] = 2.07; a[4][8] = 7.97; a[4][9] = 6.7;
a[5][0] = 8.65; a[5][1] = 6.86; a[5][2] = 5.11; a[5][3] = 6.66; a[5][4] = 1.59; a[5][5] = 2.54; a[5][6] = 9.67; a[5][7] = 0.32; a[5][8] = 2.83; a[5][9] = 9.32;
a[6][0] = 3.88; a[6][1] = 1.13; a[6][2] = 4.18; a[6][3] = 7.37; a[6][4] = 1.26; a[6][5] = 2.89; a[6][6] = 1.56; a[6][7] = 7.53; a[6][8] = 9.79; a[6][9] = 0.84;
a[7][0] = 9.31; a[7][1] = 9.95; a[7][2] = 2.23; a[7][3] = 2.76; a[7][4] = 2.6; a[7][5] = 1.96; a[7][6] = 9.16; a[7][7] = 0.87; a[7][8] = 3.59; a[7][9] = 5.92;
a[8][0] = 7.85; a[8][1] = 9.66; a[8][2] = 2.9; a[8][3] = 4.67; a[8][4] = 5.05; a[8][5] = 1.36; a[8][6] = 7.63; a[8][7] = 10.0; a[8][8] = 7.09; a[8][9] = 7.7;
a[9][0] = 1.67; a[9][1] = 8.16; a[9][2] = 4.85; a[9][3] = 9.2; a[9][4] = 7.42; a[9][5] = 6.94; a[9][6] = 5.88; a[9][7] = 7.84; a[9][8] = 2.55; a[9][9] = 3.77;

b[0][0] = -2.63; b[0][1] = 3.98; b[0][2] = 1.61; b[0][3] = -1.89; b[0][4] = 0.13; b[0][5] = 1.19; b[0][6] = -1.68; b[0][7] = 0.28; b[0][8] = -5.81; b[0][9] = -8.44;
b[1][0] = -4.75; b[1][1] = 9.77; b[1][2] = 4.18; b[1][3] = 2.71; b[1][4] = -9.21; b[1][5] = -9.9; b[1][6] = 8.85; b[1][7] = 1.44; b[1][8] = -1.18; b[1][9] = 1.05;
b[2][0] = -0.99; b[2][1] = 6.96; b[2][2] = 9.88; b[2][3] = 4.38; b[2][4] = 0.98; b[2][5] = 4.82; b[2][6] = -5.71; b[2][7] = 6.05; b[2][8] = -1.27; b[2][9] = 0.28;
b[3][0] = 0.06; b[3][1] = 8.84; b[3][2] = -1.21; b[3][3] = -9.9; b[3][4] = -4.34; b[3][5] = 3.51; b[3][6] = 0.17; b[3][7] = -5.78; b[3][8] = 2.15; b[3][9] = -3.5;
b[4][0] = 5.02; b[4][1] = 9.02; b[4][2] = 0.08; b[4][3] = 9.65; b[4][4] = -8.58; b[4][5] = -8.78; b[4][6] = -5.9; b[4][7] = -7.91; b[4][8] = -4.82; b[4][9] = 5.38;
b[5][0] = 5.08; b[5][1] = -3.86; b[5][2] = 3.38; b[5][3] = -4.63; b[5][4] = 6.48; b[5][5] = 3.64; b[5][6] = 0.95; b[5][7] = 6.73; b[5][8] = -7.37; b[5][9] = -2.45;
b[6][0] = -7.99; b[6][1] = -6.09; b[6][2] = 7.38; b[6][3] = 1.78; b[6][4] = 6.5; b[6][5] = 3.65; b[6][6] = -3.25; b[6][7] = 6.81; b[6][8] = -7.96; b[6][9] = 3.74;
b[7][0] = -1.79; b[7][1] = -4.53; b[7][2] = 5.25; b[7][3] = 2.04; b[7][4] = 9.67; b[7][5] = 1.1; b[7][6] = 6.72; b[7][7] = -7.87; b[7][8] = 9.71; b[7][9] = 4.72;
b[8][0] = 7.46; b[8][1] = 0.52; b[8][2] = 0.21; b[8][3] = 3.48; b[8][4] = 8.02; b[8][5] = -7.27; b[8][6] = -9.31; b[8][7] = -3.12; b[8][8] = -7.3; b[8][9] = 4.97;
b[9][0] = 1.5; b[9][1] = -5.62; b[9][2] = 8.34; b[9][3] = 5.42; b[9][4] = 1.48; b[9][5] = -6.05; b[9][6] = -7.68; b[9][7] = 3.84; b[9][8] = -4.91; b[9][9] = 3.17;
b[0][0] = 2.02; b[0][1] = 7.28; b[0][2] = 1.17; b[0][3] = 7.19; b[0][4] = 3.7; b[0][5] = 5.99; b[0][6] = 9.42; b[0][7] = 8.49; b[0][8] = 7.97; b[0][9] = 8.95;
b[1][0] = 8.18; b[1][1] = 4.6; b[1][2] = 4.47; b[1][3] = 1.45; b[1][4] = 3.6; b[1][5] = 9.14; b[1][6] = 6.36; b[1][7] = 5.98; b[1][8] = 9.16; b[1][9] = 2.4;
b[2][0] = 5.72; b[2][1] = 1.34; b[2][2] = 5.94; b[2][3] = 5.4; b[2][4] = 8.43; b[2][5] = 4.75; b[2][6] = 0.65; b[2][7] = 3.19; b[2][8] = 3.51; b[2][9] = 2.16;
b[3][0] = 6.69; b[3][1] = 6.1; b[3][2] = 5.43; b[3][3] = 8.81; b[3][4] = 8.12; b[3][5] = 8.02; b[3][6] = 9.91; b[3][7] = 0.88; b[3][8] = 0.06; b[3][9] = 3.12;
b[4][0] = 2.19; b[4][1] = 1.89; b[4][2] = 6.85; b[4][3] = 8.73; b[4][4] = 2.11; b[4][5] = 4.48; b[4][6] = 7.25; b[4][7] = 2.83; b[4][8] = 8.08; b[4][9] = 0.63;
b[5][0] = 2.39; b[5][1] = 4.83; b[5][2] = 5.05; b[5][3] = 0.65; b[5][4] = 4.59; b[5][5] = 8.7; b[5][6] = 9.11; b[5][7] = 1.7; b[5][8] = 2.49; b[5][9] = 2.34;
b[6][0] = 8.91; b[6][1] = 3.72; b[6][2] = 0.13; b[6][3] = 9.1; b[6][4] = 4.53; b[6][5] = 1.97; b[6][6] = 2.67; b[6][7] = 1.92; b[6][8] = 2.42; b[6][9] = 5.23;
b[7][0] = 9.89; b[7][1] = 5.62; b[7][2] = 8.5; b[7][3] = 1.06; b[7][4] = 7.61; b[7][5] = 3.62; b[7][6] = 4.59; b[7][7] = 7.37; b[7][8] = 7.92; b[7][9] = 3.33;
b[8][0] = 9.95; b[8][1] = 0.36; b[8][2] = 1.23; b[8][3] = 1.42; b[8][4] = 1.14; b[8][5] = 6.96; b[8][6] = 8.51; b[8][7] = 1.03; b[8][8] = 1.7; b[8][9] = 6.18;
b[9][0] = 4.66; b[9][1] = 5.5; b[9][2] = 7.44; b[9][3] = 9.1; b[9][4] = 2.53; b[9][5] = 2.16; b[9][6] = 5.21; b[9][7] = 9.08; b[9][8] = 8.29; b[9][9] = 8.15;

let _ = matmul(10,10,10,a,b,c);
matshow(10, 10, c)
let _ = matshow(10, 10, a);
let _ = print_endline();
let _ = matshow(10, 10, b);
let _ = print_endline();
let _ = matshow(10, 10, c);
()

};
Loading

0 comments on commit 497eecd

Please sign in to comment.