コード例 #1
0
 public void Visit(Program node)
 {
     VisitChildren(node);
     if (!Global_Function_Table.Contains("main"))
     {
         throw new SemanticError("No main function declared");
     }
     firstPass = false;
     VisitChildren(node);
 }
コード例 #2
0
 public string Visit(Identifier node)
 {
     if (parameters.Contains(node.AnchorToken.Lexeme))
     {
         return(Line(Indent() + "ldarg." + parameters[node.AnchorToken.Lexeme]));
     }
     else if (localVariables.Contains(node.AnchorToken.Lexeme))
     {
         return(Line(Indent() + "ldloc '" + node.AnchorToken.Lexeme + "'"));
     }
     else
     {
         return(Line(Indent() + "ldsfld int32 'DeepLingoProgram'::'" + node.AnchorToken.Lexeme + "'"));
     }
 }        //100%
コード例 #3
0
        //-----------------------------------------------------------
        //<id>//
        public void Visit(Identifier node, int i)
        {
            var variableName = node.AnchorToken.Lexeme;

            if (i == 1)
            {
                if (GlobalVarsTable.Contains(variableName))
                {
                    throw new SemanticError(
                              "Duplicated var: " + variableName,
                              node.AnchorToken);
                }
                else
                {
                    GlobalVarsTable[variableName] = "Global Var";
                }
            }
            else if (i == 2)
            {
                if (FunctionTable.Contains(variableName))
                {
                    throw new SemanticError(
                              "Duplicated function: " + variableName,
                              node.AnchorToken);
                }
                else
                {
                    currentFunc = variableName;
                    FunctionTable[variableName] = new GFuncStruct("u", 0, new FunContainer());
                }
            }
        }
コード例 #4
0
        //-----------------------------------------------------------
        //<id>//
        public void Visit(Identifier node, char i)
        {
            var variableName = node.AnchorToken.Lexeme;

            if (i.Equals('f'))
            {
                if (FunctionTable.Contains(variableName))
                {
                    currentFunc = variableName;
                    FunMethods[variableName] = new FunContainer();
                }
            }
            else if (i.Equals('p'))
            {
                var fctemp = FunMethods[currentFunc];
                fctemp.ParticularFunction[variableName] = new DataOfFunc("param", parameterCounter);
                FunMethods[currentFunc] = fctemp;
                parameterCounter++;
            }
            else if (i.Equals('v'))
            {
                var fctemp = FunMethods[currentFunc];
                if (fctemp.ParticularFunction.Contains(variableName))
                {
                    throw new SemanticError(
                              "Duplicated var: " + variableName,
                              node.AnchorToken);
                }
                else
                {
                    fctemp.ParticularFunction[variableName] = new DataOfFunc("local", -1);
                    FunMethods[currentFunc] = fctemp;
                }
            }
            else if (i.Equals('s'))
            {
                var fctemp = FunMethods[currentFunc];
                if (!fctemp.ParticularFunction.Contains(variableName) && !GlobalVarsTable.Contains(variableName))
                {
                    throw new SemanticError(
                              "Undeclared var: " + variableName,
                              node.AnchorToken);
                }
            }
            else if (i.Equals('c') || i.Equals('q'))
            {
                var fctemp = FunMethods[currentFunc];
                if (!fctemp.ParticularFunction.Contains(variableName) && !GlobalVarsTable.Contains(variableName))
                {
                    throw new SemanticError(
                              "Undeclared var: " + variableName,
                              node.AnchorToken);
                }
            }
        }
コード例 #5
0
        //-----------------------------------------------------------
        //<funcall>//
        public void Visit(FunCall node, char i)
        {
            var variableName = node.AnchorToken.Lexeme;
            var fctemp       = FunMethods[currentFunc];

            if (!FunMethods.Contains(variableName) && !FunctionTable.Contains(variableName))
            {
                throw new SemanticError(
                          "Func Not declared: " + variableName,
                          node.AnchorToken);
            }
            else
            {
                if (i.Equals('c'))
                {
                    parameterCounter2 = 0;
                    VisitChildren(node, 'q');
                    GFuncStruct temp = FunctionTable[variableName];
                    //Console.WriteLine(variableName);
                    if (parameterCounter2 != temp.arity)
                    {
                        throw new SemanticError(
                                  "Incorrect parameters arity in function call: " + variableName + ", expected:" + temp.arity + ", actual:" + parameterCounter2,
                                  node.AnchorToken);
                    }
                }
                else
                {
                    parameterCounter = 0;
                    VisitChildren(node, 'c');
                    GFuncStruct temp = FunctionTable[variableName];
                    if (parameterCounter != temp.arity)
                    {
                        throw new SemanticError(
                                  "Incorrect parameters arity in function call: " + variableName + ", expected:" + temp.arity + ", actual:" + parameterCounter,
                                  node.AnchorToken);
                    }
                }
            }
        }