Exemple #1
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     Init.OnAnalyzerVisit(analyzer, block);
     Test.OnAnalyzerVisit(analyzer, block);
     Update.OnAnalyzerVisit(analyzer, block);
     Block.OnAnalyzerVisit(analyzer, block);
 }
Exemple #2
0
        public override ReturnType CalcReturnType(SemanticAnalyzer analyzer)
        {
            //Dictionary<ReturnType, int> cast = new Dictionary<ReturnType, int>()
            //{
            //    {ReturnType.Char, 1},
            //    {ReturnType.Int, 2},
            //    {ReturnType.Float, 3},
            //};
            //int left, right;
            //try
            //{
            //    cast.TryGetValue(Left.CalcReturnType(analyzer), out left);
            //    cast.TryGetValue(Right.CalcReturnType(analyzer), out right);
            //}
            //catch
            //{
            //    throw new SemanticError($"Invalid Operand Type {Left}({Left.CalcReturnType(analyzer)}) and {Right}({Right.CalcReturnType(analyzer)})");
            //}
            ReturnType leftRet  = Left.CalcReturnType(analyzer);
            ReturnType rightRet = Right.CalcReturnType(analyzer);

            if (leftRet != rightRet)
            {
                throw new SemanticError($"Invalid Operand {leftRet} {Operator} {rightRet}");
            }
            return(leftRet);
        }
Exemple #3
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     foreach (VariableDeclarator declarator in Declarators)
     {
         declarator.OnAnalyzerVisit(analyzer, block);
     }
 }
Exemple #4
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     analyzer.AddBlock(this.BlockId, block);
     foreach (Statement statement in Statements)
     {
         statement.OnAnalyzerVisit(analyzer, BlockId);
     }
 }
Exemple #5
0
        public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
        {
            SemanticAnalyzer tanalyzer = analyzer;

            foreach (Statement statement in Statements)
            {
                statement.OnAnalyzerVisit(tanalyzer, 0);
            }
        }
Exemple #6
0
 // This is VISITED only within Expression as an variable
 // In FunctionDecl and VariableDecl it is added to SymbolTable
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     BlockId = block;
     if (true)
     {
         symbol = analyzer.FindVariable(BlockId, IdentifierName);
         Return = (ReturnType)((VariableSymbol)symbol).VariableType;
     }
 }
Exemple #7
0
        public override ReturnType CalcReturnType(SemanticAnalyzer analyzer)
        {
            ReturnType var = Identifier.CalcReturnType(analyzer);
            ReturnType exp = Value.CalcReturnType(analyzer);

            if (var != exp)
            {
                throw new SemanticError($"Invalid Assignment {var} = {exp}");
            }
            return(var);
        }
Exemple #8
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     symbol = new FunctionSymbol(block, Identifier.IdentifierName, ReturnType, ArgumentList);
     analyzer.AddSymbol(symbol);
     foreach (FormalArgument arg in ArgumentList)
     {
         VariableSymbol symbol = new VariableSymbol(Block.BlockId, arg.VariableType, arg.Identifier.IdentifierName);
         arg.Identifier.symbol = symbol;
         analyzer.AddSymbol(symbol);
     }
     Block.OnAnalyzerVisit(analyzer, block);
 }
Exemple #9
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     foreach (Expression arg in Arguments)
     {
         arg.OnAnalyzerVisit(analyzer, block);
     }
     Symbol = analyzer.FindFunction(Identifier.IdentifierName, Arguments);
     if (Symbol == null)
     {
         throw new SemanticError($"No corresponding function defined as {this.Identifier.IdentifierName}");
     }
     Return = Symbol.ReturnType;
 }
Exemple #10
0
        public override ReturnType CalcReturnType(SemanticAnalyzer analyzer)
        {
            ReturnType r = new Dictionary <SyntaxNodeType, ReturnType>()
            {
                { SyntaxNodeType.StringLiteral, ReturnType.Void },
                { SyntaxNodeType.CharLiteral, ReturnType.Char },
                { SyntaxNodeType.IntegerLiteral, ReturnType.Int },
                { SyntaxNodeType.FloatLiteral, ReturnType.Float },
                { SyntaxNodeType.BooleanLiteral, ReturnType.Char },
            }[this.Type];

            Return = r;
            return(r);
        }
Exemple #11
0
 public override ReturnType CalcReturnType(SemanticAnalyzer analyzer)
 {
     return(this.Expression.CalcReturnType(analyzer));
 }
Exemple #12
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     Expression.OnAnalyzerVisit(analyzer, block);
     Return = Expression.CalcReturnType(analyzer);
 }
Exemple #13
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     // 没有 Statement 类的节点
     throw new NotImplementedException();
 }
Exemple #14
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     Left.OnAnalyzerVisit(analyzer, block);
     Right.OnAnalyzerVisit(analyzer, block);
     Return = CalcReturnType(analyzer);
 }
Exemple #15
0
 public virtual ReturnType CalcReturnType(SemanticAnalyzer analyzer)
 {
     throw new NotImplementedException();
 }
Exemple #16
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     Identifier.OnAnalyzerVisit(analyzer, block);
     Value.OnAnalyzerVisit(analyzer, block);
 }
Exemple #17
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     ReturnValue.OnAnalyzerVisit(analyzer, block);
 }
Exemple #18
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     // 没有 PrimaryExpression类的节点
     throw new NotImplementedException();
 }
Exemple #19
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     symbol = new VariableSymbol(block, DeclareType, Identifier.IdentifierName);
     analyzer.AddSymbol(symbol);
     Identifier.OnAnalyzerVisit(analyzer, block);
 }
Exemple #20
0
 public override ReturnType CalcReturnType(SemanticAnalyzer analyzer)
 {
     throw new NotImplementedException();
 }
Exemple #21
0
 public override void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     analyzer.AddLiteral(this);
     CalcReturnType(analyzer);
 }
Exemple #22
0
 public override ReturnType CalcReturnType(SemanticAnalyzer analyzer)
 {
     return(Return == ReturnType.Void ? (ReturnType)analyzer.FindVariable(BlockId, IdentifierName).VariableType : Return);
 }
Exemple #23
0
 public override ReturnType CalcReturnType(SemanticAnalyzer analyzer)
 {
     return(analyzer.FindFunction(Identifier.IdentifierName, Arguments).ReturnType);
 }
Exemple #24
0
 public virtual void OnAnalyzerVisit(SemanticAnalyzer analyzer, int block)
 {
     throw new NotImplementedException();
 }