esbasic0005
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
* ES-BASIC #5
-(by [[K]], 2019.09.17)
** (7) ES-BASICのコア部分
-もしかしたらES-BASICの根幹部分だけを残して、ほかを全部削...
#include "kll0.h"
unsigned char *code = (unsigned char *) kmallocRWX(1024 ...
int qc;
void sub_print(int i) { printf("%d\n", i); }
void sub_exit() { exit(0); }
void sub_list(KSrc *src)
{
for (int i = 0, i1 = src->ln.sp.sizPtr(); i < i1; i+...
KSrcLine *sl = (KSrcLine *) src->ln.sp.getPtr(i);
printf("%4d %.*s\n", sl->ln, sl->src.s, sl->src....
}
}
int compile(int l, const char *s, int ln, KLexer *lx, KS...
{
static KPhraseCmp *phr = NEW1(kautoreleasePool, KPhr...
KLexer_Elmt *le = (KLexer_Elmt *) lx->reset()->addCo...
for (int i = lx->sidN0, i1 = *lx->sid->n; i < i1; i++)
varSp->addInt(lx->getConstInt(i)); // intではな...
for (int pc = 0, pc1 = lx->saveSidN0()->getElmtLen()...
phr->setPr(le, 0, pc, pc1);
KLexer_Elmt *lp = &le[pc];
if (phr->cmpPr( 1, "@ ;")) continue;
if (phr->cmpPr( 2, "@ @* = @* ;")) { ...
} else if (phr->cmpPr( 3, "@ @* = @* + @* ;")) {...
} else if (phr->cmpPr( 4, "@ @* = @* - @* ;")) {...
} else if (phr->cmpPr( 5, "@ PRINT @* ;")) { ...
} else if (phr->cmpPr( 6, "@ EXIT ;")) { ...
} else if (phr->cmpPr( 7, "@ LIST ;")) { ...
} else {
printf("syntax error [%d] : %.*s\n", ln, kmi...
return 1;
}
while (le[pc].i != 0)
pc++;
}
return 0;
}
int main()
{
KSizPtr varSp(kmemPool); // 変数の値を記憶しておくと...
KLexer lx(NEW1(kautoreleasePool, KStringId)(1, "; = ...
KSrc src; // ソースコードを記憶しておくところ.
char *cmdlin = (char *) kautoreleasePool->alloc(6553...
for (;;) {
ok:
printf("\nOk\n");
skip:
fgets(cmdlin, 65536, stdin);
for (p = cmdlin; *p == ' ' || *p == '\t'; p++);
if (*p == '\n') goto skip;
kupperCaseM(strlen(p), (unsigned char *) p, lx.c...
if ('0' <= *p && *p <= '9') {
src.editLine(strchr(p, '\n') - p, p); // 数...
goto skip; // プログラム入力直後はOkを出さな...
}
qc = kputBin(&code[0], "60 83_ec_7c 0xbd_%i32"...
if (strncmp(p, "RUN", 3) == 0 && p[3] <= ' ') {
for (int i = 0, i1 = src.ln.sp.sizPtr(); i <...
KSrcLine *sl = (KSrcLine *) src.ln.sp.ge...
if (compile(sl->src.s, sl->src.p, sl->ln...
}
} else {
if (compile(strlen(p), p, -1, &lx, &src, &va...
}
qc += kputBin(&code[qc], "83_c4_7c 61 c3"); //...
kputBin(&code[5], "%i32", varSp.p);
((void (*)()) code)(); // codeを関数呼び出し.
}
}
-これで74行。・・・まあ一部普段なら改行するところをあえて...
-これだけでも、コマンドラインからプログラムを入力できるし...
-あとは機能を足していけばいいだけなので、言語ってもうそん...
--全体としてすごくシンプルになっているのが伝わるでしょう...
-[[text0021]]によれば、"kll0.h"はコードを1/3くらいにして...
* こめんと欄
#comment
終了行:
* ES-BASIC #5
-(by [[K]], 2019.09.17)
** (7) ES-BASICのコア部分
-もしかしたらES-BASICの根幹部分だけを残して、ほかを全部削...
#include "kll0.h"
unsigned char *code = (unsigned char *) kmallocRWX(1024 ...
int qc;
void sub_print(int i) { printf("%d\n", i); }
void sub_exit() { exit(0); }
void sub_list(KSrc *src)
{
for (int i = 0, i1 = src->ln.sp.sizPtr(); i < i1; i+...
KSrcLine *sl = (KSrcLine *) src->ln.sp.getPtr(i);
printf("%4d %.*s\n", sl->ln, sl->src.s, sl->src....
}
}
int compile(int l, const char *s, int ln, KLexer *lx, KS...
{
static KPhraseCmp *phr = NEW1(kautoreleasePool, KPhr...
KLexer_Elmt *le = (KLexer_Elmt *) lx->reset()->addCo...
for (int i = lx->sidN0, i1 = *lx->sid->n; i < i1; i++)
varSp->addInt(lx->getConstInt(i)); // intではな...
for (int pc = 0, pc1 = lx->saveSidN0()->getElmtLen()...
phr->setPr(le, 0, pc, pc1);
KLexer_Elmt *lp = &le[pc];
if (phr->cmpPr( 1, "@ ;")) continue;
if (phr->cmpPr( 2, "@ @* = @* ;")) { ...
} else if (phr->cmpPr( 3, "@ @* = @* + @* ;")) {...
} else if (phr->cmpPr( 4, "@ @* = @* - @* ;")) {...
} else if (phr->cmpPr( 5, "@ PRINT @* ;")) { ...
} else if (phr->cmpPr( 6, "@ EXIT ;")) { ...
} else if (phr->cmpPr( 7, "@ LIST ;")) { ...
} else {
printf("syntax error [%d] : %.*s\n", ln, kmi...
return 1;
}
while (le[pc].i != 0)
pc++;
}
return 0;
}
int main()
{
KSizPtr varSp(kmemPool); // 変数の値を記憶しておくと...
KLexer lx(NEW1(kautoreleasePool, KStringId)(1, "; = ...
KSrc src; // ソースコードを記憶しておくところ.
char *cmdlin = (char *) kautoreleasePool->alloc(6553...
for (;;) {
ok:
printf("\nOk\n");
skip:
fgets(cmdlin, 65536, stdin);
for (p = cmdlin; *p == ' ' || *p == '\t'; p++);
if (*p == '\n') goto skip;
kupperCaseM(strlen(p), (unsigned char *) p, lx.c...
if ('0' <= *p && *p <= '9') {
src.editLine(strchr(p, '\n') - p, p); // 数...
goto skip; // プログラム入力直後はOkを出さな...
}
qc = kputBin(&code[0], "60 83_ec_7c 0xbd_%i32"...
if (strncmp(p, "RUN", 3) == 0 && p[3] <= ' ') {
for (int i = 0, i1 = src.ln.sp.sizPtr(); i <...
KSrcLine *sl = (KSrcLine *) src.ln.sp.ge...
if (compile(sl->src.s, sl->src.p, sl->ln...
}
} else {
if (compile(strlen(p), p, -1, &lx, &src, &va...
}
qc += kputBin(&code[qc], "83_c4_7c 61 c3"); //...
kputBin(&code[5], "%i32", varSp.p);
((void (*)()) code)(); // codeを関数呼び出し.
}
}
-これで74行。・・・まあ一部普段なら改行するところをあえて...
-これだけでも、コマンドラインからプログラムを入力できるし...
-あとは機能を足していけばいいだけなので、言語ってもうそん...
--全体としてすごくシンプルになっているのが伝わるでしょう...
-[[text0021]]によれば、"kll0.h"はコードを1/3くらいにして...
* こめんと欄
#comment
ページ名: