Skip to content

Commit

Permalink
Merge pull request #6 from 135yshr/feature
Browse files Browse the repository at this point in the history
いくつかのバグと実装漏れを修正
  • Loading branch information
135yshr committed Feb 18, 2014
2 parents 6c71f6b + cfe66c0 commit dc780de
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# _wspacego_[![Build Status](https://travis-ci.org/135yshr/wspacego.png?branch=master)](https://travis-ci.org/135yshr/wspacego)
# _wspacego_


_whitespace language interpreter_
Expand Down
5 changes: 4 additions & 1 deletion lib/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,15 @@ func readEndLf(data []byte) ([]byte, int) {

func parseInt(data []byte) int {
var ret int
for _, b := range data {
for _, b := range data[1:] {
ret = ret << 1
if b == Tab {
ret += 1
}
}
if data[0] == Tab {
ret *= -1
}
return ret
}

Expand Down
22 changes: 15 additions & 7 deletions lib/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,37 @@ func TestConerter(t *testing.T) {
})
Context("スタックに関連する命令の生成", func() {
It("スタックに1をプッシュするコマンドが作成されること", func() {
data := []byte{' ', '\t', '\n'}
data := []byte{' ', ' ', '\t', '\n'}
cmd, seek, err := stackManipulation(data)
Expect(err).To(NotExist)
Expect(seek).To(Equal, len(data))
Expect(cmd).To(Exist)
Expect(cmd).To(Equal, newSubCommandWithParam("stack", "push", 1))
})
It("スタックに2をプッシュするコマンドが作成されること", func() {
data := []byte{' ', '\t', ' ', '\n'}
data := []byte{' ', ' ', '\t', ' ', '\n'}
cmd, seek, err := stackManipulation(data)
Expect(err).To(NotExist)
Expect(seek).To(Equal, len(data))
Expect(cmd).To(Exist)
Expect(cmd).To(Equal, newSubCommandWithParam("stack", "push", 2))
})
It("スタックに4をプッシュするコマンドが作成されること", func() {
data := []byte{' ', '\t', ' ', ' ', '\n'}
data := []byte{' ', ' ', '\t', ' ', ' ', '\n'}
cmd, seek, err := stackManipulation(data)
Expect(err).To(NotExist)
Expect(seek).To(Equal, len(data))
Expect(cmd).To(Exist)
Expect(cmd).To(Equal, newSubCommandWithParam("stack", "push", 4))
})
It("スタックに-1をプッシュするコマンドが作成されること", func() {
data := []byte{' ', '\t', '\t', '\n'}
cmd, seek, err := stackManipulation(data)
Expect(err).To(NotExist)
Expect(seek).To(Equal, len(data))
Expect(cmd).To(Exist)
Expect(cmd).To(Equal, newSubCommandWithParam("stack", "push", -1))
})
It("スタックをコピーするコマンドが作成されること", func() {
data := []byte{'\n', ' '}
cmd, seek, err := stackManipulation(data)
Expand All @@ -91,16 +99,16 @@ func TestConerter(t *testing.T) {
Expect(cmd).To(Exist)
Expect(cmd).To(Equal, newSubCommand("stack", "remove"))
})
It("スタックのn個目の値をトップにコピーするコマンドが作成されること", func() {
data := []byte{'\t', ' ', '\t', '\n'}
It("スタックの1個目の値をトップにコピーするコマンドが作成されること", func() {
data := []byte{'\t', ' ', ' ', '\t', '\n'}
cmd, seek, err := stackManipulation(data)
Expect(err).To(NotExist)
Expect(seek).To(Equal, len(data))
Expect(cmd).To(Exist)
Expect(cmd).To(Equal, newSubCommandWithParam("stack", "ncopy", 1))
})
It("スタックのn個目の値をトップに移動するコマンドが作成されること", func() {
data := []byte{'\t', '\n', '\t', '\n'}
It("スタックの1個目の値をトップに移動するコマンドが作成されること", func() {
data := []byte{'\t', '\n', ' ', '\t', '\n'}
cmd, seek, err := stackManipulation(data)
Expect(err).To(NotExist)
Expect(seek).To(Equal, len(data))
Expand Down
61 changes: 45 additions & 16 deletions lib/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const (
Lf = '\n'
)

type input_chars []byte

var ichars *input_chars

type Interpreter struct {
origin []byte
source []byte
Expand Down Expand Up @@ -91,28 +95,19 @@ func (inter *Interpreter) Run() {
case "putn":
fmt.Print(stack.Pop())
case "getc":
line, err := readStdin()
c, err := getChar()
if err != nil {
fmt.Println(err)
return
}

bl := []byte(line)
for pos := len(bl); 0 < pos; pos-- {
k := stack.Pop()
heap.Push(k, int(bl[pos]))
}
k := stack.Pop()
heap.Push(k, int(c))
case "getn":
line, err := readStdin()
if err != nil {
fmt.Println(err)
return
}

num, err := strconv.Atoi(line)
num, err := getNumber()
if err != nil {
fmt.Println(err)
return
fmt.Println("Non-numeric value was entered")
continue
}

k := stack.Pop()
Expand Down Expand Up @@ -219,5 +214,39 @@ func readStdin() (string, error) {
if err != nil {
return "", err
}
return strings.Trim(line, "\r\n"), nil
return strings.Replace(line, "\r", "", -1), nil
}

func getNumber() (int, error) {
line, err := readStdin()
if err != nil {
return -1, err
}
line = strings.TrimRight(line, "\n")
return strconv.Atoi(line)
}

func getChar() (byte, error) {
if ichars == nil {
ichars = new(input_chars)
}
return ichars.getChar()
}

func (i *input_chars) getChar() (byte, error) {
chars := *i
if chars.Len() == 0 {
line, err := readStdin()
if err != nil {
return 0, err
}
chars = []byte(line)
}
ret := chars[0]
*i = chars[1:]
return ret, nil
}

func (i *input_chars) Len() int {
return len(*i)
}
12 changes: 6 additions & 6 deletions lib/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ func TestInterpretor(t *testing.T) {
})
})
Context("コマンドリストを生成", func() {
It("不要なデータを排除して指定したコマンドが作成されること", func() {
data := []byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n'}
It("スタックに0x41をpushするコマンドが1つだけ作成れること", func() {
data := []byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n'}
sut := NewInterpreter(data)
err := sut.toCode()
Expect(err).To(NotExist)
Expect(sut.commands.Len()).To(Equal, 1)
Expect(sut.commands.Get(1)).To(Equal, newSubCommandWithParam("stack", "push", 0x41))
})
It("不要なデータを排除して指定したコマンドが作成されること(パート2)", func() {
data := []byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n'}
It("スタックに0x43をpushするコマンドが1つだけ作成れること", func() {
data := []byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n'}
sut := NewInterpreter(data)
err := sut.toCode()
Expect(err).To(NotExist)
Expect(sut.commands.Len()).To(Equal, 1)
Expect(sut.commands.Get(1)).To(Equal, newSubCommandWithParam("stack", "push", 0x43))
})
It("不要なデータを排除して複数のコマンドが作成されること", func() {
data := []byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n'}
It("スタックに0x41をpushするコマンドが2つ作成れること", func() {
data := []byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\n'}
sut := NewInterpreter(data)
err := sut.toCode()
Expect(err).To(NotExist)
Expand Down

0 comments on commit dc780de

Please sign in to comment.