예제 #1
0
        private void CalculateTypeForNodes(IAstNode exprNode)
        {
            if (exprNode.AstNodeType == AstNodeTypes.BinaryOperator)
            {
                var binOpNode = exprNode.AsBinaryOp();

                CalculateTypeForNodes(binOpNode.LeftOperand);
                binOpNode.TypeName = binOpNode.LeftOperand.AsNodeWithType().TypeName;
                CalculateTypeForNodes(binOpNode.RightOperand);
            }
            else if (exprNode.AstNodeType == AstNodeTypes.UnaryOperator)
            {
                CalculateTypeForNodes(exprNode.AsUnaryOp().Operand);
            }
            else if (exprNode.AstNodeType == AstNodeTypes.VarReference)
            {
                var varRef = exprNode.AsVarReferene();

                if (!_symbolTable.ContainsKey(varRef.Name))
                {
                    throw new SemanticErrorException($"Variable '{varRef.Name}' is not declared.", exprNode);
                }

                exprNode.AsVarReferene().TypeName = _symbolTable[varRef.Name];
            }
        }