示例#1
0
        public string GenerateCode(TokenChain tokens)
        {
            var code = new StringBuilder();

            Token lastToken = null;

            foreach (var token in tokens)
            {
                if (token.TokenType == TokenType.Operation)
                {
                    code.Append(token);
                }
                else
                {
                    if (lastToken?.TokenType == TokenType.Value)
                    {
                        code.Append(" ");
                    }

                    if ((token as ValueToken).ValueType == ValueType.String)
                    {
                        code.Append($"\"{token.Pattern}\"");
                    }
                    else
                    {
                        code.Append(token.Pattern);
                    }
                }

                lastToken = token;
            }

            return(code.ToString());
        }
示例#2
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());
        }
示例#3
0
        public TokenChain Compile(string code)
        {
            var tokens = parser.Parse(code);

            var assembly = new TokenChain(tokens);

            return(assembly);
        }
示例#4
0
        public void Execute(TokenChain tokens)
        {
            Tokens = tokens;

            while (tokens.TryGetNext(out var token))
            {
                ProcessToken(token);
            }
        }