TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: A Rust macro that parses Java-like syntax and runs it as a Rust program

195 点作者 jD91mZM2将近 7 年前

9 条评论

dan-robertson将近 7 年前
There are a few things that are annoying about writing macros* (in particular when I last checked there was no way to break the hygiene which is something that is occasionally useful for eg making lots of functions that follow a similar template) in Rust but in general I think they strike a really nice balance for a language with complex syntax:<p>1. Macros only happen in certain points and don’t need to be known to be parsed so parsers can be made which don’t need to understand macro syntax (or know which macros are in scope before parsing). I think this is better than e.g. ocamlp4 (writing a plugin for an alternative compiler frontend) or perl6 (subclassing the grammar of the language and adding your own rules which is a bit nice but also a bit crazy. Perl has the advantage of being so dynamic that any kind of analysis tools are not written to parse the code but to introspect it).<p>2. Macros don’t require any code to be compiled and then run at compile time. This also means that macros are not programs that have to understand the type of the ast and operate on it, something that would be particularly painful in rust for writing something simple. Languages where macros are not like this are ocaml with ppx, Rust, and Julia (where the ast is super vaguely defined and hard to work with, especially in the way it magically deals with module scoping and quoting, quaisiquoting, and “hygiene”)<p>3. The input to a macro is not a parse tree but rather a “token tree” which is essentially a sequence of tokens where delimiters are matched and converted into “tokens,” something that feels vaguely like TeX. the only other macro system that works at all like this is Lisp but that’s because the language (and ast) <i>is</i> sequences of tokens with parenthesised bits grouped into single tokens so it doesn’t really count.<p>4. Macros are based on rewrite rules. These make it quite hard to write macros but I think they’re the best we have for not needing to invent a huge language just to write macros in, and for having macros be hygienic. Scheme macros are also rewrite based but it is easier to understand how they work because the shape of their input it more obvious<p>One thing that makes me sad about rust macros is having to specify the “type” of things that are matched against, e.g. “expr”. I can see why it’s needed but it does feel somewhat limiting. Another thing is that macro invocations look different from the rest of the syntax so a macro feels like it’s tacked on rather than a part of the language.<p>* by macro I mean the kind made of rewrite rules and not compiler plugins
评论 #17736323 未加载
评论 #17735997 未加载
monocasa将近 7 年前
Reminds me of this cool hack: a 6502 assembler as a rust macro.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;jonas-schievink&#x2F;rustasm6502" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jonas-schievink&#x2F;rustasm6502</a>
mpweiher将近 7 年前
Cool. On the other hand, projects like these suggest to me that Macro systems like Rust&#x27;s are a bit too powerful.<p>Not that I know how to make it just enough less powerful.
评论 #17735849 未加载
评论 #17736112 未加载
评论 #17736840 未加载
azhenley将近 7 年前
I don&#x27;t know what I would do with this yet, but this is certainly a cool feat!
zerb将近 7 年前
I was actually just toying with a similar thing myself. I&#x27;m experimenting with a system that generates abilities for a game within certain design parameters, then tests the games&#x27; balance by running lots of example games. I figured compiling a DSL via macro compiled down would be worth the upfront cost given how much testing I planned on giving each generated variant. Thanks for laying some groundwork in this part!
gwbas1c将近 7 年前
Perhaps I don&#x27;t understand something? (I&#x27;ve only done a very small amount of Rust.)<p>What exactly does this do? Let you write Java-style code in Rust? Why would I use this? (Is it because writing code like this is easier than straight rust?)
评论 #17736684 未加载
sharpercoder将近 7 年前
Would it be possible to write a compiler from Java to Rust? C# to rust? The goal then would be to get fast(er) execution times.
评论 #17736363 未加载
评论 #17736375 未加载
评论 #17736309 未加载
dleslie将近 7 年前
How well do LSP implementations cope with complex macros like this?
评论 #17736140 未加载
评论 #17735988 未加载
评论 #17738098 未加载
jandrese将近 7 年前
But the issue with Rust ins&#x27;t the syntax, it&#x27;s where you need to change your entire thought model to match what Rust expects.<p>Well, ok, the syntax can be pretty horrific at times, but going to something Java-like doesn&#x27;t seem like much improvement.
评论 #17735113 未加载
评论 #17736879 未加载