Compile-time calculation: Difference between revisions
Content added Content deleted
(Added Quackery.) |
(add Zig language) |
||
Line 1,482: | Line 1,482: | ||
</pre> |
</pre> |
||
Tokenize time can paste text into the source, parse time can inject a limited set of objects into the parse tree (and is used for things like __DATE__, __FILE__ constants). |
Tokenize time can paste text into the source, parse time can inject a limited set of objects into the parse tree (and is used for things like __DATE__, __FILE__ constants). |
||
=={{header|Zig}}== |
|||
Zig provides arbitrary CTFE with limitations in IO, memoization and forbidding closures for compilation efficiency. |
|||
<lang zig>const std = @import("std"); |
|||
fn factorial(n: u64) u64 { |
|||
var total: u64 = 1; |
|||
var i: u64 = 1; |
|||
while (i < n + 1) : (i += 1) { |
|||
total *= i; |
|||
} |
|||
return total; |
|||
} |
|||
pub fn main() void { |
|||
@setEvalBranchQuota(1000); // minimum loop quota for backwards branches |
|||
const res = comptime factorial(10); // arbitrary Compile Time Function Evaluation |
|||
std.debug.print("res: {d}", .{res}); // output only at runtime |
|||
}</lang> |
|||
Output: |
|||
<pre>3628800</pre> |
|||
{{omit from|AutoHotkey|AutoHotkey is an interpreted language - the 'compiler' (separate from the interpreter) just bundles the interpreter and the code inside an EXE, so this sort of thing isn't possible}} |
{{omit from|AutoHotkey|AutoHotkey is an interpreted language - the 'compiler' (separate from the interpreter) just bundles the interpreter and the code inside an EXE, so this sort of thing isn't possible}} |