Browse files

* core/string.c: Bytes `string` method converts to a UTF-8 string.

 * core/vm-x86.c: fix for callset op.
  • Loading branch information...
1 parent bf84b91 commit 32d5a3d5fe9c9ac08fe5b557bfc7ce96654c3d55 _why committed Jul 14, 2009
Showing with 74 additions and 4 deletions.
  1. +8 −2 core/number.c
  2. +11 −1 core/string.c
  3. +1 −1 core/vm-x86.c
  4. +52 −0 example/spectral-norm.pn
  5. +2 −0 test/numbers/neg.pn
View
10 core/number.c
@@ -32,6 +32,10 @@ PN potion_pow(Potion *P, PN cl, PN num, PN sup) {
return potion_real(P, z);
}
+PN potion_sqrt(Potion *P, PN cl, PN num) {
+ return potion_real(P, sqrt(PN_DBL(num)));
+}
+
#define PN_NUM_MATH(int_math) \
if (PN_IS_NUM(self) && PN_IS_NUM(num)) \
return PN_NUM(PN_INT(self) int_math PN_INT(num)); \
@@ -91,11 +95,11 @@ static PN potion_num_step(Potion *P, PN cl, PN self, PN end, PN step, PN block)
}
PN potion_num_string(Potion *P, PN closure, PN self) {
- char ints[32];
+ char ints[40];
if (PN_IS_NUM(self)) {
sprintf(ints, "%ld", PN_INT(self));
} else {
- int len = sprintf(ints, "%.10f", ((struct PNDecimal *)self)->value);
+ int len = sprintf(ints, "%.16f", ((struct PNDecimal *)self)->value);
while (len > 0 && ints[len - 1] == '0') len--;
if (ints[len - 1] == '.') len++;
ints[len] = '\0';
@@ -128,7 +132,9 @@ void potion_num_init(Potion *P) {
potion_method(num_vt, "~", potion_bitn, 0);
potion_method(num_vt, "<<", potion_bitl, "value=N");
potion_method(num_vt, ">>", potion_bitr, "value=N");
+ potion_method(num_vt, "**", potion_pow, "value=N");
potion_method(num_vt, "number", potion_num_number, 0);
+ potion_method(num_vt, "sqrt", potion_sqrt, 0);
potion_method(num_vt, "step", potion_num_step, "end=N,step=N");
potion_method(num_vt, "string", potion_num_string, 0);
potion_method(num_vt, "times", potion_num_times, "block=&");
View
12 core/string.c
@@ -203,8 +203,18 @@ static PN potion_bytes_length(Potion *P, PN closure, PN self) {
return PN_NUM(PN_STR_LEN(str));
}
+// TODO: ensure it's UTF-8 data
static PN potion_bytes_string(Potion *P, PN closure, PN self) {
- return self;
+ PN exist = potion_lookup_str(P, P->strings, PN_STR_PTR(self));
+ if (!exist) {
+ PN_SIZE len = PN_STR_LEN(self);
+ vPN(String) s = PN_ALLOC_N(PN_TSTRING, struct PNString, len + 1);
+ s->len = len;
+ PN_MEMCPY_N(s->chars, PN_STR_PTR(self), char, len + 1);
+ P->strings = potion_add_str(P, P->strings, (PN)s);
+ exist = (PN)s;
+ }
+ return exist;
}
static PN potion_bytes_print(Potion *P, PN closure, PN self) {
View
2 core/vm-x86.c
@@ -646,7 +646,7 @@ void potion_x86_call(Potion *P, struct PNProto * volatile f, PNAsm * volatile *a
void potion_x86_callset(Potion *P, struct PNProto * volatile f, PNAsm * volatile *asmp, PN_SIZE pos, long start) {
PN_OP op = PN_OP_AT(f->asmb, pos);
X86_ARGO(start - 3, 0);
- X86_ARGO(op.a, 1);
+ X86_ARGO(op.b, 1);
X86_PRE(); ASM(0xB8); ASMN(potion_obj_get_callset); // mov &potion_obj_get %rax
ASM(0xFF); ASM(0xD0); // callq %rax
X86_MOV_RBP(0x89, op.a); // mov %rax local
View
52 example/spectral-norm.pn
@@ -0,0 +1,52 @@
+A = (i, j):
+ ij = i + j
+ 1.0 / (ij * (ij + 1) / 2 + i + 1).
+
+Av = (n, x, y):
+ i = 0
+ while (i < n):
+ a = 0, j = 0
+ while (j < n):
+ a += A (i, j) * x (j)
+ j++.
+ y (i) = a
+ i++.
+ .
+
+Atv = (n, x, y):
+ i = 0
+ while (i < n):
+ a = 0, j = 0
+ while (j < n):
+ a += A (j, i) * x (j)
+ j++.
+ y (i) = a
+ i++.
+ .
+
+AtAv = (n, x, y, t):
+ Av (n, x, t)
+ Atv (n, t, y).
+
+n = 100
+u = list(n), v = list(n), t = list(n)
+i = 0
+while (i < n):
+ u (i) = 1
+ i++.
+
+i = 0
+while (i < 10):
+ AtAv (n, u, v, t)
+ AtAv (n, v, u, t)
+ i++.
+
+vBv = 0, vv = 0, i = 0
+while (i < n):
+ ui = u (i), vi = v (i)
+ vBv += ui * vi
+ vv += vi * vi
+ i++.
+
+a = vBv / vv
+a sqrt string slice (0, 11)
View
2 test/numbers/neg.pn
@@ -0,0 +1,2 @@
+(-0, -12, -68, -(-68), ~4, ~16, ~777, ~-168, ~0xFFFFFFFF)
+# (0, -12, -68, 68, -5, -17, -778, 167, 0)

0 comments on commit 32d5a3d

Please sign in to comment.