%{
/* $Id: CLTRANS.Y 2.4 1995/07/20 11:24:33 leon Exp $
 */
#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#include <time.h>

#include "cltrans.h"

#define X 0

#define Y 1

#define Z 2

extern int cllineno;
extern char cllinebuff[200];
extern char *cltext;
extern FILE *outf;
void clerror(char *str);
int clparse(void);
int cllex(void);

#define YYDEBUG 1


#define BIGF 1e10

#define MINF -1e10

#define VecCopy(a,b)     (b)[0]=(a)[0];(b)[1]=(a)[1];(b)[2]=(a)[2]; 

#define VecSub(a,b,c)    (c)[0]=(a)[0]-(b)[0];\

                         (c)[1]=(a)[1]-(b)[1];\
                         (c)[2]=(a)[2]-(b)[2]      
#define VecAdd(a,b,c)    (c)[0]=(a)[0]+(b)[0];\

                         (c)[1]=(a)[1]+(b)[1];\
                         (c)[2]=(a)[2]+(b)[2]

Vector MinP={BIGF,BIGF,BIGF}, MaxP={MINF,MINF,MINF}, lastp;


%}

%union {
   Vector vector;
   float fval;
   int  ival;
   char *sval;
}


%token <fval> NUM 

%token AAXIS BAXIS CAXIS
%token APOSTROPHE ATANGL  AUXFUN BORE   BOTH   BRKCHP CCLW   
%token CLW     COOLNT CUTCOM CYCLE  DEEP   DELAY  
%token DRILL   DWELL   END FACE   FEDRAT FINI FLOOD 
%token FROM  GOHOME  GOTO   HEAD   HELP IN     INCHES 
%token INCR    INSERT  IPM    IPR    LEFT   LENGTH LINTOL 
%token LOADTL  MACHIN  MAIN   MAXRPM MILL   MIST   MM     
%token MMPM  MMPR    MODE   MOVARC MULTAX NAME NEXT   
%token NL  OFF     OFSTNO ON     OPSKIP OPSTOP ORIENT 
%token ORIGIN  OSETNO  PARTNO PITCH  PPFUN  PPRINT RANGE  
%token RAPID  RAPID   RAPTO  REAM   RETURN REV    RIGHT  
%token ROTABL  RPM     SETANG SETOOL SFM    SIDE   SLASH 
%token SMM     SPINDL  STOP   STRING TAP    THREAD TRANS  
%token TURN    TURRET  UNITS  XAXIS  XYPLAN YZPLAN YAXIS  
%token ZAXIS  ZXPLAN 


%token <ival> GCODE MCODE FCODE SCODE NCODE TCODE
%token <fval> XCOOR YCOOR ZCOOR ACOOR BCOOR RCOOR ICOOR JCOOR KCOOR

%type <vector> point
%type <fval> num nums

%nonassoc ALONE_FEDRATE
%nonassoc ARGUMENT_FEDRATE

%%

input:  /* empty */
        | input line 
      	| error { yyerrok; }
      	;

line : 
	partno 
	| COOLNT SLASH coolnt_arg 
        | cutcom
        | cycle
        | delay
	| END  {fprintf(outf, "M30\n\x1A");}
	| FEDRAT SLASH num speed_unit  {fprintf(outf,"F%.f\n",$3);} %prec ALONE_FEDRATE 
	| FINI 
	| FROM SLASH point 
	| GOHOME 
        | HEAD SLASH head_arg 
        | HEAD SLASH NEXT NUM 
        | INSERT SLASH STRING 
        | LINTOL SLASH NUM NUM 
	| LOADTL SLASH NUM IN NUM LENGTH NUM OSETNO NUM 
	| MULTAX SLASH switch 
        | MACHIN SLASH STRING 
        | MODE SLASH mode 
        | OFSTNO SLASH NUM 
        | OPSKIP SLASH switch 
        | OPSTOP 
        | PITCH SLASH NUM 
        | PPFUN SLASH STRING 
	| ORIGIN SLASH point 
	| PPRINT SLASH STRING 
        | ROTABL SLASH NUM axis rotation_dir 
	| spindl 
        | FROM SLASH point point 
        | UNITS SLASH length_unit  
	| g00
	| g01
	| movarc
	;


g00:
	RAPID
	GOTO SLASH point 
	{
		fprintf(outf, "G00 X%.2f Y%.2f Z%.2f\n",
			$4[X], $4[Y], $4[Z]);
		VecCopy($4, lastp);		
	}
        ;

g01:
	GOTO SLASH point 
	{
		fprintf(outf, "G01 X%.2f Y%.2f Z%.2f\n",
			$3[X], $3[Y], $3[Z]);
		VecCopy($3, lastp);		
	}
	;

movarc:
	MOVARC SLASH num num num num num num num 
	GOTO SLASH point 
        {
	  if($8 < 0)
	    fprintf(outf, "G02 X%.2f Y%.2f I%.2f J%.2f\n",
		    $12[X], $12[Y],
	            $3, $4);
	  else
	    fprintf(outf, "G03 X%.2f Y%.2f I%.2f J%.2f\n",
		    $12[X], $12[Y],
		    $3, $4);
	} 
	;


partno:
	PARTNO SLASH STRING 
	{
	 	time_t t;
		time(&t);
		fprintf(outf, 
#ifdef MOVETOZERO

			"G94 X3000\n"
			"G00 X0.00  Y0.00 Z0.00\n"
			"G52\n"
#endif		

	  "%%G-CODE generated by icl2g from I-DEAS ASCII CL-DATA on %s%%Part name = %s\n",
                
		    /*	"G90\n"
			"G20\n", */
			ctime(&t),
			cltext
			);
	}
	;


spindl: SPINDL SLASH switch 
        |SPINDL SLASH NUM RPM rotation_dir 
        |SPINDL SLASH NUM RPM rotation_dir RANGE NUM
	| SPINDL SLASH NUM spindl_arg rotation_dir MAXRPM NUM RANGE NUM 
        ;

 
length_unit:
        MM
        | INCHES
        ;

switch:
	ON
	| OFF
	;

trans_dir:
        RIGHT
        | LEFT
        ;

plane:
        XYPLAN
        | YZPLAN
        | ZXPLAN
        ;

mode:
        MILL
        | TURN
        ;

axis:
        AAXIS
        | BAXIS
        | CAXIS
        ;

coolnt_arg:
        switch
        | MIST
        | FLOOD
        | TAP
	;


cutcom:
        CUTCOM SLASH switch 
        | CUTCOM SLASH trans_dir plane 
        | CUTCOM SLASH trans_dir plane OSETNO NUM 
        | CUTCOM SLASH trans_dir OSETNO NUM 
        ;

speed_unit:
        IPR
        | IPM
        | MMPR
        | MMPM
        ;

opt_cycle_args:
        /* empty */
        | opt_cycle_args RETURN NUM
        | opt_cycle_args RAPTO NUM
        | opt_cycle_args ORIENT
        | opt_cycle_args ORIENT NUM
        | opt_cycle_args DWELL NUM
        | opt_cycle_args DWELL REV NUM
        ;

nums:
        NUM
        | nums NUM
        ;

cycle:
        CYCLE SLASH OFF 
        | CYCLE SLASH BORE NUM speed_unit NUM NUM opt_cycle_args 
        | CYCLE SLASH BRKCHP NUM INCR nums speed_unit NUM NUM opt_cycle_args 
        | CYCLE SLASH DRILL NUM speed_unit NUM NUM opt_cycle_args 
        | CYCLE SLASH DEEP NUM INCR nums speed_unit NUM NUM opt_cycle_args 
        | CYCLE SLASH REAM NUM speed_unit NUM NUM opt_cycle_args 
        | CYCLE SLASH TAP NUM speed_unit NUM NUM opt_cycle_args 
        ;


delay:
        DELAY SLASH NUM 
        | DELAY SLASH REV NUM 
        ;

rotation_dir:
	CLW
	| CCLW
	;

head_arg:
        MAIN
        | SIDE
        | OFF
        | BOTH
        ;

spindl_arg:
        SFM
        | SMM
        | rotation_dir
        ;

point:
	num num num 
	{ 
		int i;
		$$[0] = $1;
		$$[1] = $2;
		$$[2] = $3;
		for(i=0; i < 3; i++)
		{
			if($$[i] > MaxP[i])
				MaxP[i] = $$[i];
			if($$[i] < MinP[i])
				MinP[i] = $$[i];
		}
			
	}
	;

num:
	NUM 
	{
		$$ = atof(cltext);
	}
        ;

%%

void clerror(str)
 char * str ;
{
        fprintf(stderr, "\nMistake at or before line : %d\n%s\n", 
                 cllineno, cllinebuff) ;
        fprintf(stderr, "%s:'%s'\n\n",  str, cltext) ; 
        exit(-1) ;
}





syntax highlighted by Code2HTML, v. 0.9.1