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()); }
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()); }
public TokenChain Compile(string code) { var tokens = parser.Parse(code); var assembly = new TokenChain(tokens); return(assembly); }
public void Execute(TokenChain tokens) { Tokens = tokens; while (tokens.TryGetNext(out var token)) { ProcessToken(token); } }