aboutsummaryrefslogtreecommitdiff
path: root/src/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler')
-rw-r--r--src/compiler/compiler.zc21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/compiler/compiler.zc b/src/compiler/compiler.zc
index 244efc5..c6e1ca4 100644
--- a/src/compiler/compiler.zc
+++ b/src/compiler/compiler.zc
@@ -4737,7 +4737,7 @@ int kw_embed() {
off = toff; n = name();
expect(s_semi);
data = m*embed_open(path);
- if (!data) { int sv = tpack(); tunpack(pp); serr("can't read the file named by embed"); tunpack(sv); data = chk_resize(0, 0, 1); }
+ if (!data) { embed_fail(pp, "can't read the file named by embed"); data = chk_resize(0, 0, 1); }
sz = data[0];
if (!t) t = tbstr;
// in a function the data sits in the code stream, so the code jumps over it
@@ -4750,10 +4750,10 @@ int kw_embed() {
}
if (t[0] == TY_ARR) {
w = embed_width(t[4]);
- if (!w) { serr("embed declares a bstr, a char*, a number, or an array of numbers"); if (skip >= 0) setlit(skip, here); return 0; }
- if (sz % w) { serr("the embedded file's size is not a whole number of array elements"); if (skip >= 0) setlit(skip, here); return 0; }
+ if (!w) { embed_fail(pp, "embed declares a bstr, a char*, a number, or an array of numbers"); if (skip >= 0) setlit(skip, here); return embed_bind(n, off, tword, 0, 2); }
+ if (sz % w) { embed_fail(pp, "the embedded file's size is not a whole number of array elements"); if (skip >= 0) setlit(skip, here); return embed_bind(n, off, tword, 0, 2); }
count = sz / w;
- if (t[3] >= 0 && count > t[3]) { serr("the embedded file is larger than the array it is declared as"); if (skip >= 0) setlit(skip, here); return 0; }
+ if (t[3] >= 0 && count > t[3]) { embed_fail(pp, "the embedded file is larger than the array it is declared as"); if (skip >= 0) setlit(skip, here); return embed_bind(n, off, tword, 0, 2); }
if (t[3] < 0) t = tarr(t[4], count);
k = tsz(t); reserve_code(k); a = here; i = 0;
if (t[4][0] == TY_CHAR) { while (i < k) { if (i < sz) emit(data[1 + i]); else emit(0); i++; } }
@@ -4768,15 +4768,22 @@ int kw_embed() {
return embed_bind(n, off, t, a, 1);
}
w = embed_width(t);
- if (!w) { serr("embed declares a bstr, a char*, a number, or an array of numbers"); return 0; }
- if (sz > w) { serr("the embedded file is larger than the type it is declared as"); return 0; }
- if (isfl(t) && sz != w) { serr("an embedded float takes exactly as many bytes as its type"); return 0; }
+ if (!w) { embed_fail(pp, "embed declares a bstr, a char*, a number, or an array of numbers"); return embed_bind(n, off, tword, 0, 2); }
+ if (sz > w) { embed_fail(pp, "the embedded file is larger than the type it is declared as"); return embed_bind(n, off, tword, 0, 2); }
+ if (isfl(t) && sz != w) { embed_fail(pp, "an embedded float takes exactly as many bytes as its type"); return embed_bind(n, off, tword, 0, 2); }
v = 0; i = 0;
while (i < sz) { v = v | (data[1 + i] << (i * 8)); i++; }
if (isfl(t) && w == 4) v = rt.s2f(v);
else v = cnorm(t, v);
return embed_bind(n, off, t, v, 2);
}
+// report msg at the embed declaration that starts at the token packed in pp. the
+// caller still declares the name, as a plain 0, so its uses are not reported too.
+int embed_fail(int pp, char* msg) {
+ int sv = tpack();
+ tunpack(pp); serr(msg); tunpack(sv);
+ return 0;
+}
// the bytes one element of type t takes in an embedded file: the packed size of a
// sized type or f32, 1 for char, 8 for the other number types, 0 when t is not a number.
int embed_width(word t) {