Exemplo n.º 1
0
 public SymArray(SymIntConst minIndex, SymIntConst maxIndex, SymType type)
 {
     MinIndex = minIndex;
     MaxIndex = maxIndex;
     Type     = type;
     BSize    = (int)Size * Type.BSize;
 }
Exemplo n.º 2
0
 protected SymVarOrConst(string name, SymType type, SymLocTypeEnum locType, bool isConst)
 {
     Name    = name;
     Type    = type;
     IsConst = isConst;
     LocType = locType;
 }
Exemplo n.º 3
0
 private static T BuildException <T>(SymType type, Token token) where T : ParserException
 {
     try {
         return((T)Activator.CreateInstance(typeof(T), type, token.Lexeme, token.Line, token.Column));
     }
     catch (TargetInvocationException e) {
         throw e.InnerException;
     }
 }
Exemplo n.º 4
0
        public SymVar AddVariable(IdentifierToken variableToken, SymType type, SymVar.SymLocTypeEnum varMod, SymConst value = null)
        {
            RequireSymbolRewritable(variableToken);
            //type check for initial value must be performed earlier. i.e. in parser.
            var symVar = new SymVar(variableToken.Value, type, value, varMod);

            _stack.Peek().Add(symVar);
            return(symVar);
        }
Exemplo n.º 5
0
        private bool IsTypesEqual(SymType target, SymType source)
        {
            var lhs = target;
            var rhs = source;

            while (true)
            {
                switch (lhs)
                {
                case SymArray lArr:

                    switch (rhs)
                    {
                    case SymArray rArr:
                        if (lArr.MinIndex.Value != rArr.MinIndex.Value ||
                            lArr.MaxIndex.Value != rArr.MaxIndex.Value)
                        {
                            return(false);
                        }

                        if (lArr.Type is SymScalar && rArr.Type is SymScalar)
                        {
                            return(lArr.Type.GetType() == rArr.Type.GetType());
                        }

                        lhs = lArr.Type;
                        rhs = rArr.Type;

                        continue;
                    }

                    break;

                case SymScalar lScalar:
                    switch (rhs)
                    {
                    case SymScalar rScalar:
                        return(lScalar.GetType() == rScalar.GetType());
                    }
                    break;

                case SymRecord lRecord:
                    switch (rhs)
                    {
                    case SymRecord rRecord:
                        return(lRecord.Name == rRecord.Name);
                    }
                    break;

                default:
                    return(lhs.GetType() == rhs.GetType());
                }

                return(false);
            }
        }
Exemplo n.º 6
0
        public void RequireCast(SymType targetType, ref ExprNode source)
        {
            if (TryCast(targetType, ref source))
            {
                return;
            }

            var token = ExprNode.GetClosestToken(source);

            throw new IncompatibleTypesException(targetType, source.Type, token.Lexeme, token.Line, token.Column);
        }
Exemplo n.º 7
0
        public SymFunc(string name, List <SymVar> parameters, SymTable localVariables, StatementNode body, SymType returnType)
        {
            Name           = name;
            Parameters     = parameters;
            LocalVariables = localVariables;
            Body           = body;

            if (returnType == null)
            {
                ReturnType = new SymVoid();
            }
            else
            {
                ReturnType = returnType;
            }

            //predefined
            if (localVariables == null)
            {
                return;
            }

            foreach (var localSymbol in LocalVariables.Reverse())
            {
                var lvar = localSymbol as SymVar;
                Debug.Assert(lvar != null);

                if (lvar.LocType != SymVar.SymLocTypeEnum.Local)
                {
                    continue;
                }
                LocalVariableBsize += lvar.Type.BSize;
                LocalVarOffsetTable.Add(lvar.Name, LocalVariableBsize);
            }
            // align
            LocalVariableBsize += LocalVariableBsize % 8 > 0 ? 8 - LocalVariableBsize % 8 : 0;

            var paramOffset = 16;

            foreach (var param in parameters)
            {
                ParamsOffsetTable.Add(param.Name, paramOffset);
                var paramSize = param.Type.BSize;
                //  parameters will be aligned on stack
                paramSize   += paramSize % 8 > 0 ? 8 - paramSize % 8  : 0;
                paramOffset += paramSize;
            }

            ParamsSizeB = paramOffset;
        }
Exemplo n.º 8
0
        public void AddFunction(IdentifierToken nameToken, List <SymVar> paramList, SymTable localVars, StatementNode body,
                                SymType returnType)
        {
            //todo: add checks
            RequireSymbolRewritable(nameToken);
//        SymType returnType = null;
//        if (returnTypeToken != null) {
//            returnType = FindType(returnTypeToken.Value);
//            if (returnType == null)
//                throw new TypeNotFoundException(returnTypeToken.Lexeme, returnTypeToken.Line, returnTypeToken.Column);
//        }
//        _stack.Peek().Add(new SymFunc(nameToken.Value, paramList, localVars, body, returnType));
            AddConst(nameToken, new SymFuncConst(nameToken.Value,
                                                 new SymFunc(nameToken.Value, paramList, localVars, body, returnType),
                                                 SymVarOrConst.SymLocTypeEnum.Global));
        }
Exemplo n.º 9
0
        public void AddAliasType(IdentifierToken aliasToken, SymType aliasType)
        {
            RequireSymbolRewritable(aliasToken);

            // compute underlying type before adding
            var realType = aliasType;

            while (realType is SymTypeAlias typeAlias)
            {
                if (typeAlias.Type is SymArray arr)
                {
                    realType = arr;
                    break;
                }
                realType = FindType(typeAlias.Type.Name);
            }

            _stack.Peek().Add(new SymTypeAlias(aliasToken.Value, realType));
        }
Exemplo n.º 10
0
 public OpenArray(SymType symInnerType)
 {
     InnerType = symInnerType;
 }
Exemplo n.º 11
0
 public bool CanCast(SymType targetType, ExprNode source)
 {
     return(TryCast(targetType, ref source, false));
 }
Exemplo n.º 12
0
 public SymStringConst(string name, SymType type, string value, SymLocTypeEnum locType) : base(name, type, locType)
 {
     Value = value;
     InitialStringValue = value;
 }
Exemplo n.º 13
0
 public SymBoolConst(string name, SymType type, bool value, SymLocTypeEnum locType) : base(name, type, locType)
 {
     Value = value;
     InitialStringValue = value.ToString();
 }
Exemplo n.º 14
0
 public SymTypeAlias(string name, SymType type)
 {
     Name = name;
     Type = type;
 }
Exemplo n.º 15
0
 public ArrayExpectedException(SymType type, string lexeme, int line, int column) : base(lexeme, line, column)
 {
     _type = type;
 }
Exemplo n.º 16
0
 public void AddAlias(IdentifierToken aliasToken, SymType type)
 {
     RequireSymbolRewritable(aliasToken);
     _stack.Peek().Add(new SymAlias(aliasToken.Value, type));
 }
Exemplo n.º 17
0
 public void AddType(SymType symType, Token token)
 {
     //todo: check?
     _stack.Peek().Add(symType);
 }
Exemplo n.º 18
0
 private void AddType(SymType symType)
 {
     //todo: check?
     _stack.Peek().Add(symType);
 }
Exemplo n.º 19
0
 public FunctionExpectedException(SymType got, string lexeme, int line, int column) : base(lexeme, line, column)
 {
     _gotType = got;
 }
Exemplo n.º 20
0
 protected PredefinedSymFunc(string name, List <SymVar> parameters, SymTable localVariables, StatementNode body, SymType returnType)
     : base(name, parameters, localVariables, body, returnType)
 {
 }
Exemplo n.º 21
0
 public IncompatibleTypesException(SymType leftType, SymType rightType, string lexeme, int line, int column)
     : base(lexeme, line, column)
 {
     _leftType  = leftType;
     _rightType = rightType;
 }
Exemplo n.º 22
0
 public CastNode(SymType type, ExprNode expr)
 {
     CastTo = type;
     Expr   = expr;
 }
Exemplo n.º 23
0
        // try cast source to target
        public bool TryCast(SymType targetType, ref ExprNode source, bool canModify = true)
        {
            var realSourceType = source.Type is SymTypeAlias sourceTypeAlias ? sourceTypeAlias.Type : source.Type;
            var realTargetType = targetType is SymTypeAlias targetTypeAlias ? targetTypeAlias.Type : targetType;

            switch (realTargetType)
            {
            // scalars
            // double
            case SymDouble _:
                switch (realSourceType)
                {
                case SymInt _:
                    var t = ExprNode.GetClosestToken(source);
                    if (!canModify)
                    {
                        return(true);
                    }

                    source      = new CastNode(_stack.SymDouble, source);
                    source.Type = _stack.SymDouble;
                    return(true);

                case SymDouble _:
                    return(true);
                }

                break;

            // end double
            // int
            case SymInt _:
                switch (realSourceType)
                {
                case SymInt _:
                    return(true);
                }

                break;

            // end int
            // char
            case SymChar _:
                switch (realSourceType)
                {
                case SymChar _:
                    return(true);
                }

                break;

            // end char
            // bool
            case SymBool _:
                switch (realSourceType)
                {
                case SymBool _:
                    return(true);

                case SymInt _:
                    if (!canModify)
                    {
                        return(true);
                    }

                    source      = new CastNode(_stack.SymBool, source);
                    source.Type = _stack.SymBool;
                    return(true);
                }

                break;

            // end bool
            case SymArray target:
                switch (realSourceType)
                {
                case SymArray sourceType:
                    return(IsTypesEqual(target, sourceType));
                }
                break;

            default:
                return(IsTypesEqual(realSourceType, realTargetType));
            }

            return(false);
        }
Exemplo n.º 24
0
 public RecordExpectedException(SymType gotType, string lexeme, int line, int column) : base(lexeme, line, column)
 {
     _gotType = gotType;
 }
Exemplo n.º 25
0
 public SymVar(string name, SymType type, SymConst initialValue, SymLocTypeEnum locType)
     : base(name, type, locType, false)
 {
     InitialValue       = initialValue;
     InitialStringValue = initialValue?.InitialStringValue;
 }
Exemplo n.º 26
0
 public WritelnUnsupportedType(SymType type, string lexeme, int line, int column) : base(lexeme, line, column)
 {
     _type = type;
 }
Exemplo n.º 27
0
 protected SymConst(string name, SymType type, SymLocTypeEnum locType) : base(name, type, locType, true)
 {
 }
Exemplo n.º 28
0
 public OperatorNotOverloaded(SymType type, string lexeme, int line, int column) : base(lexeme, line, column)
 {
     Message = $"Operator {Lexeme} is not overloaded for \"{type.Name}\" at {Line},{Column}.";
 }
Exemplo n.º 29
0
 public SymDoubleConst(string name, SymType type, double value, SymLocTypeEnum locType) : base(name, type, locType)
 {
     Value = value;
     InitialStringValue = value.ToString();
 }