Skip to content

code4fukui/yasa-c

Repository files navigation

Yasa-C

Yasa-C logo

Yasa-C は、ブラウザ上で小さな C を wasm にコンパイルし、右側の canvas で setup() を1回実行した後、loop() を毎フレーム実行する学習用アプリです。

Files

  • index.html: 左右 2 ペインの UI
  • app.js: エディタ、コンパイル、wasm 実行の接続
  • samples/*.c: サンプルプログラム
  • compiler.js: 小さな C サブセットを wasm バイナリへ変換
  • yasac.js: C ファイルを wasm ファイルへ変換する Deno CLI
  • game.js: 描画とキー入力のホスト関数

Supported C subset

  • 型: int, void, struct
  • 文: 変数宣言、if, while, for, return
  • 式: =, + - * / %, == != < > <= >=, 関数呼び出し、struct.field
  • 関数引数: int のみ
  • グローバル初期化: 整数リテラルのみ
  • 関数 export: ユーザー定義関数はすべて wasm export にする
  • 関数 import: extern int rnd(int n); のような C の extern 関数宣言を env.rnd の wasm import にする

Host functions

ホスト関数は C 側で宣言してから使います。

extern void cls(int color);
extern void rect(int x, int y, int w, int h, int color);
extern int inkey(int code);
extern int rnd(int n);
extern int width();
extern int height();
extern void stop();

Run

静的ファイルとして開けます。例えば:

deno --allow-env --allow-import --allow-read --allow-net https://code4fukui.github.io/liveserver/liveserver.js 8000

その後 http://localhost:8000/ を開きます。

CLI

yasac.js は指定した .c ファイルをコンパイルし、同じ場所に .wasm ファイルを書き出します。

deno run --allow-import --allow-read --allow-write yasac.js samples/move.c

yasac コマンドとしてインストールする場合:

deno install --allow-import --allow-read --allow-write --global --name yasac yasac.js
yasac samples/move.c

Releases

No releases published

Packages

 
 
 

Contributors