コード例 #1
0
ファイル: SetTranslator.cs プロジェクト: crispcat/Shelve
        private void TranslateExpression(LexedExpression translatedExpression)
        {
            var compiler = new ExpressionTranslator(translatedExpression, localVariables);

            var translated = compiler.CreateCalculationStack();

            localVariables[translated.TargetVariable].AddExpression(translated);
        }
コード例 #2
0
        public ExpressionTranslator(LexedExpression lexedExpression, HashedVariables variables)
        {
            this.variables       = variables;
            this.lexedExpression = lexedExpression;

            inner  = lexedExpression.LexicalQueue;
            type   = Validator.Elaborate(lexedExpression);
            result = new Expression(inner.First.Value.Represents, lexedExpression.Initial, lexedExpression.TargetSet);

            canCreateInstances = false;
        }
コード例 #3
0
ファイル: ShuntingYard.cs プロジェクト: crispcat/Shelve
        public ShuntingYard(LexedExpression lexedExpression)
        {
            this.lexedExpression = lexedExpression;

            outSequence     = new Queue <Lexema>();
            shuntingMachine = new Stack <Lexema>();

            wereArguments            = new Stack <bool>();
            argumentsCountMachine    = new Stack <int>();
            argumentsRecievedMachine = new Stack <int>();

            inSequence = new Queue <Lexema>(lexedExpression.LexicalQueue);
        }
コード例 #4
0
ファイル: Lexer.cs プロジェクト: crispcat/Shelve
        public LexedExpression Tokenize(string expression)
        {
            current = new LexedExpression(expression.Replace(" ", string.Empty));

            position = 0;

            ProcessNextToken();

            if (current.ExpressionString != string.Empty)
            {
                throw new ArgumentException($"Unable to match against any tokens in " +
                                            $"\"{current.Initial}\". " +
                                            $"Position: {position}. Lexer state: {state.ToString()}");
            }
            else
            {
                return(current);
            }
        }
コード例 #5
0
        public static Type Elaborate(LexedExpression expression)
        {
            var tokens = expression.LexicalQueue.ToArray();

            try
            {
                var operatorString = tokens.First(t => t.Token == Token.Binar).Represents;

                if (!operatorString.Contains("="))
                {
                    throw new ArgumentException($"Assign operator expected as first operator in expression \"{expression.Initial}\" " +
                                                $"but operator \"{operatorString}\" pass." +
                                                $"VariableSet: \"{expression.TargetSet}\"");
                }

                var assignOperators = tokens.Where(t => t.Token == Token.Binar && t.Represents.Contains("="));

                if (assignOperators.Count() > 1)
                {
                    throw new ArgumentException($"Multiple assignment in expression \"{expression.Initial}\" " +
                                                $"VariableSet: \"{expression.TargetSet}\"");
                }
            }
            catch (InvalidOperationException)
            {
                throw new ArgumentException($"Operators and assignments expected in expression \"{expression.Initial}\" " +
                                            $"but no one operator pass." +
                                            $"VariableSet: \"{expression.TargetSet}\"");
            }

            if (expression.LexicalQueue.Count == 3)
            {
                return(typeof(DynamicValueHolder));
            }

            var sqBrackets = tokens.Where(t => t.Token == Token.SqLeftBracket || t.Token == Token.SqRightBracket);

            bool isIterator = sqBrackets.Count() != 0;

            if (isIterator)
            {
                bool isCorrectIterator = sqBrackets.First().Token == Token.SqLeftBracket &&
                                         sqBrackets.Last().Token == Token.SqRightBracket;

                if (isCorrectIterator)
                {
                    return(typeof(Iterator));
                }
                else
                {
                    throw new ArgumentException($"Iterator definition in expression " +
                                                $"\"{expression.Initial}\" expected in \" (targetVariable) (accumulateOperator) " +
                                                $"[(startValue), (stepFunction)]\" format" +
                                                $"VariableSet: \"{expression.TargetSet}\"");
                }
            }

            bool isVariable = Lexica.variableRegex.IsMatch(tokens.First().Represents);

            if (isVariable)
            {
                return(typeof(Variable));
            }
            else
            {
                throw new ArgumentException($"Target variable declaration expected " +
                                            $"as first element of expression \"{expression.Initial}\"." +
                                            $"VariableSet: \"{expression.TargetSet}\"");
            }
        }
コード例 #6
0
 private void ShuntExpression()
 {
     lexedExpression = new ShuntingYard(lexedExpression).ProcessInput();
 }