How many languages can you use simultaneously in ONE functioning program in under 500 bytes
HTML, CSS, PHP, JavaScript, CoffeeScript, RegEx, sed, bash; 8 languages
Displays colors of the rainbow.
(Only 396 bytes) will try to add more languages
HTML + CSS is turning complete and counts as a valid language on PPCG. I've been told CoffeeScript is different enough as JavaScript to be counted as a separate language. And RegEx has been specifically allowed also.
PHP allows for addition of many languages especially with exec
and shell_exec
functions.
Golfed to fit inside byte limit (ES6)
<html><style><?php foreach(explode(" ",exec('sed "s/;/ /g"<<<"red;orange;yellow;green;blue;violet"')) as $color){echo "#{$color} { color: $color }\n"}?></style><script>b="red orange yellow green blue violet".split(/ /).map(c=>`<p id="${c}">${c}</p>`)</script><script type="text/coffeescript">document.documentElement.innerHTML+=b.join "\n"</script><script src="http://v.ht/u31R"></script></html>
Uses:
- PHP to generate CSS
- PHP, uses exec to run bash
- sed to split color items for bash
- CSS to specify text colors
- JavaScript to generate elements
- RegEx to split color items for JS
- CoffeScript to print elements
- HTML as a wrapper / output
Faster & Ungolfed (All modern browsers)
<html>
<style>
/* CSS */
<?php// Loop through all colors
foreach(explode(" ",
exec('sed "s/;/ /g"<<<"red;orange;yellow;green;blue;violet"')) as $color) {
// Print it out, add it to the CSS
echo"#{$color} { color: $color }\n"}
?>
</style>
<script>
// Create an element for each color, store as variable
window.colors = "red orange yellow green blue violet".split(/ /).map(function(color) {
return "<p id=\"" + color + "\">" + color + "</p>"
});
</script>
<script type="text/coffeescript">
document.documentElement.innerHTML += window.colors.join "\n"
</script>
<!-- CoffeScript Link -->
<script src="https://raw.githubusercontent.com/jashkenas/coffeescript/master/extras/coffee-script.js"></script>
</html>
make, sh, awk, sed, regex, yacc, lex, C; 8 languages.
Including the input and output languages: brainfuck and D; 10 languages
This is a brainfuck to D compiler. It takes the brainfuck program over standard input and prints the D code to standard output.
Make uses awk, sed, and sh to generate a yacc program, which in conjunction with a lex program is used to generate a C program that takes a brainfuck program as input and outputs an equivalent D program. I tried to only use languages in ways that were actually useful, instead of just throwing in a bunch of no-ops. The lex/yacc/C combination is fairly standard for simple compilers, the make/sh combination is useful for building, and the awk/sed line was the only way I could get the whole thing under 500 bytes. (It's at 498 bytes currently.)
Code
define L
%%
[][+-<>.,] return *yytext;
. ;
%%
endef
define Y
%{
#include <stdio.h>
%}
%left '+' '-' '.' ',' '>' '<' '[' ']'
%%
q: q q {}
+ p[i]++
- p[i]--
> i++
< i--
, p[i]=getchar()
. putchar(p[i])
[ while(p[i]){
] }
%%
yywrap(){}yyerror(){}main(){puts("import std.stdio;int p[30000];int i;void main(){");yyparse();puts("}");}
endef
export L
export Y
a:
@echo "$$L">y;lex y;echo "$$Y"|awk '/^[+-^]/{printf("q: X%sX {puts(\"%s;\");}\n",$$1,$$2)} /^[^+-^]/'|sed "y/X/'/">y;yacc y;cc *.c;./a.out;:
Example of use
$ cat helloworld.bf
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
$ make < helloworld.bf > tmp.d
y: warning: 8 shift/reduce conflicts [-Wconflicts-sr]
$ dmd tmp.d && ./tmp
Hello World!