Esempio n. 1
0
        public void AddFunctionToForward(FunctionDefinition definition)
        {
            bool ok = true;
            foreach (FunctionDefinition def in f_definitions)
            {
                if (def.Header.FunctionName == definition.Header.FunctionName)
                {
                    if (!def.Header.ReturnType.Equals(definition.Header.ReturnType))
                    {
                        CompilerServices.AddError(
                            definition.Location,
                            "Can't overload function by returning value!");
                        ok = false;
                    }
                    else
                    {
                        List<FunctionParameter> p_d2 = new List<FunctionParameter>(def.Header.Parameters);
                        List<FunctionParameter> p_d1
                            = new List<FunctionParameter>(definition.Header.Parameters);

                        if (p_d1.Count == p_d2.Count)
                        {
                            bool match = true;
                            for (int i = 0; i < p_d1.Count; ++i)
                            {
                                if (!p_d1[i].Type.Equals(p_d2[i].Type))
                                {
                                    match = false;
                                    break;
                                }
                            }
                            if (match)
                            {
                                CompilerServices.AddError(
                                    definition.Location,
                                    "Duplicate function header!");
                                ok = false;
                            }
                        }
                    }
                }
            }
            if (ok)
                f_definitions.Insert(0, definition);
        }
        public static void SemanticAnalise(L1Program program)
        {
            foreach (FunctionDefinition functionDef in program.Functions)
            {
                if (functionDef.IsEmbedded)
                    continue;

                SymbolTableLight table = new SymbolTableLight();
                foreach (FunctionParameter parameter in functionDef.Header.Parameters)
                {
                    SymbolLight symbol = new SymbolLight(parameter.Name, parameter.Type);
                    table.AddSymbol(symbol);
                }

                f_currentFunction = functionDef;

                CheckForLabelsDuplicates(f_currentFunction.Statements);

                ValidateStatementList(functionDef.Statements, table);

                if (functionDef.Header.ReturnType != null && !HasReturn(functionDef.Statements))
                {
                    CompilerServices.AddError(
                        functionDef.Location,
                        "Not all branches of execution return value"
                    );
                }
            }
        }
        public static void InitStdFunctions(L1Program program)
        {
            Dictionary<FunctionHeader, MethodInfo> stdlib = new Dictionary<FunctionHeader, MethodInfo>();
            Type runtimeContainer = typeof(L1Runtime.L1Stdlib);

            MethodInfo[] methods = runtimeContainer.GetMethods(BindingFlags.Static | BindingFlags.Public);

            foreach (MethodInfo mi in methods)
            {

                Attribute signatureAttrA = Attribute.GetCustomAttribute(mi, typeof(L1Runtime.SignatureAttribute), false);
                if (signatureAttrA != null)
                {
                    L1Runtime.SignatureAttribute signatureAttr = (L1Runtime.SignatureAttribute)signatureAttrA;
                    FunctionHeader fh = new FunctionHeader(mi.Name, GetVariableTypeByTypeId(signatureAttr.ReturnTypeId));

                    int i = 0;
                    foreach (L1Runtime.VariableTypeId typeId in signatureAttr.ParametersTypeIds)
                    {
                        FunctionParameter p =
                            new FunctionParameter("a"+i.ToString(), GetVariableTypeByTypeId(typeId));
                        System.Diagnostics.Debug.Assert(p.Type != null);
                        fh.AddParameter(p);
                        i++;
                    }

                    FunctionDefinition fdef = new FunctionDefinition();
                    fdef.Header = fh;
                    fdef.IsEmbedded = true;
                    fdef.Body = mi;
                    fdef.Location = new LexLocation();
                    fdef.Statements = new StatementList();

                    program.AddFunctionToForward(fdef);

                    stdlib.Add(fh, mi);
                }
            }

            EmitServices.DefineStandartFunctions(stdlib);
        }
 public static List<string> GetAllLables(FunctionDefinition function)
 {
     if (f_labels.ContainsKey(function))
     {
         return f_labels[function];
     }
     else
     {
         return new List<string>();
     }
 }
Esempio n. 5
0
        public static void EmitFunction(FunctionDefinition fDef, SymbolTable table, ILGenerator ilGen)
        {
            List<string> labelNames = CompilerServices.GetAllLables(fDef);
            f_contextLabels = new Dictionary<string, Label>();
            foreach (string labelName in labelNames)
            {
                f_contextLabels.Add(labelName, ilGen.DefineLabel());
            }

            EmitStatementList(fDef.Statements, table, ilGen);
            if (fDef.Header.ReturnType == null)
                ;
            else if (fDef.Header.ReturnType.TypeEnum != VariableTypeEnum.Array)
            {
                ilGen.Emit(OpCodes.Ldc_I4_0);
            }
            else
            {
                ilGen.Emit(OpCodes.Ldnull);
            }
            ilGen.Emit(OpCodes.Ret);
        }