Exemple #1
0
        public override void EnterVarDecl([NotNull] DaedalusParser.VarDeclContext context)
        {
            if (context.Parent.Parent is DaedalusParser.DaedalusFileContext || _assemblyBuilder.IsContextInsideExecBlock())
            {
                var typeName = context.typeReference().GetText();
                var type     = DatSymbolTypeFromString(typeName);
                if (type == DatSymbolType.Class)
                {
                    type = DatSymbolType.Instance;
                }

                for (int i = 0; i < context.ChildCount; i++)
                {
                    var varContext = context.GetChild(i);

                    if (varContext is TerminalNodeImpl)
                    {
                        continue; // skips ','
                    }
                    if (varContext is DaedalusParser.VarValueDeclContext varValueContext)
                    {
                        var name = varValueContext.nameNode().GetText();
                        if (_assemblyBuilder.IsContextInsideExecBlock())
                        {
                            // TODO consider making assemblyBuilder.active public and using it here
                            BaseExecBlockContext baseExecBlock = _assemblyBuilder.ExecBlocks.Last();
                            string execBlockName = baseExecBlock.GetSymbol().Name;
                            name = $"{execBlockName}.{name}";
                        }

                        var location = GetLocation(context);

                        int           parentIndex       = DatSymbol.NULL_INDEX;
                        string        parameterTypeName = context.typeReference().GetText();
                        DatSymbolType?parameterType     = DatSymbolTypeFromString(parameterTypeName);
                        if (parameterType is DatSymbolType.Class)
                        {
                            var parentSymbol = _assemblyBuilder.ResolveSymbol(parameterTypeName);
                            parentIndex = parentSymbol.Index;
                        }

                        var symbol = SymbolBuilder.BuildVariable(name, type, location, parentIndex); // TODO : Validate params
                        _assemblyBuilder.AddSymbol(symbol);
                    }

                    if (varContext is DaedalusParser.VarArrayDeclContext varArrayContext)
                    {
                        var name     = varArrayContext.nameNode().GetText();
                        var location = GetLocation(context);
                        var size     = EvaluatorHelper.EvaluteArraySize(varArrayContext.arraySize(), _assemblyBuilder);

                        var symbol =
                            SymbolBuilder.BuildArrOfVariables(name, type, (uint)size,
                                                              location); // TODO : Validate params
                        _assemblyBuilder.AddSymbol(symbol);
                    }
                }
            }
        }
Exemple #2
0
 public override void EnterFloatLiteralValue(DaedalusParser.FloatLiteralValueContext context)
 {
     if (!_assemblyBuilder.IsInsideConstDef)
     {
         int parsedFloat = EvaluatorHelper.EvaluateFloatExpression(context.Parent.Parent.GetText());
         _assemblyBuilder.AddInstruction(new PushInt(parsedFloat));
     }
 }
Exemple #3
0
        public override void EnterClassDef([NotNull] DaedalusParser.ClassDefContext context)
        {
            var className   = context.nameNode().GetText();
            var classSymbol = SymbolBuilder.BuildClass(className, 0, 0, GetLocation(context));

            _assemblyBuilder.AddSymbol(classSymbol);

            var  classId        = classSymbol.Index;
            int  classVarOffset = classSymbol.ClassOffset;
            uint classLength    = 0;

            // TODO: refactor later
            foreach (var varDeclContext in context.varDecl())
            {
                var typeName = varDeclContext.typeReference().GetText();
                var type     = DatSymbolTypeFromString(typeName);

                for (int i = 0; i < varDeclContext.ChildCount; i++)
                {
                    var varContext = varDeclContext.GetChild(i);

                    if (varContext is TerminalNodeImpl)
                    {
                        continue; // skips ','
                    }
                    if (varContext is DaedalusParser.VarValueDeclContext varValueContext)
                    {
                        var name     = varValueContext.nameNode().GetText();
                        var location = GetLocation(context);

                        var symbol = SymbolBuilder.BuildClassVar(name, type, 1, className, classId,
                                                                 classVarOffset, location); // TODO : Validate params
                        _assemblyBuilder.AddSymbol(symbol);

                        classVarOffset += (type == DatSymbolType.String ? 20 : 4);
                        classLength++;
                    }

                    if (varContext is DaedalusParser.VarArrayDeclContext varArrayContext)
                    {
                        var name     = varArrayContext.nameNode().GetText();
                        var location = GetLocation(context);
                        var size     = EvaluatorHelper.EvaluteArraySize(varArrayContext.arraySize(), _assemblyBuilder);

                        var symbol = SymbolBuilder.BuildClassVar(name, type, (uint)size, className, classId,
                                                                 classVarOffset, location); // TODO : Validate params
                        _assemblyBuilder.AddSymbol(symbol);

                        classVarOffset += (type == DatSymbolType.String ? 20 : 4) * size;
                        classLength++;
                    }
                }
            }

            classSymbol.ArrayLength = classLength;
            classSymbol.ClassSize   = classVarOffset - classSymbol.ClassOffset;
        }
Exemple #4
0
        public override void EnterIntegerLiteralValue(DaedalusParser.IntegerLiteralValueContext context)
        {
            if (!_assemblyBuilder.IsInsideConstDef)
            {
                bool isInsideFloatAssignment = _assemblyBuilder.IsInsideAssignment &&
                                               _assemblyBuilder.AssignmentType == DatSymbolType.Float;
                bool isInsideFloatArgument = _assemblyBuilder.IsInsideArgList &&
                                             _assemblyBuilder.FuncCallCtx.GetParameterType() == DatSymbolType.Float;

                if (isInsideFloatAssignment || isInsideFloatArgument)
                {
                    int parsedFloat = EvaluatorHelper.EvaluateFloatExpression(context.Parent.Parent.GetText());
                    _assemblyBuilder.AddInstruction(new PushInt(parsedFloat));
                }
                else
                {
                    _assemblyBuilder.AddInstruction(new PushInt(int.Parse(context.GetText())));
                }
            }
        }
Exemple #5
0
        public override void EnterConstDef([NotNull] DaedalusParser.ConstDefContext context)
        {
            _assemblyBuilder.IsInsideConstDef = true;
            var typeName = context.typeReference().GetText();
            var type     = DatSymbolTypeFromString(typeName);

            if (type == DatSymbolType.Func)
            {
                type = DatSymbolType.Int;
            }

            for (int i = 0; i < context.ChildCount; i++)
            {
                var constContext = context.GetChild(i);

                if (constContext is TerminalNodeImpl)
                {
                    continue; // skips ','
                }
                if (constContext is DaedalusParser.ConstValueDefContext constValueContext)
                {
                    var name = constValueContext.nameNode().GetText();
                    if (_assemblyBuilder.IsContextInsideExecBlock())
                    {
                        BaseExecBlockContext baseExecBlock = _assemblyBuilder.ExecBlocks.Last();
                        string execBlockName = baseExecBlock.GetSymbol().Name;
                        name = $"{execBlockName}.{name}";
                    }

                    var location             = GetLocation(context);
                    var assignmentExpression = constValueContext.constValueAssignment().expressionBlock().expression();
                    var value = EvaluatorHelper.EvaluateConst(assignmentExpression, _assemblyBuilder, type);

                    var symbol = SymbolBuilder.BuildConst(name, type, value, location); // TODO : Validate params
                    _assemblyBuilder.AddSymbol(symbol);

                    continue;
                }

                if (constContext is DaedalusParser.ConstArrayDefContext constArrayContext)
                {
                    _assemblyBuilder.ErrorContext.Context = constArrayContext;

                    var name     = constArrayContext.nameNode().GetText();
                    var location = GetLocation(context);

                    int  declaredSize = 0;
                    bool compareDeclaredSizeAndElementsCount = true;

                    try
                    {
                        _assemblyBuilder.ErrorContext.Context = constArrayContext.arraySize();
                        declaredSize = EvaluatorHelper.EvaluteArraySize(constArrayContext.arraySize(), _assemblyBuilder);
                    }
                    catch (System.ArgumentNullException)
                    {
                        compareDeclaredSizeAndElementsCount = false;
                    }

                    if (declaredSize == 0 && compareDeclaredSizeAndElementsCount)
                    {
                        compareDeclaredSizeAndElementsCount = false;
                        _assemblyBuilder.Errors.Add(
                            new InvalidArraySizeError(
                                _assemblyBuilder.ErrorContext,
                                name,
                                declaredSize
                                )
                            );
                    }


                    var elements = constArrayContext.constArrayAssignment().expressionBlock()
                                   .Select(expr => EvaluatorHelper.EvaluateConst(expr.expression(), _assemblyBuilder, type))
                                   .ToArray();


                    if (compareDeclaredSizeAndElementsCount && declaredSize != elements.Length)
                    {
                        _assemblyBuilder.Errors.Add(
                            new InconsistentArraySizeError(
                                _assemblyBuilder.ErrorContext,
                                name,
                                declaredSize,
                                elements.Length
                                )
                            );
                    }

                    var symbol =
                        SymbolBuilder.BuildArrOfConst(name, type, elements, location); // TODO : Validate params
                    _assemblyBuilder.AddSymbol(symbol);
                }
            }
        }