Here I propose to add a multi-stage programming (macro) system to Erg.
Motivation
Erg is compatible with Python's API. In other words, Erg is an alt Python.
However, Erg's syntax is far different from Python's. Whereas Python has statements, almost everything in Erg is expressions. if and for are also implemented as functions rather than statements.
This makes Erg code look very different from Python code.
if! True:
do!:
print! "True"
do!:
print! "False"
for! [1, 2, 3], i =>
print! i
If you try to write an else clause in if, you will need two indentations. for! has the opposite order of i and iterable compared to Python's for i in iterable: ..., which is confusing. I admit that Python's syntax is simpler.
However, I feel reluctant to bring the concept of statements into Erg. I want to minimize basic grammar.
Draft plan
Therefore, I will introduce a hygienic macro system to Erg. It is heavily inspired by Scala and defines macros as functions that receive syntax elements and return syntax elements.
Macro = Class *Expr -> Expr
To introduce macros, two syntaxes will be added: ${} and '{}.
${ x } takes an expression x of type T and returns Expr T.
'{ x } takes an expression of type Expr T and returns T.
{Expr; Name} = import "macro/ast"
# Name[Int] <: Expr[Int]
i: Int = 1
expr: Name Int = '{ i }
# expr + 1 # ERROR
i2: Int = ${ expr }
# ${ expr + 1 } # ERROR
# ${ expr } + 1 # OK
assert i == i2
Use these to implement if, for!, import.
Note that if this proposal is implemented, the current if function etc. will be moved to the control module instead of built-in.
Here I propose to add a multi-stage programming (macro) system to Erg.
Motivation
Erg is compatible with Python's API. In other words, Erg is an alt Python. However, Erg's syntax is far different from Python's. Whereas Python has statements, almost everything in Erg is expressions.
if
andfor
are also implemented as functions rather than statements. This makes Erg code look very different from Python code.If you try to write an else clause in
if
, you will need two indentations.for!
has the opposite order ofi
anditerable
compared to Python'sfor i in iterable: ...
, which is confusing. I admit that Python's syntax is simpler. However, I feel reluctant to bring the concept of statements into Erg. I want to minimize basic grammar.Draft plan
Therefore, I will introduce a hygienic macro system to Erg. It is heavily inspired by Scala and defines macros as functions that receive syntax elements and return syntax elements.
To introduce macros, two syntaxes will be added:
${}
and'{}
.${ x }
takes an expressionx
of typeT
and returnsExpr T
.'{ x }
takes an expression of typeExpr T
and returnsT
.Use these to implement
if, for!, import
. Note that if this proposal is implemented, the currentif
function etc. will be moved to thecontrol
module instead of built-in.Then we can use them: