How to create a Python-like compiled language?

Hello World.

I would create a Python-like compiled language with static typing.

I know that ANTRL can create the lexer and parser for a Python-like language, but how to create the compiler (for my machine, at the moment)?

Should I use LLVM, Cython or PyPy? What’s the easier solution to create a working prototype and let’s start to play?

If it is about using (and not creating) a Python-like language, check out Nim.

I didn’t know about Nim. I do not like the syntax, but I can use it as a base.

It’s been a long time since I thought about compiler implementation, but I think LLVM or Pypy should be fine. Cython seems to target performance and foreign function calling, not writing compilers.

LLVM is used extensively for compiler implementation.

Pypy is intended to be useful for compiler implementation, but I haven’t heard of anyone using it for that other than the Pypy project itself.

There’s also gcc.

Well, for now I would start from something “basic”: a lexer and a parser.

I discarded ANTLR, since it’s not a PEG parser generator, and its Python grammar is stuck to Python 3.3.

I’m trying to use the PEG parser generator Tatsu, but it seems it does not recognize the grammar rule ["as" NAME]:

@@grammar :: Python3

start = import_stmt ;

import_stmt = import_name | import_from ;

# Import statements
# -----------------

import_name = 'import' dotted_as_names ;
import_from =
    'from' dotted_name 'import' import_from_targets ;
import_from_targets =
    | '(' import_from_as_names [','] ')' 
    | import_from_as_names !','
    | '*' ;
import_from_as_names =
    ','.{import_from_as_name}+ ;
import_from_as_name =
    NAME ['as' NAME ] ;
dotted_as_names =
    ','.{dotted_as_name}+ ;
dotted_as_name =
    dotted_name ['as' NAME ] ;
dotted_name =
    | dotted_name '.' NAME 
    | ['.'] NAME ;

NAME = /[a-zA-Z_][a-zA-Z_0-9]*/ ;

If get:

>>> tatsu.parse(grammar, "import a as b")
('import', ['a'])

It completely ignores the “as b”.