public override Exp Analy( ) { if (this.IsAnalyed) { return(this); } OpKind = OpToken.Kind; if (OpKind != TokenKindSymbol.ADD && OpKind != TokenKindSymbol.SUB) { Errorf(OpToken.Position, "运算符'{0}'缺少表达式", OpToken.Text); //return null; } RightExp = AnalySubExp(RightExp); if (RightExp.AnalyCorrect) { this.RetType = RightExp.RetType; if (!CanUnary(RetType)) { Errorf(RightExp.Position, "不能进行'{0}'运算", OpToken.Text); } } IsAnalyed = true; if (OpKind == TokenKindSymbol.ADD) { return(RightExp); } else { return(this); } }
public static MethodInfo GetCalcMethod(TokenKindSymbol opKind, ZType ltype, ZType rtype) { if (ltype is ZLType && rtype is ZLType) { return(GetCalcMethod(opKind, (ltype as ZLType).SharpType, (rtype as ZLType).SharpType)); } return(null); }
public override Exp Analy( ) { if (this.IsAnalyed) { return(this); } AnalyCorrect = true; if (LeftExp == null && RightExp != null) { ExpUnary unexp = new ExpUnary(this.ExpContext, OpToken, RightExp); var exp = unexp.Analy(); return(exp); } else if (LeftExp == null && RightExp == null) { Errorf(this.OpToken.Position, "运算符'{0}'两边缺少表达式", OpToken.Text); } OpKind = OpToken.Kind; LeftExp = AnalySubExp(LeftExp); RightExp = AnalySubExp(RightExp); if (RightExp == null) { Errorf(OpToken.Position, "运算符'{0}'右边缺少运算元素", OpToken.Text); } else { this.AnalyCorrect = this.LeftExp.AnalyCorrect && RightExp.AnalyCorrect && this.AnalyCorrect; if (LeftExp.AnalyCorrect && RightExp.AnalyCorrect) { ZType ltype = LeftExp.RetType; ZType rtype = RightExp.RetType; if (ZTypeUtil.IsVoid(ltype) || ZTypeUtil.IsVoid(rtype)) { Errorf(OpToken.Position, "没有结果的表达式无法进行'{0}'运算", OpToken.ToCode()); } else { OpMethod = ExpBinaryUtil.GetCalcMethod(OpKind, ltype, rtype); if (OpMethod != null) { RetType = ZTypeManager.GetBySharpType(OpMethod.ReturnType) as ZType; } else { Errorf(OpToken.Position, "两种类型无法进行'{0}'运算", OpToken.ToCode()); } } } else { this.RetType = ZLangBasicTypes.ZOBJECT; } } //AnalyResultLocal(); IsAnalyed = true; return(this); }
public static MethodInfo GetCalcMethod(TokenKindSymbol opKind, Type ltype, Type rtype) { CalculaterMethodTypeEnum calculaterMethodType = GetCalculaterMethodType(opKind, ltype, rtype); if (calculaterMethodType == CalculaterMethodTypeEnum.None) { return(null); } return(GetCalcMethod(calculaterMethodType, opKind, ltype, rtype)); }
public static string GetTextByKind(TokenKindSymbol tokKind) { if (Dict.ContainsV(tokKind)) { return(Dict.GetK(tokKind)); } else { return(null); } }
public bool Has(TokenKindSymbol kind) { foreach (var item in Tokens) { if (item.IsKind(kind)) { return(true); } } return(false); }
public static string GetOpName(TokenKindSymbol opKind) { if (opKind == TokenKindSymbol.ADD) { return("Add"); } if (opKind == TokenKindSymbol.SUB) { return("Sub"); } if (opKind == TokenKindSymbol.MUL) { return("Mul"); } if (opKind == TokenKindSymbol.DIV) { return("Div"); } if (opKind == TokenKindSymbol.GT) { return("GT"); } if (opKind == TokenKindSymbol.GE) { return("GE"); } if (opKind == TokenKindSymbol.EQ) { return("EQ"); } if (opKind == TokenKindSymbol.NE) { return("NE"); } if (opKind == TokenKindSymbol.LT) { return("LT"); } if (opKind == TokenKindSymbol.LE) { return("LE"); } if (opKind == TokenKindSymbol.AND) { return("AND"); } if (opKind == TokenKindSymbol.OR) { return("OR"); } return(null); }
public bool Match(TokenKindSymbol tokKind) { if (!CurrentIsKind(tokKind)) { error(this.Current, this.Current.ToCode() + "不正确,应该是" + LexTokenSymbol.GetTextByKind(tokKind)); return(false); } else { MoveNext(); return(true); } }
//public bool isBracketEnd(TokenKindSymbol kind) //{ // return (kind == TokenKindSymbol.EOF || kind == TokenKindSymbol.Semi // || kind == TokenKindSymbol.RBS // // || kind == TokenKindSymbol.NewLine // );// || isNewLine()); //} //public TokenKindSymbol CurrentKind //{ // get { return this.Current.Kind; } //} private bool CurrentIsKind(TokenKindSymbol kind) { return(this.Current.IsKind(kind)); //if( this.Current is LexTokenSymbol) //{ // LexTokenSymbol tokensymbol = (LexTokenSymbol)this.Current; // if(tokensymbol.Kind==kind) // { // return true; // } //} //return false; }
public static CalculaterMethodTypeEnum GetCalculaterMethodType(TokenKindSymbol opKind, Type ltype, Type rtype) { if (ltype == typeof(string) || rtype == typeof(string)) { if (opKind == TokenKindSymbol.ADD) { return(CalculaterMethodTypeEnum.ContactString); } } if (opKind == TokenKindSymbol.ADD || opKind == TokenKindSymbol.SUB || opKind == TokenKindSymbol.MUL) { return(CalculaterMethodTypeEnum.MathOp); } if (opKind == TokenKindSymbol.DIV) { return(CalculaterMethodTypeEnum.MathDiv); } if (opKind == TokenKindSymbol.GT || opKind == TokenKindSymbol.GE || opKind == TokenKindSymbol.LT || opKind == TokenKindSymbol.LE) { return(CalculaterMethodTypeEnum.MathCompare); } if (opKind == TokenKindSymbol.AND || opKind == TokenKindSymbol.OR) { return(CalculaterMethodTypeEnum.Logic); } if (opKind == TokenKindSymbol.EQ || opKind == TokenKindSymbol.NE) { if (Calculater.IsNumberType(ltype) && Calculater.IsNumberType(rtype)) { return(CalculaterMethodTypeEnum.MathCompare); } else { return(CalculaterMethodTypeEnum.RefCompare); } } return(CalculaterMethodTypeEnum.None); }
private static MethodInfo GetCalcMethod(CalculaterMethodTypeEnum calculaterMethodType, TokenKindSymbol opKind, Type ltype, Type rtype) { Type calcType = typeof(Calculater); if (calculaterMethodType == CalculaterMethodTypeEnum.ContactString) { return(calcType.GetMethod("AddString")); } string opName = GetOpName(opKind); if (opName == null) { return(null); } string typeName = GetTypeName(ltype, rtype); if (typeName == null) { return(null); } string methodName = opName + typeName; if (calculaterMethodType == CalculaterMethodTypeEnum.MathOp) { return(calcType.GetMethod(methodName)); } if (calculaterMethodType == CalculaterMethodTypeEnum.MathDiv) { return(calcType.GetMethod(methodName)); } if (calculaterMethodType == CalculaterMethodTypeEnum.MathCompare) { return(calcType.GetMethod(methodName)); } if (calculaterMethodType == CalculaterMethodTypeEnum.RefCompare) { return(calcType.GetMethod(methodName)); } if (calculaterMethodType == CalculaterMethodTypeEnum.Logic) { return(calcType.GetMethod(methodName)); } return(null); }
public LexTokenSymbol(int line, int col, TokenKindSymbol kind) { Kind = kind; Line = line; Col = col; }