Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler tests / bugfixes #81

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions cmd/compile_test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main

import (
"flag"
"fmt"
"io/fs"
"log"
"os"
"path"
"strings"

"github.com/bobertlo/gmars"
)

func listFiles(dirpath string) ([]string, error) {
output := make([]string, 0)

fileSystem := os.DirFS(dirpath)

err := fs.WalkDir(fileSystem, ".", func(filepath string, d fs.DirEntry, err error) error {
if err != nil {
log.Fatal(err)
}
if d.IsDir() {
return nil
}
ext := strings.ToLower(path.Ext(filepath))
if ext != ".red" {
return nil
}

output = append(output, filepath)
return nil
})
if err != nil {
return nil, err
}

return output, nil
}

func main() {
// legacyFlag := flag.Bool("8", false, "compile in ICWS88 mode")
presetFlag := flag.String("preset", "nop94", "preset to use when compiling files")
flag.Parse()

args := flag.Args()
if len(args) != 2 {
fmt.Fprintf(os.Stderr, "usage: compile_test [-preset name] <code_dir> <compiled_dir>\n")
os.Exit(1)
}

config, err := gmars.PresetConfig(*presetFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading preset: %s\n", err)
os.Exit(1)
}

fileList, err := listFiles(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error reading input dir '%s': %s\n", args[0], err)
}

compileErrors := 0
compileSuccess := 0
matches := 0
mismatches := 0
for _, file := range fileList {
inPath := path.Join(args[0], file)
expectedPath := path.Join(args[1], file)

fIn, err := os.Open(inPath)
if err != nil {
fmt.Printf("error reading file '%s': %s\n", inPath, err)
continue
}
defer fIn.Close()

fExpected, err := os.Open(expectedPath)
if err != nil {
fmt.Printf("error reading file '%s': %s\n", inPath, err)
continue
}
defer fExpected.Close()

in, err := gmars.CompileWarrior(fIn, config)
if err != nil {
fmt.Printf("error compiling warrior '%s': %s\n", inPath, err)
compileErrors++
continue
}

expected, err := gmars.ParseLoadFile(fExpected, config)
if err != nil {
fmt.Printf("error loading warrior '%s': %s\n", expectedPath, err)
compileErrors++
continue
}

compileSuccess++

if len(in.Code) != len(expected.Code) {
fmt.Printf("%s: length mismatch: %d != %d", inPath, len(in.Code), len(expected.Code))
mismatches++
continue
}

instructionsMatch := true
for i, inst := range in.Code {
if expected.Code[i] != inst {
fmt.Printf("%s: instruction mismatch: '%s' != '%s'\n", inPath, inst, expected.Code[i])
instructionsMatch = false
}
}

if instructionsMatch {
matches++
} else {
mismatches++
}
}

fmt.Println(compileErrors, "errors")
fmt.Println(compileSuccess, "successfully compiled")
fmt.Println(mismatches, "mismatches")
fmt.Println(matches, "matches")
}
16 changes: 16 additions & 0 deletions compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (c *compiler) loadSymbols() {
c.values = make(map[string][]token)
c.labels = make(map[string]int)

curPseudoLine := 0
for _, line := range c.lines {
if line.typ == linePseudoOp {
if strings.ToLower(line.op) == "equ" {
Expand All @@ -49,19 +50,25 @@ func (c *compiler) loadSymbols() {
if len(line.a) > 0 {
c.startExpr = line.a
}
for _, label := range line.labels {
c.labels[label] = curPseudoLine
}
}

}
if line.typ == lineInstruction {
for _, label := range line.labels {
c.labels[label] = line.codeLine
}
curPseudoLine++
}
}
}

func (c *compiler) reloadReferences() error {
c.labels = make(map[string]int)

curPseudoLine := 1
for _, line := range c.lines {
if line.typ == lineInstruction {
for _, label := range line.labels {
Expand All @@ -70,6 +77,15 @@ func (c *compiler) reloadReferences() error {
return fmt.Errorf("line %d: label '%s' redefined", line.line, label)
}
c.labels[label] = line.codeLine
curPseudoLine++
}
} else if line.typ == linePseudoOp {
for _, label := range line.labels {
_, ok := c.labels[label]
if ok {
return fmt.Errorf("line %d: label '%s' redefined", line.line, label)
}
c.labels[label] = curPseudoLine
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ func TestLexer(t *testing.T) {
{tokError, "unexpected character: '~'"},
},
},
{
input: "label ;\ndat 0",
expected: []token{
{tokText, "label"},
{tokComment, ";"},
{tokNewline, ""},
{tokText, "dat"},
{tokNumber, "0"},
{tokEOF, ""},
},
},
}

runLexTests(t, "TestLexer", testCases)
Expand Down
1 change: 1 addition & 0 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ func parseLoadFile88(reader io.Reader, coresize Address) (WarriorData, error) {
if fields[0] == "end" {
break
}
continue
}

// comma is ignored, but required
Expand Down
24 changes: 16 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ func parseComment(p *parser) parseStateFn {
// newline / comments: consume
// anyting else: nil
func parseLabels(p *parser) parseStateFn {
// just consume newlines and comments for now
if p.nextToken.typ == tokNewline || p.nextToken.typ == tokComment {
p.next()
return parseLabels
}

if p.nextToken.IsOp() {
if p.nextToken.IsPseudoOp() {
return parsePseudoOp
Expand All @@ -244,12 +250,6 @@ func parseLabels(p *parser) parseStateFn {
p.currentLine.labels = append(p.currentLine.labels, p.nextToken.val)
nextToken := p.next()

// just consume newlines and comments for now
if nextToken.typ == tokNewline || nextToken.typ == tokComment {
p.next()
return parseLabels
}

if nextToken.typ != tokText {
p.err = fmt.Errorf("line %d: label or op expected, got '%s'", p.line, nextToken)
return nil
Expand All @@ -259,10 +259,14 @@ func parseLabels(p *parser) parseStateFn {

// from: parseLabels
// newline or comments: parseColon
// op: parseOp
// op text: parseOp
// psudo op text: parsePseudoOp
// text: parseLabels
// anything else: nil
func parseColon(p *parser) parseStateFn {
p.next()
for p.nextToken.typ == tokColon {
p.next()
}

// just consume newlines and comments for now
if p.nextToken.typ == tokNewline || p.nextToken.typ == tokComment {
Expand All @@ -277,6 +281,10 @@ func parseColon(p *parser) parseStateFn {
return parseOp
}

if p.nextToken.typ == tokText {
return parseLabels
}

p.err = fmt.Errorf("line %d: op expected after colon, got '%s'", p.line, p.nextToken)
return nil
}
Expand Down
34 changes: 33 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func runParserTests(t *testing.T, setName string, tests []parserTestCase) {
assert.Error(t, err, fmt.Sprintf("%s test %d: error should be present", setName, i))
} else {
assert.NoError(t, err)
assert.Equal(t, test.output, source)
if test.output != nil {
assert.Equal(t, test.output, source)
}
}
}
}
Expand Down Expand Up @@ -163,11 +165,41 @@ func TestParserPositive(t *testing.T) {
},
},
},
{
input: "label ;\ndat $0, $0\n",
output: []sourceLine{
{
line: 1,
codeLine: 0,
labels: []string{"label"},
typ: lineInstruction,
op: "dat",
amode: "$",
a: []token{{typ: tokNumber, val: "0"}},
bmode: "$",
b: []token{{typ: tokNumber, val: "0"}},
comment: "",
newlines: 1,
},
},
},
}

runParserTests(t, "parser positive", testCases)
}

func TestParserNoError(t *testing.T) {
testCases := []string{
"dat 0\n",
"label: label2: dat 0",
"label: : dat 0",
}

for _, val := range testCases {
runParserTests(t, "parser non negative", []parserTestCase{{input: val}})
}
}

func TestParserNegative(t *testing.T) {
testCases := []parserTestCase{
{
Expand Down
Loading