http server
This commit is contained in:
@@ -91,6 +91,10 @@ gen_val :: () -> string {
|
||||
return "print(\"insert-gen: {}\\n\", 42);";
|
||||
}
|
||||
|
||||
// --- Foreign function binding ---
|
||||
libc :: #library "c";
|
||||
c_abs :: (n: s32) -> s32 #foreign libc "abs";
|
||||
|
||||
// ============================================================
|
||||
main :: {
|
||||
|
||||
@@ -634,76 +638,76 @@ END;
|
||||
|
||||
// For loop basic
|
||||
farr : [4]s32 = .[10, 20, 30, 40];
|
||||
write("for:");
|
||||
out("for:");
|
||||
for farr: (it) {
|
||||
write(" ");
|
||||
write(int_to_string(it));
|
||||
out(" ");
|
||||
out(int_to_string(it));
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For with print
|
||||
write("for-print:");
|
||||
out("for-print:");
|
||||
for farr: (it) {
|
||||
print(" {}", it);
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For with index
|
||||
write("for-idx:");
|
||||
out("for-idx:");
|
||||
for farr: (_, ix) {
|
||||
write(" ");
|
||||
write(int_to_string(ix));
|
||||
out(" ");
|
||||
out(int_to_string(ix));
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For with print two args
|
||||
write("for-2arg:");
|
||||
out("for-2arg:");
|
||||
for farr: (it, ix) {
|
||||
print(" {}@{}", it, ix);
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For with break
|
||||
write("for-break:");
|
||||
out("for-break:");
|
||||
for farr: (it) {
|
||||
if it == 30 { break; }
|
||||
print(" {}", it);
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For with continue
|
||||
write("for-continue:");
|
||||
out("for-continue:");
|
||||
for farr: (it) {
|
||||
if it == 20 { continue; }
|
||||
print(" {}", it);
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For on slice
|
||||
fsl : []s32 = .[10, 20, 30];
|
||||
write("for-slice:");
|
||||
out("for-slice:");
|
||||
for fsl: (it) {
|
||||
print(" {}", it);
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For on slice with index
|
||||
write("for-slice-idx:");
|
||||
out("for-slice-idx:");
|
||||
for fsl: (it, ix) {
|
||||
print(" {}:{}", ix, it);
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// Nested for
|
||||
nf_a : [2]s32 = .[0, 1];
|
||||
nf_b : [2]s32 = .[0, 1];
|
||||
write("for-nested:");
|
||||
out("for-nested:");
|
||||
for nf_a: (oa) {
|
||||
for nf_b: (ob) {
|
||||
print(" ({},{})", oa, ob);
|
||||
}
|
||||
}
|
||||
write("\n");
|
||||
out("\n");
|
||||
|
||||
// For with break preserving index
|
||||
fbi : [5]s32 = .[10, 20, 30, 40, 50];
|
||||
@@ -857,8 +861,8 @@ END;
|
||||
// ========================================================
|
||||
print("=== 7. Builtins ===\n");
|
||||
|
||||
// write
|
||||
write("write-ok\n");
|
||||
// out
|
||||
out("out-ok\n");
|
||||
|
||||
// sqrt
|
||||
print("sqrt: {}\n", sqrt(9.0));
|
||||
@@ -932,12 +936,12 @@ END;
|
||||
|
||||
// field_value (use any_to_string to avoid sext-on-Any bug)
|
||||
fv_pt := Point.{ 11, 22 };
|
||||
write("fieldval0: ");
|
||||
write(any_to_string(field_value(fv_pt, 0)));
|
||||
write("\n");
|
||||
write("fieldval1: ");
|
||||
write(any_to_string(field_value(fv_pt, 1)));
|
||||
write("\n");
|
||||
out("fieldval0: ");
|
||||
out(any_to_string(field_value(fv_pt, 0)));
|
||||
out("\n");
|
||||
out("fieldval1: ");
|
||||
out(any_to_string(field_value(fv_pt, 1)));
|
||||
out("\n");
|
||||
|
||||
// field_index on plain enum
|
||||
fi_c : Color = .green;
|
||||
@@ -1039,5 +1043,13 @@ END;
|
||||
print("3-way: {} {} {}\n", ra, rb, rc);
|
||||
}
|
||||
|
||||
// ========================================================
|
||||
// 15. FOREIGN FUNCTION BINDING
|
||||
// ========================================================
|
||||
print("=== 15. Foreign ===\n");
|
||||
|
||||
// Symbol rename: c_abs maps to C's abs()
|
||||
print("foreign-rename: {}\n", c_abs(xx -42));
|
||||
|
||||
print("=== DONE ===\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user