Skip to content

zurl/web-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

web-cpp

Build Status Coverage Status

A experimental c++ compiler in typescript

online demo => zurl.github.io/web-cpp

Introduction

This project is a In-browser C++ Compiler and Runtime toolchains, which requires no server and could run in all major browsers.

Our compiler could compile C++ to WebAssembly, which is a low-level programming language. WebAssembly is supported by most browsers currently, it is much more faster than JavaScript, and able to be compiled from all static languages.

LLVM provides excellent support of WebAssembly Backend, our compiler is inspired by them and learned a lot from its good design.

Our Compiler and Runtime is much more fast than all C++ interpreter in browser, the c++ code will be compiled directly to WebAssembly, and WebAssembly will be executed by high performance JIT execution engine in our browser. According to our experiment, our compiler could have nearly same performance to gcc/g++ without optimization.

How to Use

  1. build web-cpp compiler itself
npm install
npm run build
npm run test
  1. build the web-cpp online ide

we use parcel as our packager, you could use any kind of web packager with your custom configuration.

npm run build
cd ide
parcel build index.html

How to develop/extend

Our Compiler is designed as a loosely coupled, componentized compiler, so it is easy for you to extend or further develop our compiler system.

Write Javascript Library

The Javascript Library in our library is called "syscall", which is similar to UNIX syscall concept.

Before you add new JavaScript syscall, you should write a header file in resource/libstdcpp/include, you could extend or create new cpp file in the directory. The syntax is similar to ordinary C++ function declaration, but you need a __libcall prefix before your declaration.

The syscall code is located in src/library/syscall.ts, please just write plain js function (not arrow function) that receive js number and return js number or none. The this pointer of js function will be WASM runtime, you could access C++ Virtual Machine via this pointer, string could be pass by memory address.

After any modification, please run npm run build to refresh the binary library file.

Write C++ Library

Similarly, Library in C++ is also supported by our system, your need to add .h file in resource/libstdcpp/include and .cpp file in resource/libstdcpp/lib, our compiler will automatically load these files if you build the compiler.

After any modification, please run npm run build to refresh the binary library file.

Add new C++ Grammar

Our grammar files are located in resource/grammar, it is divided into several files for readability, the file are in parsing expression grammar which supported by PEG.js(https://pegjs.org/), you could write your own rules to extend our compiler.

After any modification, please run npm run build to refresh the binary parser file.

Using WebAssembly Backend

Our compiler has a high-level abstraction of WebAssembly AST, which located in src/wasm directory,

The WASM AST is abstracted in a tree-like structure, the hierarchy is WModule -> WSection -> WStatement -> WExpression WExpression is the minimal unit of WASM AST, it could emit a return value, and consume by WStatement. WStatements contains several control flow structure, and several WStatements will be composed to a WCodeSection.

For other WSection, you could refer to the official standard of WebAssembly.

Road Map

Version

  • 0.4 Classic C with class support
  • 0.5 With interpreter runtime
  • 0.6 With function template
  • 0.7 With class template
  • 0.8 With std library (some)

C language

  • function call codegen / return codegen
  • & && | || >> <<
  • ++ --
  • + - ! ~
  • += -= *= /= ...
  • vm
  • array
  • sizeof
  • typedef
  • union
  • js native function
  • string
  • var initilizer
  • data segment data
  • doConstant about < > <= >= == & && | || >> << ...
  • struct / class
  • cast ope (hard)
  • void return type;
  • function call parameter type conversion
  • var args
  • allocator
  • char * a = "123"
  • write, read
  • printf
  • postfix ++ --
  • do-while
  • break continue
  • [deprecated] goto label
  • switch case
  • enum
  • non-return detect
  • js highlevel api
  • constant fold on tree
  • init instructions
  • print sourceMap
  • cc-cli
  • local address
  • const
  • malloc need
  • & array alias
  • &
  • int64
  • ?:
  • function pointer
  • array initial list
  • multi-dim array
  • bit field of struct
  • #if #elif
  • #line line file
  • debuginfo

C++ Language

  • default parameter
  • default constructor
  • copy-constructor(use memcpy)
  • temporary object destruction
  • destructor
  • left reference
  • static member variable
  • A a(c,d)
  • ctor
  • inner scope
  • DTOR
  • ctor initialize list
  • copy-ctor
  • copy-assignment-ctor
  • static member function
  • member function
  • function overload
  • member function overload
  • __cxx_global_var_init
  • inheriant
  • operator overload => working, bin->ok, unary->working, syntax
  • implict this
  • public/private/ protect, access control (syntax ok, todo)
  • new/delete
  • new array []
  • using
  • namespace
  • virtual member function
  • [not support by wasm] exception handling
  • function template
  • member function template in class
  • class template
  • mangled/demangled
  • template class in template class
  • template member function

TODO LIST

High
  • repeative param name detect

  • cast overload, like if(object) { ... }

  • warning

  • class specialization

  • using template

  • A a[50] decons

  • subclass B A::a => set children?

  • explicit class ins

  • placement new

  • C std lib

  • C++ std lib

  • iostream∂

  • string

  • vector

  • map

  • queue/stack

  • priority_queue

  • algorithm

  • id could not be keyword => special judge

  • operator []

  • operator ()

  • static_cast / dynamic_cast / reinterpret_cast

  • const left value reference ====== the upper is all plan of web-cpp in 2019

    Other

  • const/override member function

  • real const

  • real override

  • real accessControl

  • seperate define class function

  • seperate delaration => to be test

  • typeinfo

  • virtual inheriant

ide

  • multi-language
  • config
  • help

Miscellaneous

This project is my thesis of my bachelor's degree, which is inspired by the rapid development of online programing education. This project is targeted to improve the C++ learning experience for new students, thanks to everyone who helps me in the development of this project.