📄 cncparse.c
字号:
//
///Files:parse.c
//The parser implementation for the CNC interperter
#include "..\inc\CNCsoft\CNCparseGlobals.h"
#include "..\inc\CNCsoft\CNCSHM.h"
#include "..\inc\CNCsoft\CNCparseScan.h"
#include "..\inc\CNCsoft\CNCparse.h"
#include "..\inc\CNCsoft\CNC.h"
#include "..\inc\CNCsoft\CNCparseConvert.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
extern int IOMacroBuf[24];
extern int JumpFlag;
int MSTIllegal=0; //MST代码在参数CM98下非法性判断0:合法1:非法但赋值
int MSTFlag=0; //在参数CM98参数下M,S,T的区别标志0:M 1:S 2:T
extern long ParaBuf[PARAMETER_NUM];
extern Interpolator_MSG INTP_Interpolator_MSG;
extern int EXOcodeCounter;
extern long xx;
//extern long StartLineNum; //读该文件的起始行数默认值为1
int ParseFlag[9999*7]={0}; //是否词法分析标记0:否1:是
extern int EXOcode;
extern int G65Flag;
extern double MacroBuf[MACRO_NUM]; //用于存储G65变量的数组
long G65P=-1; //用于记录P的地址号
extern long NcodeTemp[9999*7]; //用来在转移指令时联系N值和numCtrl的对应关系
extern int InitFlag;
extern long G65TempP;
TokenType token=A; //holds current token
extern int CNC_ERR; //报警编号
extern int CNC_IsErr; //报警状态 1:处理 0:未处理
extern int EndFlag;
extern int ProgramEndFlag;
extern int NotingFlag;
extern BOOLEAN MDI_Flag;
//extern int OcodeFlag;
extern int IsProgramM99;
extern int linepos; //current position in LineBuf
extern int bufsize; //current size of buffer string
int McodeType[19]={0,30,3,4,5,8,9,10,11,12,13,
32,33,41,42,43,44,98,99
};
//function prototypes for recursive calls
int N_stmt(dataInterp *data,long Ncode);
int G_stmt(dataInterp *data,int Gcode);
int M_stmt(dataInterp *data,int Mcode);
int S_stmt(dataInterp *data,int Scode);
int T_stmt(dataInterp *data,int Tcode);
int F_stmt(dataInterp *data,double Fcode);
int P_stmt(dataInterp *data,double Pcode);
int Q_stmt(dataInterp *data,double Qcode);
int R_stmt(dataInterp *data,double Rcode);
int H_stmt(dataInterp *data,int Hcode);
int I_stmt(dataInterp *data,double Icode);
int K_stmt(dataInterp *data,double Kcode);
int J_stmt(dataInterp *data,double Jcode);
int X_stmt(dataInterp *data,double Xcode);
int Y_stmt(dataInterp *data,double Ycode);
int Z_stmt(dataInterp *data,double Zcode);
int U_stmt(dataInterp *data,double Ucode);
int V_stmt(dataInterp *data,double Vcode);
int W_stmt(dataInterp *data,double Wcode);
int O_stmt(dataInterp *data,int Ocode);
int L_stmt(dataInterp *data,int Lcode);
int Gcode=InvalidateCode;
//Error = TRUE prevents further passes if an error occurs
int Error;
extern long lineno;
extern char tokenString[MAXTOKENLEN+1];
void syntaxError(void) {
// Uart_Printf("\n>>>");
// Uart_Printf("Unexpected token error at line %d\n",lineno);
Error = TRUE;
if (CNC_ERR==140) {
}
else {
CNC_ERR=9;
}
CNC_IsErr=1;
}
//function addNumCheck check the address number
void addNumCheck(int addNum,char *s) {
if (addNum>=2) {
// Uart_Printf("\n>>>");
// Uart_Printf("syntax error at line %d: %s address num error\n",lineno,s);
Error = TRUE;
CNC_ERR=129;
CNC_IsErr=1;
}
}
//function OverLimitError check whether the value is over limit
void OverLimitError(int flag,char *s) {
if (flag) {
// Uart_Printf("\n>>>");
// Uart_Printf("syntax error at line %d:%s over limit error\n",lineno,s);
Error = TRUE;
CNC_ERR=130;
CNC_IsErr=1;
}
}
void addG00G01NumCheck(int addNum) {
if (addNum>=2) {
// Uart_Printf("\n>>>");
// Uart_Printf("syntax error at line %d: the total G00 G01 address num error\n",lineno);
Error = TRUE;
if (CNC_ERR!=129) {
CNC_ERR=131;
CNC_IsErr=1;
}
}
}
void addIKRNumCheck(int addNum) {
if (addNum>=2) {
// Uart_Printf("\n>>>");
// Uart_Printf("syntax error at line %d: the total IK R address num error\n",lineno);
Error = TRUE;
CNC_ERR=27;
CNC_IsErr=1;
}
}
void addXUYVZWNumCheck(void) {
// Uart_Printf("\n>>>");
// Uart_Printf("syntax error at line %d: the XUYVZW address num error\n",lineno);
Error = TRUE;
CNC_ERR=132;
CNC_IsErr=1;
}
void UnexpectedtokenError(char *s) {
// Uart_Printf("\n>>>");
// Uart_Printf("Unexpected token %s at line %d\n",s,lineno);
Error = TRUE;
CNC_ERR=132;
CNC_IsErr=1;
}
/*void OverSpaceError(char *s) {
// Uart_Printf("\n>>>");
// Uart_Printf("Over Place Error %s at line %d\n",s,lineno);
CNC_ERR=3;
CNC_IsErr=1;
}*/
void SegmentBeginError(void) {
// Uart_Printf("\n>>>");
// Uart_Printf("synatx error at line %d:data block not begin with N or O address\n",lineno);
if (xx==1) {
CNC_ERR=4;
CNC_IsErr=1;
}
}
void AddressUnknowError(void) {
// Uart_Printf("\n>>>");
// Uart_Printf("Address Unknow error at line %d\n",lineno);
CNC_ERR=9;
CNC_IsErr=1;
}
void VarOverLimitError(void) {
// Uart_Printf("\n>>>");
// Uart_Printf("Address Unknow error at line %d\n",lineno);
CNC_ERR=115;
CNC_IsErr=1;
}
void VarNOError(void) {
// Uart_Printf("\n>>>");
// Uart_Printf("Address Unknow error at line %d\n",lineno);
CNC_ERR=146;
CNC_IsErr=1;
}
void DecimalError1(void) {
CNC_ERR=7;
CNC_IsErr=1;
}
static void match(TokenType expected) {
if (token == expected) token = getToken();
else {
syntaxError();
// Uart_Printf(" ");
}
}
void parse(dataInterp *data) {
//address num
double Pcode=0;
double Xcode=0;
double Ucode=0;
double Ycode=0;
double Vcode=0;
double Zcode=0;
double Wcode=0;
double Rcode=0;
double Hcode=0;
double Icode=0;
double Kcode=0;
double Jcode=0;
double Fcode=0;
double Scode=0;
double Qcode=0;
double Ocode=0;
double Lcode=0;
int Mcode=InvalidateCode;
// long int Ncode=0;
double Ncode=0;
int Tcode=InvalidateCode;
int OverLimit=0;
int IKRFlag=0;
int NPositionErrorFlag=0; //用于判断N存在是否在程序段开头判错
// 0:在N之前还没有出现其他地址字
// 1:在N之前已经出现了其他地址字判错
//NPXUYVZWRIKFSTMQOLG00G01G02G03G06G07H is only one in a line
int Nnum = 0;
int Pnum = 0;
int Xnum = 0;
int Unum = 0;
int Ynum = 0;
int Vnum = 0;
int Znum = 0;
int Wnum = 0;
int Rnum = 0;
int Inum = 0;
int Knum = 0;
int Jnum=0;
int Fnum = 0;
int Snum = 0;
int Tnum = 0;
int Mnum = 0;
int Qnum = 0;
int Hnum = 0;
int Onum = 0;
int Lnum = 0;
int Gnum[6];
int i;
for (i=0;i<6;i++) {
Gnum[i]=0; //the same G group address is only one in a line
}
token = getToken();
//用来减少跳转指令的工作时间
// if ((InitFlag==1)&&(ParseFlag[lineno]==1)&&(G65TempP!=NcodeTemp[lineno])) {
// return;
// }
while (EndFlag==1) {
token = getToken();
}
while (token==ENDFILE) {
data->EOB=DataBlockEnd;
data->ProgramEndMark = ProgramEnd;
data->numCtrl=lineno;
return;
}
/* if ((token != N)&&(token != O)&&(NotingFlag==0)&&(InitFlag==0)) { //data block begin with N or O address
SegmentBeginError() ;
}*/
data->EOB = NoDataBlockEnd; //data block start
data->bBusy = Busy;
while (token != ENDLINE) {
switch (token) {
case N:
if (NPositionErrorFlag==1) {
SegmentBeginError();
// return;
}
match(N);
if (token==VAR) {
G65Flag=1;
match(VAR);
}
Nnum++;
addNumCheck(Nnum,"N");
if (token == NUM) {
Ncode = atof(tokenString);
if (Ncode!=(int)(Ncode)) {
DecimalError1();
// return;
}
if ((strlen(tokenString)>4)&&(Ncode>0)){
CNC_ERR=130;
CNC_IsErr=1;
// OverSpaceError("N");
// return;
}
OverLimit=N_stmt(data,Ncode);
OverLimitError(OverLimit,"N value");
NcodeTemp[lineno]=Ncode;
ParseFlag[lineno]=1;
/* if ((InitFlag==1)&&(Ncode<G65TempP)) {
return 0;
}*/
}
match(NUM);
break;
case G:
NPositionErrorFlag=1;
ParseFlag[lineno]=1;
match(G);
if (token==VAR) {
G65Flag=1;
match(VAR);
}
if (token == NUM) {
Gcode = atof(tokenString);
if (Gcode!=(int)(Gcode)) {
DecimalError1();
// return;
}
switch (Gcode) {
case 4: //00:G04 G10 G28 G39 G50 G65 G70 G71 G72 G73 G74 G75 G76
case 10:
case 28:
case 39:
case 50:
case 65:
Gnum[0]++;
addNumCheck(Gnum[0],"G04 G10 G28 G39 G50 G65 G70 G71 G72 G73 G74 G75 G76 same group or");
addG00G01NumCheck(Gnum[0]+Gnum[1]);
if(CNC_IsErr==1)return;
break;
case 70:
case 71:
case 72:
case 73:
Gnum[0]++;
addNumCheck(Gnum[0],"G04 G10 G28 G39 G50 G65 G70 G71 G72 G73 G74 G75 G76 same group or");
addG00G01NumCheck(Gnum[0]+Gnum[1]);
break;
case 74:
case 75:
case 76:
Gnum[0]++;
addNumCheck(Gnum[0],"G04 G10 G28 G39 G50 G65 G70 G71 G72 G73 G74 G75 G76 same group or");
addG00G01NumCheck(Gnum[0]+Gnum[1]);
break;
case 0: //01: G00 G01 G02 G03 G32 G33 G90 G92 G94
case 1:
case 32:
case 33:
case 90:
case 92:
case 94:
Gnum[1]++;
addG00G01NumCheck(Gnum[0]+Gnum[1]);
if(CNC_IsErr==1)return;
break;
case 2:
case 3:
Gnum[1]++;
addG00G01NumCheck(Gnum[0]+Gnum[1]);
IKRFlag=1;
break;
case 96: //02: G96 G97
case 97:
Gnum[2]++;
addNumCheck(Gnum[2],"G96 G97 same group or");
break;
case 98: //03: G98 G99
case 99:
Gnum[3]++;
addNumCheck(Gnum[3],"G98 G99 same group or");
break;
case 20: //06: G20 G21
case 21:
Gnum[4]++;
addNumCheck(Gnum[4],"G20 G21 same group or");
break;
case 40: //07: G40 G41 G42
case 41:
case 42:
Gnum[5]++;
addNumCheck(Gnum[5],"G40 G41 G42 same group or");
break;
default:
break;
}
OverLimit=G_stmt(data,Gcode);
OverLimitError(OverLimit,"G code");
if (OverLimit==1) {
CNC_ERR=10;
CNC_IsErr=1;
}
}
match(NUM);
break;
case M:
NPositionErrorFlag=1;
ParseFlag[lineno]=1;
match(M);
if (token==VAR) {
G65Flag=1;
match(VAR);
}
Mnum++;
addNumCheck(Mnum,"M");
if (token == NUM) {
Mcode = atof(tokenString);
if (Mcode!=(int)(Mcode)) {
DecimalError1();
}
if ((strlen(tokenString)>2)&&(Mcode>0)&&(G65Flag==0)) {
CNC_ERR=181;
CNC_IsErr=1;
}
OverLimit=M_stmt(data,Mcode);
}
if (((data->MST[0])==30) &&((JumpFlag==0)||(G65TempP==Ncode))){
linepos=0;
bufsize=0;
token=ENDFILE;
ProgramEndFlag=0;
data->IsProgramM99=1; //主要是用来计算加工件数
data->ProgramEndMark=ProgramEnd;
data->EOB=DataBlockEnd;
data->numCtrl=lineno;
return;
}
match(NUM);
break;
case T:
NPositionErrorFlag=1;
ParseFlag[lineno]=1;
match(T);
if (token==VAR) {
G65Flag=1;
match(VAR);
}
Tnum++;
addNumCheck(Tnum,"T");
if (token == NUM) {
Tcode = atof(tokenString);
if (Tcode!=(int)(Tcode)) {
DecimalError1();
}
if ((strlen(tokenString)>4)&&(Tcode>0)){
CNC_ERR=183;
CNC_IsErr=1;
}
OverLimit=T_stmt(data,Tcode);
OverLimitError(OverLimit,"T value");
}
match(NUM);
break;
case S:
NPositionErrorFlag=1;
ParseFlag[lineno]=1;
match(S);
if (token==VAR) {
G65Flag=1;
match(VAR);
}
Snum++;
addNumCheck(Snum,"S");
if (token == NUM) {
Scode = atof(tokenString);
OverLimit=S_stmt(data,Scode);
}
match(NUM);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -