-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Make the run macro method also accept a program as an AST node #2639
Comments
It's nice that it solves the intermediate auxiliary file. Not sure about the Although this approach will be as slow as the auxiliary file, right? The built in macro of #2638 is still the fastest way. In which context the expression will be compiled?
If this is added, the next question will be if it is able to build an expression during compile time and compile/execute it ... 💫 Despite no been an optimal way to solve the Time.now macro, I prefer this approach since it would be more maintainable IMO. Yet I will second the idea of adding a IMO having more and more semantics of macros in the compiler feels the same way as C internals in Ruby. |
I like that idea! Von meinem iPhone gesendet
|
Similar to above, but more implicit, I would really like something along the lines of: astmacro foo(a : ASTNode, b : NumberLiteral, c : BlockLiteral)
x = do_stuff_here a
y = which_goes_into_a_file b
z = and_is_used_with_run c
Expressions.from [x, y, z]
end Which would be a regular function but run in external file, with arguments passed over and parsed to AST-nodes passed in. Return either AST or String as fit your bill (the AST would of course be to_s'ed before printing back over stdout). And the run call and the node-stringification etc. is of course fluffed in to a macro automatically... |
@bcardiff Yes, it's still slower than the @ozra Maybe passing AST nodes to a sub-program, operating on them is something that can already be done, though of course the AST nodes are first serialized with |
I just tried this, and this program: build_date = {{ run(print Time.now.to_s).stringify }}
p build_date takes 0.3s more to compile, compared to an empty program. Well,actually, the first compile takes more time (2s more), but then the compiler caches the bc/o files for this program (it uses We could have another name instead of |
@asterite, yeah, understandably: it would add a lot of overhead indeed. |
Right now we have the
run
macro method that can be used like this:foo.cr:
bar.cr:
This is nice and powerful, but requires us to create an extra file for the sub-program. Many times this sub-program is very small. Some cases I can see right now is ecr/process, slang/process (similar to the above), but we can also imagine simple programs like
Time.now.to_s
.Another issue with the sub-program being in a separate file is that this file is usually included in the output of
crystal doc
and other tools.So, the idea is to allow any expression as the first
run
argument. If it's a string literal then it must denote a program to compile and run. Otherwise, a program with that argument is compiled and run. For example:Another example, removing the need to have an
ecr/process.cr
file:With this, there's no need to merge #2638 because we can do:
And we could even create a small program that computes the target triple using LLVM, but at compile time (of course we can do it right now with a separate file, but it feels more correct to just embed it in the only place it's going to be used).
In all the snippets above you can see all the expressions are
puts
, but one can imagine a sub-program that invokes some routine, passing STDOUT and appending directly to that, so I'd rather leave thatputs
explicit and not add it implicitly to all sub-programs.Thoughts?
/cc @bcardiff
The text was updated successfully, but these errors were encountered: