Skip to content

Commit

Permalink
implement assert testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bobertlo committed Nov 22, 2024
1 parent 1f269c9 commit 93eb643
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
43 changes: 42 additions & 1 deletion compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,43 @@ func (c *compiler) expandExpression(expr []token, line int) ([]token, error) {
return output, nil
}

func (c *compiler) evaluateAssertion(assertText string) error {

assertTokens, err := LexInput(strings.NewReader(assertText))
if err != nil {
return err
}
assertTokens = assertTokens[:len(assertTokens)-1]
exprTokens, err := c.expandExpression(assertTokens, 0)
if err != nil {
return err
}
exprVal, err := evaluateExpression(exprTokens)
if err != nil {
return err
}
if exprVal == 0 {
return fmt.Errorf("assertion '%s' failed", assertText)
}
return nil
}

func (c *compiler) evaluateAssertions() error {
for _, line := range c.lines {
if line.typ != lineComment {
continue
}
if strings.HasPrefix(line.comment, ";assert") {
assertText := line.comment[7:]
err := c.evaluateAssertion(assertText)
if err != nil {
return err
}
}
}
return nil
}

func (c *compiler) assembleLine(in sourceLine) (Instruction, error) {
opLower := strings.ToLower(in.op)
var aMode, bMode AddressMode
Expand Down Expand Up @@ -351,9 +388,13 @@ func (c *compiler) expandForLoops() error {
}

func (c *compiler) compile() (WarriorData, error) {

c.loadSymbols()

err := c.evaluateAssertions()
if err != nil {
return WarriorData{}, err
}

graph := buildReferenceGraph(c.values)
cyclic, cyclicKey := graphContainsCycle(graph)
if cyclic {
Expand Down
28 changes: 28 additions & 0 deletions compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,31 @@ func TestCompileDoubleForLoop(t *testing.T) {
}, w.Code)
assert.Equal(t, 7, w.Start)
}

func TestAssertPositive(t *testing.T) {
config := ConfigNOP94

input := `
;assert CORESIZE == 8000
dat.f $123, $123
`

w, err := CompileWarrior(strings.NewReader(input), config)
require.NoError(t, err)
assert.Equal(t, []Instruction{
{Op: DAT, OpMode: F, AMode: DIRECT, A: 123, BMode: DIRECT, B: 123},
}, w.Code)
}

func TestAssertNegative(t *testing.T) {
config := ConfigNOP94

input := `
;assert CORESIZE == 8192
dat.f $123, $123
`

w, err := CompileWarrior(strings.NewReader(input), config)
require.Error(t, err)
require.Equal(t, WarriorData{}, w)
}
1 change: 1 addition & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func TestEvaluateExpressionNegative(t *testing.T) {
cases := []string{
")21",
"2^3",
"2{2",
}

for _, input := range cases {
Expand Down
1 change: 1 addition & 0 deletions warriors/88/imp.red
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
;author A K Dewdney
;strategy this is the simplest program
;strategy it was described in the initial articles
;assert 1

MOV $ 0, $ 1
END 0
1 change: 1 addition & 0 deletions warriors/94/bombspiral.red
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
;name bomb spiral
;author Robert Lowry
;strategy stone and imp launcher
;assert CORESIZE == 8000

spl istart
jmp bstart
Expand Down
1 change: 1 addition & 0 deletions warriors/94/imp.red
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
;author A K Dewdney
;strategy this is the simplest program
;strategy it was described in the initial articles
;assert 1

ORG 0
MOV.I # 0, $ 1

0 comments on commit 93eb643

Please sign in to comment.