-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
460425c
commit 378f9fe
Showing
7 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package parser | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func abs(x int) int { | ||
if x < 0 { | ||
return -x | ||
} | ||
return x | ||
} | ||
|
||
func repeatDirection(neg, pos string, vect int) string { | ||
if vect > 0 { | ||
return strings.Repeat(pos, vect) | ||
} | ||
return strings.Repeat(neg, abs(vect)) | ||
} | ||
|
||
func instPrint(inst, lastInst Instruction) string { | ||
switch inst.operator { | ||
case opAddDp: | ||
return repeatDirection("<", ">", inst.operand) | ||
case opAddVal: | ||
return repeatDirection("-", "+", inst.operand) | ||
case opSetVal: | ||
prefix := "[-]" | ||
if lastInst.IsZeroOp() && inst.operand != 0 { | ||
prefix = "" | ||
} | ||
return prefix + repeatDirection("-", "+", inst.operand) | ||
case opOut: | ||
return "." | ||
case opIn: | ||
return "," | ||
case opJmpZ: | ||
return "[" | ||
case opJmpNz: | ||
return "]" | ||
case opMove: | ||
return "[-" + repeatDirection("<", ">", inst.operand) + "+" + repeatDirection(">", "<", inst.operand) + "]" | ||
case opSkip: | ||
return "[" + repeatDirection("<", ">", inst.operand) + "]" | ||
case opMulVal: | ||
return "" | ||
case opNoop: | ||
if lastInst.operator == opMulVal { | ||
multiplier := strings.Repeat("+", abs(inst.operand)) | ||
if inst.operand < 0 { | ||
multiplier = strings.Repeat("-", abs(inst.operand)) | ||
} | ||
if lastInst.operand > 0 { | ||
return "[-" + strings.Repeat(">", lastInst.operand) + multiplier + strings.Repeat("<", lastInst.operand) + "]" | ||
} | ||
return "[-" + strings.Repeat("<", abs(lastInst.operand)) + multiplier + strings.Repeat(">", abs(lastInst.operand)) + "]" | ||
} | ||
return "" | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
// Print pretty prints out the parsed program. | ||
func Print(program []Instruction, writer *bufio.Writer) { | ||
depth := 0 | ||
startLoop := NewInstruction('[') | ||
endLoop := NewInstruction(']') | ||
lastInst := NewInstruction('!') | ||
for _, inst := range program { | ||
if inst.SameOp(endLoop) { | ||
depth-- | ||
} | ||
fmt.Fprintln(writer, strings.Repeat("\t", depth), instPrint(inst, lastInst)) | ||
if inst.SameOp(startLoop) { | ||
depth++ | ||
} | ||
lastInst = inst | ||
} | ||
writer.Flush() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package parser | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPrint(t *testing.T) { | ||
program := []Instruction{ | ||
{opNoop, 0}, | ||
{opAddDp, 5}, | ||
{opSetVal, 0}, | ||
{opAddVal, 5}, | ||
{opMove, 2}, | ||
{opJmpZ, 7}, | ||
{opIn, 1}, | ||
{opJmpNz, 5}, | ||
{opAddDp, 2}, | ||
} | ||
var buf bytes.Buffer | ||
outputBuf := bufio.NewWriter(&buf) | ||
Print(program, outputBuf) | ||
got := buf.String() | ||
want := " \n >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n ]\n >>\n" | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got %v want %v", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env bf | ||
,+[-[>>++++[>++++++++<-]<+<-[>+>+>-[>>>]<[[>+<-]>>+>]<<<<<-]]>>>[-]+>-- | ||
[-[<->+++[-]]]<[++++++++++++<[>-[>+>>]>[+[<+>-]>+>>]<<<<<-]>>[<+>-]>[-[ | ||
[-[<->[-]]]<[++++++++++++<[>-[>+>>]>[+[<+>-]>+>>]<<<<<-]>>[<+>-]>[-[ | ||
-<<[-]>>]<<[<<->>-]>>]<<[<<+>>-]]<[-]<.[-]<,+] |