Skip to content

Commit

Permalink
fix: fix basic variables parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome Le Saux committed Jun 26, 2024
1 parent 295010e commit e4badaa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (
autoextract = flag.String("autoextract", "", "Extract all DSK contained in the folder path")
snaVersion = flag.Int("snaversion", 1, "Set the sna version (1 or 2 available).")
quiet = flag.Bool("quiet", false, "remove useless display (for scripting for instance)")
appVersion = "0.23"
appVersion = "0.24"
version = flag.Bool("version", false, "Display the app's version and quit.")
)

Expand Down
40 changes: 19 additions & 21 deletions utils/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,26 @@ func getByte(buf []byte, pos uint16, deprotect uint8) byte {
func getWord(buf []byte, pos uint16, deprotect uint8) int {
ret := int(buf[pos] ^ (DproBasic[pos&0x7F] * deprotect))
pos++
ret += int(int(buf[pos])^int((DproBasic[pos&0x7F]*deprotect))) << 8
ret += int(buf[pos]^(DproBasic[pos&0x7F]*byte(deprotect))) << 8
return ret
}

func addWord(buf []byte, pos uint16, listing []byte, deprotect uint8) ([]byte, uint16) {
var lenVar int
l := len(listing)
for {
b := getByte(buf, pos, deprotect)
pos++
listing = append(listing, (b & 0x7f))
if b&0x80 != 0 || lenVar >= 0xff {
listing = append(listing, b&0x7f)
l++
lenVar++
if b&0x80 == 0 && lenVar < 0xff {
continue
} else {
break
}
lenVar++
}
listing = append(listing, 0)
return listing, pos
}

Expand All @@ -99,8 +104,8 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
}
numLigne := getWord(buf, pos, deprotect)
pos += 2
tmp := fmt.Sprintf("%d ", numLigne)
listing = append(listing, tmp...)

listing = append(listing, fmt.Sprintf("%d ", numLigne)...)
} else {
if token != 0 || token == 0x1a {
break
Expand All @@ -119,7 +124,7 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
dansChaine ^= 1
}
} else {
if token > 0x7F && token < 0xFF {
if token > 0x7F && token < 0xFF { // mot clefs
if listing[len(listing)-1] == ':' && token == 0x97 {
listing[len(listing)-1] = 0
}
Expand All @@ -136,7 +141,7 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
dansChaine ^= 1
}
} else {
tmp := make([]byte, 2)

switch token {
case 0x01:
listing = append(listing, ':')
Expand All @@ -149,16 +154,13 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
case 0x04: // Variable float (type !)
listing, pos = addWord(buf, 2+pos, listing, deprotect)
listing = append(listing, '!')
case 0x0B:
case 0x0C:
case 0x0D: // Variable "standard"
case 0x0B, 0x0C, 0x0D: // Variable "standard"
listing, pos = addWord(buf, 2+pos, listing, deprotect)
case 0x19: // Constante entière 8 bits
val := fmt.Sprintf("%d", getByte(buf, pos, deprotect))
listing = append(listing, val...)
pos++
case 0x1A:
case 0x1E: // Constante entière 16 bits
case 0x1A, 0x1E: // Constante entière 16 bits
w := getWord(buf, pos, deprotect)
val := fmt.Sprintf("%d", w)
listing = append(listing, val...)
Expand All @@ -168,10 +170,9 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
val := fmt.Sprintf("&X%X", w)
listing = append(listing, val...)
pos += 2
case 0x1C:
case 0x1C: // address
w := getWord(buf, pos, deprotect)
val := fmt.Sprintf("&%X", w)
listing = append(listing, val...)
listing = append(listing, fmt.Sprintf("&%X", w)...)
pos += 2

case 0x1F: // Constante flottante
Expand All @@ -180,17 +181,14 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
int(getByte(buf, pos, deprotect)) +
((int(getByte(buf, pos+3, deprotect) & 0x7F)) << 24))
f = 1 + (f / 0x80000000)

if getByte(buf, pos+3, deprotect)&0x80 == 0 {
f = -f
}

exp := getByte(buf, pos+4, deprotect) - 129
pos += 5
val := fmt.Sprintf("%f", f*math.Pow(float64(2), float64(exp)))
// Suppression des '0' inutiles
listing = append(listing, val...)

case 0x7C:
listing = append(listing, '|')
listing, pos = addWord(buf, 1+pos, listing, deprotect)
Expand All @@ -199,13 +197,13 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
listing = append(listing, Fcts[getByte(buf, pos, deprotect)]...)
pos++
} else {
tmp := make([]byte, 2)
tmp[1] = 0
tmp[0] = getByte(buf, pos, deprotect) & 0x7F
pos++
listing = append(listing, tmp...)
}
default:

}
}
}
Expand All @@ -223,7 +221,7 @@ func Basic(buf []byte, fileSize uint16, isBasic bool) []byte {
// Conversion des caractères accentués si nécessaire
for i := len(listing) - 1; i >= 0; i-- {
if !unicode.IsPrint(rune(listing[i])) && listing[i] != '\n' && listing[i] != '\r' {
listing[i] = '?'
listing[i] = ' '
}
}

Expand Down

0 comments on commit e4badaa

Please sign in to comment.