a21_txt01_9a
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
* 川合のプログラミング言語自作のためのテキスト第三版#9a
-(by [[K]], 2021.03.24)
** (1) HL-9a
-今回は、HL-9に対して、いくつかの命令を意図的に無視するこ...
-しかし任意のC言語プログラムが実行できるわけではなく、HL-...
-これは真面目な開発であるというよりは、ちょっとした遊びと...
-言語を自作しているからこその遊びです。
|//の扱い|行末まで無視する|これは余興じゃなくて本来必要な...
|#include行|行末まで無視する|#include <acl.c>を読み飛ばす...
|変数宣言構文|セミコロンまで無視する|HL-9aは配列以外は宣...
|void aMain() {|中身は無視しないが、この記述そのものは無...
|上記に対応する }|中身は無視しないが、この記述そのものは...
-HL-9aでは、#includeを「#」と「include」に分けて認識して...
-「行末まで」の処理は、lexerを抜けてしまうと改行コードが...
#include <acl.c>
typedef unsigned char *String; // こう書くと String は u...
int loadText(String path, String t, int siz) → HL-4と同...
////////////////////////////////////////////////////////...
#define MAX_TC 1000 // トークンコードの最大値.
String ts[MAX_TC + 1]; // トークンの内容(文字列)を記憶.
int tl[MAX_TC + 1]; // トークンの長さ.
unsigned char tcBuf[(MAX_TC + 1) * 10]; // トークン1つ当...
int tcs = 0, tcb = 0;
AInt var[MAX_TC + 1]; // 変数.
int getTc(String s, int len) → HL-8aと同じなので省略
////////////////////////////////////////////////////////...
int isAlphabetOrNumber(unsigned char c) → HL-2と同じなの...
int lexer(String s, int tc[])
{
int i = 0, j = 0, len; // i:今s[]のどこを読んでいる...
for (;;) {
(中略)
} else {
printf("syntax error : %.10s\n", &s[i]);
exit(1);
}
+ if (strncmp(&s[i], "//", 2) == 0) { // コメント.
+comment:
+ while (s[i] != 0 && s[i] != '\n')
+ i++;
+ continue;
+ }
+ if (len == 7 && strncmp(&s[i], "include", 7) == ...
tc[j] = getTc(&s[i], len);
i += len;
j++;
}
}
int tc[10000]; // トークンコード.
enum { TcSemi = 0, TcDot, TcWiCard, Tc0, Tc1, Tc2, Tc3, ...
TcEEq, TcNEq, TcLt, TcGe, TcLe, TcGt, TcPlus, TcMinu...
TcComma, TcExpr, TcExpr0, TcTmp0, TcTmp1, TcTmp2, Tc...
char tcInit[] = "; . !!* 0 1 2 3 4 5 6 7 8 ( ) [ ] { } =...
////////////////////////////////////////////////////////...
int phrCmp_tc[32 * 100], ppc1, wpc[9], wpc1[9]; // ppc1:...
int phrCmp(int pid, String phr, int pc) → HL-7と同じなの...
////////////////////////////////////////////////////////...
typedef AInt *IntP; // こう書くと IntP は AInt * の代わ...
enum { OpCpy = 0, OpCeq, OpCne, OpClt, OpCge, OpCle, OpC...
OpAdd1, OpNeg, OpGoto, OpJeq, OpJne, OpJlt, OpJge, O...
OpPrints, OpAryNew, OpAryInit, OpArySet, OpAryGet, O...
OpXorShift, OpGetPix, OpFilRct0, OpPrm, OpF16Sin, Op...
IntP ic[10000], *icq; // ic[]:内部コード、icq:ic[]への書...
void putIc(int op, IntP p0, IntP p1, IntP p2, IntP p3) →...
////////////////////////////////////////////////////////...
char tmp_flag[10]; // 一時変数の利用状況を管理.
int tmpAlloc() → HL-7と同じなので省略
void tmpFree(int i) → HL-7と同じなので省略
////////////////////////////////////////////////////////...
int epc, epc1; // exprのためのpcとpc1.
int exprSub(int priority); // exprSub1()が参照するので、...
int expr(int j);
int phrCmpPutIc(int pid, String phr, int pc, int *pi, in...
int exprSub1(int i, int priority, int op) → HL-7と同じな...
int exprSub(int priority) → HL-9と同じなので省略
int expr(int j) → HL-7と同じなので省略
////////////////////////////////////////////////////////...
enum { IfTrue = 0, IfFalse = 1 };
void ifgoto(int i, int not, int label) → HL-8と同じなの...
int tmpLabelNo;
int tmpLabelAlloc() → HL-8と同じなので省略
#define BInfSiz 10
int binf[BInfSiz * 100], bd, lbd; // binf:block-info, bd...
enum { BlkIf = 1, BlkFor, BlkMain }; // BlkMainをHL-9aで...
enum { IfLabel0 = 1, IfLabel1 };
enum { ForLopBgn = 1, ForCont, ForBrk, ForLbd0, ForWpc01...
int phrCmpPutIc(int pid, String phr, int pc, int *pi, in...
////////////////////////////////////////////////////////...
int compile(String s)
{
(中略)
} else if (phrCmpPutIc(28, "bitblt(!!***8, !!**0...
+ } else if (phrCmpPutIc(29, "printTime();", pc, 0...
+ } else if (phrCmp(30, "void aMain() {", pc)) {
+ bd += BInfSiz;
+ binf[bd] = BlkMain; // ただ認識しているだけ...
+ } else if (phrCmp(31, "}", pc) && binf[bd] == Bl...
+ bd -= BInfSiz; // ただコードブロックを閉じて...
+ } else if (phrCmp(32, "#", pc)) { // 何もせずに...
+ } else if (phrCmp(33, "int", pc) || phrCmp(34, "...
+ while (tc[pc] != TcSemi) // セミコロンまでを...
+ pc++;
+ ppc1 = pc;
} else if (phrCmp( 8, "!!***0;", pc)) { // これ...
e0 = expr(0);
(中略)
}
AWindow *win;
void exec() → HL-9と同じなので省略
int run(String s) → HL-6と同じなので省略
////////////////////////////////////////////////////////...
void aMain() → HL-9と同じなので省略
-プログラムは772行になりました。今回は少ししか増えません...
-今回は、くだらないことしかやっていないので、改造内容の説...
** (2) プログラム例 (HL-9aで実行できるプログラム)
-(以下のプログラムは、aclライブラリがあれば、普通のCコン...
----
-''mandel.c''
--マンデルブロー集合の描画プログラムです。上記説明にある...
#include <acl.c>
#include "tl9builtin.c"
void aMain()
{
AWindow *w;
int x, y, sx, sy, cx, cy, zx, zy, xx, yy, t, n, sn, c;
w = aOpenWin(512, 384, "mandel", 1);
for (y = 0; y < 384; y++) {
for (x = 0; x < 512; x++) {
sn = 0;
for (sx = 0; sx < 4; sx++) {
cx = (x * 4 + sx) * 56 + 4673536;
for (sy = 0; sy < 4; sy++) {
cy = (y * 4 + sy) * -56 - 124928;
zx = cx; zy = cy;
for (n = 1; n < 447; n++) {
xx = mul64shr(zx, zx, 24);
yy = mul64shr(zy, zy, 24);
t = xx + yy;
if (t > 0x4000000) break;
zy = mul64shr(zy, zx, 23);
zx = xx + cx;
zy = zy + cy;
zx = zx - yy;
}
sn = sn + n;
}
}
n = sn >> 4;
c = aRgb8(n, 0, 0);
if (n > 255) {
c = aRgb8(0, 0, 0);
if (n != 447) {
c = aRgb8(255, n - 255, 0);
}
}
aSetPix0(w, x, y, c);
}
}
printTime();
aWait(-1);
}
|http://k.osask.jp/files/esb20190827b.png|
----
-''maze.c''
--迷路作成(穴掘り法)のプログラムです。上記説明にある通...
--乱数で迷路を作っているので、実行するたびに違う迷路が出...
#include <acl.c>
void aMain()
{
AWindow *w;
int i, x, y, xx, yy, d0, d1, d2, d3, d, dd;
w = aOpenWin(752, 496, "maze", 1);
aFillRect0(w, 752, 496, 0, 0, 0x00ff00);
aFillRect0(w, 16, 16, 16, 16, 0x000000);
for (i = 0; i < 1000000; i++) {
x = ((aXorShift32() & 0x7fffffff) % 23) * 2 + 1;
y = ((aXorShift32() & 0x7fffffff) % 15) * 2 + 1;
if (aGetPix(w, x * 16, y * 16) == 0x000000) {
for (;;) {
d0 = d1 = d2 = d3 = 0;
xx = x * 16;
yy = y * 16;
aFillRect0(w, 16, 16, xx, yy, 0x000000);
if (x != 45) { d0 = (aGetPix(w, xx + 16,...
if (x != 1) { d1 = (aGetPix(w, xx - 16,...
if (y != 29) { d2 = (aGetPix(w, xx, yy +...
if (y != 1) { d3 = (aGetPix(w, xx, yy -...
d = d0 + d1 + d2 + d3;
if (d == 0) break;
dd = (aXorShift32() & 0x7fffffff) % d;
if (d0) { if (dd == 0) { aFillRect0(w, 1...
if (d1) { if (dd == 0) { aFillRect0(w, 1...
if (d2) { if (dd == 0) { aFillRect0(w, 1...
if (d3) { if (dd == 0) { aFillRect0(w, 1...
}
}
}
aWait(-1);
}
|http://essen.osask.jp/download/esb20191120a.png|
----
-''kcube.c''
--[[K]]にとっては定番の、キューブ回転プログラムです。上記...
#include <acl.c>
#include "tl9buitin.c"
int vertx[8] ={ 2, 2, 2, 2, 0, 0, 0, 0 };
int verty[8] ={ 2, 2, 0, 0, 2, 2, 0, 0 };
int vertz[8] ={ 2, 0, 2, 0, 2, 0, 2, 0 };
int squar[24] = { 0,4,6,2, 1,3,7,5, 0,2,3,1, 0,1,5,4, 4,...
int col[6] = { 0xff0000, 0x00ff00, 0xffff00, 0x0000ff, 0...
void aMain()
{
int vx[8]; int vy[8]; int vz[8];
int centerz4[6];
int scx[8]; int scy[8];
int buf0[160]; int buf1[160];
int i, j, k, l, thx, thy, thz, xp, xa, yp, ya, zp, z...
int k0, k1, k2, e0x, e0y, e1x, e1y, l1, p0x, p0y, p1...
int dx, *buf, y0, y1;
AWindow *w;
w = aOpenWin(256, 160, "kcube", 1);
for (i = 0; i < 8; i++) {
vertx[i] = (vertx[i] - 1) * 50;
verty[i] = (verty[i] - 1) * 50;
vertz[i] = (vertz[i] - 1) * 50;
}
thx = thy = thz = 0;
for (;;) {
thx = (thx + 182) & 0xffff;
thy = (thy + 273) & 0xffff;
thz = (thz + 364) & 0xffff;
xp = ff16cos(thx); xa = ff16sin(thx);
yp = ff16cos(thy); ya = ff16sin(thy);
zp = ff16cos(thz); za = ff16sin(thz);
for (i = 0; i < 8; i++) {
zt = vertz[i] * xp + verty[i] * xa;
yt = verty[i] * xp - vertz[i] * xa;
xt = vertx[i] * yp + mul64shr(zt, ...
vz[i] = mul64shr(zt, yp, 16) - vertx[i] * ya;
vx[i] = mul64shr(xt, zp, 16) - mul64shr(yt, ...
vy[i] = mul64shr(yt, zp, 16) + mul64shr(xt, ...
}
for (l = i = 0; i < 6; i++) {
centerz4[i] = vz[squar[l]] + vz[squar[l + 1]...
l = l + 4;
}
aFillRect0(w, 160, 160, 48, 0, 0x000000);
for (i = 0; i < 8; i++) {
t = (vz[i] + 13107200) >> 16;
t = 4915200 / t;
scx[i] = mul64shr(vx[i], t, 31) + 128;
scy[i] = mul64shr(vy[i], t, 31) + 80;
}
for (;;) {
m = j = 0;
for (i = 0; i < 6; i++) {
t = centerz4[i];
if (m < t) {
m = t;
j = i;
}
}
if (m == 0) break;
l = j * 4; centerz4[j] = 0;
k0 = squar[l];
k1 = squar[l + 1];
k2 = squar[l + 2];
e0x = vx[k1] - vx[k0];
e0y = vy[k1] - vy[k0];
e1x = vx[k2] - vx[k1];
e1y = vy[k2] - vy[k1];
if (mul64shr(e0x, e1y, 16) <= mul64shr(e0y, ...
l1 = l + 4;
k = squar[l + 3];
p0x = scx[k]; p0y = scy[k];
ymin = 99999; ymax = 0;
for (; l < l1; l++) {
k = squar[l];
p1x = scx[k]; p1y = scy[k];
if (ymin > p1y) { ymin = p1y; }
if (ymax < p1y) { ymax = p1y; }
if (p0y != p1y) {
if (p0y < p1y) {
buf = buf0; y0 = p0y; y1 = p...
} else {
buf = buf1; y0 = p1y; y1 = p...
}
x = x * 65536;
dx = dx * 65536;
dx = dx / (y1 - y0);
if (dx >= 0) {
x = x + 0x8000;
} else {
x = x - 0x8000;
}
++y1;
for (y = y0; y < y1; y++) {
buf[y] = x >> 16;
x = x + dx;
}
}
p0x = p1x; p0y = p1y;
}
c = col[j];
++ymax;
for (y = ymin; y < ymax; y++) {
p0x = buf0[y];
p1x = buf1[y];
if (p0x <= p1x) {
aFillRect0(w, p1x - p0x + 1, 1, ...
} else {
aFillRect0(w, p0x - p1x + 1, 1, ...
}
}
}
}
aWait(50);
if (aInkey(w, 1) != 0) break;
}
}
|http://essen.osask.jp/download/esb20191120c.png| |http:...
----
-''invader.c''
--[[K]]にとっては定番の、インベーダゲームのプログラムです...
#include <acl.c>
#include "tl9builtin.c"
int fght[384] = {
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,
0,0,0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,1,1,1,1,...
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...
0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,...
0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,...
0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,...
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...
};
void aMain()
{
int inv[192];
int i, j, k, score, pnt, mwt0, fx, idir, wunit, ix, ...
AWindow *w;
w = aOpenWin(324, 228, "graphics", 1);
for (i = 0; i < 384; i++) {
fght[i] = fght[i] * 0x00ffff;
}
for (i = 0; i < 512; i++) {
invd[i] = invd[i] * 0x00ff00;
}
restart:
score = 0; pnt = 1; mwt0 = 20; fx = 18;
nxtinv:
ix = 7; iy = 1; invlin = 5;
ly = 0; lwt = 0; mwt = mwt0;
idir = 1; wunit = 1024;
for (i = 0; i < 6; i++) {
for (j = 0; j < 26; j++) {
inv[i * 32 + j] = j % 5;
}
}
for(;;) {
// 表示.
aFillRect0(w, 324, 228, 0, 0, 0x000000);
aDrawStr0(w, 34, 2, 0xffffff, 0x000000, "SCO...
gprintDec(w, 82, 2, 6, 0xffffff, 0x000000, scor...
gprintDec(w, 226, 2, 6, 0xffffff, 0x000000, high);
bitblt(w, 24, 16, fx * 8 + 2, 13 * 16 + 2, fght);
for (i = 0; i < 6; i++) {
for (j = 0; j < 26; j++) {
if (inv[i * 32 + j] == 1) {
bitblt(w, 32, 16, (ix + j) * 8 + 2, ...
}
}
}
if (ly != 0) {
aFillRect0(w, 2, 14, lx * 8 + 5, ly * 16 + 3...
}
// インベーダ全滅判定.
for (;;) {
for (i = 0; i < 26; i++) {
if (inv[invlin * 32 + i] != 0) goto skip0;
}
invlin = invlin - 1;
if (invlin < 0) {
mwt0 = mwt0 - mwt0 / 3;
aWait(1024);
goto nxtinv;
}
}
skip0:
// wait処理.
aWait(wunit);
wunit = 40;
lwt = lwt - 1;
// キー入力.
j = 0;
for (;;) {
i = aInkey(w, 1);
if (i == 0) break;
if (i == 27) goto end; // esc
if (i == 0x1024) { j = -1; }
if (i == 0x1025) { j = 1; }
if (i == 0x1026) { i = 32; }
if (i == 32) {
if (lwt <= 0) {
lwt = 15;
lx = fx + 1;
ly = 13;
}
}
}
// 自機の移動.
i = fx + j;
if (i >= 0) {
if (i <= 37) {
fx = i;
}
}
// レーザ移動.
if (ly > 0) {
ly = ly - 1;
if (ly == 0) {
pnt = pnt - 10;
if (pnt < 1) {
pnt = 1;
}
}
}
// あたり判定.
j = lx - ix;
k = ly - iy;
if (k >= 0) {
if (k <= 5) {
if (j >= 0) {
if (j < 24) {
i = inv[k * 32 + j];
if (i > 0) {
ly = 0;
j = j - i;
for (i = 0; i < 6; i++) {
inv[k * 32 + j + i] = 0;
}
score = score + pnt;
pnt++;
if (high < score) {
high = score;
}
}
}
}
}
}
// インベーダ移動.
if (mwt > 0) {
mwt = mwt - 1;
} else {
mwt = mwt0;
ix = ix + idir;
if (ix >= 0) {
if (ix <= 14) continue;
}
if (iy + invlin == 12) {
aDrawStr0(w, 122, 98, 0xff0000, 0x000000...
for (;;) {
aWait(128);
i = aInkey(w, 1);
if (i == 10) break;
if (i == 27) goto end; // esc
}
goto restart;
}
idir = idir * -1;
iy++;
ix = ix +idir;
}
}
end:
aWait(-1);
}
|http://k.osask.jp/files/esb20191218d.png|
** (3) "tl9builtin.c"の中身
-tl9builtin.cは、上記プログラム例をgccなどの普通のコンパ...
-HL-9aの組み込み関数の処理に相当する処理が書いてあります。
static inline int mul64shr(int i,int j, int k)
{
return ((long long) i) * ((long long) j) >> k;
}
static inline void printTime()
{
printf("time: %.3f[sec]\n", clock() / (double) CLOCK...
}
static inline int ff16sin(int x)
{
return (int) (sin(x * (2 * 3.14159265358979323 / 655...
}
static inline int ff16cos(int x)
{
return (int) (cos(x * (2 * 3.14159265358979323 / 655...
}
void gprintDec(AWindow *w, int x, int y, int l, int c, i...
{
char s[100];
sprintf(s, "%*d", l, i);
aDrawStr0(w, x, y, c, b, s);
}
void bitblt(AWindow *w, int sx, int sy, int x, int y, in...
{
AInt32 *q = &w->buf[x + y * w->xsiz];
int i, j;
for (j = 0; j < sy; j++) {
for (i = 0; i < sx; i++) {
q[i] = p[i];
}
p += sx;
q += w->xsiz;
}
}
** 次回に続く
-次回: [[a21_txt01_10]]
*こめんと欄
#comment
終了行:
* 川合のプログラミング言語自作のためのテキスト第三版#9a
-(by [[K]], 2021.03.24)
** (1) HL-9a
-今回は、HL-9に対して、いくつかの命令を意図的に無視するこ...
-しかし任意のC言語プログラムが実行できるわけではなく、HL-...
-これは真面目な開発であるというよりは、ちょっとした遊びと...
-言語を自作しているからこその遊びです。
|//の扱い|行末まで無視する|これは余興じゃなくて本来必要な...
|#include行|行末まで無視する|#include <acl.c>を読み飛ばす...
|変数宣言構文|セミコロンまで無視する|HL-9aは配列以外は宣...
|void aMain() {|中身は無視しないが、この記述そのものは無...
|上記に対応する }|中身は無視しないが、この記述そのものは...
-HL-9aでは、#includeを「#」と「include」に分けて認識して...
-「行末まで」の処理は、lexerを抜けてしまうと改行コードが...
#include <acl.c>
typedef unsigned char *String; // こう書くと String は u...
int loadText(String path, String t, int siz) → HL-4と同...
////////////////////////////////////////////////////////...
#define MAX_TC 1000 // トークンコードの最大値.
String ts[MAX_TC + 1]; // トークンの内容(文字列)を記憶.
int tl[MAX_TC + 1]; // トークンの長さ.
unsigned char tcBuf[(MAX_TC + 1) * 10]; // トークン1つ当...
int tcs = 0, tcb = 0;
AInt var[MAX_TC + 1]; // 変数.
int getTc(String s, int len) → HL-8aと同じなので省略
////////////////////////////////////////////////////////...
int isAlphabetOrNumber(unsigned char c) → HL-2と同じなの...
int lexer(String s, int tc[])
{
int i = 0, j = 0, len; // i:今s[]のどこを読んでいる...
for (;;) {
(中略)
} else {
printf("syntax error : %.10s\n", &s[i]);
exit(1);
}
+ if (strncmp(&s[i], "//", 2) == 0) { // コメント.
+comment:
+ while (s[i] != 0 && s[i] != '\n')
+ i++;
+ continue;
+ }
+ if (len == 7 && strncmp(&s[i], "include", 7) == ...
tc[j] = getTc(&s[i], len);
i += len;
j++;
}
}
int tc[10000]; // トークンコード.
enum { TcSemi = 0, TcDot, TcWiCard, Tc0, Tc1, Tc2, Tc3, ...
TcEEq, TcNEq, TcLt, TcGe, TcLe, TcGt, TcPlus, TcMinu...
TcComma, TcExpr, TcExpr0, TcTmp0, TcTmp1, TcTmp2, Tc...
char tcInit[] = "; . !!* 0 1 2 3 4 5 6 7 8 ( ) [ ] { } =...
////////////////////////////////////////////////////////...
int phrCmp_tc[32 * 100], ppc1, wpc[9], wpc1[9]; // ppc1:...
int phrCmp(int pid, String phr, int pc) → HL-7と同じなの...
////////////////////////////////////////////////////////...
typedef AInt *IntP; // こう書くと IntP は AInt * の代わ...
enum { OpCpy = 0, OpCeq, OpCne, OpClt, OpCge, OpCle, OpC...
OpAdd1, OpNeg, OpGoto, OpJeq, OpJne, OpJlt, OpJge, O...
OpPrints, OpAryNew, OpAryInit, OpArySet, OpAryGet, O...
OpXorShift, OpGetPix, OpFilRct0, OpPrm, OpF16Sin, Op...
IntP ic[10000], *icq; // ic[]:内部コード、icq:ic[]への書...
void putIc(int op, IntP p0, IntP p1, IntP p2, IntP p3) →...
////////////////////////////////////////////////////////...
char tmp_flag[10]; // 一時変数の利用状況を管理.
int tmpAlloc() → HL-7と同じなので省略
void tmpFree(int i) → HL-7と同じなので省略
////////////////////////////////////////////////////////...
int epc, epc1; // exprのためのpcとpc1.
int exprSub(int priority); // exprSub1()が参照するので、...
int expr(int j);
int phrCmpPutIc(int pid, String phr, int pc, int *pi, in...
int exprSub1(int i, int priority, int op) → HL-7と同じな...
int exprSub(int priority) → HL-9と同じなので省略
int expr(int j) → HL-7と同じなので省略
////////////////////////////////////////////////////////...
enum { IfTrue = 0, IfFalse = 1 };
void ifgoto(int i, int not, int label) → HL-8と同じなの...
int tmpLabelNo;
int tmpLabelAlloc() → HL-8と同じなので省略
#define BInfSiz 10
int binf[BInfSiz * 100], bd, lbd; // binf:block-info, bd...
enum { BlkIf = 1, BlkFor, BlkMain }; // BlkMainをHL-9aで...
enum { IfLabel0 = 1, IfLabel1 };
enum { ForLopBgn = 1, ForCont, ForBrk, ForLbd0, ForWpc01...
int phrCmpPutIc(int pid, String phr, int pc, int *pi, in...
////////////////////////////////////////////////////////...
int compile(String s)
{
(中略)
} else if (phrCmpPutIc(28, "bitblt(!!***8, !!**0...
+ } else if (phrCmpPutIc(29, "printTime();", pc, 0...
+ } else if (phrCmp(30, "void aMain() {", pc)) {
+ bd += BInfSiz;
+ binf[bd] = BlkMain; // ただ認識しているだけ...
+ } else if (phrCmp(31, "}", pc) && binf[bd] == Bl...
+ bd -= BInfSiz; // ただコードブロックを閉じて...
+ } else if (phrCmp(32, "#", pc)) { // 何もせずに...
+ } else if (phrCmp(33, "int", pc) || phrCmp(34, "...
+ while (tc[pc] != TcSemi) // セミコロンまでを...
+ pc++;
+ ppc1 = pc;
} else if (phrCmp( 8, "!!***0;", pc)) { // これ...
e0 = expr(0);
(中略)
}
AWindow *win;
void exec() → HL-9と同じなので省略
int run(String s) → HL-6と同じなので省略
////////////////////////////////////////////////////////...
void aMain() → HL-9と同じなので省略
-プログラムは772行になりました。今回は少ししか増えません...
-今回は、くだらないことしかやっていないので、改造内容の説...
** (2) プログラム例 (HL-9aで実行できるプログラム)
-(以下のプログラムは、aclライブラリがあれば、普通のCコン...
----
-''mandel.c''
--マンデルブロー集合の描画プログラムです。上記説明にある...
#include <acl.c>
#include "tl9builtin.c"
void aMain()
{
AWindow *w;
int x, y, sx, sy, cx, cy, zx, zy, xx, yy, t, n, sn, c;
w = aOpenWin(512, 384, "mandel", 1);
for (y = 0; y < 384; y++) {
for (x = 0; x < 512; x++) {
sn = 0;
for (sx = 0; sx < 4; sx++) {
cx = (x * 4 + sx) * 56 + 4673536;
for (sy = 0; sy < 4; sy++) {
cy = (y * 4 + sy) * -56 - 124928;
zx = cx; zy = cy;
for (n = 1; n < 447; n++) {
xx = mul64shr(zx, zx, 24);
yy = mul64shr(zy, zy, 24);
t = xx + yy;
if (t > 0x4000000) break;
zy = mul64shr(zy, zx, 23);
zx = xx + cx;
zy = zy + cy;
zx = zx - yy;
}
sn = sn + n;
}
}
n = sn >> 4;
c = aRgb8(n, 0, 0);
if (n > 255) {
c = aRgb8(0, 0, 0);
if (n != 447) {
c = aRgb8(255, n - 255, 0);
}
}
aSetPix0(w, x, y, c);
}
}
printTime();
aWait(-1);
}
|http://k.osask.jp/files/esb20190827b.png|
----
-''maze.c''
--迷路作成(穴掘り法)のプログラムです。上記説明にある通...
--乱数で迷路を作っているので、実行するたびに違う迷路が出...
#include <acl.c>
void aMain()
{
AWindow *w;
int i, x, y, xx, yy, d0, d1, d2, d3, d, dd;
w = aOpenWin(752, 496, "maze", 1);
aFillRect0(w, 752, 496, 0, 0, 0x00ff00);
aFillRect0(w, 16, 16, 16, 16, 0x000000);
for (i = 0; i < 1000000; i++) {
x = ((aXorShift32() & 0x7fffffff) % 23) * 2 + 1;
y = ((aXorShift32() & 0x7fffffff) % 15) * 2 + 1;
if (aGetPix(w, x * 16, y * 16) == 0x000000) {
for (;;) {
d0 = d1 = d2 = d3 = 0;
xx = x * 16;
yy = y * 16;
aFillRect0(w, 16, 16, xx, yy, 0x000000);
if (x != 45) { d0 = (aGetPix(w, xx + 16,...
if (x != 1) { d1 = (aGetPix(w, xx - 16,...
if (y != 29) { d2 = (aGetPix(w, xx, yy +...
if (y != 1) { d3 = (aGetPix(w, xx, yy -...
d = d0 + d1 + d2 + d3;
if (d == 0) break;
dd = (aXorShift32() & 0x7fffffff) % d;
if (d0) { if (dd == 0) { aFillRect0(w, 1...
if (d1) { if (dd == 0) { aFillRect0(w, 1...
if (d2) { if (dd == 0) { aFillRect0(w, 1...
if (d3) { if (dd == 0) { aFillRect0(w, 1...
}
}
}
aWait(-1);
}
|http://essen.osask.jp/download/esb20191120a.png|
----
-''kcube.c''
--[[K]]にとっては定番の、キューブ回転プログラムです。上記...
#include <acl.c>
#include "tl9buitin.c"
int vertx[8] ={ 2, 2, 2, 2, 0, 0, 0, 0 };
int verty[8] ={ 2, 2, 0, 0, 2, 2, 0, 0 };
int vertz[8] ={ 2, 0, 2, 0, 2, 0, 2, 0 };
int squar[24] = { 0,4,6,2, 1,3,7,5, 0,2,3,1, 0,1,5,4, 4,...
int col[6] = { 0xff0000, 0x00ff00, 0xffff00, 0x0000ff, 0...
void aMain()
{
int vx[8]; int vy[8]; int vz[8];
int centerz4[6];
int scx[8]; int scy[8];
int buf0[160]; int buf1[160];
int i, j, k, l, thx, thy, thz, xp, xa, yp, ya, zp, z...
int k0, k1, k2, e0x, e0y, e1x, e1y, l1, p0x, p0y, p1...
int dx, *buf, y0, y1;
AWindow *w;
w = aOpenWin(256, 160, "kcube", 1);
for (i = 0; i < 8; i++) {
vertx[i] = (vertx[i] - 1) * 50;
verty[i] = (verty[i] - 1) * 50;
vertz[i] = (vertz[i] - 1) * 50;
}
thx = thy = thz = 0;
for (;;) {
thx = (thx + 182) & 0xffff;
thy = (thy + 273) & 0xffff;
thz = (thz + 364) & 0xffff;
xp = ff16cos(thx); xa = ff16sin(thx);
yp = ff16cos(thy); ya = ff16sin(thy);
zp = ff16cos(thz); za = ff16sin(thz);
for (i = 0; i < 8; i++) {
zt = vertz[i] * xp + verty[i] * xa;
yt = verty[i] * xp - vertz[i] * xa;
xt = vertx[i] * yp + mul64shr(zt, ...
vz[i] = mul64shr(zt, yp, 16) - vertx[i] * ya;
vx[i] = mul64shr(xt, zp, 16) - mul64shr(yt, ...
vy[i] = mul64shr(yt, zp, 16) + mul64shr(xt, ...
}
for (l = i = 0; i < 6; i++) {
centerz4[i] = vz[squar[l]] + vz[squar[l + 1]...
l = l + 4;
}
aFillRect0(w, 160, 160, 48, 0, 0x000000);
for (i = 0; i < 8; i++) {
t = (vz[i] + 13107200) >> 16;
t = 4915200 / t;
scx[i] = mul64shr(vx[i], t, 31) + 128;
scy[i] = mul64shr(vy[i], t, 31) + 80;
}
for (;;) {
m = j = 0;
for (i = 0; i < 6; i++) {
t = centerz4[i];
if (m < t) {
m = t;
j = i;
}
}
if (m == 0) break;
l = j * 4; centerz4[j] = 0;
k0 = squar[l];
k1 = squar[l + 1];
k2 = squar[l + 2];
e0x = vx[k1] - vx[k0];
e0y = vy[k1] - vy[k0];
e1x = vx[k2] - vx[k1];
e1y = vy[k2] - vy[k1];
if (mul64shr(e0x, e1y, 16) <= mul64shr(e0y, ...
l1 = l + 4;
k = squar[l + 3];
p0x = scx[k]; p0y = scy[k];
ymin = 99999; ymax = 0;
for (; l < l1; l++) {
k = squar[l];
p1x = scx[k]; p1y = scy[k];
if (ymin > p1y) { ymin = p1y; }
if (ymax < p1y) { ymax = p1y; }
if (p0y != p1y) {
if (p0y < p1y) {
buf = buf0; y0 = p0y; y1 = p...
} else {
buf = buf1; y0 = p1y; y1 = p...
}
x = x * 65536;
dx = dx * 65536;
dx = dx / (y1 - y0);
if (dx >= 0) {
x = x + 0x8000;
} else {
x = x - 0x8000;
}
++y1;
for (y = y0; y < y1; y++) {
buf[y] = x >> 16;
x = x + dx;
}
}
p0x = p1x; p0y = p1y;
}
c = col[j];
++ymax;
for (y = ymin; y < ymax; y++) {
p0x = buf0[y];
p1x = buf1[y];
if (p0x <= p1x) {
aFillRect0(w, p1x - p0x + 1, 1, ...
} else {
aFillRect0(w, p0x - p1x + 1, 1, ...
}
}
}
}
aWait(50);
if (aInkey(w, 1) != 0) break;
}
}
|http://essen.osask.jp/download/esb20191120c.png| |http:...
----
-''invader.c''
--[[K]]にとっては定番の、インベーダゲームのプログラムです...
#include <acl.c>
#include "tl9builtin.c"
int fght[384] = {
0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,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0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,1,...
0,1,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,1,...
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...
0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,...
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...
0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,...
0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,...
0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,...
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...
};
void aMain()
{
int inv[192];
int i, j, k, score, pnt, mwt0, fx, idir, wunit, ix, ...
AWindow *w;
w = aOpenWin(324, 228, "graphics", 1);
for (i = 0; i < 384; i++) {
fght[i] = fght[i] * 0x00ffff;
}
for (i = 0; i < 512; i++) {
invd[i] = invd[i] * 0x00ff00;
}
restart:
score = 0; pnt = 1; mwt0 = 20; fx = 18;
nxtinv:
ix = 7; iy = 1; invlin = 5;
ly = 0; lwt = 0; mwt = mwt0;
idir = 1; wunit = 1024;
for (i = 0; i < 6; i++) {
for (j = 0; j < 26; j++) {
inv[i * 32 + j] = j % 5;
}
}
for(;;) {
// 表示.
aFillRect0(w, 324, 228, 0, 0, 0x000000);
aDrawStr0(w, 34, 2, 0xffffff, 0x000000, "SCO...
gprintDec(w, 82, 2, 6, 0xffffff, 0x000000, scor...
gprintDec(w, 226, 2, 6, 0xffffff, 0x000000, high);
bitblt(w, 24, 16, fx * 8 + 2, 13 * 16 + 2, fght);
for (i = 0; i < 6; i++) {
for (j = 0; j < 26; j++) {
if (inv[i * 32 + j] == 1) {
bitblt(w, 32, 16, (ix + j) * 8 + 2, ...
}
}
}
if (ly != 0) {
aFillRect0(w, 2, 14, lx * 8 + 5, ly * 16 + 3...
}
// インベーダ全滅判定.
for (;;) {
for (i = 0; i < 26; i++) {
if (inv[invlin * 32 + i] != 0) goto skip0;
}
invlin = invlin - 1;
if (invlin < 0) {
mwt0 = mwt0 - mwt0 / 3;
aWait(1024);
goto nxtinv;
}
}
skip0:
// wait処理.
aWait(wunit);
wunit = 40;
lwt = lwt - 1;
// キー入力.
j = 0;
for (;;) {
i = aInkey(w, 1);
if (i == 0) break;
if (i == 27) goto end; // esc
if (i == 0x1024) { j = -1; }
if (i == 0x1025) { j = 1; }
if (i == 0x1026) { i = 32; }
if (i == 32) {
if (lwt <= 0) {
lwt = 15;
lx = fx + 1;
ly = 13;
}
}
}
// 自機の移動.
i = fx + j;
if (i >= 0) {
if (i <= 37) {
fx = i;
}
}
// レーザ移動.
if (ly > 0) {
ly = ly - 1;
if (ly == 0) {
pnt = pnt - 10;
if (pnt < 1) {
pnt = 1;
}
}
}
// あたり判定.
j = lx - ix;
k = ly - iy;
if (k >= 0) {
if (k <= 5) {
if (j >= 0) {
if (j < 24) {
i = inv[k * 32 + j];
if (i > 0) {
ly = 0;
j = j - i;
for (i = 0; i < 6; i++) {
inv[k * 32 + j + i] = 0;
}
score = score + pnt;
pnt++;
if (high < score) {
high = score;
}
}
}
}
}
}
// インベーダ移動.
if (mwt > 0) {
mwt = mwt - 1;
} else {
mwt = mwt0;
ix = ix + idir;
if (ix >= 0) {
if (ix <= 14) continue;
}
if (iy + invlin == 12) {
aDrawStr0(w, 122, 98, 0xff0000, 0x000000...
for (;;) {
aWait(128);
i = aInkey(w, 1);
if (i == 10) break;
if (i == 27) goto end; // esc
}
goto restart;
}
idir = idir * -1;
iy++;
ix = ix +idir;
}
}
end:
aWait(-1);
}
|http://k.osask.jp/files/esb20191218d.png|
** (3) "tl9builtin.c"の中身
-tl9builtin.cは、上記プログラム例をgccなどの普通のコンパ...
-HL-9aの組み込み関数の処理に相当する処理が書いてあります。
static inline int mul64shr(int i,int j, int k)
{
return ((long long) i) * ((long long) j) >> k;
}
static inline void printTime()
{
printf("time: %.3f[sec]\n", clock() / (double) CLOCK...
}
static inline int ff16sin(int x)
{
return (int) (sin(x * (2 * 3.14159265358979323 / 655...
}
static inline int ff16cos(int x)
{
return (int) (cos(x * (2 * 3.14159265358979323 / 655...
}
void gprintDec(AWindow *w, int x, int y, int l, int c, i...
{
char s[100];
sprintf(s, "%*d", l, i);
aDrawStr0(w, x, y, c, b, s);
}
void bitblt(AWindow *w, int sx, int sy, int x, int y, in...
{
AInt32 *q = &w->buf[x + y * w->xsiz];
int i, j;
for (j = 0; j < sy; j++) {
for (i = 0; i < sx; i++) {
q[i] = p[i];
}
p += sx;
q += w->xsiz;
}
}
** 次回に続く
-次回: [[a21_txt01_10]]
*こめんと欄
#comment
ページ名: