Weird Compiler

Because TeX and SH are wonderful VMs.

Examples

The exemples here are also present both in the sources and the Online Weird Compiler.

Simple

 

The first example is plain simple:

  extern myprint 1 ;;
  extern newline 1 ;;
  let f = fun x -> ( myprint "toto" )
  in ( f 0 ; (newline 0) )

It declares the external functions myprint and newline both of arity 1. myprint prints its argument and newline prints a newline regardless of its argument. Then it defines a function f that prints “toto” and apply it 0.

simple.wc can be compiled and run to TeX via the commands

wc.native --TeX --input simple.wc --output simple.tex
pdftex simple.tex

or to shell via

wc.native --shell --input simple.wc --output simple.sh
sh simple.sh

It produces the output:

toto

simple.tex and simple.sh are both made of three parts: the core library wich implements closure creation and application (it is located in the file tex/lib.tex for TeX and shell/lib for shell) and
the external library which implements user defined primitives. The last one is located in the files
tex/extern.tex for TeX and shell/extern for shell. You can add which ever primitive you want to this it, they will be imported by the compiler and put in the compiled code. Finaly, the last part is the program itself.

For example, simple.sh is :

##########################
#                        #
#     STANDARD  LIB      #
#                        #
##########################

[.....]

##########################
#                        #
#     EXTERN  LIB        #
#                        #
##########################

[.....]

##########################
#                        #
#        PROGRAM         #
#                        #
##########################
##---------  sc1 : 1  ----------
sc1 () {
    myprint "toto"

}
##---------  main : 0  ----------
main () {
    sc1 0
    newline 0

}
##---------  MAIN  ---------
main

and simple.tex is:

% %%%%%%%%%%%%%%%%%%%%%
% %    STANDARD LIB   %
% %%%%%%%%%%%%%%%%%%%%%

[.....]

% %%%%%%%%%%%%%%%%%%%%%
% %    EXTERN LIB     %
% %%%%%%%%%%%%%%%%%%%%%

[.....]

% %%%%%%%%%%%%%%%%%%%%%
% %      PROGRAM      %
% %%%%%%%%%%%%%%%%%%%%%
%
%
%%---------  scI : 1  ----------
\def\scI#1%
{%
\myprint{{toto}}%
}%
%
%%---------  main : 0  ----------
\def\main%
{%
\scI{0}%
\newline{0}%
}%
%
%%---------  MAIN  ---------
\main%
%
\end

Here we can see that the program has been compiled into 2 functions:

sc1

which represents f
and

main

which is the main entry point.



Leave a Reply

Your email address will not be published. Required fields are marked *

*

2,155 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>