Exemple #1
0
        internal static void DefineFunction(CType signature, string name, CStorageClass cStorageClass)
        {
            //cStorageClass (static, extern, or none)
            //ignore for now

            if (currentFunctionScope != null || BlockScopes.Count > 0)
            {
                throw new SemanticException("can only define function at file scope");
            }
            else
            {
                if (fileScope.ContainsKey(name))
                {
                    if (fileScope[name].functionBody == null && fileScope[name].type.Equals(signature))
                    {
                        fileScope[name].functionBody = CStatment.PopStatement();
                    }
                    else
                    {
                        throw new SemanticException("canot redifein function");
                    }
                }
                else
                {
                    fileScope[name] = new CIdentifier(signature, name, CStatment.PopStatement());
                }
            }
        }
Exemple #2
0
        //todo handle scope
        public static CIdentifier CreateIdentifierInCurrentScope(string name, CType cType, CStorageClass StorageClass = CStorageClass.Ignore, CConstType ConstType = CConstType.Ignore)
        {
            if (CType.InStructDefinition)
            {
                //member variable
                CIdentifier ident = new CIdentifier(cType, name, true, false, 0, CType.MemberOffset(cType.Size));
                CType.GlobalStructMembers.Add(ident);
                return(ident);
            }
            else if (BlockScopes.Count > 0)
            {
                if (BlockScopes[BlockScopes.Count - 1].ContainsKey(name))
                {
                    throw new SemanticException("idenitier " + name + " is already defined in this scope");
                }

                //local var
                CIdentifier ident = new CIdentifier(cType, name, false, true, CStatment.PushLocal(cType.Size), 0);
                BlockScopes[BlockScopes.Count - 1][name] = ident;
                return(ident);
            }
            else
            {
                if (fileScope.ContainsKey(name))
                {
                    throw new SemanticException("idenitier " + name + " is already defined in this scope");
                }

                //global var
                CIdentifier ident = new CIdentifier(cType, name, false, false, 0, 0);
                fileScope[name] = ident;
                return(ident);
            }
        }