package ZwgcZion;

use vars qw( @functions @operators @commands @tokens );
use vars qw( %function  %operator  %command  $VERSION );

$VERSION = '0.10 (alpha)';

sub argl {
  my @argl = ( '(', ( '{expr}', ',' ) x $_[0] );
  $argl[$#argl] = ')';
  @argl;
}

%function  = (
	      'buffer'		=> undef,
	      'downcase'	=> undef,
	      'get'		=> undef,
	      'getenv'		=> undef,
	      'lany'		=> [sub {}, argl(2)],
	      'lbreak'		=> undef,
	      'lspan'		=> undef,
	      'protect'		=> [[' Zion::Zwgc::protect(',$1,')'], argl(1)],
	      'rany'		=> [sub {}, argl(2) ],
	      'rbreak'		=> undef,
	      'rspan'		=> undef,
	      'substitute'	=> [[' Zion::Zwgc::substitute(',$1,')'],
				    argl(1)],
	      'upcase'		=> [[' uc(', '$1', ')'], argl(1)],
	      'verbatim'	=> undef,
	      'zvar'		=> undef
	     );
@functions = keys %function;

%primary  =  (
	      'appendport'	=> undef,
	      'break'		=> undef,
	      'clearbuf'	=> undef,
	      'closeinput'	=> undef,
	      'closeoutput'	=> undef,
	      'closeport'	=> undef,
	      'fields'		=> [sub {}, '{tokens}'],
	      'exec'		=> undef,
	      'execport'	=> undef,
	      'exit'		=> [[ 'exit;' ]],
	      'inputport'	=> undef,
	      'noop'		=> undef,
	      'outputport'	=> undef,
	      'print'		=> [[ '$_buffer_ .= join("", ', '$1', ');' ],
				    '{expr s}'],
	      'put'		=> [[ ' Zion::put($_buffer_);' ]],
	      # NOTE: this may break if the $ and symbol name get separated,
	      # in which case we'll have to implement a new way of adding '$'
	      'set'		=> [[ "\$", '$1', '=', '$2', ';' ],
				    '{token}', '=', '{expr}' ],
	      'show'		=> undef,
	      'if'		=> [[ ' if( ', '$1', ' ) ' ],
				    '{expr}', '@if>then' ],
	      'while'		=> undef,
	      'case'		=> [[ split(/:/, <<'EndOfCase') ],
{
  my $_case_ = :$1:;
  if (0) {}		# thus, every "match" is an elsif
EndOfCase
				    '{expr}', '@case>match/default' ],
	     );
%command =   (%primary,
	      # case...
	      'match'		=> [[ ' elsif(grep($_case_ eq $_,','$1',')',
				      '{', '$2', '}' ],
				    '?case', '{expr,s}', '{cmds}',
				    '@>match/default/endcase' ],
	      'default'		=> [[ ' else ', '{', '$1', '}' ],
				    '?case', '{cmds}', '@>endcase' ],
	      'endcase'		=> [[ '}' ], '@<case'],
	      # if...
	      'then'		=> [[ '{', '$1', '}' ],
				    '?if', '{cmds}', '@>elseif/else/endif' ],
	      'elseif'		=> [[ ' elsif( ', '$1', ' ) ' ],
				    '?if', '{expr}', '@>then' ],
	      'else'		=> [[ '{', '$1', '}' ],
				    '?if', '{cmds}', '@>endif'],
	      'endif'		=> [[], '@<if' ],
	      # while...
	      'do'		=> undef,
	      'endwhile'	=> undef
	    );
@commands = keys %command;

@tokens = (@functions, @commands);

%operator  = (
	      '+'		=> '.',
	      '=='		=> ' eq ',
	      '!='		=> ' ne ',
	      '=~'		=> '=~',
	      '!~'		=> '!~',
	      '&'		=> '&&',
	      '|'		=> '||',
	      '!'		=> '!'
	     );
@operators = keys %operator;

1;
