Ejemplo n.º 1
0
        private void ProcessToken(Token token)
        {
            if (token.TokenType == TokenType.Value)
            {
                Stack.Push(token.Pattern);
            }
            else if (token.TokenType == TokenType.Operation)
            {
                Debug.WriteLine($"Execute: {token.Pattern}");

                var operation = OperationsRegister.GetOperation(token.Pattern);

                var arguments = PopArguments(operation.Arity);

                var results = operation.Execute(arguments, this);

                if (results != null)
                {
                    foreach (var result in results)
                    {
                        Stack.Push(result);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public IEnumerable <Token> Parse(string code)
        {
            var matches = tokenParsingRegex.Matches(code);

            foreach (Match match in matches)
            {
                if (integerPattern.Match(match.Value).Success)
                {
                    yield return(new ValueToken(match.Value, ValueType.Integer));
                }
                else if (floatPattern.Match(match.Value).Success)
                {
                    var value = match.Value;
                    if (value == ".")
                    {
                        value = "0.0";
                    }

                    yield return(new ValueToken(value, ValueType.Float));
                }
                else if (stringPattern.Match(match.Value).Success)
                {
                    yield return(new ValueToken(match.Value.Substring(1, match.Value.Length - 2), ValueType.String));
                }
                else if (operationPattern.Match(match.Value).Success)
                {
                    var operation = OperationsRegister.GetOperationVerbose(match.Value);
                    yield return(new OperationToken(operation.Identifier));
                }
            }
        }
Ejemplo n.º 3
0
        public string GenerateCode(TokenChain tokens)
        {
            var sb = new StringBuilder();

            indent = 0;

            Token lastToken = null;

            foreach (var token in tokens)
            {
                FormatWhitespace(token, lastToken, sb);

                switch (token)
                {
                case OperationToken operation:
                    sb.Append(OperationsRegister.GetOperation(operation.Pattern).VerboseIdentifier);
                    break;

                case ValueToken value:
                    sb.Append(value.ValueType == ValueType.String
                            ? $"\"{value.Pattern}\""
                            : value.Pattern);
                    break;

                default:
                    throw new InvalidOperationException("Invalid token type.");
                }

                lastToken = token;
            }

            return(sb.ToString());
        }
Ejemplo n.º 4
0
        private bool IsIndentDecreasing(Token token)
        {
            if (token.TokenType != TokenType.Operation)
            {
                return(false);
            }

            return(indentDecreasingOperations.Contains(OperationsRegister.GetOperation(token.Pattern)));
        }