Exemple #1
0
        /// <summary>
        /// This function will scan a secquence of characters beginning with a digit.
        /// The function will traverse the stream until it sees a character that is no longer recognized as a digit.
        /// If the non-digit character is a period (.) and it is the first period in the sequence, the function will treat the following digits as floating points.
        /// If there are no digits after the period character, the scanner will append a 0 to the value, to ensure the value is a float in the target language.
        /// <example>The following sequences are recognized as numeric:
        /// <code>
        /// 2
        /// 42
        /// 42.1
        /// 54. # Will become 54.0
        /// </code>
        /// </example>
        /// </summary>
        private void ScanNumeric()
        {
            bool   isFloat   = false;
            string subString = CurrentChar.ToString();

            while (recogniser.IsDigit(Peek()))
            {
                Pop();
                subString += CurrentChar;
            }
            // Make sure it isn't a range
            if (Peek() == '.' && Peek(2) != '.')
            {
                isFloat = !isFloat;
                Pop();
                subString += CurrentChar;
                while (recogniser.IsDigit(Peek()))
                {
                    Pop();
                    subString += CurrentChar;
                }
                if (subString.Last() == '.')
                {
                    subString += "0";
                }
            }
            ScannerToken token = Token(TokenType.NUMERIC, subString);

            token.SymbolicType         = new TypeContext(TokenType.NUMERIC);
            token.SymbolicType.IsFloat = isFloat;
            Tokens.AddLast(token);
        }
Exemple #2
0
 /// <summary>
 /// The constructor for an AST node. This constructor is used for the terminal statement node
 /// </summary>
 /// <param name="type">The type of the token</param>
 /// <param name="token">The kind of token</param>
 public AstNode(TokenType type, ScannerToken token)
 {
     this.Type       = type;
     this.Value      = token.Value;
     this.Line       = token.Line;
     this.Offset     = token.Offset;
     this.SymbolType = token.SymbolicType;
     this.Visited    = false;
 }
 internal void ResetCurrentToken(ScannerState state)
 {
     if (state != _currentToken.State)
     {
         AbandonAllTokens();
         Debug.Assert(_currentToken.Position == _lineBufferOffset);
         Debug.Assert(_currentToken.EndOfTerminatorTrivia == _endOfTerminatorTrivia);
         _currentToken = _currentToken.With(state, null);
     }
 }
        public SyntaxToken GetCurrentToken()
        {
            var tk = _currentToken.InnerTokenObject;
            if (tk == null)
            {
                Debug.Assert(_currentToken.Position == _lineBufferOffset);
                var state = _currentToken.State;
                tk = GetScannerToken(state);
                _currentToken = _currentToken.With(state, tk);
            }

            return tk;
        }
 internal void GetNextTokenInState(ScannerState state)
 {
     _prevToken = _currentToken;
     if (_tokens.Count == 0)
     {
         _currentToken = new ScannerToken(_lineBufferOffset, _endOfTerminatorTrivia, null, state);
     }
     else
     {
         _currentToken = _tokens[0];
         _tokens.RemoveAt(0);
         ResetCurrentToken(state);
     }
 }
Exemple #6
0
        /// <summary>
        /// This is the constructor for numeric node
        /// It enables the program to use period seperator for floating points
        /// FValue is set to be floating points and IValue is set to be integer
        /// </summary>
        /// <param name="value"></param>
        /// <param name="token"></param>
        public NumericNode(string value, ScannerToken token) : base(token)
        {
            CultureInfo customCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();

            customCulture.NumberFormat.NumberDecimalSeparator = ".";
            Thread.CurrentThread.CurrentCulture = customCulture;
            float _f;
            int   _i;

            // float.TryParse(value, CultureInfo.InvariantCulture.,, out _f);
            _f = float.Parse(value, customCulture);
            int.TryParse(value, out _i);

            FValue = _f;
            IValue = _i;
        }
 /// <summary>
 /// This is the constructor for statement node
 /// </summary>
 /// <param name="type">This is the type</param>
 /// <param name="token">This is the token</param>
 public StatementNode(TokenType type, ScannerToken token) : base(type, token)
 {
 }
Exemple #8
0
 /// <summary>
 /// This is the constructor for operator node
 /// </summary>
 /// <param name="token">This is the token</param>
 public OperatorNode(ScannerToken token) : base(token)
 {
 }
 /// <summary>
 /// This is the constructor for time minuse node
 /// </summary>
 /// <param name="token">This is the token</param>
 public TimeMinuteNode(ScannerToken token) : base(token)
 {
 }
Exemple #10
0
 /// <summary>
 /// This is the constructor for or node
 /// </summary>
 /// <param name="token">This is the token</param>
 public OrNode(ScannerToken token) : base(token)
 {
 }
Exemple #11
0
 /// <summary>
 /// This is the constructor for divide node
 /// </summary>
 /// <param name="token">This is the token</param>
 public DivideNode(ScannerToken token) : base(token)
 {
 }
Exemple #12
0
 /// <summary>
 /// This is the constructor for time millisecond node
 /// </summary>
 /// <param name="token">This is the token</param>
 public TimeMillisecondNode(ScannerToken token) : base(token)
 {
 }
Exemple #13
0
 /// <summary>
 /// This is the constructor for plus node
 /// </summary>
 /// <param name="token">This is the token</param>
 public PlusNode(ScannerToken token) : base(token)
 {
 }
Exemple #14
0
        public void Test_CanConstructToken_WithThreeParams()
        {
            ScannerToken token = new ScannerToken(TokenType.VAL, 1, 4);

            Assert.AreEqual(TokenType.VAL, token.Type, "The token with three params did not match the desired type");
        }
Exemple #15
0
 /// <summary>
 /// This is the constructor for modulo node
 /// </summary>
 /// <param name="token">This is the token</param>
 public ModuloNode(ScannerToken token) : base(token)
 {
 }
Exemple #16
0
 /// <summary>
 /// This is the DPin constructor
 /// The ID is set to the pin number
 /// </summary>
 /// <param name="pinNum">This is the pin number</param>
 /// <param name="token">This is the token</param>
 public DPinNode(string pinNum, ScannerToken token) : base(token)
 {
     this.Id = pinNum;
 }
Exemple #17
0
 /// <summary>
 /// This is the constructor for equal node
 /// </summary>
 /// <param name="token">This is the token</param>
 public EqualNode(ScannerToken token) : base(token)
 {
 }
Exemple #18
0
 /// <summary>
 /// this is the constructor of Pin node
 /// </summary>
 /// <param name="token">This is the token</param>
 public PinNode(ScannerToken token) : base(token)
 {
 }
Exemple #19
0
 /// <summary>
 /// Access of table using scanner tokens
 /// </summary>
 /// <value>A transition rule. <see cref="ParseAction"/></value>
 public ParseAction this[ScannerToken key1, ScannerToken key2]
 {
     get => Table[key1.Type][key2.Type];
Exemple #20
0
 /// <summary>
 /// This is the constructor for statement node
 /// </summary>
 /// <param name="token">this is the token</param>
 public StatementNode(ScannerToken token) : base(token)
 {
 }
Exemple #21
0
 /// <summary>
 /// This is the constructor for var node
 /// Id is assigned to id
 /// </summary>
 /// <param name="id">This is the ID</param>
 /// <param name="token">This is the token</param>
 public VarNode(string id, ScannerToken token) : base(token)
 {
     this.Id = id;
 }
Exemple #22
0
 /// <summary>
 /// This is the construcor for less node
 /// </summary>
 /// <param name="token">This is the token</param>
 public LessNode(ScannerToken token) : base(token)
 {
 }
Exemple #23
0
 /// <summary>
 /// This is the constructor for string node
 /// Value is assigned to value
 /// </summary>
 /// <param name="value">This is the value</param>
 /// <param name="token">This is the token</param>
 public StringNode(string value, ScannerToken token) : base(token)
 {
     Value = value;
 }
Exemple #24
0
 /// <summary>
 /// This is the constructor for minus node
 /// </summary>
 /// <param name="token">This is the token</param>
 public MinusNode(ScannerToken token) : base(token)
 {
 }
Exemple #25
0
 private void RevertState(ScannerToken revertTo)
 {
     _lineBufferOffset = revertTo.Position;
     _endOfTerminatorTrivia = revertTo.EndOfTerminatorTrivia;
 }
 /// <summary>
 /// This is the constructor for expression term node
 /// </summary>
 /// <param name="token">This is the token</param>
 public ExpressionTerm(ScannerToken token) : base(token)
 {
 }
 /// <summary>
 /// This is the constructor of binaryesxpressions
 /// </summary>
 /// <param name="token">This is the token</param>
 public BinaryExpression(ScannerToken token) : base(token)
 {
 }
Exemple #28
0
 /// <summary>
 /// This is the greater node constructor
 /// </summary>
 /// <param name="token">This is the token </param>
 public GreaterNode(ScannerToken token) : base(token)
 {
 }
Exemple #29
0
        public void Test_TokenDoesNotAssignWrongType()
        {
            ScannerToken token = new ScannerToken(TokenType.VAL, "test", 1, 4);

            Assert.AreNotEqual(TokenType.ASSIGN, token.Type, "The token matched unexpectedly");
        }
Exemple #30
0
 private void AbandonAllTokens()
 {
     RevertState(_currentToken);
     _tokens.Clear();
     _currentToken = _currentToken.With(ScannerState.Content, null);
 }
Exemple #31
0
        public void Test_TokenDoesNotAssignWrongType_WithThreeParams()
        {
            ScannerToken token = new ScannerToken(TokenType.VAL, 1, 4);

            Assert.AreNotEqual(TokenType.ASSIGN, token.Type, "The token with three params matched unexpectedly");
        }
Exemple #32
0
 /// <summary>
 /// This is the time hour node constructor
 /// </summary>
 /// <param name="token">This is the token</param>
 public TimeHourNode(ScannerToken token) : base(token)
 {
 }
        /// <summary>
        /// Adds most predefined functions to the program
        /// </summary>
        private void AddPredefinedFunctions()
        {
            PredefinedFunctions = new List <FuncNode>();
            ScannerToken predefinedToken = new ScannerToken(TokenType.FUNC, "", 0, 0);

            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("min", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("a", predefinedToken), new VarNode("b", predefinedToken)
                }
            });
            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("max", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("a", predefinedToken), new VarNode("b", predefinedToken)
                }
            });
            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("abs", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("x", predefinedToken)
                }
            });
            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("constrain", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("amt", predefinedToken), new VarNode("low", predefinedToken), new VarNode("high", predefinedToken)
                }
            });
            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("round", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("x", predefinedToken)
                }
            });
            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("radians", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("deg", predefinedToken)
                }
            });
            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("degrees", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("rad", predefinedToken)
                }
            });
            PredefinedFunctions.Add(new FuncNode(0, 0)
            {
                Name = new VarNode("sq", predefinedToken), SymbolType = new TypeContext(TokenType.NUMERIC), FunctionParameters = new List <VarNode>()
                {
                    new VarNode("x", predefinedToken)
                }
            });
        }
 /// <summary>
 /// This is the constructor for binaryexpressions
 /// </summary>
 /// <param name="type">This is the type</param>
 /// <param name="token">This is the token</param>
 public BinaryExpression(TokenType type, ScannerToken token) : base(type, token)
 {
 }
Exemple #35
0
        public void Test_CanConstructToken()
        {
            ScannerToken token = new ScannerToken(TokenType.VAL, "test", 1, 4);

            Assert.AreEqual(TokenType.VAL, token.Type, "The token did not match the desired type");
        }