{"id":13783174,"url":"https://github.com/yasuo-ozu/cantang","last_synced_at":"2025-07-07T13:37:37.250Z","repository":{"id":162832322,"uuid":"79419522","full_name":"yasuo-ozu/cantang","owner":"yasuo-ozu","description":"interpreter","archived":false,"fork":false,"pushed_at":"2022-05-15T10:49:33.000Z","size":35,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-04T15:48:21.379Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yasuo-ozu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-01-19T05:39:55.000Z","updated_at":"2022-05-15T10:49:36.000Z","dependencies_parsed_at":"2023-05-20T11:00:26.100Z","dependency_job_id":null,"html_url":"https://github.com/yasuo-ozu/cantang","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/yasuo-ozu/cantang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasuo-ozu%2Fcantang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasuo-ozu%2Fcantang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasuo-ozu%2Fcantang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasuo-ozu%2Fcantang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yasuo-ozu","download_url":"https://codeload.github.com/yasuo-ozu/cantang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yasuo-ozu%2Fcantang/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264086925,"owners_count":23555388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:54.948Z","updated_at":"2025-07-07T13:37:37.176Z","avatar_url":"https://github.com/yasuo-ozu.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"# cantang - たった600行の簡易インタープリタのモデル実装\n\n## cantang とは\ncantangは、1ファイル(約600行)のC言語コードで実装されたインタープリタです。実行できるコードはC言語のサブセットとなっています。\n\n## 特徴\nトークン解析、構文解析をかなり単純化しています。また構文木を生成せず、トークン列を直接実行します。\nコンパイラをシンプルに実装するために速度を犠牲にしており、以下の理由により、処理に通常の処理系と比べると1000~10000倍の時間がかかります。\n\n- コード上の同じ箇所を毎回構文解析を行うため。\n- コード上の実行しない部分についても毎回構文解析を行うため。\n- 再帰下降パーサにより式を処理しているため。\n\nまた、速度以外にも以下のような特徴が有ります。\n\n- 型がない (すべてlong long型と同等のものとして処理する)\n\n## 仕様\n実際に動作するコードは\nhttps://github.com/yasuo-ozu/cantang/tree/master/tests\nで確認できます。\n\n### 対応している機能\n\n- 変数の宣言、初期化\n- 各種演算子( 前置 , 後置 , 二項 , 三項演算子等 )\n- 各種リテラル\n- 文字列\n- print 文 ( 数値表示 ), puts 文 ( 文字列表示 )\n- if-else, for, while, do-while, return, break, continue\n- 多次元配列\n- 関数宣言 , 引数 , 再帰関数\n- struct\n- #include\n\n### 対応していない(C言語の)機能\n\n- 入れ子になった構造体\n- enum, union\n- typedef\n- ポインタへの足し算(バイト単位の計算になる)\n- キャスト(型がないため)\n- ファイルアクセス, 標準ライブラリの多くの機能\n- goto / label\n- and so on\n\n## 仕組み\n\n### 1. トークン解析\n入力されたコードはまずトークン解析器にかけられます。トークン解析では、入力をトークン列に分解します。このとき、各種リテラル、記号、キーワード、それ以外の識別子を区別しフラグにセットします。キーワードを区別するためにキーワードリストを保持します。これは変数名としてキーワードを使用する等のエラーを検出するために有用です。また、記号についても2文字以上の記号によって構成される演算子を1つのものと認識するために記号列を保持しています。\n\nなお、ここで作られたトークン列はそのまま実行に利用されるので、一度作成されたトークンが削除されることはありません。\n\n### 2. 実行\ncantangでは、構文解析と実行を同時に行っています。例えば、現在注目しているトークンが「for」であれば、即座にforを処理する部分に飛びます。繰り返し処理では、繰り返し処理の最初のトークンの位置が記憶されていて、繰り返し処理の終端にて条件に合致すれば記憶されていたトークン位置に現在注目しているトークンが戻されます。\n\nif文などで条件が成立しない場合、if文の処理の続きをスキップする必要があります。この場合は、if文の内部の処理を「空実行」します。なぜこれが必要であるかというと、「if」というトークンを解釈している段階ではまだどこまでスキップすればよいか把握できないためです。\n\n## コンパイルの方法\n以下のようにmain.cをコンパイルするだけです。\n```\n\u003e gcc main.c\n```\n\n以下のように実行することが出来ます。\n\n```\n\u003e ./a.out -\nint a = 1+2*3/4+5*(6+7-8);\nprint a;\n27\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyasuo-ozu%2Fcantang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyasuo-ozu%2Fcantang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyasuo-ozu%2Fcantang/lists"}