{"id":18019822,"url":"https://github.com/captainarash/captcc","last_synced_at":"2025-06-30T00:32:57.325Z","repository":{"id":44388949,"uuid":"113690844","full_name":"Captainarash/CaptCC","owner":"Captainarash","description":"A tiny C compiler written purely in JavaScript.","archived":false,"fork":false,"pushed_at":"2019-07-14T20:16:48.000Z","size":109,"stargazers_count":216,"open_issues_count":1,"forks_count":35,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-06-11T11:45:56.492Z","etag":null,"topics":["compiler","compiler-construction","compiler-design","compiler-optimization","compiler-principles","parser","parsing"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Captainarash.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-09T18:14:47.000Z","updated_at":"2025-06-10T10:55:06.000Z","dependencies_parsed_at":"2022-07-15T00:45:59.508Z","dependency_job_id":null,"html_url":"https://github.com/Captainarash/CaptCC","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Captainarash/CaptCC","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Captainarash%2FCaptCC","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Captainarash%2FCaptCC/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Captainarash%2FCaptCC/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Captainarash%2FCaptCC/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Captainarash","download_url":"https://codeload.github.com/Captainarash/CaptCC/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Captainarash%2FCaptCC/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262689607,"owners_count":23349133,"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":["compiler","compiler-construction","compiler-design","compiler-optimization","compiler-principles","parser","parsing"],"created_at":"2024-10-30T05:12:32.911Z","updated_at":"2025-06-30T00:32:57.294Z","avatar_url":"https://github.com/Captainarash.png","language":"JavaScript","readme":"--there-will-be-huge-rebuild-improvment-update, if I have time :(\ncontributions are accepted and appreciated.\n\n# CaptCC\nA tiny Proof-of-Concept C compiler written purely in JavaScript.   \nIt's been a while that I learned JS and it has worked for me in many different scenarios.      \nI actually learned JS while analyzing a piece of malware written in JS. Then I got really interested.      \nI was always curious if I can write a compiler; a tiny one.  \nThe funny part was writing it in JS :D\n\n\nParts of this project is derived from James Kyle talk at EmberConf 2016 (https://www.youtube.com/watch?v=Tar4WgAfMr4).  \nI appreciate his effort to make this concept fairly easy to understand.    \nHe made a Lisp Compiler converting Lisp syntax to JS.   \nI want to convert C to ASM.   \n\n###### The parts below are almost complete:\n\n1. tokenizer.js   \n2. parser.js   \n3. traverser.js   \n4. processor.js   \n\nTo be completed:   \n\n5. verifier.js   \n6. codeGenerator.js     \n\n\n#### State of the project:\n\n###### What can be compiled:\n\n* Function Definitions\n* Integer Variable Assignments\n* Char variable Assignments (i.e. char my_name[] = \"Arash\")\n* Increments (var++)\n* Additions and Subtraction\n* Global Variables\n* Return Statement\n* Ifs Only (not nested, not if/elseif/else) Limited Condition support currently\n* Function Calls with integer arguments\n\n###### What can be parsed into the AST:\n\n* Every legit character in C syntax\n* Everything is grouped and parsed in a meaningful way\n* Current groupings are \"GlobalStatements\" and \"Functions\".\n* Inside GlobalStatements, there are Macros (Only #include for now :p), global varibales and structs.\n\n###### What's missing from the parser:\n\ntypedef, define, ..., a lot!\n\n##### Usage in browser console (remember to replace the new-lines with the actual \\n while copy pasting this):\n\n    initGenerate(processor(transformer(parser(tokenizer(\"\n    //some comment here :D\n    int glob = 10;\n    int jack = 36;\n\n    void test_void(void){\n        int a = 777;\n        int b = a;\n        b++;\n        return;\n    }\n\n    int test_int_ret(int a, int b){\n        a++;\n        b++;\n        char my_name[] = \\\"Arash\\\";\n        return a;\n    }\n\n    int main(){\n        char greet[] = \\\"hello\\n\\\";\n        int v = 1;\n        int f = 8;\n\n        v++;\n        f++;\n\n        int k = -4 - 3 + 5 - 7 - 8 + 2 - 32;\n\n        int e = v;\n\n        test(1,2);\n\n        if(v == 2) {\n            int y = 5;\n            y++;\n        }\n\n        if(k == 35) {\n            int p = 55 + 34;\n        }\n\n\treturn 1;\n    }\n    \"\n    )))))                   \n\n##### Output:   \n  .text  \n  .globl\t_test_void  \n\n  _test_void:  \n  push %rbp  \n  mov %rsp,%rbp  \n  push $666  \n  mov 0(%rsp),%rax  \n  push %rax  \n  incl (%rsp)  \n  add $16,%rsp  \n  pop %rbp  \n  ret  \n\n  .globl\t_test_int_ret  \n\n  _test_int_ret:  \n  push %rbp    \n  mov %rsp,%rbp  \n  push %rcx  \n  push %rdx  \n  incl 8(%rsp)  \n  incl (%rsp)  \n  mov 8(%rsp), %rax  \n  add $16,%rsp  \n  pop %rbp  \n  ret  \n  \n  .globl\tmain\n\n  main:  \n  push %rbp  \n  mov %rsp,%rbp  \n  push $1  \n  push $8  \n  incl 8(%rsp)  \n  incl (%rsp)  \n  xor %rax,%rax  \n  sub $4,%rax  \n  sub $3,%rax  \n  add $5,%rax  \n  sub $7,%rax  \n  sub $8,%rax  \n  add $2,%rax  \n  sub $32,%rax  \n  push %rax  \n  mov 16(%rsp),%rax  \n  push %rax  \n  cmp $2,24(%rsp)  \n  jne _ifv2_after  \n  push $5    \n  incl (%rsp)  \n  add $8,%rsp  \n\n   _ifv2_after:  \n    cmp $35,8(%rsp)  \n    jne _ifk35_after  \n    xor %rax,%rax  \n    add $55,%rax  \n    add $34,%rax   \n    push %rax  \n    add $8,%rsp  \n\n  _ifk35_after:\n    mov $1,%rax  \n    add $32,%rsp  \n    pop %rbp  \n    ret  \n\n  .data    \n    .globl\t_glob  \n    _glob:  \n      .long\t10  \n\n  .globl\t_jack  \n    _jack:  \n      .long\t36  \n\n\n##### To test the compiler code:\n\n1. Copy the output into a file, let's say compiler_test.s  \n2. generate the binary using GCC  \n\n        gcc compiler_test.s -o compiler_test  \n\n\n3. Run the binary  \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaptainarash%2Fcaptcc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaptainarash%2Fcaptcc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaptainarash%2Fcaptcc/lists"}