//------------------------------------------------------------ // コンストラクタ。 public FunctionArgumentDecl( TypePath aTypePath , Identifier aIdent , bool aIsConst , bool aIsRef , bool aIsIn ) { TypePath = aTypePath; Ident = aIdent; IsConst = aIsConst; IsRef = aIsRef; IsIn = aIsIn; }
//------------------------------------------------------------ /// <summary> /// CastExpression: /// "cast" "(" TypePath ")" UnaryExpression /// </summary> /// <returns></returns> IExpression parseCastExpression() { // "cast" Assert.Check(currentToken().Value == Token.Kind.KeyCast); nextToken(); // "(" if (!expectToken(Token.Kind.OpLParen, ErrorKind.CAST_EXPRESSION_LPAREN_EXPECTED)) { return(null); } // TypePath TypePath typePath = parseTypePath(); if (typePath == null) { return(null); } // ")" if (!expectToken(Token.Kind.OpRParen, ErrorKind.CAST_EXPRESSION_RPAREN_EXPECTED)) { return(null); } // UnaryExpression IExpression expr = parseUnaryExpression(); if (expr == null) { return(null); } return(new CastExpression(typePath, expr)); }
//------------------------------------------------------------ /// <summary> /// NewExpression: /// "new" TypePath FunctionCallExpression /// </summary> /// <returns></returns> IExpression parseNewExpression() { // "new" Assert.Check(currentToken().Value == Token.Kind.KeyNew); nextToken(); // TypePath TypePath typePath = parseTypePath(); if (typePath == null) { return(null); } // FunctionCallExpression FunctionCallExpression funcCallExpr = parseFunctionCallExpression(); if (funcCallExpr == null) { return(null); } return(new NewExpression(typePath, funcCallExpr)); }
//------------------------------------------------------------ // コンストラクタ。 public NewExpression(TypePath aTypePath, FunctionCallExpression aFuncCallExpr) { mTypePath = aTypePath; mFuncCallExpr = aFuncCallExpr; }
//------------------------------------------------------------ // コンストラクタ。 public CastExpression(TypePath aTypePath, IExpression aExpr) { mTypePath = aTypePath; mExpr = aExpr; }
//------------------------------------------------------------ // DeclarationStatement。 DeclarationStatement parseDeclarationStatement() { // 属性のパース Token isConst = null; while (true) { if (currentToken().Value == Token.Kind.KeyConst) {// const if (isConst == null) { setErrorKind(ErrorKind.DECLARATION_STATEMENT_ALREADY_ASSIGNED_ATTRIBUTE_CONST); return(null); } isConst = currentToken(); } else { break; } nextToken(); } // TypePath TypePath typePath = parseTypePath(); if (typePath == null) { return(null); } // Identifier if (currentToken().Value != Token.Kind.Identifier) { return(null); } Identifier ident = new Identifier(currentToken()); nextToken(); // '=' IExpression expr = null; if (currentToken().Value == Token.Kind.OpAssign) { nextToken(); // Expression expr = parseExpression(); if (expr == null) { return(null); } } // ';' if (currentToken().Value != Token.Kind.OpSemicolon) { setErrorKind(ErrorKind.DECLARATION_STATEMENT_SEMICOLON_EXPECTED); return(null); } nextToken(); return(new DeclarationStatement( new VariableDecl(typePath, ident, expr) , isConst != null )); }
//------------------------------------------------------------ // SymbolDef。 SymbolDef parseSymbolDef(StaticTypeDef aTD, Protection aDefaultProtection, bool aIsProtoType) { Protection protect = Protection.DEFAULT; Token isAbstract = null; Token isConst = null; Token isOverride = null; Token isReadonly = null; Token isRef = null; Token isStatic = null; while (true) { // pre Token t = currentToken(); { // attribute { // ChangeProtection Protection tmpProtection = parseTypeProtection(t); if (tmpProtection != Protection.Unknown) {// set protect if (protect != Protection.DEFAULT) { setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASIGNED_TYPE_PROTECTION); return(null); } protect = tmpProtection; if (!checkProtection(protect, aIsProtoType)) { return(null); } nextToken(); continue; } } if (t.Value == Token.Kind.KeyAbstract) {// SetAbstractAttribute if (aTD.TypeKind != StaticTypeDef.Kind.Interface) { setErrorKind(ErrorKind.SYMBOL_DEF_ONLY_INTERFACE_TYPE_CAN_ABSTRACT_ATTRIBUTE_MEMBER); return(null); } if (isAbstract != null) { setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_ABSTRACT); return(null); } isAbstract = currentToken(); nextToken(); continue; } if (t.Value == Token.Kind.KeyConst) {// SetConstAttribute if (isConst != null) { setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_CONST); return(null); } isConst = currentToken(); nextToken(); continue; } if (t.Value == Token.Kind.KeyOverride) {// SetOverrideAttribute if (aTD.TypeKind != StaticTypeDef.Kind.Class) { setErrorKind(ErrorKind.SYMBOL_DEF_ONLY_CLASS_TYPE_CAN_OVERRIDE_ATTRIBUTE_MEMBER); return(null); } if (isConst != null) { setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_OVERRIDE); return(null); } isOverride = currentToken(); nextToken(); continue; } if (t.Value == Token.Kind.KeyRef) {// SetRefAttribute if (isRef != null) { setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_REF); return(null); } isRef = currentToken(); nextToken(); continue; } if (t.Value == Token.Kind.KeyReadonly) {// SetReadonlyAttribute if (isReadonly != null) { setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_READONLY); return(null); } isReadonly = currentToken(); nextToken(); continue; } if (t.Value == Token.Kind.KeyStatic) {// SetStaticAttribute if (isStatic != null) { setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_STATIC); return(null); } isStatic = currentToken(); nextToken(); continue; } } // シンボルの実装の前準備 protect = getProtectionWithDefaultValue(protect, aDefaultProtection); {// StaticTypeDef StaticTypeDef.Kind typeKind = parseStaticTypeKind(t); if (typeKind != StaticTypeDef.Kind.Unknown) { // 無効な属性のチェック if (checkErrorSymbolDefIllegalAttr(isAbstract, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) || checkErrorSymbolDefIllegalAttr(isConst, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) || checkErrorSymbolDefIllegalAttr(isOverride, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) || checkErrorSymbolDefIllegalAttr(isReadonly, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) || checkErrorSymbolDefIllegalAttr(isRef, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) || checkErrorSymbolDefIllegalAttr(isStatic, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) ) { return(null); } // 解析 StaticTypeDef staticTypeDef = parseStaticTypeDef(protect, aIsProtoType); if (staticTypeDef == null) { return(null); } return(new SymbolDef(staticTypeDef)); } } {// StaticCtorDef,CtorDef // todo: impl } {// StaticDtorDef,DtorDef // todo: impl } {// MemberDef // 戻り値の型もしくは変数の型 TypePath firstTypePath = parseTypePath(); if (firstTypePath != null) { // メンバ名 if (currentToken().Value != Token.Kind.Identifier) { setErrorKind(ErrorKind.MEMBER_DEF_IDENTIFIER_EXPECTED); return(null); } Identifier nameIdent = new Identifier(currentToken()); nextToken(); if (currentToken().Value == Token.Kind.OpAssign || currentToken().Value == Token.Kind.OpSemicolon ) {// '=' or ';' ならMemberVariableDecl // エラーチェック if (checkErrorSymbolDefIllegalAttr(isAbstract, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_MEMBER_VARIABLE_DECL) || checkErrorSymbolDefIllegalAttr(isOverride, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_MEMBER_VARIABLE_DECL) || checkErrorSymbolDefIllegalAttr(isRef, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_MEMBER_VARIABLE_DECL) ) { return(null); } if (isStatic == null) { if (aTD.TypeKind == StaticTypeDef.Kind.Interface) {// interface型は非staticメンバ変数をもてない setErrorKind(ErrorKind.MEMBER_VARIABLE_DECL_INTERFACE_CANT_HAVE_NONSTATIC_MEMBER_VARIABLE); return(null); } if (aTD.TypeKind == StaticTypeDef.Kind.Utility) {// utility型は非staticメンバ変数をもてない setErrorKind(ErrorKind.MEMBER_VARIABLE_DECL_UTILITY_CANT_HAVE_NONSTATIC_MEMBER_VARIABLE); return(null); } } // 右辺 IExpression expr = null; if (currentToken().Value == Token.Kind.OpAssign) { nextToken(); // 右辺解析 expr = parseConditionalExpression(); } // ';' if (currentToken().Value != Token.Kind.OpSemicolon) { setErrorKind(ErrorKind.SYMBOL_DEF_SEMICOLON_EXPECTED); return(null); } nextToken(); return(new SymbolDef( new MemberVariableDecl( new VariableDecl(firstTypePath, nameIdent, expr) , isStatic != null , isConst != null , isReadonly != null ) )); } else if (currentToken().Value == Token.Kind.OpLParen) {// '(' ならMemberFunctionDecl // 戻り値 FunctionReturnValueDecl retValueDecl = new FunctionReturnValueDecl( firstTypePath , isConst != null , isRef != null ); // 引数リスト FunctionArgumentDeclList argDeclList = parseFunctionArgumentDeclList(); if (argDeclList == null) { return(null); } // ) の後のconst Token isFunctionConst = null; if (currentToken().Value == Token.Kind.KeyConst) { isFunctionConst = currentToken(); nextToken(); } // '{' or ';' BlockStatement blockStatement = null; if (isAbstract != null || aIsProtoType) {// 宣言のみ。セミコロンがくるはず。 if (currentToken().Value != Token.Kind.OpSemicolon) { setErrorKind(ErrorKind.MEMBER_FUNCTION_DECL_SEMICOLON_EXPECTED); return(null); } nextToken(); } else {// non abstract function if (currentToken().Value != Token.Kind.OpLCurly) { setErrorKind(ErrorKind.MEMBER_FUNCTION_DECL_LCURLY_EXPECTED); return(null); } // BlockStatement blockStatement = parseBlockStatement(); if (blockStatement == null) { return(null); } } // 作成 return(new SymbolDef(new MemberFunctionDecl( nameIdent , retValueDecl , argDeclList , blockStatement , isAbstract != null , isFunctionConst != null , isOverride != null , isStatic != null ))); } else {// エラー setErrorKind(ErrorKind.SYMBOL_DEF_ILLEGAL_MEMBER_SYNTAX); return(null); } } } break; } return(null); }
//------------------------------------------------------------ // 関数の1つの引数の宣言。 FunctionArgumentDecl parseFunctionArgumentDecl() { Token isConst = null; Token isRef = null; Token isIn = null; while (true) { if (currentToken().Value == Token.Kind.KeyConst) { if (isConst != null) { setErrorKind(ErrorKind.FUNCTION_ARGUMENT_DECL_ALREADY_ASSIGNED_ATTRIBUTE_CONST); return(null); } isConst = currentToken(); } else if (currentToken().Value == Token.Kind.KeyRef) { if (isRef != null) { setErrorKind(ErrorKind.FUNCTION_ARGUMENT_DECL_ALREADY_ASSIGNED_ATTRIBUTE_REF); return(null); } isRef = currentToken(); } else if (currentToken().Value == Token.Kind.KeyIn) { if (isIn != null) { setErrorKind(ErrorKind.FUNCTION_ARGUMENT_DECL_ALREADY_ASSIGNED_ATTRIBUTE_IN); return(null); } isIn = currentToken(); } else { break; } nextToken(); } TypePath typePath = parseTypePath(); if (typePath == null) { return(null); } Identifier ident = null; if (currentToken().Value != Token.Kind.OpColon && currentToken().Value != Token.Kind.OpRParen ) { if (currentToken().Value != Token.Kind.Identifier) { setErrorKind(ErrorKind.FUNCTION_ARGUMENT_DECL_IDENTIFIER_EXPECTED); return(null); } ident = new Identifier(currentToken()); nextToken(); } return(new FunctionArgumentDecl( typePath , ident , isConst != null , isRef != null , isIn != null )); }
//------------------------------------------------------------ // コンストラクタ。 public VariableDecl(TypePath aTypePath, Identifier aIdent, IExpression aExpr) { TypePath = aTypePath; Ident = aIdent; Expr = aExpr; }