Skip to content

Instantly share code, notes, and snippets.

@myrdd
Last active August 29, 2015 14:25
Show Gist options
  • Save myrdd/a819a691b62566e04790 to your computer and use it in GitHub Desktop.
Save myrdd/a819a691b62566e04790 to your computer and use it in GitHub Desktop.
using GCC's preprocessor for JavaScript
usage: see the bash script
function test() {
// #ifdef DEBUG
var x = "debug";
// #else
var x = "normal";
// #endif
console.log(x);
}
/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* This header is separate from features.h so that the compiler can
include it implicitly at the start of every compilation. It must
not itself include <features.h> or any other header that includes
<features.h> because the implicit include comes before any feature
test macros that may be defined in a source file before it first
explicitly includes a system header. GCC knows the name of this
header in order to preinclude it. */
/* glibc's intent is to support the IEC 559 math functionality, real
and complex. If the GCC (4.9 and later) predefined macros
specifying compiler intent are available, use them to determine
whether the overall intent is to support these features; otherwise,
presume an older compiler has intent to support these features and
define these macros by default. */
/* wchar_t uses ISO/IEC 10646 (2nd ed., published 2011-03-15) /
Unicode 6.0. */
/* We do not support C11 <threads.h>. */
function test() {
var x = "debug";
console.log(x);
}
s,^ *// *#(define|undef|ifdef|ifndef|if|elif|else|endif)( |$),#\1\2,g
#!/bin/bash
infile="example-in.js"
outfile="example-out.js"
defines="-D DEBUG"
cat $infile \
| sed -r -f preparation.sed \
| gcc -E -P -C -w -x c $defines - \
> $outfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment