Guest User

Untitled

a guest
Dec 10th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. // "Hello, world!"
  2. static PROGRAM: &'static str = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
  3.  
  4. fn main() {
  5. let tokens = tokenize(PROGRAM);
  6. //println!("{:?}", tokens);
  7.  
  8. let generated_code = generate(&tokens);
  9. println!("{}", generated_code);
  10. }
  11.  
  12.  
  13. #[derive(Debug, PartialEq, Copy, Clone)]
  14. enum Token {
  15. Add, // +
  16. Sub, // -
  17. Right, // >
  18. Left, // <
  19. Read, // ,
  20. Write, // .
  21. BeginLoop, // [
  22. EndLoop, // ]
  23. }
  24. use self::Token::*;
  25.  
  26. fn tokenize(input: &str) -> Vec<Token> {
  27. let mut tokens = Vec::<Token>::new();
  28.  
  29. let mut chars = input.chars();
  30. while let Some(c) = chars.next() {
  31. match c {
  32. '+' => tokens.push(Add),
  33. '-' => tokens.push(Sub),
  34. '>' => tokens.push(Right),
  35. '<' => tokens.push(Left),
  36. ',' => tokens.push(Read),
  37. '.' => tokens.push(Write),
  38. '[' => tokens.push(BeginLoop),
  39. ']' => tokens.push(EndLoop),
  40. _ => {}
  41. }
  42. }
  43.  
  44. tokens
  45. }
  46.  
  47. fn generate(tokens: &[Token]) -> String {
  48. let mut output = String::from(include_str!("preface.c"));
  49.  
  50. // Tabs are just for pretty printing indented text.
  51. let mut indentation = 1u32;
  52.  
  53. fn indent(count: u32) -> String {
  54. let mut indentation = String::new();
  55. for _ in 0..count {
  56. indentation.push_str(" "); // 4-space indentions
  57. }
  58. indentation
  59. }
  60.  
  61. for &token in tokens {
  62. match token {
  63. Add => {
  64. // Increment the value at the selected cell
  65. output.push_str(&indent(indentation));
  66. output.push_str("*++ptr;\n");
  67. }
  68. Sub => {
  69. // Decrement the value at the selected cell
  70. output.push_str(&indent(indentation));
  71. output.push_str("*--ptr;\n");
  72. }
  73. Right => {
  74. // Change our selected cell to the next to the right
  75. output.push_str(&indent(indentation));
  76. output.push_str("ptr++;\n");
  77. }
  78. Left => {
  79. // Change our selected cell to the next to the left
  80. output.push_str(&indent(indentation));
  81. output.push_str("ptr--;\n");
  82. }
  83. Read => {
  84. // Read a single character into the selected cell
  85. output.push_str(&indent(indentation));
  86. output.push_str("*ptr=getchar();\n");
  87. }
  88. Write => {
  89. // Print the character at the selected cell
  90. output.push_str(&indent(indentation));
  91. output.push_str("putchar(*ptr);\n");
  92. }
  93. BeginLoop => {
  94. // Begin a loop at the current cell
  95. output.push_str(&indent(indentation));
  96. output.push_str("while (*ptr) {\n");
  97. indentation += 1;
  98. }
  99. EndLoop => {
  100. // Close a loop
  101. indentation -= 1;
  102. output.push_str(&indent(indentation));
  103. output.push_str("}\n");
  104. }
  105. }
  106. }
  107. output.push_str("}\n");
  108.  
  109. output
  110. }
Add Comment
Please, Sign In to add comment