A Fidget-Symbol based Brainfuck Dialect
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package fidgetlang
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strings"
  8. )
  9. func NewState() *State {
  10. return &State{
  11. registers: [256]int32{},
  12. position: 0,
  13. instruction: 0,
  14. instructionMemory: []instruction{},
  15. buffReader: bufio.NewReader(os.Stdin),
  16. }
  17. }
  18. type State struct {
  19. registers [256]int32
  20. position byte
  21. instruction uint32
  22. instructionMemory []instruction
  23. buffReader *bufio.Reader
  24. DebugOut bool
  25. SuperDebug bool
  26. }
  27. func (s *State) CompileDebug(program string) {
  28. s.instructionMemory = compileDebug(program)
  29. }
  30. func (s *State) Compile(program string) {
  31. s.instructionMemory = compileProgram(program)
  32. }
  33. func (s *State) GetDebugProgram() string {
  34. return makeDebug(s.instructionMemory)
  35. }
  36. func (s *State) bug(text string, args ...interface{}) {
  37. if !s.SuperDebug {
  38. return
  39. }
  40. var sArgs = make([]string, len(args))
  41. for k := range args {
  42. sArgs[k] = fmt.Sprint(args[k])
  43. }
  44. println(strings.Join(append([]string{text}, sArgs...), " "))
  45. }
  46. // Step returns false if the end of memory is reached
  47. func (s *State) Step() bool {
  48. if s.overEnd() {
  49. return false
  50. }
  51. s.bug("X at I", s.instruction)
  52. switch s.curInstr() {
  53. case iIncrPtr:
  54. s.bug("I of P")
  55. s.position++
  56. case iDecrPtr:
  57. s.bug("D of P")
  58. s.position--
  59. case iIncrVal:
  60. s.bug("I of V")
  61. s.setData(s.curData() + 1)
  62. case iDecrVal:
  63. s.bug("D of V")
  64. s.setData(s.curData() - 1)
  65. case iPutVal:
  66. s.bug("P of V")
  67. if s.DebugOut {
  68. fmt.Printf("0x%04X\n", s.curData())
  69. } else {
  70. fmt.Printf("%s", string(rune(s.curData())))
  71. }
  72. case iGetVal:
  73. s.bug("R of V")
  74. rrune, _, err := s.buffReader.ReadRune()
  75. if err == io.EOF {
  76. return false
  77. }
  78. if err != nil {
  79. panic(err)
  80. }
  81. s.setData(rrune)
  82. case iJumpBehind:
  83. if s.curData() != 0 {
  84. for s.curInstr() != iJumpAhead {
  85. s.instruction--
  86. }
  87. s.bug("JB to I", s.instruction)
  88. return true
  89. }
  90. case iJumpAhead:
  91. if s.atEnd() {
  92. s.bug("JA abort at", s.instruction)
  93. return false
  94. }
  95. if s.curData() == 0 {
  96. for s.curInstr() != iJumpBehind {
  97. s.instruction++
  98. }
  99. s.bug("JA to I", s.instruction)
  100. return true
  101. }
  102. }
  103. if s.atEnd() {
  104. return false
  105. }
  106. s.instruction++
  107. return true
  108. }
  109. func (s *State) atEnd() bool {
  110. return int(s.instruction) == len(s.instructionMemory)-1
  111. }
  112. func (s *State) overEnd() bool {
  113. return int(s.instruction) >= len(s.instructionMemory)
  114. }
  115. func (s *State) curInstr() instruction {
  116. return instruction(s.instructionMemory[s.instruction])
  117. }
  118. func (s *State) curData() int32 {
  119. return s.registers[s.position]
  120. }
  121. func (s *State) setData(d int32) {
  122. s.registers[s.position] = d
  123. }