Overview: Translating Oil to C++

category total lines num files
mycpp Translator 4,767 11
mycpp Test Data 3,393 42
Zephyr ASDL 3,178 12
pgen2 Parser Generator 1,847 8
Other Code Generators 2,949 11
Hand-written C++ Code 3,081 28
Garbage-Collected Runtime 4,358 18
Unit tests in C++ 6,263 22
Incremental C++ Build 3,064 17
OSH (and common libraries) 39,893 79
Spec Tests 41,973 160
Gold Tests 2,775 27
Generated C++ Code 95,699 30

Related Documents

The README for oilshell/oil has another overview of the repository.

mycpp Translator

This prototype uses the MyPy frontend to translate statically-typed Python to C++. The generated code calls a small runtime which implements things like List[T], Dict[K, V], and Python's len().

mycpp/compare_pairs.py                           60
mycpp/const_pass.py                             530
mycpp/cppgen_pass.py                          2,796
mycpp/crash.py                                   38
mycpp/debug_pass.py                             465
mycpp/format_strings.py                         114
mycpp/mycpp_main.py                             361
mycpp/mylib.py                                  296
mycpp/pass_state.py                              93
mycpp/util.py                                    14

4,767 lines in 11 files  

mycpp Test Data

Small Python examples that translate to C++, compile, and run.

mycpp/examples/arith_ops.py                      45
mycpp/examples/asdl_generated.py                 64
mycpp/examples/cartesian.py                      78
mycpp/examples/cgi.py                            62
mycpp/examples/classes_gc.py                    209
mycpp/examples/classes.py                       294
mycpp/examples/containers.py                    234
mycpp/examples/control_flow.py                  113
mycpp/examples/escape.py                         70
mycpp/examples/fib_iter.py                       54
mycpp/examples/fib_recursive.py                  49
mycpp/examples/files.py                          64
mycpp/examples/__init__.py                        0
mycpp/examples/invalid_condition.py              34
mycpp/examples/invalid_default_args.py           44
mycpp/examples/invalid_except.py                 40
mycpp/examples/invalid_format_args.py            15
mycpp/examples/invalid_format_strings.py         20
mycpp/examples/invalid_global.py                 16
mycpp/examples/invalid_print.py                  13
mycpp/examples/invalid_try_else.py               37
mycpp/examples/invalid_types_2.py                32
mycpp/examples/invalid_types.py                  32
mycpp/examples/iterators.py                     101
mycpp/examples/length.py                        110
mycpp/examples/lexer_main.py                     47
mycpp/examples/loops.py                         177
mycpp/examples/modules.py                        80
mycpp/examples/parse.py                         264
mycpp/examples/pgen2_demo.py                    106
mycpp/examples/scoped_resource.py               196
mycpp/examples/strings.py                        93
mycpp/examples/test_cast.py                      59
mycpp/examples/test_conditional.py               67
mycpp/examples/test_default_args.py              75
mycpp/examples/test_globals.py                   62
mycpp/examples/test_hoist.py                     61
mycpp/examples/test_io_os_error.py              131
mycpp/examples/test_switch.py                    42
mycpp/examples/tuple_return_value.py             55
mycpp/examples/varargs.py                        48

3,393 lines in 42 files  

Zephyr ASDL

A DSL for algebraic data types, borrowed from Python. Oil is the most strongly typed Bourne shell implementation!

asdl/asdl_main.py                               299
asdl/ast.py                                     231
asdl/format.py                                  497
asdl/front_end.py                               510
asdl/gen_cpp.py                                 766
asdl/gen_python.py                              570
asdl/NINJA_subgraph.py                           58
asdl/pybase.py                                   60
asdl/runtime.py                                  40
asdl/util.py                                     11
asdl/visitor.py                                 136

3,178 lines in 12 files  

pgen2 Parser Generator

An LL(1) parser generator used to parse Oil expressions. Also borrowed from CPython.

pgen2/driver.py                                 101
pgen2/grammar.py                                303
pgen2/parse.py                                  220
pgen2/pgen.py                                   428
pgen2/pnode.py                                   67
pgen2/tokenize.py                               579
pgen2/token.py                                  149

1,847 lines in 8 files  

Other Code Generators

In order to make Oil statically typed, we had to abandon Python reflection and use C++ source code generation instead. The lexer, flag definitions, and constants can be easily compiled to C++.

core/optview_gen.py                              94
cpp/embedded_file_gen.py                         66
doctools/help_gen.py                            631
frontend/consts_gen.py                          608
frontend/flag_gen.py                            525
frontend/lexer_gen.py                           487
frontend/option_gen.py                           94
frontend/signal_gen.py                          105
osh/arith_parse_gen.py                          100
ysh/grammar_gen.py                              239

2,949 lines in 11 files  

Hand-written C++ Code

Includes OS bindings. Small C++ files like cpp/osh_arith_parse.{cc,h} correspond to larger Python files like osh/arith_parse.py.

cpp/core.cc                                     389
cpp/core.h                                      273
cpp/embedded_file.h                              18
cpp/fanos.cc                                     57
cpp/fanos.h                                      18
cpp/fanos_shared.h                               49
cpp/frontend_flag_spec.cc                       273
cpp/frontend_flag_spec.h                        161
cpp/frontend_match.cc                           110
cpp/frontend_match.h                             77
cpp/frontend_pyreadline.cc                      313
cpp/frontend_pyreadline.h                        74
cpp/libc.cc                                     219
cpp/libc.h                                       36
cpp/osh.cc                                      161
cpp/osh.h                                        50
cpp/osh_tdop.cc                                  16
cpp/osh_tdop.h                                   52
cpp/pgen2.cc                                     44
cpp/pgen2.h                                      85
cpp/preamble.h                                   38
cpp/pylib.cc                                     58
cpp/pylib.h                                      22
cpp/qsn.h                                        57
cpp/stdlib.cc                                   228
cpp/stdlib.h                                    165
cpp/translation_stubs.h                          38

3,081 lines in 28 files  

Garbage-Collected Runtime

Uses a fork-friendly Mark-Sweep collector.

mycpp/common.h                                   66
mycpp/comparators.h                              65
mycpp/gc_alloc.h                                230
mycpp/gc_builtins.cc                            430
mycpp/gc_builtins.h                             168
mycpp/gc_dict.h                                 422
mycpp/gc_list.h                                 505
mycpp/gc_mylib.cc                               249
mycpp/gc_mylib.h                                280
mycpp/gc_obj.h                                  169
mycpp/gc_slab.h                                  58
mycpp/gc_str.cc                                 651
mycpp/gc_str.h                                  166
mycpp/gc_tuple.h                                147
mycpp/mark_sweep_heap.cc                        438
mycpp/mark_sweep_heap.h                         294
mycpp/runtime.h                                  20

4,358 lines in 18 files  

Unit tests in C++

The goal is to make the spec tests pass, but unit tests are helpful too.

cpp/core_test.cc                                411
cpp/data_race_test.cc                           206
cpp/frontend_flag_spec_test.cc                  154
cpp/frontend_match_test.cc                       87
cpp/libc_test.cc                                135
cpp/obj_layout_test.cc                          187
cpp/osh_test.cc                                  50
cpp/pylib_test.cc                                52
cpp/qsn_test.cc                                  47
cpp/stdlib_test.cc                              152
mycpp/bump_leak_heap_test.cc                     62
mycpp/gc_builtins_test.cc                       356
mycpp/gc_dict_test.cc                           523
mycpp/gc_heap_test.cc                           417
mycpp/gc_list_test.cc                           492
mycpp/gc_mylib_test.cc                          301
mycpp/gc_stress_test.cc                         222
mycpp/gc_str_test.cc                          1,475
mycpp/gc_tuple_test.cc                          138
mycpp/mark_sweep_heap_test.cc                   294
mycpp/small_str_test.cc                         502

6,263 lines in 22 files  

Incremental C++ Build

asdl/NINJA_subgraph.py                           58
bin/NINJA_subgraph.py                           107
build/ninja_lib.py                              487
build/ninja_lib_test.py                         253
build/ninja_main.py                             406
build/ninja-rules-cpp.sh                        356
build/ninja-rules-py.sh                         336
core/NINJA_subgraph.py                           48
cpp/NINJA_subgraph.py                           221
frontend/NINJA_subgraph.py                      151
mycpp/NINJA_subgraph.py                         400
NINJA-config.sh                                 107
osh/NINJA_subgraph.py                            31
pea/NINJA_subgraph.py                            20
prebuilt/NINJA_subgraph.py                       45
ysh/NINJA_subgraph.py                            38

3,064 lines in 17 files  

OSH (and common libraries)

This is the input to the translators, written in statically-typed Python. Note that bash is at least 140K lines of code, and OSH implements a large part of bash and more.

bin/oils_for_unix.py                            208
core/alloc.py                                   278
core/ansi.py                                     15
core/code.py                                    174
core/completion.py                            1,482
core/comp_ui.py                                 584
core/dev.py                                     597
core/error.py                                   256
core/executor.py                                660
core/main_loop.py                               388
core/NINJA_subgraph.py                           48
core/optview.py                                  54
core/process.py                               1,883
core/pyos.py                                    409
core/pyutil.py                                  213
core/runtime.asdl                               237
core/shell.py                                 1,015
core/state.py                                 2,521
core/ui.py                                      418
core/util.py                                    129
core/vm.py                                      329
frontend/args.py                                651
frontend/builtin_def.py                         160
frontend/consts.py                              332
frontend/flag_def.py                            508
frontend/flag_spec.py                           392
frontend/id_kind_def.py                         779
frontend/lexer_def.py                           983
frontend/lexer.py                               408
frontend/location.py                            454
frontend/match.py                               274
frontend/NINJA_subgraph.py                      151
frontend/option_def.py                          394
frontend/parse_lib.py                           499
frontend/py_readline.py                          94
frontend/reader.py                              233
frontend/signal_def.py                           94
frontend/syntax_abbrev.py                       137
frontend/syntax.asdl                            627
frontend/typed_args.py                          312
frontend/types.asdl                              45
mycpp/mylib.pyi                                  89
osh/arith_parse.py                              194
osh/bool_parse.py                               280
osh/bool_stat.py                                135
osh/braces.py                                   526
osh/builtin_assign.py                           558
osh/builtin_bracket.py                          283
osh/builtin_comp.py                             561
osh/builtin_lib.py                              134
osh/builtin_meta.py                             516
osh/builtin_misc.py                             930
osh/builtin_printf.py                           500
osh/builtin_process.py                          374
osh/builtin_pure.py                             971
osh/builtin_trap.py                             274
osh/cmd_eval.py                               2,070
osh/cmd_parse.py                              2,598
osh/glob_.py                                    484
osh/history.py                                  192
osh/NINJA_subgraph.py                            31
osh/prompt.py                                   309
osh/sh_expr_eval.py                           1,068
osh/split.py                                    302
osh/string_ops.py                               461
osh/tdop.py                                     339
osh/word_compile.py                             269
osh/word_eval.py                              2,319
osh/word_parse.py                             1,949
osh/word_.py                                    698
pyext/fanos.c                                   119
pyext/fastlex.c                                 246
pyext/fastlex.pyi                                15
pyext/libc.c                                    414
pyext/libc.pyi                                   13
pyext/line_input.pyi                             32
pyext/posix_.pyi                                211
pyext/yajl.pyi                                    4

39,893 lines in 79 files  

Spec Tests

A comprehensive test suite that compares OSH against other shells. If OSH passes these tests in BOTH Python and C++, it means that the translation works.

spec/alias.test.sh                              552
spec/append.test.sh                             302
spec/arith-context.test.sh                       81
spec/arith.test.sh                              626
spec/array-compat.test.sh                       189
spec/array.test.sh                              627
spec/assign-deferred.test.sh                    105
spec/assign-dialects.test.sh                     59
spec/assign-extended.test.sh                    810
spec/assign.test.sh                             672
spec/assoc.test.sh                              645
spec/assoc-zsh.test.sh                           53
spec/background.test.sh                         182
spec/ble-features.test.sh                       644
spec/ble-idioms.test.sh                         157
spec/blog1.test.sh                               95
spec/blog2.test.sh                               49
spec/blog-other1.test.sh                         57
spec/brace-expansion.test.sh                    441
spec/bugs.test.sh                               342
spec/builtin-bash.test.sh                       348
spec/builtin-bracket.test.sh                    579
spec/builtin-completion.test.sh                 484
spec/builtin-dirs.test.sh                       238
spec/builtin-eval-source.test.sh                249
spec/builtin-getopts.test.sh                    347
spec/builtin-history.test.sh                    196
spec/builtin-io.test.sh                         755
spec/builtin-printf.test.sh                     898
spec/builtins2.test.sh                          211
spec/builtin-special.test.sh                    111
spec/builtins.test.sh                           434
spec/builtin-times.test.sh                       15
spec/builtin-trap-bash.test.sh                  474
spec/builtin-trap.test.sh                       337
spec/builtin-vars.test.sh                       699
spec/case_.test.sh                              178
spec/command-parsing.test.sh                     63
spec/command-sub.test.sh                        285
spec/command_.test.sh                           192
spec/comments.test.sh                            11
spec/dbracket.test.sh                           434
spec/dparen.test.sh                             204
spec/empty-bodies.test.sh                        24
spec/errexit-osh.test.sh                        758
spec/errexit.test.sh                            393
spec/exit-status.test.sh                        238
spec/explore-parsing.test.sh                     42
spec/extglob-files.test.sh                      377
spec/extglob-match.test.sh                      400
spec/fatal-errors.test.sh                       186
spec/for-expr.test.sh                           117
spec/func-parsing.test.sh                       105
spec/glob.test.sh                               386
spec/hay-isolation.test.sh                      169
spec/hay-meta.test.sh                           200
spec/hay.test.sh                                684
spec/here-doc.test.sh                           370
spec/if_.test.sh                                 56
spec/interactive-parse.test.sh                   37
spec/interactive.test.sh                        343
spec/introspect.test.sh                         296
spec/let.test.sh                                 23
spec/loop.test.sh                               473
spec/nameref.test.sh                            638
spec/nix-idioms.test.sh                         153
spec/nocasematch-match.test.sh                   71
spec/nul-bytes.test.sh                          157
spec/parse-errors.test.sh                       192
spec/pipeline.test.sh                           197
spec/posix.test.sh                              153
spec/process-sub.test.sh                        201
spec/prompt.test.sh                             255
spec/quote.test.sh                              230
spec/redirect-multi.test.sh                     309
spec/redirect.test.sh                           674
spec/regex.test.sh                              356
spec/serialize.test.sh                          229
spec/shell-grammar.test.sh                      206
spec/sh-func.test.sh                            161
spec/sh-options.test.sh                         697
spec/sh-usage.test.sh                            94
spec/smoke.test.sh                              126
spec/strict-options.test.sh                     249
spec/subshell.test.sh                            23
spec/tea-func.test.sh                           288
spec/tilde.test.sh                              172
spec/TODO-deprecate.test.sh                      29
spec/toysh-posix.test.sh                        347
spec/toysh.test.sh                              144
spec/type-compat.test.sh                        105
spec/var-num.test.sh                             43
spec/var-op-bash.test.sh                        310
spec/var-op-len.test.sh                         219
spec/var-op-patsub.test.sh                      399
spec/var-op-slice.test.sh                       315
spec/var-op-strip.test.sh                       357
spec/var-op-test.test.sh                        339
spec/var-ref.test.sh                            509
spec/vars-bash.test.sh                           30
spec/vars-special.test.sh                       609
spec/var-sub-quote.test.sh                      312
spec/var-sub.test.sh                             60
spec/word-eval.test.sh                           62
spec/word-split.test.sh                         421
spec/xtrace.test.sh                             344
spec/ysh-array.test.sh                           52
spec/ysh-assign.test.sh                         531
spec/ysh-bin.test.sh                             19
spec/ysh-blocks.test.sh                         385
spec/ysh-bugs.test.sh                            94
spec/ysh-builtin-argparse.test.sh                74
spec/ysh-builtin-describe.test.sh                45
spec/ysh-builtin-error.test.sh                  131
spec/ysh-builtin-help.test.sh                    50
spec/ysh-builtin-module.test.sh                  88
spec/ysh-builtin-pp.test.sh                      47
spec/ysh-builtin-process.test.sh                 85
spec/ysh-builtin-shopt.test.sh                  180
spec/ysh-builtins.test.sh                       570
spec/ysh-case.test.sh                           231
spec/ysh-command-sub.test.sh                    109
spec/ysh-completion.test.sh                     204
spec/ysh-convert.test.sh                        151
spec/ysh-demo.test.sh                            59
spec/ysh-dev.test.sh                             86
spec/ysh-expr-arith.test.sh                     214
spec/ysh-expr-bool.test.sh                      156
spec/ysh-expr-compare.test.sh                   345
spec/ysh-expr-sub.test.sh                        42
spec/ysh-expr.test.sh                           727
spec/ysh-for.test.sh                            153
spec/ysh-funcs-builtin.test.sh                  146
spec/ysh-funcs-external.test.sh                  87
spec/ysh-funcs.test.sh                          498
spec/ysh-interactive.test.sh                     37
spec/ysh-json.test.sh                           178
spec/ysh-keywords.test.sh                        37
spec/ysh-methods.test.sh                         66
spec/ysh-multiline.test.sh                      179
spec/ysh-options-assign.test.sh                 106
spec/ysh-options.test.sh                        815
spec/ysh-proc.test.sh                           326
spec/ysh-regex.test.sh                          961
spec/ysh-reserved.test.sh                        48
spec/ysh-scope.test.sh                          768
spec/ysh-slice-range.test.sh                    154
spec/ysh-source.test.sh                          29
spec/ysh-special-vars.test.sh                    50
spec/ysh-stdlib.test.sh                         195
spec/ysh-string.test.sh                         347
spec/ysh-tuple.test.sh                           28
spec/ysh-unicode.test.sh                         20
spec/ysh-usage.test.sh                           29
spec/ysh-user-feedback.test.sh                  219
spec/ysh-var-sub.test.sh                         45
spec/ysh-with-sh.test.sh                        221
spec/ysh-word-eval.test.sh                      145
spec/ysh-xtrace.test.sh                         519

41,973 lines in 160 files  

Gold Tests

Another suite that tests shells "from the outside". Instead of making explicit assertions, we verify that OSH behaves like bash.

test/gold/abuild.sh                               9
test/gold/and-or.sh                              27
test/gold/autoconf-backtick.sh                   50
test/gold/big-here-doc.sh                     1,531
test/gold/case-in-subshell.sh                    37
test/gold/char-class.sh                          33
test/gold/command-sub-2.sh                       12
test/gold/command-sub.sh                         39
test/gold/comments.sh                            22
test/gold/complex-here-docs.sh                  192
test/gold/configure-bug.sh                       13
test/gold/de1.sh                                 24
test/gold/declare.sh                             28
test/gold/dollar-sq.sh                           42
test/gold/echo-e.sh                              36
test/gold/errexit-confusion.sh                  139
test/gold/errexit.sh                            121
test/gold/export.sh                              65
test/gold/glob.sh                                26
test/gold/lineno.sh                              23
test/gold/nix.sh                                 32
test/gold/readlink.sh                            61
test/gold/readonly.sh                            24
test/gold/scope.sh                              126
test/gold/strip-op-char-class.sh                  6
test/gold/word-eval.sh                           57

2,775 lines in 27 files  

Generated C++ Code

mycpp generates the big file _gen/bin/oils-for-unix.mycpp.cc. Other programs like Zephyr ASDL and re2c generate other files.

_gen/asdl/hnode.asdl.h                          176
_gen/bin/oils_for_unix.mycpp.cc              46,189
_gen/bin/oils_for_unix.mycpp.h                    5
_gen/bin/text_files.cc                          943
_gen/core/optview.h                             119
_gen/core/runtime.asdl.cc                     1,374
_gen/core/runtime.asdl.h                      1,639
_gen/cpp/build_stamp.cc                           1
_gen/cpp/build_stamp.h                            1
_gen/frontend/arg_types.cc                      584
_gen/frontend/arg_types.h                       777
_gen/frontend/consts.cc                       1,570
_gen/frontend/consts.h                           52
_gen/frontend/help_meta.cc                      302
_gen/frontend/help_meta.h                         6
_gen/frontend/id_kind.asdl.cc                   858
_gen/frontend/id_kind.asdl_c.h                  383
_gen/frontend/id_kind.asdl.h                    446
_gen/frontend/match.re2c.h                   23,756
_gen/frontend/option.asdl.h                     200
_gen/frontend/signal.cc                         205
_gen/frontend/signal.h                           16
_gen/frontend/syntax.asdl.cc                  4,900
_gen/frontend/syntax.asdl.h                   5,673
_gen/frontend/types.asdl_c.h                     40
_gen/frontend/types.asdl.h                       77
_gen/osh/arith_parse.cc                         129
_gen/ysh/grammar_nt.h                            97
_gen/ysh/grammar_tables.cc                    5,181

95,699 lines in 30 files