All Projects → Captainarash → CaptCC

Captainarash / CaptCC

Licence: BSD-3-Clause license
A tiny C compiler written purely in JavaScript.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to CaptCC

JuCC
JuCC - Jadavpur University Compiler Compiler
Stars: ✭ 34 (-80.57%)
Mutual labels:  parsing, compiler-design, compiler-construction
Metatheory.jl
General purpose algebraic metaprogramming and symbolic computation library for the Julia programming language: E-Graphs & equality saturation, term rewriting and more.
Stars: ✭ 266 (+52%)
Mutual labels:  compiler-optimization, compiler-construction
tiny-lang
tiny-lang — A different programming language. Supports a bunch of spoken languages.
Stars: ✭ 26 (-85.14%)
Mutual labels:  compiler-design, compiler-construction
jet
A Fast C and Python like Programming Language that puts the Developer first. WIP
Stars: ✭ 41 (-76.57%)
Mutual labels:  compiler-design, compiler-optimization
hascal
Hascal is a general purpose and open source programming language designed to build optimal, maintainable, reliable and efficient software.
Stars: ✭ 56 (-68%)
Mutual labels:  compiler-design, compiler-optimization
regal86
Register Allocator for 8086
Stars: ✭ 62 (-64.57%)
Mutual labels:  compiler-design, compiler-optimization
awesome-internals
A curated list of awesome resources and learning materials in the field of X internals
Stars: ✭ 78 (-55.43%)
Mutual labels:  compiler-optimization, compiler-construction
PigletC
A toy C-like language compiler for PigletVM
Stars: ✭ 51 (-70.86%)
Mutual labels:  compiler-design, compiler-construction
cppcombinator
parser combinator and AST generator in c++17
Stars: ✭ 20 (-88.57%)
Mutual labels:  parsing, compiler-construction
librxvm
non-backtracking NFA-based regular expression library, for C and Python
Stars: ✭ 57 (-67.43%)
Mutual labels:  parsing, compiler-design
Dissect.cstruct
A no-nonsense c-like structure parsing library for Python
Stars: ✭ 215 (+22.86%)
Mutual labels:  parsing
Pypacker
📦 The fastest and simplest packet manipulation lib for Python
Stars: ✭ 216 (+23.43%)
Mutual labels:  parsing
Jsmn
Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket
Stars: ✭ 2,794 (+1496.57%)
Mutual labels:  parsing
Ohm
A library and language for building parsers, interpreters, compilers, etc.
Stars: ✭ 3,938 (+2150.29%)
Mutual labels:  parsing
Vscode Antlr4
ANTLR4 language support for Visual Studio Code
Stars: ✭ 213 (+21.71%)
Mutual labels:  parsing
Scrapysharp
reborn of https://bitbucket.org/rflechner/scrapysharp
Stars: ✭ 226 (+29.14%)
Mutual labels:  parsing
Pollinate
Template your base files and generate new projects from Git(Hub).
Stars: ✭ 213 (+21.71%)
Mutual labels:  parsing
Goose Parser
Universal scrapping tool, which allows you to extract data using multiple environments
Stars: ✭ 211 (+20.57%)
Mutual labels:  parsing
Gramfuzz
gramfuzz is a grammar-based fuzzer that lets one define complex grammars to generate text and binary data formats.
Stars: ✭ 209 (+19.43%)
Mutual labels:  parsing
autumn
A Java parser combinator library written with an unmatched feature set.
Stars: ✭ 112 (-36%)
Mutual labels:  parsing

--there-will-be-huge-rebuild-improvment-update, if I have time :( contributions are accepted and appreciated.

CaptCC

A tiny Proof-of-Concept C compiler written purely in JavaScript.
It's been a while that I learned JS and it has worked for me in many different scenarios.
I actually learned JS while analyzing a piece of malware written in JS. Then I got really interested.
I was always curious if I can write a compiler; a tiny one.
The funny part was writing it in JS :D

Parts of this project is derived from James Kyle talk at EmberConf 2016 (https://www.youtube.com/watch?v=Tar4WgAfMr4).
I appreciate his effort to make this concept fairly easy to understand.
He made a Lisp Compiler converting Lisp syntax to JS.
I want to convert C to ASM.

The parts below are almost complete:
  1. tokenizer.js
  2. parser.js
  3. traverser.js
  4. processor.js

To be completed:

  1. verifier.js
  2. codeGenerator.js

State of the project:

What can be compiled:
  • Function Definitions
  • Integer Variable Assignments
  • Char variable Assignments (i.e. char my_name[] = "Arash")
  • Increments (var++)
  • Additions and Subtraction
  • Global Variables
  • Return Statement
  • Ifs Only (not nested, not if/elseif/else) Limited Condition support currently
  • Function Calls with integer arguments
What can be parsed into the AST:
  • Every legit character in C syntax
  • Everything is grouped and parsed in a meaningful way
  • Current groupings are "GlobalStatements" and "Functions".
  • Inside GlobalStatements, there are Macros (Only #include for now :p), global varibales and structs.
What's missing from the parser:

typedef, define, ..., a lot!

Usage in browser console (remember to replace the new-lines with the actual \n while copy pasting this):
initGenerate(processor(transformer(parser(tokenizer("
//some comment here :D
int glob = 10;
int jack = 36;

void test_void(void){
    int a = 777;
    int b = a;
    b++;
    return;
}

int test_int_ret(int a, int b){
    a++;
    b++;
    char my_name[] = \"Arash\";
    return a;
}

int main(){
    char greet[] = \"hello\n\";
    int v = 1;
    int f = 8;

    v++;
    f++;

    int k = -4 - 3 + 5 - 7 - 8 + 2 - 32;

    int e = v;

    test(1,2);

    if(v == 2) {
        int y = 5;
        y++;
    }

    if(k == 35) {
        int p = 55 + 34;
    }

return 1;
}
"
)))))                   
Output:

.text
.globl _test_void

_test_void:
push %rbp
mov %rsp,%rbp
push $666
mov 0(%rsp),%rax
push %rax
incl (%rsp)
add $16,%rsp
pop %rbp
ret

.globl _test_int_ret

_test_int_ret:
push %rbp
mov %rsp,%rbp
push %rcx
push %rdx
incl 8(%rsp)
incl (%rsp)
mov 8(%rsp), %rax
add $16,%rsp
pop %rbp
ret

.globl main

main:
push %rbp
mov %rsp,%rbp
push $1
push $8
incl 8(%rsp)
incl (%rsp)
xor %rax,%rax
sub $4,%rax
sub $3,%rax
add $5,%rax
sub $7,%rax
sub $8,%rax
add $2,%rax
sub $32,%rax
push %rax
mov 16(%rsp),%rax
push %rax
cmp $2,24(%rsp)
jne _ifv2_after
push $5
incl (%rsp)
add $8,%rsp

_ifv2_after:
cmp $35,8(%rsp)
jne _ifk35_after
xor %rax,%rax
add $55,%rax
add $34,%rax
push %rax
add $8,%rsp

_ifk35_after: mov $1,%rax
add $32,%rsp
pop %rbp
ret

.data
.globl _glob
_glob:
.long 10

.globl _jack
_jack:
.long 36

To test the compiler code:
  1. Copy the output into a file, let's say compiler_test.s

  2. generate the binary using GCC

     gcc compiler_test.s -o compiler_test  
    
  3. Run the binary

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].