Skip to content

Commit

Permalink
Add pseudo instruction for GETC
Browse files Browse the repository at this point in the history
  • Loading branch information
smoynes committed Feb 13, 2024
1 parent 204496a commit 7247d8c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
10 changes: 9 additions & 1 deletion internal/asm/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,18 @@ func (trap *TRAP) Parse(opcode string, operands []string) error {

*trap = TRAP{LITERAL: uint16(vm.TrapHALT)}

return nil
case opcode == "GETC":
if len(operands) != 0 {
return fmt.Errorf("GETC: %w", ErrOperand)
}

*trap = TRAP{LITERAL: uint16(vm.TrapGETC)}

return nil
case opcode == "OUT":
if len(operands) != 0 {
return fmt.Errorf("HALT: %w", ErrOperand)
return fmt.Errorf("OUT: %w", ErrOperand)
}

*trap = TRAP{LITERAL: uint16(vm.TrapOUT)}
Expand Down
27 changes: 25 additions & 2 deletions internal/asm/ops_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,30 @@ func TestTRAP_Parse(t *testing.T) {
want: &TRAP{LITERAL: 0x00ff},
wantErr: &SyntaxError{},
},
{
name: "GETC with arg",
opcode: "GETC", operands: []string{"x00"},
want: nil,
wantErr: &SyntaxError{},
},
{
name: "GETC",
opcode: "GETC", operands: []string{},
want: &TRAP{LITERAL: 0x20},
wantErr: nil,
},
{
name: "OUT with arg",
opcode: "OUT", operands: []string{"x00"},
want: nil,
wantErr: &SyntaxError{},
},
{
name: "OUT",
opcode: "OUT", operands: []string{},
want: &TRAP{LITERAL: 0x21},
wantErr: nil,
},
}

for _, tt := range tests {
Expand All @@ -823,15 +847,14 @@ func TestTRAP_Parse(t *testing.T) {

if (tt.wantErr != nil && err == nil) || err != nil && tt.wantErr == nil {
t.Fatalf("not expected: %#v, want: %#v", err, tt.wantErr)
return
}

if tt.wantErr != nil && errors.Is(err, tt.wantErr) {
t.Fatalf("expected err: %#v, got: %#v", tt.wantErr, err)
}

if (err == nil) && !reflect.DeepEqual(got, tt.want) {
t.Errorf("NOT.Parse() = %#v, want %#v", got, tt.want)
t.Errorf("Parse() = %#v, want %#v", got, tt.want)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/asm/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (p *Parser) parseOperator(opcode string) Operation {
return &STI{}
case "TRAP":
return &TRAP{}
case "HALT":
case "HALT", "OUT", "GETC":
return &TRAP{}
case "RTI":
return &RTI{}
Expand Down

0 comments on commit 7247d8c

Please sign in to comment.