Skip to content

Commit

Permalink
Add first pass of integer basic decoding
Browse files Browse the repository at this point in the history
It doesn't do proper spacing yet - the intbasic code listing is
extremely tricky to undestand.
  • Loading branch information
zellyn committed Nov 23, 2016
1 parent 5dac18f commit e6508a3
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 37 deletions.
5 changes: 3 additions & 2 deletions cmd/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/zellyn/diskii/lib/applesoft"
"github.com/zellyn/diskii/lib/basic"
"github.com/zellyn/diskii/lib/basic/applesoft"
"github.com/zellyn/diskii/lib/helpers"
)

Expand Down Expand Up @@ -61,7 +62,7 @@ func runDecode(args []string) error {
if rawControlCodes {
os.Stdout.WriteString(listing.String())
} else {
os.Stdout.WriteString(applesoft.ChevronControlCodes(listing.String()))
os.Stdout.WriteString(basic.ChevronControlCodes(listing.String()))
}
return nil
}
31 changes: 0 additions & 31 deletions lib/applesoft/applesoft.go → lib/basic/applesoft/applesoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package applesoft
import (
"bytes"
"fmt"
"regexp"
)

// TokensByCode is a map from byte value to token text.
Expand Down Expand Up @@ -215,33 +214,3 @@ func (l Listing) String() string {
}
return buf.String()
}

var controlCharRegexp = regexp.MustCompile(`[\x00-\x1F]`)

// ChevronControlCodes converts ASCII control characters like chr(4)
// to chevron-surrounded codes like «ctrl-D».
func ChevronControlCodes(s string) string {
return controlCharRegexp.ReplaceAllStringFunc(s, func(s string) string {
if s == "\n" || s == "\t" {
return s
}
if s >= "\x01" && s <= "\x1a" {
return "«ctrl-" + string('A'-1+s[0]) + "»"
}
code := "?"
switch s[0] {
case '\x00':
code = "NUL"
case '\x1C':
code = "FS"
case '\x1D':
code = "GS"
case '\x1E':
code = "RS"
case '\x1F':
code = "US"
}

return "«" + code + "»"
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

package applesoft

import "testing"
import (
"testing"

"github.com/zellyn/diskii/lib/basic"
)

// helloBinary is a simple basic program used for testing. Listing
// below.
Expand Down Expand Up @@ -46,7 +50,7 @@ func TestParse(t *testing.T) {
if err != nil {
t.Fatal(err)
}
text := ChevronControlCodes(listing.String())
text := basic.ChevronControlCodes(listing.String())
if text != helloListing {
t.Fatalf("Wrong listing; want:\n%s\ngot:\n%s", helloListing, text)
}
Expand Down
35 changes: 35 additions & 0 deletions lib/basic/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Package basic contains routines useful for both Applesoft and
// Integer BASIC.
package basic

import "regexp"

var controlCharRegexp = regexp.MustCompile(`[\x00-\x1F]`)

// ChevronControlCodes converts ASCII control characters like chr(4)
// to chevron-surrounded codes like «ctrl-D».
func ChevronControlCodes(s string) string {
return controlCharRegexp.ReplaceAllStringFunc(s, func(s string) string {
if s == "\n" || s == "\t" {
return s
}
if s >= "\x01" && s <= "\x1a" {
return "«ctrl-" + string('A'-1+s[0]) + "»"
}
code := "?"
switch s[0] {
case '\x00':
code = "NUL"
case '\x1C':
code = "FS"
case '\x1D':
code = "GS"
case '\x1E':
code = "RS"
case '\x1F':
code = "US"
}

return "«" + code + "»"
})
}
Loading

0 comments on commit e6508a3

Please sign in to comment.